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;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
eclipse package explorer vs project explorer view | 2 | 79 | |
Rubik's Cube Code for Effective Presentation | 3 | 40 | |
Plain Text Editor for iPad | 6 | 57 | |
Meaning of "exceptions()" in "ist.exceptions( ist.exceptions() | ios_base:: badbit);" | 3 | 55 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
17 Experts available now in Live!