Skip to content

Commit 48ffbfd

Browse files
committed
0007
1 parent ee7a3bc commit 48ffbfd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

rust/0007-reverse-integer.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::i32::{MAX, MIN};
2+
3+
impl Solution {
4+
pub fn reverse(x: i32) -> i32 {
5+
let mut res = 0;
6+
let mut x = x;
7+
while x != 0 {
8+
let digit = x % 10;
9+
x /= 10;
10+
11+
if res > MAX / 10 || (res == MAX / 10 && digit > MAX % 10) {
12+
return 0;
13+
}
14+
if res < MIN / 10 || (res == MIN / 10 && digit < MIN % 10) {
15+
return 0;
16+
}
17+
18+
res = (res * 10) + digit;
19+
}
20+
res
21+
}
22+
}

0 commit comments

Comments
 (0)