diff --git a/solution/0000-0099/0001.Two Sum/README.md b/solution/0000-0099/0001.Two Sum/README.md index 0cad633c89bfe..b4d9cc164cd70 100644 --- a/solution/0000-0099/0001.Two Sum/README.md +++ b/solution/0000-0099/0001.Two Sum/README.md @@ -145,14 +145,13 @@ use std::collections::HashMap; impl Solution { pub fn two_sum(nums: Vec, target: i32) -> Vec { - let mut map = HashMap::new(); - for (i, item) in nums.iter().enumerate() { - if map.contains_key(item) { - return vec![i as i32, map[item]]; - } else { - let x = target - nums[i]; - map.insert(x, i as i32); + let mut m = HashMap::new(); + for (i, &x) in nums.iter().enumerate() { + let y = target - x; + if let Some(&j) = m.get(&y) { + return vec![j as i32, i as i32]; } + m.insert(x, i as i32); } unreachable!() } @@ -204,12 +203,13 @@ class Solution { * @return Integer[] */ function twoSum($nums, $target) { - foreach ($nums as $key => $x) { + $m = []; + foreach ($nums as $i => $x) { $y = $target - $x; - if (isset($hashtable[$y])) { - return [$hashtable[$y], $key]; + if (isset($m[$y])) { + return [$m[$y], $i]; } - $hashtable[$x] = $key; + $m[$x] = $i; } } } diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index 16cb2ee53b89a..43743e8133f42 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -140,14 +140,13 @@ use std::collections::HashMap; impl Solution { pub fn two_sum(nums: Vec, target: i32) -> Vec { - let mut map = HashMap::new(); - for (i, item) in nums.iter().enumerate() { - if map.contains_key(item) { - return vec![i as i32, map[item]]; - } else { - let x = target - nums[i]; - map.insert(x, i as i32); + let mut m = HashMap::new(); + for (i, &x) in nums.iter().enumerate() { + let y = target - x; + if let Some(&j) = m.get(&y) { + return vec![j as i32, i as i32]; } + m.insert(x, i as i32); } unreachable!() } @@ -199,12 +198,13 @@ class Solution { * @return Integer[] */ function twoSum($nums, $target) { - foreach ($nums as $key => $x) { + $m = []; + foreach ($nums as $i => $x) { $y = $target - $x; - if (isset($hashtable[$y])) { - return [$hashtable[$y], $key]; + if (isset($m[$y])) { + return [$m[$y], $i]; } - $hashtable[$x] = $key; + $m[$x] = $i; } } } diff --git a/solution/0000-0099/0001.Two Sum/Solution.php b/solution/0000-0099/0001.Two Sum/Solution.php index 525b923056574..e706f0382e539 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.php +++ b/solution/0000-0099/0001.Two Sum/Solution.php @@ -5,12 +5,13 @@ class Solution { * @return Integer[] */ function twoSum($nums, $target) { - foreach ($nums as $key => $x) { + $m = []; + foreach ($nums as $i => $x) { $y = $target - $x; - if (isset($hashtable[$y])) { - return [$hashtable[$y], $key]; + if (isset($m[$y])) { + return [$m[$y], $i]; } - $hashtable[$x] = $key; + $m[$x] = $i; } } -} +} \ No newline at end of file diff --git a/solution/0000-0099/0001.Two Sum/Solution.rs b/solution/0000-0099/0001.Two Sum/Solution.rs index d7b994288ccd5..7f3e7361c7838 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.rs +++ b/solution/0000-0099/0001.Two Sum/Solution.rs @@ -2,14 +2,13 @@ use std::collections::HashMap; impl Solution { pub fn two_sum(nums: Vec, target: i32) -> Vec { - let mut map = HashMap::new(); - for (i, item) in nums.iter().enumerate() { - if map.contains_key(item) { - return vec![i as i32, map[item]]; - } else { - let x = target - nums[i]; - map.insert(x, i as i32); + let mut m = HashMap::new(); + for (i, &x) in nums.iter().enumerate() { + let y = target - x; + if let Some(&j) = m.get(&y) { + return vec![j as i32, i as i32]; } + m.insert(x, i as i32); } unreachable!() } diff --git a/solution/0000-0099/0035.Search Insert Position/README.md b/solution/0000-0099/0035.Search Insert Position/README.md index a159d931af49d..82642cde9ea99 100644 --- a/solution/0000-0099/0035.Search Insert Position/README.md +++ b/solution/0000-0099/0035.Search Insert Position/README.md @@ -166,7 +166,11 @@ var searchInsert = function (nums, target) { -### 方法二 +### 方法二:二分查找(内置函数) + +我们也可以直接使用内置函数进行二分查找。 + +时间复杂度 $O(\log n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。 @@ -176,6 +180,15 @@ class Solution: return bisect_left(nums, target) ``` +```java +class Solution { + public int searchInsert(int[] nums, int target) { + int i = Arrays.binarySearch(nums, target); + return i < 0 ? -i - 1 : i; + } +} +``` + ```cpp class Solution { public: diff --git a/solution/0000-0099/0035.Search Insert Position/README_EN.md b/solution/0000-0099/0035.Search Insert Position/README_EN.md index f6fad6f757e41..e2f68d799a720 100644 --- a/solution/0000-0099/0035.Search Insert Position/README_EN.md +++ b/solution/0000-0099/0035.Search Insert Position/README_EN.md @@ -162,7 +162,11 @@ var searchInsert = function (nums, target) { -### Solution 2 +### Solution 2: Binary Search (Built-in Function) + +We can also directly use the built-in function for binary search. + +The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$. @@ -172,6 +176,15 @@ class Solution: return bisect_left(nums, target) ``` +```java +class Solution { + public int searchInsert(int[] nums, int target) { + int i = Arrays.binarySearch(nums, target); + return i < 0 ? -i - 1 : i; + } +} +``` + ```cpp class Solution { public: diff --git a/solution/0000-0099/0035.Search Insert Position/Solution2.java b/solution/0000-0099/0035.Search Insert Position/Solution2.java new file mode 100644 index 0000000000000..8f769a852266c --- /dev/null +++ b/solution/0000-0099/0035.Search Insert Position/Solution2.java @@ -0,0 +1,6 @@ +class Solution { + public int searchInsert(int[] nums, int target) { + int i = Arrays.binarySearch(nums, target); + return i < 0 ? -i - 1 : i; + } +} \ No newline at end of file