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