题目链接

LeetCode 1512. 好数对的数目

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @param {number[]} nums
* @return {number}
*/
var numIdenticalPairs = function(nums) {
let end = nums.length-1;
let start = 0;
let total = 0;

while(start != nums.length-1) {
if(nums[start] === nums[end]) {
total++;
}
end--;

if(end == start) {
end = nums.length-1;
start++;
}
}

return total;
};