Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4cff39a

Browse files
committed
feat: add rust solution to lc problem: No.0035
No.0035.Search Insert Position
1 parent 3ebda25 commit 4cff39a

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ var searchInsert = function (nums, target) {
155155
};
156156
```
157157

158+
### **Rust**
159+
160+
```rust
161+
use std::cmp::Ordering;
162+
163+
impl Solution {
164+
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
165+
let mut l = 0;
166+
let mut r = nums.len();
167+
while l < r {
168+
let mid = l + (r - l) / 2;
169+
match nums[mid].cmp(&target) {
170+
Ordering::Less => l = mid + 1,
171+
Ordering::Greater => r = mid,
172+
Ordering::Equal => return mid as i32,
173+
}
174+
}
175+
l as i32
176+
}
177+
}
178+
```
179+
158180
### **...**
159181

160182
```

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,28 @@ var searchInsert = function (nums, target) {
131131
};
132132
```
133133

134+
### **Rust**
135+
136+
```rust
137+
use std::cmp::Ordering;
138+
139+
impl Solution {
140+
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
141+
let mut l = 0;
142+
let mut r = nums.len();
143+
while l < r {
144+
let mid = l + (r - l) / 2;
145+
match nums[mid].cmp(&target) {
146+
Ordering::Less => l = mid + 1,
147+
Ordering::Greater => r = mid,
148+
Ordering::Equal => return mid as i32,
149+
}
150+
}
151+
l as i32
152+
}
153+
}
154+
```
155+
134156
### **...**
135157

136158
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::cmp::Ordering;
2+
3+
impl Solution {
4+
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;
9+
match nums[mid].cmp(&target) {
10+
Ordering::Less => l = mid + 1,
11+
Ordering::Greater => r = mid,
12+
Ordering::Equal => return mid as i32,
13+
}
14+
}
15+
l as i32
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /