has77([1, 7, 7]) → true
has77([1, 7, 1, 7]) → true
has77([1, 7, 1, 1, 7]) → false
Answer 1:
public boolean has77(int[] nums) {
for(int i=0;i<nums.length-1;i++){
if(nums[i]==7&&nums[i+1]==7) return true;
if(i<nums.length-2 && (nums[i]==7&&nums[i+2]==7)) return true;
}
return false;
}
Answer 2:
public boolean has77(int[] nums) {
boolean result = false;
for (int i = 0; i < nums.length-1; i++)
if ((nums[i] == 7 && nums[i+1] == 7))
result = true;
for (int i = 0; i < nums.length-2; i++)
if ((nums[i] == 7 && nums[i+2] == 7))
result = true;
No comments :
Post a Comment