题目:

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.

我是这样写的:

 class Solution {
public:
int reverse(int x) {
// int temp = INT_MAX;
int count = ;
int y = x;
while(abs(y) > )
{
y = y/;
count++;
}
if(x == )
return ;
else if(x > )
{
int *a = new int[count];
for(int i = ; i < count; i++)
{
a[i] = x%;
x = x/;
}
int value = ;
int temp = ;
for(int j = ; j < count; j++)
{
value = value* + a[j];
if(temp != (value - a[j])/) value = temp = ;
else temp = value;
}
delete []a;
return value;
}
else
{
x = -x;
int *a = new int[count];
for(int i = ; i < count; i++)
{
a[i] = x%;
x = x/;
}
int value = ;
int temp = ;
for(int j = ; j < count; j++)
{
value = value* + a[j];
if(temp != (value - a[j])/) value = temp = ;
else temp = value;
}
delete []a;
return -value; } }
};

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

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

他是这样写的:

 int reverse(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function long res = ;
while(x)
{
res = res* + x%;
x /= ;
}
return res;
}

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

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

  1. LeetCode: Reverse Integer 解题报告

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  2. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  3. [Leetcode] reverse integer 反转整数

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  4. C++ leetcode::Reverse Integer

    第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...

  5. leetcode:Reverse Integer【Python版】

    1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...

  6. LeetCode——Reverse Integer(逆置一个整数)

    问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321   Ha ...

  7. LeetCode——Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  8. [Leetcode]Reverse Integer

      核心思想:原数对10取余数赋值给新数后降一位,再把新数升一位加上下一次原数取余值,直到原数降为0. 解法如下: int reverse(int x) { bool minus = false; ) ...

  9. leetcode reverse integer&&Palindrome Number

    public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...

随机推荐

  1. Ehcache简单说明及使用

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存 ...

  2. IE浏览器中发送到onenote的选项没有调出来??

    最近使用onenote 作为笔记本,发现这个比word好用很多,特别是还有一个功能很好用,发送到onenote,可以选中网页中的内容,发送到onenote.但是有一些IE浏览器这个选项没有调出来,还是 ...

  3. Java HexString

    byte[]和十六进制字符串相互转换 Java中byte用二进制表示占用8位,而我们知道16进制的每个字符需要用4位二进制位来表示. 所以我们就可以把每个byte转换成两个相应的16进制字符,即把by ...

  4. Delphi 记事本 TMemo

    Windows记事本记事本     描述:     用Delphi模仿的Windows记事本 界面和功能都和Windows的记事本一样,是用Memo实现的而不是RichEdit 可以执行以下功能 文件 ...

  5. ajax jsonp跨域处理问题

    客户端 html $.ajax({ type : "get", async:false, dataType : "jsonp", jsonp: "js ...

  6. location.href的用户总结

    *.location.href 使用方法: top.location.href="url"          在顶层页面打开url(跳出框架) self.location.href ...

  7. apache不解析php文档?提示需要下载(转)

    在httpd.cong中 LoadModule php5_module modules/libphp5.so #这一行php5安装的时候就已经自动添加上了 AddType application/x- ...

  8. android 18 Bundle类

    Bundle类:竖屏的activity换到横屏的activity的时候,会把竖屏的activity杀掉横屏的activity创建,竖屏的activity会有一些计算结果,可以用数据存起来,存到内存里面 ...

  9. calltree查看工程代码中的函数调用关系

    http://blog.csdn.net/elitemouse/article/details/41680113 http://www.tinylab.org/callgraph-draw-the-c ...

  10. FastDFS问题汇总

    问题1: 增加分组后,新的storge不可用. 增加一个分组group2,发现上传文件失败.在group2中的storage中使用netstat -anp|grep fdfs,发现端口状态为CLOSE ...