lc/1/ #2250
Replies: 7 comments 4 replies
-
最简单高效的解法,就是用哈希表 |
BetaWas this translation helpful?Give feedback.
-
其他解法 |
BetaWas this translation helpful?Give feedback.
-
zhuhui 2024.06.04 打卡 |
BetaWas this translation helpful?Give feedback.
-
Additionally to hash-table you can use approach with 2 pointers. functiontwoSum(nums: number[],target: number): number[]{constmap: Record<number,number>={}constn=nums.lengthfor(letl=0,r=n-1;l<=r;l++,r--){letv=nums[l]letdiff=target-vif(map[diff]!==undefined)return[map[diff],l]map[v]=lv=nums[r]diff=target-vif(map[diff]!==undefined)return[map[diff],r]map[v]=r}return[]} |
BetaWas this translation helpful?Give feedback.
-
我直接return num1 + num2,然后你告诉我还能这么些 |
BetaWas this translation helpful?Give feedback.
-
小白请教一下,在java的实现中为什么for的循环条件可以设置为空,这样不会导致无限循环吗 |
BetaWas 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: ***@***.***> |
BetaWas 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/
BetaWas this translation helpful?Give feedback.
All reactions