Skip to content

Commit c78aa1e

Browse files
authored
feat: update solutions to lc problems: No.0001,0035 (#2467)
1 parent 316b8e8 commit c78aa1e

File tree

7 files changed

+68
-36
lines changed

7 files changed

+68
-36
lines changed

solution/0000-0099/0001.Two Sum/README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,13 @@ use std::collections::HashMap;
145145

146146
impl Solution {
147147
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
148-
let mut map = HashMap::new();
149-
for (i, item) in nums.iter().enumerate() {
150-
if map.contains_key(item) {
151-
return vec![i as i32, map[item]];
152-
} else {
153-
let x = target - nums[i];
154-
map.insert(x, i as i32);
148+
let mut m = HashMap::new();
149+
for (i, &x) in nums.iter().enumerate() {
150+
let y = target - x;
151+
if let Some(&j) = m.get(&y) {
152+
return vec![j as i32, i as i32];
155153
}
154+
m.insert(x, i as i32);
156155
}
157156
unreachable!()
158157
}
@@ -204,12 +203,13 @@ class Solution {
204203
* @return Integer[]
205204
*/
206205
function twoSum($nums, $target) {
207-
foreach ($nums as $key => $x) {
206+
$m = [];
207+
foreach ($nums as $i => $x) {
208208
$y = $target - $x;
209-
if (isset($hashtable[$y])) {
210-
return [$hashtable[$y], $key];
209+
if (isset($m[$y])) {
210+
return [$m[$y], $i];
211211
}
212-
$hashtable[$x] = $key;
212+
$m[$x] = $i;
213213
}
214214
}
215215
}

solution/0000-0099/0001.Two Sum/README_EN.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,13 @@ use std::collections::HashMap;
140140

141141
impl Solution {
142142
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
143-
let mut map = HashMap::new();
144-
for (i, item) in nums.iter().enumerate() {
145-
if map.contains_key(item) {
146-
return vec![i as i32, map[item]];
147-
} else {
148-
let x = target - nums[i];
149-
map.insert(x, i as i32);
143+
let mut m = HashMap::new();
144+
for (i, &x) in nums.iter().enumerate() {
145+
let y = target - x;
146+
if let Some(&j) = m.get(&y) {
147+
return vec![j as i32, i as i32];
150148
}
149+
m.insert(x, i as i32);
151150
}
152151
unreachable!()
153152
}
@@ -199,12 +198,13 @@ class Solution {
199198
* @return Integer[]
200199
*/
201200
function twoSum($nums, $target) {
202-
foreach ($nums as $key => $x) {
201+
$m = [];
202+
foreach ($nums as $i => $x) {
203203
$y = $target - $x;
204-
if (isset($hashtable[$y])) {
205-
return [$hashtable[$y], $key];
204+
if (isset($m[$y])) {
205+
return [$m[$y], $i];
206206
}
207-
$hashtable[$x] = $key;
207+
$m[$x] = $i;
208208
}
209209
}
210210
}

solution/0000-0099/0001.Two Sum/Solution.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ class Solution {
55
* @return Integer[]
66
*/
77
function twoSum($nums, $target) {
8-
foreach ($nums as $key => $x) {
8+
$m = [];
9+
foreach ($nums as $i => $x) {
910
$y = $target - $x;
10-
if (isset($hashtable[$y])) {
11-
return [$hashtable[$y], $key];
11+
if (isset($m[$y])) {
12+
return [$m[$y], $i];
1213
}
13-
$hashtable[$x] = $key;
14+
$m[$x] = $i;
1415
}
1516
}
16-
}
17+
}

solution/0000-0099/0001.Two Sum/Solution.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use std::collections::HashMap;
22

33
impl Solution {
44
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
5-
let mut map = HashMap::new();
6-
for (i, item) in nums.iter().enumerate() {
7-
if map.contains_key(item) {
8-
return vec![i as i32, map[item]];
9-
} else {
10-
let x = target - nums[i];
11-
map.insert(x, i as i32);
5+
let mut m = HashMap::new();
6+
for (i, &x) in nums.iter().enumerate() {
7+
let y = target - x;
8+
if let Some(&j) = m.get(&y) {
9+
return vec![j as i32, i as i32];
1210
}
11+
m.insert(x, i as i32);
1312
}
1413
unreachable!()
1514
}

solution/0000-0099/0035.Search Insert Position/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ var searchInsert = function (nums, target) {
166166

167167
<!-- tabs:end -->
168168

169-
### 方法二
169+
### 方法二:二分查找(内置函数)
170+
171+
我们也可以直接使用内置函数进行二分查找。
172+
173+
时间复杂度 $O(\log n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。
170174

171175
<!-- tabs:start -->
172176

@@ -176,6 +180,15 @@ class Solution:
176180
return bisect_left(nums, target)
177181
```
178182

183+
```java
184+
class Solution {
185+
public int searchInsert(int[] nums, int target) {
186+
int i = Arrays.binarySearch(nums, target);
187+
return i < 0 ? -i - 1 : i;
188+
}
189+
}
190+
```
191+
179192
```cpp
180193
class Solution {
181194
public:

solution/0000-0099/0035.Search Insert Position/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ var searchInsert = function (nums, target) {
162162

163163
<!-- tabs:end -->
164164

165-
### Solution 2
165+
### Solution 2: Binary Search (Built-in Function)
166+
167+
We can also directly use the built-in function for binary search.
168+
169+
The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
166170

167171
<!-- tabs:start -->
168172

@@ -172,6 +176,15 @@ class Solution:
172176
return bisect_left(nums, target)
173177
```
174178

179+
```java
180+
class Solution {
181+
public int searchInsert(int[] nums, int target) {
182+
int i = Arrays.binarySearch(nums, target);
183+
return i < 0 ? -i - 1 : i;
184+
}
185+
}
186+
```
187+
175188
```cpp
176189
class Solution {
177190
public:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public int searchInsert(int[] nums, int target) {
3+
int i = Arrays.binarySearch(nums, target);
4+
return i < 0 ? -i - 1 : i;
5+
}
6+
}

0 commit comments

Comments
 (0)