only14([1, 4, 1, 4]) → true
only14([1, 4, 2, 4]) → false
only14([1, 1]) → true
Answer 1:
public boolean only14(int[] nums) {Answer 2:
for (int i = 0; i < nums.length; i++){
if(nums[i]!=1 && nums[i]!=4) return false;
}
return true;
}
public boolean only14(int[] nums) {Reference
boolean isTrue = true;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 1 && nums[i] != 4)
isTrue = false;
}
return isTrue;
}
http://www.javaproblems.com/2013/11/java-array-2-only14-codingbat-solution.html
No comments :
Post a Comment