Skip to content

Commit 1073f0f

Browse files
committed
feat: add rust solution to lc problem: No.0009
No.0009.Palindrome Number
1 parent 8f85e97 commit 1073f0f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

solution/0000-0099/0009.Palindrome Number/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,48 @@ func isPalindrome(x int) bool {
128128
}
129129
```
130130

131+
### **Rust**
132+
133+
```rust
134+
impl Solution {
135+
pub fn is_palindrome(x: i32) -> bool {
136+
if x < 0 {
137+
return false;
138+
}
139+
let s = x.to_string();
140+
let bs = s.as_bytes();
141+
let n = bs.len();
142+
let mut l = 0;
143+
let mut r = n - 1;
144+
while l < r {
145+
if bs[l] != bs[r] {
146+
return false;
147+
}
148+
l += 1;
149+
r -= 1;
150+
}
151+
true
152+
}
153+
}
154+
```
155+
156+
```rust
157+
impl Solution {
158+
pub fn is_palindrome(mut x: i32) -> bool {
159+
if x < 0 || (x % 10 == 0 && x != 0) {
160+
return false;
161+
}
162+
let mut y = 0;
163+
while x > y {
164+
y *= 10;
165+
y += x % 10;
166+
x /= 10;
167+
}
168+
x == y || x == y / 10
169+
}
170+
}
171+
```
172+
131173
### **...**
132174

133175
```

solution/0000-0099/0009.Palindrome Number/README_EN.md

+42
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,48 @@ func isPalindrome(x int) bool {
118118
}
119119
```
120120

121+
### **Rust**
122+
123+
```rust
124+
impl Solution {
125+
pub fn is_palindrome(x: i32) -> bool {
126+
if x < 0 {
127+
return false;
128+
}
129+
let s = x.to_string();
130+
let bs = s.as_bytes();
131+
let n = bs.len();
132+
let mut l = 0;
133+
let mut r = n - 1;
134+
while l < r {
135+
if bs[l] != bs[r] {
136+
return false;
137+
}
138+
l += 1;
139+
r -= 1;
140+
}
141+
true
142+
}
143+
}
144+
```
145+
146+
```rust
147+
impl Solution {
148+
pub fn is_palindrome(mut x: i32) -> bool {
149+
if x < 0 || (x % 10 == 0 && x != 0) {
150+
return false;
151+
}
152+
let mut y = 0;
153+
while x > y {
154+
y *= 10;
155+
y += x % 10;
156+
x /= 10;
157+
}
158+
x == y || x == y / 10
159+
}
160+
}
161+
```
162+
121163
### **...**
122164

123165
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn is_palindrome(mut x: i32) -> bool {
3+
if x < 0 || (x % 10 == 0 && x != 0) {
4+
return false;
5+
}
6+
let mut y = 0;
7+
while x > y {
8+
y *= 10;
9+
y += x % 10;
10+
x /= 10;
11+
}
12+
x == y || x == y / 10
13+
}
14+
}

0 commit comments

Comments
 (0)