Skip to content

Commit 3db68e2

Browse files
committed
feat: add rust solution to lc problems: No.0035,0852
- No.0035.Search Insert Position - No.0852.Peak Index in a Mountain Array
1 parent f010fe0 commit 3db68e2

File tree

6 files changed

+76
-24
lines changed

6 files changed

+76
-24
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,19 @@ var searchInsert = function (nums, target) {
152152

153153
```rust
154154
use std::cmp::Ordering;
155-
156155
impl Solution {
157156
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;
162161
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,
165164
Ordering::Equal => return mid as i32,
166165
}
167166
}
168-
l as i32
167+
left as i32
169168
}
170169
}
171170
```

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,19 @@ var searchInsert = function (nums, target) {
142142

143143
```rust
144144
use std::cmp::Ordering;
145-
146145
impl Solution {
147146
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;
152151
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,
155154
Ordering::Equal => return mid as i32,
156155
}
157156
}
158-
l as i32
157+
left as i32
159158
}
160159
}
161160
```
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use std::cmp::Ordering;
2-
32
impl Solution {
43
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;
98
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,
1211
Ordering::Equal => return mid as i32,
1312
}
1413
}
15-
l as i32
14+
left as i32
1615
}
1716
}

solution/0800-0899/0852.Peak Index in a Mountain Array/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,26 @@ function peakIndexInMountainArray(arr: number[]): number {
192192
}
193193
```
194194

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+
195215
### **...**
196216

197217
```

solution/0800-0899/0852.Peak Index in a Mountain Array/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ function peakIndexInMountainArray(arr: number[]): number {
167167
}
168168
```
169169

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+
170190
### **...**
171191

172192
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

0 commit comments

Comments
 (0)