File tree 7 files changed +68
-36
lines changed
0035.Search Insert Position
7 files changed +68
-36
lines changed Original file line number Diff line number Diff line change @@ -145,14 +145,13 @@ use std::collections::HashMap;
145
145
146
146
impl Solution {
147
147
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 ];
155
153
}
154
+ m . insert (x , i as i32 );
156
155
}
157
156
unreachable! ()
158
157
}
@@ -204,12 +203,13 @@ class Solution {
204
203
* @return Integer[]
205
204
*/
206
205
function twoSum($nums, $target) {
207
- foreach ($nums as $key => $x) {
206
+ $m = [];
207
+ foreach ($nums as $i => $x) {
208
208
$y = $target - $x;
209
- if (isset($hashtable [$y])) {
210
- return [$hashtable [$y], $key ];
209
+ if (isset($m [$y])) {
210
+ return [$m [$y], $i ];
211
211
}
212
- $hashtable [$x] = $key ;
212
+ $m [$x] = $i ;
213
213
}
214
214
}
215
215
}
Original file line number Diff line number Diff line change @@ -140,14 +140,13 @@ use std::collections::HashMap;
140
140
141
141
impl Solution {
142
142
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 ];
150
148
}
149
+ m . insert (x , i as i32 );
151
150
}
152
151
unreachable! ()
153
152
}
@@ -199,12 +198,13 @@ class Solution {
199
198
* @return Integer[]
200
199
*/
201
200
function twoSum($nums, $target) {
202
- foreach ($nums as $key => $x) {
201
+ $m = [];
202
+ foreach ($nums as $i => $x) {
203
203
$y = $target - $x;
204
- if (isset($hashtable [$y])) {
205
- return [$hashtable [$y], $key ];
204
+ if (isset($m [$y])) {
205
+ return [$m [$y], $i ];
206
206
}
207
- $hashtable [$x] = $key ;
207
+ $m [$x] = $i ;
208
208
}
209
209
}
210
210
}
Original file line number Diff line number Diff line change @@ -5,12 +5,13 @@ class Solution {
5
5
* @return Integer[]
6
6
*/
7
7
function twoSum($nums, $target) {
8
- foreach ($nums as $key => $x) {
8
+ $m = [];
9
+ foreach ($nums as $i => $x) {
9
10
$y = $target - $x;
10
- if (isset($hashtable [$y])) {
11
- return [$hashtable [$y], $key ];
11
+ if (isset($m [$y])) {
12
+ return [$m [$y], $i ];
12
13
}
13
- $hashtable [$x] = $key ;
14
+ $m [$x] = $i ;
14
15
}
15
16
}
16
- }
17
+ }
Original file line number Diff line number Diff line change @@ -2,14 +2,13 @@ use std::collections::HashMap;
2
2
3
3
impl Solution {
4
4
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 ] ;
12
10
}
11
+ m. insert ( x, i as i32 ) ;
13
12
}
14
13
unreachable ! ( )
15
14
}
Original file line number Diff line number Diff line change @@ -166,7 +166,11 @@ var searchInsert = function (nums, target) {
166
166
167
167
<!-- tabs: end -->
168
168
169
- ### 方法二
169
+ ### 方法二:二分查找(内置函数)
170
+
171
+ 我们也可以直接使用内置函数进行二分查找。
172
+
173
+ 时间复杂度 $O(\log n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。
170
174
171
175
<!-- tabs: start -->
172
176
@@ -176,6 +180,15 @@ class Solution:
176
180
return bisect_left(nums, target)
177
181
```
178
182
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
+
179
192
``` cpp
180
193
class Solution {
181
194
public:
Original file line number Diff line number Diff line change @@ -162,7 +162,11 @@ var searchInsert = function (nums, target) {
162
162
163
163
<!-- tabs: end -->
164
164
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)$.
166
170
167
171
<!-- tabs: start -->
168
172
@@ -172,6 +176,15 @@ class Solution:
172
176
return bisect_left(nums, target)
173
177
```
174
178
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
+
175
188
``` cpp
176
189
class Solution {
177
190
public:
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments