File tree Expand file tree Collapse file tree 6 files changed +76
-24
lines changed
0000-0099/0035.Search Insert Position
0800-0899/0852.Peak Index in a Mountain Array Expand file tree Collapse file tree 6 files changed +76
-24
lines changed Original file line number Diff line number Diff line change @@ -152,20 +152,19 @@ var searchInsert = function (nums, target) {
152
152
153
153
``` rust
154
154
use std :: cmp :: Ordering ;
155
-
156
155
impl Solution {
157
156
pub fn search_insert (nums : Vec <i32 >, target : i32 ) -> i32 {
158
- let mut l = 0 ;
159
- let mut r = nums . len ();
160
- while l < r {
161
- let mid = l + (r - l ) / 2 ;
157
+ let mut left = 0 ;
158
+ let mut right = nums . len ();
159
+ while left < right {
160
+ let mid = left + (right - left ) / 2 ;
162
161
match nums [mid ]. cmp (& target ) {
163
- Ordering :: Less => l = mid + 1 ,
164
- Ordering :: Greater => r = mid ,
162
+ Ordering :: Less => left = mid + 1 ,
163
+ Ordering :: Greater => right = mid ,
165
164
Ordering :: Equal => return mid as i32 ,
166
165
}
167
166
}
168
- l as i32
167
+ left as i32
169
168
}
170
169
}
171
170
```
Original file line number Diff line number Diff line change @@ -142,20 +142,19 @@ var searchInsert = function (nums, target) {
142
142
143
143
``` rust
144
144
use std :: cmp :: Ordering ;
145
-
146
145
impl Solution {
147
146
pub fn search_insert (nums : Vec <i32 >, target : i32 ) -> i32 {
148
- let mut l = 0 ;
149
- let mut r = nums . len ();
150
- while l < r {
151
- let mid = l + (r - l ) / 2 ;
147
+ let mut left = 0 ;
148
+ let mut right = nums . len ();
149
+ while left < right {
150
+ let mid = left + (right - left ) / 2 ;
152
151
match nums [mid ]. cmp (& target ) {
153
- Ordering :: Less => l = mid + 1 ,
154
- Ordering :: Greater => r = mid ,
152
+ Ordering :: Less => left = mid + 1 ,
153
+ Ordering :: Greater => right = mid ,
155
154
Ordering :: Equal => return mid as i32 ,
156
155
}
157
156
}
158
- l as i32
157
+ left as i32
159
158
}
160
159
}
161
160
```
Original file line number Diff line number Diff line change 1
1
use std:: cmp:: Ordering ;
2
-
3
2
impl Solution {
4
3
pub fn search_insert ( nums : Vec < i32 > , target : i32 ) -> i32 {
5
- let mut l = 0 ;
6
- let mut r = nums. len ( ) ;
7
- while l < r {
8
- let mid = l + ( r - l ) / 2 ;
4
+ let mut left = 0 ;
5
+ let mut right = nums. len ( ) ;
6
+ while left < right {
7
+ let mid = left + ( right - left ) / 2 ;
9
8
match nums[ mid] . cmp ( & target) {
10
- Ordering :: Less => l = mid + 1 ,
11
- Ordering :: Greater => r = mid,
9
+ Ordering :: Less => left = mid + 1 ,
10
+ Ordering :: Greater => right = mid,
12
11
Ordering :: Equal => return mid as i32 ,
13
12
}
14
13
}
15
- l as i32
14
+ left as i32
16
15
}
17
16
}
Original file line number Diff line number Diff line change @@ -192,6 +192,26 @@ function peakIndexInMountainArray(arr: number[]): number {
192
192
}
193
193
```
194
194
195
+ ### ** Rust**
196
+
197
+ ``` rust
198
+ impl Solution {
199
+ pub fn peak_index_in_mountain_array (arr : Vec <i32 >) -> i32 {
200
+ let mut left = 1 ;
201
+ let mut right = arr . len () - 2 ;
202
+ while left < right {
203
+ let mid = left + (right - left ) / 2 ;
204
+ if arr [mid ] > arr [mid + 1 ] {
205
+ right = mid ;
206
+ } else {
207
+ left = left + 1 ;
208
+ }
209
+ }
210
+ left as i32
211
+ }
212
+ }
213
+ ```
214
+
195
215
### ** ...**
196
216
197
217
```
Original file line number Diff line number Diff line change @@ -167,6 +167,26 @@ function peakIndexInMountainArray(arr: number[]): number {
167
167
}
168
168
```
169
169
170
+ ### ** Rust**
171
+
172
+ ``` rust
173
+ impl Solution {
174
+ pub fn peak_index_in_mountain_array (arr : Vec <i32 >) -> i32 {
175
+ let mut left = 1 ;
176
+ let mut right = arr . len () - 2 ;
177
+ while left < right {
178
+ let mid = left + (right - left ) / 2 ;
179
+ if arr [mid ] > arr [mid + 1 ] {
180
+ right = mid ;
181
+ } else {
182
+ left = left + 1 ;
183
+ }
184
+ }
185
+ left as i32
186
+ }
187
+ }
188
+ ```
189
+
170
190
### ** ...**
171
191
172
192
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn peak_index_in_mountain_array ( arr : Vec < i32 > ) -> i32 {
3
+ let mut left = 1 ;
4
+ let mut right = arr. len ( ) - 2 ;
5
+ while left < right {
6
+ let mid = left + ( right - left) / 2 ;
7
+ if arr[ mid] > arr[ mid + 1 ] {
8
+ right = mid;
9
+ } else {
10
+ left = left + 1 ;
11
+ }
12
+ }
13
+ left as i32
14
+ }
15
+ }
You can’t perform that action at this time.
0 commit comments