Problem:
Given arrays nums1 and nums2 of the same length, for every element in nums1, consider the corresponding element in nums2 (at the same index). Return the count of the number of times that the two elements differ by 2 or less, but are not equal.
matchUp({1, 2, 3}, {2, 3, 10}) → 2
matchUp({1, 2, 3}, {2, 3, 5}) → 3
matchUp({1, 2, 3}, {2, 3, 3}) → 2
Solution:
public int matchUp(int[] nums1, int[] nums2) { int count = 0; for (int i = 0; i < nums1.length; i++) { int tmp = Math.abs(nums1[i] - nums2[i]); if (tmp <= 2 && tmp > 0) count++; } return count; }
This is my solution. I think yours is better.
ReplyDeletepublic int matchUp(int[] nums1, int[] nums2) {
int count=0;
for(int i=0;i<nums1.length && i<nums2.length;i++) {
if(Math.abs(nums1[i]-nums2[i])<=2 && (nums1[i]!=nums2[i])) count++;
}
return count;
}
This is my solution :
ReplyDeletepublic int matchUp(int[] nums1, int[] nums2) {
int count=0;
for(int i=0;i<nums1.length && i<nums2.length;i++){
if(Math.abs(nums1[i]-nums2[i])<=2 && Math.abs(nums1[i]-nums2[i])!=0) count++;
}
return count;
}
public int matchUp(int[] nums1, int[] nums2) {
ReplyDeleteint count=0;
for(int i=0;i0)){
count++;
}
}return count;
}
public int matchUp(int[] nums1, int[] nums2) {
ReplyDeleteint count=0;
for(int i=0;i0)){
count++;
}
}return count;
}
public int matchUp(int[] nums1, int[] nums2) {
ReplyDeleteint count = 0;
for(int i=0; i < nums1.length; i++){
int temp = Math.abs(nums1[i] - nums2[i]);
if(temp==1 || temp==2) {
count++;
}
}
return count;
}