We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 15fb143 commit 98c3625Copy full SHA for 98c3625
reverse_integer.md
@@ -2,6 +2,8 @@
2
整数反转
3
## Problem
4
Given a 32-bit signed integer, reverse digits of an integer.
5
+## 分析
6
+注意题目为32个字节整型,因此最终返回为long int
7
## Approach1
8
将整型转化为字符型数组处理
9
### 代码实现
@@ -31,5 +33,27 @@ int reverse(int x)
31
33
return 0;
32
34
return (int)result*sign;
35
}
36
+```
37
## Approach2
38
+利用除法和mod运算
39
40
+```ruby
41
+int reverse(int x) {
42
+ int sign;
43
+ if(x>0)
44
+ sign=1;
45
+ else{
46
+ sign=-1;
47
+ x=abs(x);
48
+ }
49
+ long result=0;
50
+ while(x>0)
51
+ {
52
+ result=result*10+x%10;
53
+ x=x/10;
54
55
+ if(result>2147483647)
56
+ return 0;
57
+ return result*sign;
58
+}
59
0 commit comments