matchUp([1, 2, 3], [2, 3, 10]) → 2
matchUp([1, 2, 3], [2, 3, 5]) → 3
matchUp([1, 2, 3], [2, 3, 3]) → 2
Answer 1:
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;
}
Answer 2:
public int matchUp(int[] nums1, int[] nums2) {
int diffCount = 0;
for(int i=0;i<nums1.length;i++){
for(int j=0;j<nums2.length;j++){
if(i==j){
int diff = Math.abs(nums2[j] - nums1[i]);
if(diff<=2 && diff!=0) diffCount++;
}
}
}
return diffCount;
}
No comments :
Post a Comment