Problem:
Return true if the group of N numbers at the start and end of the array are the same. For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume that n is in the range 0..nums.length inclusive.
sameEnds({5, 6, 45, 99, 13, 5, 6}, 1) → false
sameEnds({5, 6, 45, 99, 13, 5, 6}, 2) → true
sameEnds({5, 6, 45, 99, 13, 5, 6}, 3) → false
Solution:
public boolean sameEnds(int[] nums, int len) { boolean foo = true; for (int i = 0; i < len; i++) { if (nums[i] == nums[nums.length-len+i]) foo = true; else foo = false; } return foo; }
public boolean sameEnds(int[] nums, int len) {
ReplyDeletefor(int i = 0, j = nums.length - len; i < len; i++, j++) {
if(nums[i] != nums[j]) return false;
}
return true;
}
public boolean sameEnds(int[] nums, int len) {
ReplyDeleteint[] arr1 = new int[len];
int[] arr2 = new int[len];
boolean check = true;
for (int i = 0; i < len; i++){
arr1[i] = nums[i];
arr2[i] = nums[nums.length-1];
}
for (int i = 0; i < len; i++){
if (arr1[i] == arr2[i])
check = true;
else
check = false;
}
return check;
}
public boolean sameEnds(int[] nums, int len) {
ReplyDeleteint [] A = new int[len];
int [] B = new int[len];
for(int i=0;i<len;i++){
A[i] = nums[i];}
for(int i=0;i<len;i++){
B[i] = nums[nums.length-len+i];}
return Arrays.equals(A, B);
}
public boolean sameEnds(int[] nums, int len) {
ReplyDeleteint count=0;
if (len==0) return true;
for (int i=0; i<len; i++) {
if (nums[i] == nums[nums.length-len+i]) count++;
}
if(count==len) return true;
else return false;
}
public boolean sameEnds(int[] nums, int len) {
ReplyDeleteint length = nums.length;
boolean isSame = false;
int[] newArr1 = new int[len];
int[] newArr2 = new int[len];
for(int index = 0; index < len; index++) {
newArr1[index] = nums[index];
}
for(int index = 0; index < len; index++){
newArr2[index] = nums[length - len + index];
}
if(Arrays.equals(newArr1, newArr2)) {
isSame = true;
}
return isSame;
}