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