diff --git a/README.md b/README.md index 5de86bb4..f3b17a95 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ Step 2. Add the dependency 展开查看 +https://leetcode-cn.com/problems/count-nice-pairs-in-an-array/ + https://leetcode-cn.com/problems/sentence-similarity-iii/ https://leetcode.cn/problems/min-max-game/ diff --git a/count-nice-pairs-in-an-array/index.ts b/count-nice-pairs-in-an-array/index.ts new file mode 100644 index 00000000..14b0c740 --- /dev/null +++ b/count-nice-pairs-in-an-array/index.ts @@ -0,0 +1,17 @@ +function countNicePairs(nums: number[]): number { + + const MOD = 1000000007; + let res = 0; + const h = new Map(); + for (const i of nums) { + let temp = i, j = 0; + while (temp > 0) { + j = j * 10 + temp % 10; + temp = Math.floor(temp / 10); + } + res = (res + (h.get(i - j) || 0)) % MOD; + h.set(i - j, (h.get(i - j) || 0) + 1); + } + return res; +}; +export default countNicePairs