Skip to content

Commit b278073

Browse files
committed
https://leetcode.cn/problems/two-sum-iii-data-structure-design/
1 parent 7e65333 commit b278073

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/two-sum-iii-data-structure-design/
49+
4850
https://leetcode.cn/problems/minimum-adjacent-swaps-for-k-consecutive-ones
4951

5052
https://leetcode.cn/problems/form-array-by-concatenating-subarrays-of-another-array/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default class TwoSum {
2+
#m = new Map<number, number>();
3+
4+
add(n: number) {
5+
this.#m.set(n, (this.#m.get(n) ?? 0) + 1);
6+
}
7+
find(n: number) {
8+
for (const [a, cnt] of this.#m) {
9+
const b = n - a;
10+
if (a > b) return false;
11+
if (a === b) return cnt > 1;
12+
13+
if (this.#m.has(b)) return true;
14+
}
15+
return false;
16+
}
17+
}

0 commit comments

Comments
 (0)