Leetcode: Reverse Integer 正确的思路下-要考虑代码简化

题目:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer‘s last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

我是这样写的:

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4        // int temp = INT_MAX;
 5         int count = 0;
 6         int y = x;
 7         while(abs(y) > 0)
 8         {
 9             y = y/10;
10             count++;
11         }
12         if(x == 0)
13         return 0;
14         else if(x > 0)
15         {
16             int *a = new int[count];
17             for(int i = 0; i < count; i++)
18             {
19                 a[i] = x%10;
20                 x = x/10;
21             }
22             int value = 0;
23             int temp = 0;
24             for(int j = 0; j < count; j++)
25             {
26                 value = value*10 + a[j];
27                 if(temp != (value - a[j])/10) value = temp = 0;
28                 else temp = value;
29             }
30             delete []a;
31             return value;
32         }
33         else
34         {
35             x = -x;
36             int *a = new int[count];
37             for(int i = 0; i < count; i++)
38             {
39                 a[i] = x%10;
40                 x = x/10;
41             }
42             int value = 0;
43             int temp = 0;
44             for(int j = 0; j < count; j++)
45             {
46                 value = value*10 + a[j];
47                 if(temp != (value - a[j])/10) value = temp = 0;
48                 else temp = value;
49             }
50             delete []a;
51             return -value;
52
53         }
54
55     }
56 };

通过之后,我参考了网友doc_sgl的代码

http://blog.csdn.net/doc_sgl/article/details/12190507

他是这样写的:

 1 int reverse(int x) {
 2         // Start typing your C/C++ solution below
 3         // DO NOT write int main() function
 4
 5         long res = 0;
 6         while(x)
 7         {
 8             res = res*10 + x%10;
 9             x /= 10;
10         }
11         return res;
12     }

十几行与几十行的区别,却能更简便的表述。虽然这些代码没有仔细思量裁剪,但却表现了代码能力的区别。以此自省之。

时间: 2024-10-09 07:33:22

Leetcode: Reverse Integer 正确的思路下-要考虑代码简化的相关文章

[LeetCode] Reverse Integer [8]

题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought th

LeetCode——Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer'

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign

LeetCode: Reverse Integer 解题报告

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. SOLUTION 1: 注意越界后返回0.先用long来计算,然后,把越界的处理掉. public class Solution { public int reverse(int x) { long ret = 0; while (x !

leetcode:Reverse Integer【Python版】

1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: 1 class Solution: 2 # @return an integer 3 def reverse(self, x): 4 ret = 0 5 flag = 1 6 if x < 0: 7 flag = -1 8 x *= -1 9 while(x!=0): 10 ret = ret*10+x

[LeetCode] Reverse Integer 翻转整数

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throu

leetcode : reverse integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throug

LeetCode Reverse Integer 反转整数

1 class Solution { 2 public: 3 int reverse(int x) { 4 int e,s,num,k=0; 5 num=x; 6 e=0; 7 if(x<0) 8 num=-1*x; 9 while( num!=0 ){ 10 s=num%10; 11 e=e*10+s; 12 num=num/10; 13 k++; 14 } 15 if(x<0) 16 return -e; 17 else 18 return e; 19 } 20 }; 题意: Exampl

LeetCode&mdash;&mdash;Reverse Integer(逆置一个整数)

问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321   Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the in