isEverywhere([1, 2, 1, 3], 1) → true
isEverywhere([1, 2, 1, 3], 2) → false
isEverywhere([1, 2, 1, 3, 4], 1) → false
Answer 1:
public boolean isEverywhere(int[] nums, int val) {Answer 2:
for(int i=0;i<nums.length-1;i++){
if(nums[i]!=val && nums[i+1]!=val) return false;
}
return true;
}
public boolean isEverywhere(int[] nums, int val) {
boolean result = true;
for (int i = 0; i <=nums.length-2;i++)
{
if ( nums[i] != val && nums[i+1] != val)
result = false;
}
return result;
}
Reference
http://www.javaproblems.com/2012/12/coding-bat-java-array-2-iseverywhere.html
No comments :
Post a Comment