简体   繁体   English

空数组返回true? 给定一个整数数组,如果每个元素都是 1 或 4,则返回 true

[英]Empty array returns true? Given an array of ints, return true if every element is a 1 or a 4

Given an array of ints, return true if every element is a 1 or a 4. only14([1, 4, 1, 4]) → true only14([1, 4, 2, 4]) → false only14([1, 1]) → true

I came up with the following algorithm which returns all the expected answers, EXCEPT for an empty array which should return true :我想出了以下算法,它返回所有预期的答案,除了一个应该返回true的空数组:

public boolean only14(int[] nums) {

  boolean flag = false;

  for (int i = 0; i < nums.length; i++) {

    if (nums[i] == 1 || nums[i] == 4) {
      flag = true;
    } else {
      return false;
    }
  }
  return flag;
}

From the solutions, the following input should yield true , but I can't figure out why:从解决方案中,以下输入应该产生true ,但我不知道为什么:

only14([]) → true

I understand the default value is 0, so what am I missing such that I should expect the return value of the boolean to be true ?我知道默认值是 0,所以我错过了什么,以至于我应该期望布尔值的返回值为true

The following should work.以下应该工作。 It returns true unless there are any values that are not 1 or 4.除非有任何不是 1 或 4 的值,否则它会返回true

public boolean only14(int[] nums) {
  for (int i = 0; i < nums.length; i++) {
    int val = nums[i];
    if (val != 1 && val != 4) {
      return false;
    }
  }
  return true;
}

The empty set contains no values that aren't 1 or 4 , so it should be true .空集不包含不是14 ,所以它应该是true I would prefer a for-each loop like我更喜欢for-each循环

public boolean only14(int[] nums) {
    for (int n : nums) {        // for each int n in nums
        if (n == 1 || n == 4) { // if it is 1 or 4 keep looping
            continue;
        }
        return false;           // it isn't 1 or 4, return false
    }
    return true;                // every value is 1 or 4.
}

In Java 8+, you could implement it with an IntStream like在 Java 8+ 中,您可以使用IntStream来实现它,例如

public boolean only14(int[] nums) {
    return IntStream.of(nums).allMatch(x -> x == 1 || x == 4);
}

This code works fine:-此代码工作正常:-

public boolean only14(int[] nums) {
  for(int i=0;i<=nums.length-1;i++){
     if (nums[i]!= 1 && nums[i]!= 4){
      return false;
    }
  }
  return true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个整数数组,如果数字序列 1、2、3 出现在数组中的某个位置,则返回 true - Given an array of ints, return true if the sequence of numbers 1, 2, 3 appears in the array somewhere 给定一个整数数组,如果该数组同时包含 1 和 3,至少各一个,则返回 true - Given an array of ints, return true if the array contains both 1's and 3's, at least one each 使用循环检查数组中的每个元素并返回 true 或 false - Using a loop to check each element in an array and return true or false 初始化布尔数组元素为true吗? - Initialize a boolean array element to be true? 给定一个整数数组 arr,编写一个 function 当且仅当数组中每个值的出现次数唯一时才返回 true - Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique CodingBat-Excercise:如果给定数组在数组的前 2 位或后 2 位包含不吉利的 1,则返回 true - CodingBat-Excercise: Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array Java 接受数组然后当每个元素是前一个元素值的两倍时返回true的递归方法 - Java Recursive method that accepts an array then returns true when each element is twice the value of the previous element Java:取2个数组查找是否存在于两个数组中的元素如果存在则返回true否则为false - Java : Taking 2 array find if the element that exists in both array return true if exist else false 如果输入在数组中,则返回true,否则返回false - If an input is in the array return true, else false 检查数组是否已排序,返回 true 或 false - Check if an array is sorted, return true or false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM