From e91e98352436a49b99e61b5db28475867ae3d073 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 9 Aug 2024 15:24:07 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.0001 --- solution/0000-0099/0001.Two Sum/README.md | 145 +++++++++--------- solution/0000-0099/0001.Two Sum/README_EN.md | 145 +++++++++--------- solution/0000-0099/0001.Two Sum/Solution.cpp | 10 +- solution/0000-0099/0001.Two Sum/Solution.cs | 8 +- solution/0000-0099/0001.Two Sum/Solution.go | 8 +- solution/0000-0099/0001.Two Sum/Solution.java | 10 +- solution/0000-0099/0001.Two Sum/Solution.js | 8 +- solution/0000-0099/0001.Two Sum/Solution.nim | 17 +- solution/0000-0099/0001.Two Sum/Solution.php | 10 +- solution/0000-0099/0001.Two Sum/Solution.py | 8 +- solution/0000-0099/0001.Two Sum/Solution.rb | 12 +- solution/0000-0099/0001.Two Sum/Solution.rs | 8 +- .../0000-0099/0001.Two Sum/Solution.scala | 23 +-- .../0000-0099/0001.Two Sum/Solution.swift | 14 +- solution/0000-0099/0001.Two Sum/Solution.ts | 11 +- 15 files changed, 217 insertions(+), 220 deletions(-) diff --git a/solution/0000-0099/0001.Two Sum/README.md b/solution/0000-0099/0001.Two Sum/README.md index 41f53671375c1..0ac2190d21afe 100644 --- a/solution/0000-0099/0001.Two Sum/README.md +++ b/solution/0000-0099/0001.Two Sum/README.md @@ -70,11 +70,11 @@ tags: ### 方法一:哈希表 -我们可以用哈希表 $m$ 存放数组值以及对应的下标。 +我们可以使用一个哈希表 $\textit{d}$ 来存储每个元素及其对应的索引。 -遍历数组 `nums`,当发现 `target - nums[i]` 在哈希表中,说明找到了目标值,返回 `target - nums[i]` 的下标以及 $i$ 即可。 +遍历数组 $\textit{nums}$,对于当前元素 $\textit{nums}[i]$,我们首先判断 $\textit{target} - \textit{nums}[i]$ 是否在哈希表 $\textit{d}$ 中,如果在 $\textit{d}$ 中,说明 $\textit{target}$ 值已经找到,返回 $\textit{target} - \textit{nums}[i]$ 的索引和 $i$ 即可。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -83,12 +83,12 @@ tags: ```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: - m = {} + d = {} for i, x in enumerate(nums): y = target - x - if y in m: - return [m[y], i] - m[x] = i + if y in d: + return [d[y], i] + d[x] = i ``` #### Java @@ -96,14 +96,14 @@ class Solution: ```java class Solution { public int[] twoSum(int[] nums, int target) { - Map m = new HashMap<>(); + Map d = new HashMap<>(); for (int i = 0;; ++i) { int x = nums[i]; int y = target - x; - if (m.containsKey(y)) { - return new int[] {m.get(y), i}; + if (d.containsKey(y)) { + return new int[] {d.get(y), i}; } - m.put(x, i); + d.put(x, i); } } } @@ -115,14 +115,14 @@ class Solution { class Solution { public: vector twoSum(vector& nums, int target) { - unordered_map m; + unordered_map d; for (int i = 0;; ++i) { int x = nums[i]; int y = target - x; - if (m.count(y)) { - return {m[y], i}; + if (d.contains(y)) { + return {d[y], i}; } - m[x] = i; + d[x] = i; } } }; @@ -132,14 +132,14 @@ public: ```go func twoSum(nums []int, target int) []int { - m := map[int]int{} + d := map[int]int{} for i := 0; ; i++ { x := nums[i] y := target - x - if j, ok := m[y]; ok { + if j, ok := d[y]; ok { return []int{j, i} } - m[x] = i + d[x] = i } } ``` @@ -148,17 +148,14 @@ func twoSum(nums []int, target int) []int { ```ts function twoSum(nums: number[], target: number): number[] { - const m: Map = new Map(); - + const d = new Map(); for (let i = 0; ; ++i) { const x = nums[i]; const y = target - x; - - if (m.has(y)) { - return [m.get(y)!, i]; + if (d.has(y)) { + return [d.get(y)!, i]; } - - m.set(x, i); + d.set(x, i); } } ``` @@ -170,15 +167,15 @@ use std::collections::HashMap; impl Solution { pub fn two_sum(nums: Vec, target: i32) -> Vec { - let mut m = HashMap::new(); + let mut d = HashMap::new(); for (i, &x) in nums.iter().enumerate() { let y = target - x; - if let Some(&j) = m.get(&y) { + if let Some(&j) = d.get(&y) { return vec![j as i32, i as i32]; } - m.insert(x, i as i32); + d.insert(x, i); } - unreachable!() + vec![] } } ``` @@ -192,14 +189,14 @@ impl Solution { * @return {number[]} */ var twoSum = function (nums, target) { - const m = new Map(); + const d = new Map(); for (let i = 0; ; ++i) { const x = nums[i]; const y = target - x; - if (m.has(y)) { - return [m.get(y), i]; + if (d.has(y)) { + return [d.get(y), i]; } - m.set(x, i); + d.set(x, i); } }; ``` @@ -209,15 +206,15 @@ var twoSum = function (nums, target) { ```cs public class Solution { public int[] TwoSum(int[] nums, int target) { - var m = new Dictionary(); + var d = new Dictionary(); for (int i = 0, j; ; ++i) { int x = nums[i]; int y = target - x; - if (m.TryGetValue(y, out j)) { + if (d.TryGetValue(y, out j)) { return new [] {j, i}; } - if (!m.ContainsKey(x)) { - m.Add(x, i); + if (!d.ContainsKey(x)) { + d.Add(x, i); } } } @@ -234,13 +231,13 @@ class Solution { * @return Integer[] */ function twoSum($nums, $target) { - $m = []; + $d = []; foreach ($nums as $i => $x) { $y = $target - $x; - if (isset($m[$y])) { - return [$m[$y], $i]; + if (isset($d[$y])) { + return [$d[$y], $i]; } - $m[$x] = $i; + $d[$x] = $i; } } } @@ -252,17 +249,20 @@ class Solution { import scala.collection.mutable object Solution { - def twoSum(nums: Array[Int], target: Int): Array[Int] = { - var map = new mutable.HashMap[Int, Int]() - for (i <- 0 to nums.length) { - if (map.contains(target - nums(i))) { - return Array(map(target - nums(i)), i) - } else { - map += (nums(i) -> i) - } + def twoSum(nums: Array[Int], target: Int): Array[Int] = { + val d = mutable.Map[Int, Int]() + var ans: Array[Int] = Array() + for (i <- nums.indices if ans.isEmpty) { + val x = nums(i) + val y = target - x + if (d.contains(y)) { + ans = Array(d(y), i) + } else { + d(x) = i + } + } + ans } - Array(0, 0) - } } ``` @@ -271,17 +271,15 @@ object Solution { ```swift class Solution { func twoSum(_ nums: [Int], _ target: Int) -> [Int] { - var m = [Int: Int]() - var i = 0 - while true { - let x = nums[i] - let y = target - nums[i] - if let j = m[target - nums[i]] { + var d = [Int: Int]() + for (i, x) in nums.enumerated() { + let y = target - x + if let j = d[y] { return [j, i] } - m[nums[i]] = i - i += 1 + d[x] = i } + return [] } } ``` @@ -293,12 +291,14 @@ class Solution { # @param {Integer} target # @return {Integer[]} def two_sum(nums, target) - nums.each_with_index do |x, idx| - if nums.include? target - x - return [idx, nums.index(target - x)] if nums.index(target - x) != idx + d = {} + nums.each_with_index do |x, i| + y = target - x + if d.key?(y) + return [d[y], i] + end + d[x] = i end - next - end end ``` @@ -306,17 +306,16 @@ end ```nim import std/enumerate +import std/tables proc twoSum(nums: seq[int], target: int): seq[int] = - var - bal: int - tdx: int - for idx, val in enumerate(nums): - bal = target - val - if bal in nums: - tdx = nums.find(bal) - if idx != tdx: - return @[idx, tdx] + var d = initTable[int, int]() + for i, x in nums.pairs(): + let y = target - x + if d.hasKey(y): + return @[d[y], i] + d[x] = i + return @[] ``` diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index 798d0152d3b24..abdb96e24b706 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -67,11 +67,11 @@ tags: ### Solution 1: Hash Table -We can use the hash table $m$ to store the array value and the corresponding subscript. +We can use a hash table $\textit{d}$ to store each element and its corresponding index. -Traverse the array `nums`, when you find `target - nums[i]` in the hash table, it means that the target value is found, and the index of `target - nums[i]` and $i$ are returned. +Traverse the array $\textit{nums}$, for the current element $\textit{nums}[i]$, we first check if $\textit{target} - \textit{nums}[i]$ is in the hash table $\textit{d}$. If it is in $\textit{d}$, it means the $\textit{target}$ value has been found, and we return the indices of $\textit{target} - \textit{nums}[i]$ and $i$. -The time complexity is $O(n)$ and the space complexity is $O(n)$. Where $n$ is the length of the array `nums`. +Time complexity is $O(n)$, and space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. @@ -80,12 +80,12 @@ The time complexity is $O(n)$ and the space complexity is $O(n)$. Where $n$ is t ```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: - m = {} + d = {} for i, x in enumerate(nums): y = target - x - if y in m: - return [m[y], i] - m[x] = i + if y in d: + return [d[y], i] + d[x] = i ``` #### Java @@ -93,14 +93,14 @@ class Solution: ```java class Solution { public int[] twoSum(int[] nums, int target) { - Map m = new HashMap<>(); + Map d = new HashMap<>(); for (int i = 0;; ++i) { int x = nums[i]; int y = target - x; - if (m.containsKey(y)) { - return new int[] {m.get(y), i}; + if (d.containsKey(y)) { + return new int[] {d.get(y), i}; } - m.put(x, i); + d.put(x, i); } } } @@ -112,14 +112,14 @@ class Solution { class Solution { public: vector twoSum(vector& nums, int target) { - unordered_map m; + unordered_map d; for (int i = 0;; ++i) { int x = nums[i]; int y = target - x; - if (m.count(y)) { - return {m[y], i}; + if (d.contains(y)) { + return {d[y], i}; } - m[x] = i; + d[x] = i; } } }; @@ -129,14 +129,14 @@ public: ```go func twoSum(nums []int, target int) []int { - m := map[int]int{} + d := map[int]int{} for i := 0; ; i++ { x := nums[i] y := target - x - if j, ok := m[y]; ok { + if j, ok := d[y]; ok { return []int{j, i} } - m[x] = i + d[x] = i } } ``` @@ -145,17 +145,14 @@ func twoSum(nums []int, target int) []int { ```ts function twoSum(nums: number[], target: number): number[] { - const m: Map = new Map(); - + const d = new Map(); for (let i = 0; ; ++i) { const x = nums[i]; const y = target - x; - - if (m.has(y)) { - return [m.get(y)!, i]; + if (d.has(y)) { + return [d.get(y)!, i]; } - - m.set(x, i); + d.set(x, i); } } ``` @@ -167,15 +164,15 @@ use std::collections::HashMap; impl Solution { pub fn two_sum(nums: Vec, target: i32) -> Vec { - let mut m = HashMap::new(); + let mut d = HashMap::new(); for (i, &x) in nums.iter().enumerate() { let y = target - x; - if let Some(&j) = m.get(&y) { + if let Some(&j) = d.get(&y) { return vec![j as i32, i as i32]; } - m.insert(x, i as i32); + d.insert(x, i); } - unreachable!() + vec![] } } ``` @@ -189,14 +186,14 @@ impl Solution { * @return {number[]} */ var twoSum = function (nums, target) { - const m = new Map(); + const d = new Map(); for (let i = 0; ; ++i) { const x = nums[i]; const y = target - x; - if (m.has(y)) { - return [m.get(y), i]; + if (d.has(y)) { + return [d.get(y), i]; } - m.set(x, i); + d.set(x, i); } }; ``` @@ -206,15 +203,15 @@ var twoSum = function (nums, target) { ```cs public class Solution { public int[] TwoSum(int[] nums, int target) { - var m = new Dictionary(); + var d = new Dictionary(); for (int i = 0, j; ; ++i) { int x = nums[i]; int y = target - x; - if (m.TryGetValue(y, out j)) { + if (d.TryGetValue(y, out j)) { return new [] {j, i}; } - if (!m.ContainsKey(x)) { - m.Add(x, i); + if (!d.ContainsKey(x)) { + d.Add(x, i); } } } @@ -231,13 +228,13 @@ class Solution { * @return Integer[] */ function twoSum($nums, $target) { - $m = []; + $d = []; foreach ($nums as $i => $x) { $y = $target - $x; - if (isset($m[$y])) { - return [$m[$y], $i]; + if (isset($d[$y])) { + return [$d[$y], $i]; } - $m[$x] = $i; + $d[$x] = $i; } } } @@ -249,17 +246,20 @@ class Solution { import scala.collection.mutable object Solution { - def twoSum(nums: Array[Int], target: Int): Array[Int] = { - var map = new mutable.HashMap[Int, Int]() - for (i <- 0 to nums.length) { - if (map.contains(target - nums(i))) { - return Array(map(target - nums(i)), i) - } else { - map += (nums(i) -> i) - } + def twoSum(nums: Array[Int], target: Int): Array[Int] = { + val d = mutable.Map[Int, Int]() + var ans: Array[Int] = Array() + for (i <- nums.indices if ans.isEmpty) { + val x = nums(i) + val y = target - x + if (d.contains(y)) { + ans = Array(d(y), i) + } else { + d(x) = i + } + } + ans } - Array(0, 0) - } } ``` @@ -268,17 +268,15 @@ object Solution { ```swift class Solution { func twoSum(_ nums: [Int], _ target: Int) -> [Int] { - var m = [Int: Int]() - var i = 0 - while true { - let x = nums[i] - let y = target - nums[i] - if let j = m[target - nums[i]] { + var d = [Int: Int]() + for (i, x) in nums.enumerated() { + let y = target - x + if let j = d[y] { return [j, i] } - m[nums[i]] = i - i += 1 + d[x] = i } + return [] } } ``` @@ -290,12 +288,14 @@ class Solution { # @param {Integer} target # @return {Integer[]} def two_sum(nums, target) - nums.each_with_index do |x, idx| - if nums.include? target - x - return [idx, nums.index(target - x)] if nums.index(target - x) != idx + d = {} + nums.each_with_index do |x, i| + y = target - x + if d.key?(y) + return [d[y], i] + end + d[x] = i end - next - end end ``` @@ -303,17 +303,16 @@ end ```nim import std/enumerate +import std/tables proc twoSum(nums: seq[int], target: int): seq[int] = - var - bal: int - tdx: int - for idx, val in enumerate(nums): - bal = target - val - if bal in nums: - tdx = nums.find(bal) - if idx != tdx: - return @[idx, tdx] + var d = initTable[int, int]() + for i, x in nums.pairs(): + let y = target - x + if d.hasKey(y): + return @[d[y], i] + d[x] = i + return @[] ``` diff --git a/solution/0000-0099/0001.Two Sum/Solution.cpp b/solution/0000-0099/0001.Two Sum/Solution.cpp index 8ba078b1bc02e..8a7d8354e0614 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.cpp +++ b/solution/0000-0099/0001.Two Sum/Solution.cpp @@ -1,14 +1,14 @@ class Solution { public: vector twoSum(vector& nums, int target) { - unordered_map m; + unordered_map d; for (int i = 0;; ++i) { int x = nums[i]; int y = target - x; - if (m.count(y)) { - return {m[y], i}; + if (d.contains(y)) { + return {d[y], i}; } - m[x] = i; + d[x] = i; } } -}; \ No newline at end of file +}; diff --git a/solution/0000-0099/0001.Two Sum/Solution.cs b/solution/0000-0099/0001.Two Sum/Solution.cs index f01832c83837f..1a3fd2a6100c9 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.cs +++ b/solution/0000-0099/0001.Two Sum/Solution.cs @@ -1,14 +1,14 @@ public class Solution { public int[] TwoSum(int[] nums, int target) { - var m = new Dictionary(); + var d = new Dictionary(); for (int i = 0, j; ; ++i) { int x = nums[i]; int y = target - x; - if (m.TryGetValue(y, out j)) { + if (d.TryGetValue(y, out j)) { return new [] {j, i}; } - if (!m.ContainsKey(x)) { - m.Add(x, i); + if (!d.ContainsKey(x)) { + d.Add(x, i); } } } diff --git a/solution/0000-0099/0001.Two Sum/Solution.go b/solution/0000-0099/0001.Two Sum/Solution.go index 6c5c6ca899fff..9e43d34ab8880 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.go +++ b/solution/0000-0099/0001.Two Sum/Solution.go @@ -1,11 +1,11 @@ func twoSum(nums []int, target int) []int { - m := map[int]int{} + d := map[int]int{} for i := 0; ; i++ { x := nums[i] y := target - x - if j, ok := m[y]; ok { + if j, ok := d[y]; ok { return []int{j, i} } - m[x] = i + d[x] = i } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0001.Two Sum/Solution.java b/solution/0000-0099/0001.Two Sum/Solution.java index f0e517db5b9c3..8399b8e0af458 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.java +++ b/solution/0000-0099/0001.Two Sum/Solution.java @@ -1,13 +1,13 @@ class Solution { public int[] twoSum(int[] nums, int target) { - Map m = new HashMap<>(); + Map d = new HashMap<>(); for (int i = 0;; ++i) { int x = nums[i]; int y = target - x; - if (m.containsKey(y)) { - return new int[] {m.get(y), i}; + if (d.containsKey(y)) { + return new int[] {d.get(y), i}; } - m.put(x, i); + d.put(x, i); } } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0001.Two Sum/Solution.js b/solution/0000-0099/0001.Two Sum/Solution.js index 1d8802042aba1..3181501f6df19 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.js +++ b/solution/0000-0099/0001.Two Sum/Solution.js @@ -4,13 +4,13 @@ * @return {number[]} */ var twoSum = function (nums, target) { - const m = new Map(); + const d = new Map(); for (let i = 0; ; ++i) { const x = nums[i]; const y = target - x; - if (m.has(y)) { - return [m.get(y), i]; + if (d.has(y)) { + return [d.get(y), i]; } - m.set(x, i); + d.set(x, i); } }; diff --git a/solution/0000-0099/0001.Two Sum/Solution.nim b/solution/0000-0099/0001.Two Sum/Solution.nim index 5d5dc18ff9b38..91ad470acaff9 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.nim +++ b/solution/0000-0099/0001.Two Sum/Solution.nim @@ -1,12 +1,11 @@ import std/enumerate +import std/tables proc twoSum(nums: seq[int], target: int): seq[int] = - var - bal: int - tdx: int - for idx, val in enumerate(nums): - bal = target - val - if bal in nums: - tdx = nums.find(bal) - if idx != tdx: - return @[idx, tdx] + var d = initTable[int, int]() + for i, x in nums.pairs(): + let y = target - x + if d.hasKey(y): + return @[d[y], i] + d[x] = i + return @[] diff --git a/solution/0000-0099/0001.Two Sum/Solution.php b/solution/0000-0099/0001.Two Sum/Solution.php index e706f0382e539..1b90c6f04a2a7 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.php +++ b/solution/0000-0099/0001.Two Sum/Solution.php @@ -5,13 +5,13 @@ class Solution { * @return Integer[] */ function twoSum($nums, $target) { - $m = []; + $d = []; foreach ($nums as $i => $x) { $y = $target - $x; - if (isset($m[$y])) { - return [$m[$y], $i]; + if (isset($d[$y])) { + return [$d[$y], $i]; } - $m[$x] = $i; + $d[$x] = $i; } } -} \ No newline at end of file +} diff --git a/solution/0000-0099/0001.Two Sum/Solution.py b/solution/0000-0099/0001.Two Sum/Solution.py index 07e85af43d323..35a41d01ac455 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.py +++ b/solution/0000-0099/0001.Two Sum/Solution.py @@ -1,8 +1,8 @@ class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: - m = {} + d = {} for i, x in enumerate(nums): y = target - x - if y in m: - return [m[y], i] - m[x] = i + if y in d: + return [d[y], i] + d[x] = i diff --git a/solution/0000-0099/0001.Two Sum/Solution.rb b/solution/0000-0099/0001.Two Sum/Solution.rb index 26235fca40c3f..777f10f4bcf3f 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.rb +++ b/solution/0000-0099/0001.Two Sum/Solution.rb @@ -2,10 +2,12 @@ # @param {Integer} target # @return {Integer[]} def two_sum(nums, target) - nums.each_with_index do |x, idx| - if nums.include? target - x - return [idx, nums.index(target - x)] if nums.index(target - x) != idx + d = {} + nums.each_with_index do |x, i| + y = target - x + if d.key?(y) + return [d[y], i] + end + d[x] = i end - next - end end diff --git a/solution/0000-0099/0001.Two Sum/Solution.rs b/solution/0000-0099/0001.Two Sum/Solution.rs index 7f3e7361c7838..fbd2efd3e4c60 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.rs +++ b/solution/0000-0099/0001.Two Sum/Solution.rs @@ -2,14 +2,14 @@ use std::collections::HashMap; impl Solution { pub fn two_sum(nums: Vec, target: i32) -> Vec { - let mut m = HashMap::new(); + let mut d = HashMap::new(); for (i, &x) in nums.iter().enumerate() { let y = target - x; - if let Some(&j) = m.get(&y) { + if let Some(&j) = d.get(&y) { return vec![j as i32, i as i32]; } - m.insert(x, i as i32); + d.insert(x, i); } - unreachable!() + vec![] } } diff --git a/solution/0000-0099/0001.Two Sum/Solution.scala b/solution/0000-0099/0001.Two Sum/Solution.scala index 15e2a5ccec960..6a28ccb83e821 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.scala +++ b/solution/0000-0099/0001.Two Sum/Solution.scala @@ -1,15 +1,18 @@ import scala.collection.mutable object Solution { - def twoSum(nums: Array[Int], target: Int): Array[Int] = { - var map = new mutable.HashMap[Int, Int]() - for (i <- 0 to nums.length) { - if (map.contains(target - nums(i))) { - return Array(map(target - nums(i)), i) - } else { - map += (nums(i) -> i) - } + def twoSum(nums: Array[Int], target: Int): Array[Int] = { + val d = mutable.Map[Int, Int]() + var ans: Array[Int] = Array() + for (i <- nums.indices if ans.isEmpty) { + val x = nums(i) + val y = target - x + if (d.contains(y)) { + ans = Array(d(y), i) + } else { + d(x) = i + } + } + ans } - Array(0, 0) - } } diff --git a/solution/0000-0099/0001.Two Sum/Solution.swift b/solution/0000-0099/0001.Two Sum/Solution.swift index 6a84df9aaf965..0f53f111fed5c 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.swift +++ b/solution/0000-0099/0001.Two Sum/Solution.swift @@ -1,15 +1,13 @@ class Solution { func twoSum(_ nums: [Int], _ target: Int) -> [Int] { - var m = [Int: Int]() - var i = 0 - while true { - let x = nums[i] - let y = target - nums[i] - if let j = m[target - nums[i]] { + var d = [Int: Int]() + for (i, x) in nums.enumerated() { + let y = target - x + if let j = d[y] { return [j, i] } - m[nums[i]] = i - i += 1 + d[x] = i } + return [] } } diff --git a/solution/0000-0099/0001.Two Sum/Solution.ts b/solution/0000-0099/0001.Two Sum/Solution.ts index 624e48d251021..3e425cb5ca1cf 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.ts +++ b/solution/0000-0099/0001.Two Sum/Solution.ts @@ -1,14 +1,11 @@ function twoSum(nums: number[], target: number): number[] { - const m: Map = new Map(); - + const d = new Map(); for (let i = 0; ; ++i) { const x = nums[i]; const y = target - x; - - if (m.has(y)) { - return [m.get(y)!, i]; + if (d.has(y)) { + return [d.get(y)!, i]; } - - m.set(x, i); + d.set(x, i); } }