gudii9
asked on
has22 challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p121853
I wrote my code as below
I am not passing all tests
Expected Run
has22([1, 2, 2]) → true true OK
has22([1, 2, 1, 2]) → false true X
has22([2, 1, 2]) → false true X
has22([2, 2, 1, 2]) → true true OK
has22([1, 3, 2]) → false true X
has22([1, 3, 2, 2]) → true true OK
has22([2, 3, 2, 2]) → true true OK
has22([4, 2, 4, 2, 2, 5]) → true true OK
has22([1, 2]) → false true X
has22([2, 2]) → true true OK
has22([2]) → false true X
has22([]) → false false OK
has22([3, 3, 2, 2]) → true true OK
has22([5, 2, 5, 2]) → false true X
other tests
X
How to improve my design, approach, code? please advise
I am working on below challenge
http://codingbat.com/prob/p121853
I wrote my code as below
public boolean has22(int[] nums) {
boolean result=false;
int sum=0;
for(int n:nums){
if(n==2)
result=true;
}
return result;
}
I am not passing all tests
Expected Run
has22([1, 2, 2]) → true true OK
has22([1, 2, 1, 2]) → false true X
has22([2, 1, 2]) → false true X
has22([2, 2, 1, 2]) → true true OK
has22([1, 3, 2]) → false true X
has22([1, 3, 2, 2]) → true true OK
has22([2, 3, 2, 2]) → true true OK
has22([4, 2, 4, 2, 2, 5]) → true true OK
has22([1, 2]) → false true X
has22([2, 2]) → true true OK
has22([2]) → false true X
has22([]) → false false OK
has22([3, 3, 2, 2]) → true true OK
has22([5, 2, 5, 2]) → false true X
other tests
X
How to improve my design, approach, code? please advise
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Is this snippet relevant or not in this question:
if(nums[y]==2&&nums[y+1]==2)
ASKER
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;
}
above passed all tests? any improvements, suggestions, comments?
You could return true as soon as you find two 2's next to each other and save a little processing time. (If you have an array that's a thousand elements long and the first two elements are 2's, you don't need to check the rest of the array, since you already know you'll be returning true.)
ASKER
You could return true as soon as you find two 2's next to each other and save a little processing time.
how to do this?
return true;
ASKER
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;
}
i see . this is very powerful to use return here.
Yep! You can't always break out of a loop early or return from a loop early, but it can be nice, when you can.
i see . this is very powerful to use return here.
I ALREADY posted code with this return clause in it !
Did you even *look* at that ???
You ARE a troll, are you not ????
ASKER