Problem:
Return true if the array contains, somewhere, three increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.
tripleUp({1, 4, 5, 6, 2}) → true
tripleUp({1, 2, 3}) → true
tripleUp({1, 2, 4}) → false
Solution:
public boolean tripleUp(int[] nums) { boolean foo = false; int count = 0; for (int i = 0; i < nums.length; i++) { if (i < nums.length-1) { int sum = nums[i+1] - nums[i]; if (sum == 1) count++; else count = 0; } if (count == 2) foo = true; } return foo; }
My solution:
ReplyDeletepublic boolean tripleUp(int[] nums) {
for (int i = 0; i < nums.length -1; i++) {
if (i > 0 && nums[i] - 1 == nums[i-1] && nums[i+1] - 1 == nums[i]) {
return true;
}
}
return false;
}
public boolean tripleUp(int[] nums) {
ReplyDeletefor(int i=0; i<nums.length-2; i++){
if( nums[i+2]-nums[i+1]==1 && nums[i+1]-nums[i]==1) return true;
}
return false;
}
public boolean tripleUp(int[] nums) {
ReplyDeleteboolean res = false;
for (int i=0; i<nums.length-2; i++){
if (nums[i]+1 == nums[i+1] && nums[i]+2 == nums[i+2]) res = true;
}
return res;
}
public boolean tripleUp(int[] nums) {
ReplyDeleteboolean triple = false;
for(int i=0; i1 && nums[i-2]+1==nums[i-1] && nums[i-1]+1==nums[i])
triple = true;
}
return triple;
}
public int[] shiftLeft(int[] nums) {
ReplyDeleteint tmp;
for (int i = 0; i < nums.length - 1; i++){
tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
}
return nums;
}
public boolean tripleUp(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length - 2; i ++) {
if (nums[i+1] == nums[i] + 1 && nums[i+2] == nums[i] + 2)
return true;
}
return false;
}
public boolean tripleUp(int[] nums)
ReplyDelete{
boolean res=false;
for(int i=0;i<nums.length-2;i++)
{
if(nums[i]==nums[i+1]-1 && nums[i]==nums[i+2]-2)
res=true;
}
return res;
}
public boolean tripleUp(int[] nums) {
ReplyDeletefor(int i=0;i<nums.length-2;i++){
if((nums[i+2]-nums[i+1]==1)&&(nums[i+1]-nums[i]==1))
return true;
}return false;
}
public boolean tripleUp(int[] nums) {
ReplyDeletefor(int i=0; i < nums.length-2; i++){
if((nums[i] + 1 == nums[i+1]) && (nums[i+1] + 1 == nums[i+2])){
return true;
}
}
return false;
}
public boolean tripleUp(int[] nums) {
ReplyDeleteboolean flag = false;
int temp = 0;
int temp2 = 0;
for(int i=0; i<nums.length-2 ; i++){
temp=nums[i]+1;
temp2=nums[i]+2;
if(nums[i+1]==temp && nums[i+2]==temp2){
flag = true;
}
}
return flag;
}