-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.整数反转.rs
80 lines (78 loc) · 1.34 KB
/
7.整数反转.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @lc app=leetcode.cn id=7 lang=rust
*
* [7] 整数反转
*
* https://leetcode-cn.com/problems/reverse-integer/description/
*
* algorithms
* Easy (35.20%)
* Likes: 3184
* Dislikes: 0
* Total Accepted: 862.8K
* Total Submissions: 2.5M
* Testcase Example: '123'
*
* 给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
*
* 如果反转后整数超过 32 位的有符号整数的范围 [−2^31, 2^31 − 1] ,就返回 0。
* 假设环境不允许存储 64 位整数(有符号或无符号)。
*
*
*
* 示例 1:
*
*
* 输入:x = 123
* 输出:321
*
*
* 示例 2:
*
*
* 输入:x = -123
* 输出:-321
*
*
* 示例 3:
*
*
* 输入:x = 120
* 输出:21
*
*
* 示例 4:
*
*
* 输入:x = 0
* 输出:0
*
*
*
*
* 提示:
*
*
* -2^31
*
*
*/
// @lc code=start
impl Solution {
pub fn reverse(x: i32) -> i32 {
let mut temp = x;
let mut s: i32 = 0;
while temp != 0 {
match s.checked_mul(10) {
None => return 0,
Some(tem) => match tem.checked_add(temp % 10) {
None => return 0,
Some(tem1) => s = tem1,
}
}
temp /= 10;
}
s
}
}
// @lc code=end