lc/1/ #2250
Replies: 7 comments 4 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.
-
我直接return num1 + num2,然后你告诉我还能这么些 |
Beta Was this translation helpful? Give feedback.
-
小白请教一下,在java的实现中为什么for的循环条件可以设置为空,这样不会导致无限循环吗 |
Beta Was this translation helpful? Give feedback.
-
java有两种for循环。这里只讲和C语言格式一致的for循环。你只需要知道for循环代码块的执行顺序就可以了。确实可以设空。按顺序执行能退出就不是无限循环。
for(1;2;4){
3
}
执行顺序是这样的。
另一种for循环,for(元素变量:集合){
...
}
我不是特别清楚,应该集合为空或者迭代到最后一个元素就可以退出了吧
…---Original---
From: ***@***.***>
Date: Mon, May 12, 2025 22:39 PM
To: ***@***.***>;
Cc: ***@***.******@***.***>;
Subject: Re: [doocs/leetcode] lc/1/ (Discussion #2250)
小白请教一下,在java的实现中为什么for的循环条件可以设置为空,这样不会导致无限循环吗
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
lc/1/
多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
https://leetcode.doocs.org/lc/1/
Beta Was this translation helpful? Give feedback.
All reactions