题目:

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. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  2. centos7免密码登录

    2台主机 192.168.30.207 Master 192.168.30.251 Node1 三台主机检查 ~/.ssh 文件夹没有则新建 ssh-keygen -t rsa 一路狂按回车,最终生成 ...

  3. mac 别名 ll

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

  4. php类获取静态变量值以及调用

    <?php class Test{ public static $static_var = 20; } echo Test::$static_var;exit;

  5. c#基础语言编程-程序集和反射

    程序集 什么是程序集? 1.程序集(assembly)是一个及一个以上托管模块,以及一些资源文件的逻辑组合. 2.程序集是组件复用,以及实施安全策略和版本策略的最小单位. 3.程序集是包含一个或者多个 ...

  6. js 解析 bytearray 成 字符串

    function bin2String(array) { return String.fromCharCode.apply(String, array); } var bit=[104,101,108 ...

  7. [RxJS] Filtering operators: skipWhile and skipUntil

    After takeUntil() and takeWhile() function, let's have a look on skipWhile() and skilUntil() functio ...

  8. PHP读取文件头(2字节)判断文件类型(转)

    看到此标题或许你会说是否是多此一举,直接判断扩展名不就知道文件类型了吗,但是扩展名很容易伪造,这样就绕过了判断.大部分的文件都会将一个特殊的数字或字符存放在文件的特定位置里(开始处的2个字节) /** ...

  9. [转] ReactNative Animated动画详解

    http://web.jobbole.com/84962/     首页 所有文章 JavaScript HTML5 CSS 基础技术 前端职场 工具资源 更多频道▼ - 导航条 - 首页 所有文章 ...

  10. [转] Initial Impressions on GraphQL & Relay

    https://kadira.io/blog/graphql/initial-impression-on-relay-and-graphql http://graphql.org/blog/subsc ...