Skip to content

Commit 69f1d79

Browse files
committed
feat: add rust solution to lc problem: No.0069
No.0069.Sqrt(x)
1 parent d744fee commit 69f1d79

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

solution/0000-0099/0069.Sqrt(x)/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ public class Solution {
154154
}
155155
```
156156

157+
### **Rust**
158+
159+
```rust
160+
impl Solution {
161+
pub fn my_sqrt(x: i32) -> i32 {
162+
if x < 2 {
163+
return x;
164+
}
165+
let mut l = 1;
166+
let mut r = x / 2;
167+
while l < r {
168+
let mid = (l + r + 1) >> 1;
169+
if x / mid < mid {
170+
r = mid - 1
171+
} else {
172+
l = mid;
173+
}
174+
}
175+
l
176+
}
177+
}
178+
```
179+
157180
### **...**
158181

159182
```

solution/0000-0099/0069.Sqrt(x)/README_EN.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,29 @@ public class Solution {
152152
}
153153
```
154154

155+
### **Rust**
156+
157+
```rust
158+
impl Solution {
159+
pub fn my_sqrt(x: i32) -> i32 {
160+
if x < 2 {
161+
return x;
162+
}
163+
let mut l = 1;
164+
let mut r = x / 2;
165+
while l < r {
166+
let mid = (l + r + 1) >> 1;
167+
if x / mid < mid {
168+
r = mid - 1
169+
} else {
170+
l = mid;
171+
}
172+
}
173+
l
174+
}
175+
}
176+
```
177+
155178
### **...**
156179

157180
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn my_sqrt(x: i32) -> i32 {
3+
if x < 2 {
4+
return x;
5+
}
6+
let mut l = 1;
7+
let mut r = x / 2;
8+
while l < r {
9+
let mid = (l + r + 1) >> 1;
10+
if x / mid < mid {
11+
r = mid - 1
12+
} else {
13+
l = mid;
14+
}
15+
}
16+
l
17+
}
18+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy