public boolean has22(int[] nums) {
boolean result=false;
int sum=0;
for(int n:nums){
if(n==2)
result=true;
}
return result;
}
public boolean has22(int[] nums) {
for(int y=0;y<nums.length-1;y++){xxxxxxxxxxx){return true;}}
return false;
}
public boolean has22(int[] nums) {
boolean result=false;
int sum=0;
int len=nums.length;
for(int i=0;i<len-1;i++){
if(nums[i]==2&&nums[i+1]==2)
result=true;
}
return result;
}
You could return true as soon as you find two 2's next to each other and save a little processing time.
public boolean has22(int[] nums) {
// boolean result=false;
int sum=0;
int len=nums.length;
for(int i=0;i<len-1;i++){
if(nums[i]==2&&nums[i+1]==2)
return true;
}
// return result;
return false;
}
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
17 Experts available now in Live!