题目:

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. PDO 提供了三种不同的错误处理模式

    PDO::ERRMODE_SILENT 此为默认模式. PDO 将只简单地设置错误码,可使用 PDO::errorCode() 和 PDO::errorInfo() 方法来检查语句和数据库对象.如果错 ...

  2. HDU3336-Count the string(KMP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 关于verilog中if与case语句不完整产生锁存器的问题 分类: FPGA 2014-11-08 17:39 260人阅读 评论(0) 收藏

    在很多地方都能看到,verilog中if与case语句必须完整,即if要加上else,case后要加上default语句,以防止锁存器的发生,接下来就来说说其中原因. 一,什么是锁存器?锁存器与触发器 ...

  4. 权限系统与RBAC模型概述

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3793894.html ...

  5. CakePHP之请求与响应对象

    请求与响应对象 请求与响应对象在 CakePHP 2.0 是新增加的.在之前的版本中,这两个对象是由数组表示的,而相关的方法是分散在RequestHandlerComponent,Router,Dis ...

  6. 导入sql时报日期类型错误

    导入的脚本中有的日期类型数据是:0000-00-00 00:..这种格式的. 需要把这种格式修改一下.有的mysql版本不支持这种0000.设置成当前时间即可

  7. oracle decode函数使用方法

    1.decode(V1,1,A,2,B,C) 如果V1=1 那么显示A =2显示B 其他显示C ........ 2. 含义解释:  decode(条件,值1,返回值1,值2,返回值2,...值n,返 ...

  8. 百度ios 开发面试题

    百度移动云可穿戴部门的面试经历,面试官都非常热情友好,一上来到弄的我挺不好意思的.下面记录一下自己的面试过程,因为我真的没啥面试经验,需要总结下. 1面 Objective C runtime lib ...

  9. SSL VPN 详解

    SSL VPN是专栏VPN系列技术原理的最后一篇,SSL VPN作为远程接入型的VPN,已经具备非常广阔的前景,它的主要适应场景是取代L2TP Over IPSec,但功能要比L2TP Over IP ...

  10. 计算机网络基础_01IP地址

    1,IP地址组成和分级分级 IP地址=网络地址+主机地址 32位,4段组成 A:最高位是0 ,1个字节的网络地址,3个字节的主机地址 B:最高位是10,2个字节的网络地址,2个字节的主机地址 C:最高 ...