Given an array of ints, return true if .. 1, 2, 3, .. appears in the array somewhere.
array123([1, 1, 2, 3, 1]) → true array123([1, 1, 2, 4, 1]) → false array123([1, 1, 2, 1, 2, 3]) → true |
Hisoka :
1 2 3 4 5 6 7 8 9 10 | public boolean array123(int[] nums) { boolean satu=false,dua=false,tiga = false; for(int a:nums) { if(a == 1)satu = true; else if(a == 2) dua = true; else if(a == 3) tiga = true; } return satu && dua && tiga; } |
Hasil :
Codingbat:
1 2 3 4 5 6 7 | public boolean array123(int[] nums) { // Note: iterate < length-2, so can use i+1 and i+2 in the loop for (int i=0; i < (nums.length-2); i++) { if (nums[i]==1 && nums[i+1]==2 && nums[i+2]==3) return true; } return false; } |
No comments:
Post a Comment