leetcode/lc/1/ #2250
Replies: 4 comments 2 replies
-
最简单高效的解法,就是用哈希表 |
Beta Was this translation helpful? Give feedback.
-
其他解法 |
Beta Was this translation helpful? Give feedback.
-
zhuhui 2024.06.04 打卡 |
Beta Was this translation helpful? Give feedback.
-
Additionally to hash-table you can use approach with 2 pointers. function twoSum(nums: number[], target: number): number[] {
const map: Record<number, number> = {}
const n = nums.length
for (let l = 0, r = n - 1; l <= r; l++, r--) {
let v = nums[l]
let diff = target - v
if (map[diff] !== undefined) return [map[diff], l]
map[v] = l
v = nums[r]
diff = target - v
if (map[diff] !== undefined) return [map[diff], r]
map[v] = r
}
return []
} |
Beta Was this translation helpful? Give feedback.
-
leetcode/lc/1/
多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
https://doocs.github.io/leetcode/lc/1/
Beta Was this translation helpful? Give feedback.
All reactions