Ques: Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.
Ans:
public static boolean has22(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] ==2&& nums[i + 1]==2) {
count++;
}
}
return count > 0 ? true : false;
}
Ans:
public static boolean has22(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] ==2&& nums[i + 1]==2) {
count++;
}
}
return count > 0 ? true : false;
}
No comments:
Post a Comment