Reverse Integer
2016-02-25 12:53
211 查看
又是一个简单的但是都没一次通过的题目, 第一遍刷题就这么差么
自己丑陋的代码 so ugly
1. 不需要要那么多的中间变量啊 什么mod之类的 不过买
2.
大家都是 result = result * 10 + x %10; 而我是奇葩的 result = (result + x %10)*10
关键其实是可以改变result的type 这样就避免了在运算过程中的overflow从而只要最后check就好 改成long long
但是如果是64 bit的机器呢,long long也不管用了 所以提前判断也不是一个差的选择
或者是方法二如果overflow 比较就会不同 但是check for
3. 写数字显得很不专业啊 要用0xf7777777 INT_MIN
4.为什么我要特别handle 负数的情况
其实根本不需要啊!!!
Rethink
for a while. And actually, we don't need to use special logic to handle '+' and '-', because the sign can be kept during calculating.
good link : https://leetcode.com/discuss/18785/my-accepted-15-lines-of-code-for-java https://leetcode.com/discuss/18523/shortest-code-possible-in-c
自己丑陋的代码 so ugly
class Solution { public: int reverse(int x) { if ((x >= 2147483647) || (x <= -2147483648) || x == 0) { return 0; } int result = 0; bool flag = false; if (x < 0) { x = -x; flag = true; } while ((x / 10) != 0) { int mod = x % 10; x = x / 10; if ((result + mod) > 214748364.7) { return 0; } result = (result + mod) * 10; } result += (x % 10); if (flag == true) { result = result * -1; } return result; } };有几个地方很丑
1. 不需要要那么多的中间变量啊 什么mod之类的 不过买
2.
if ((result + mod) > 214748364.7)这个很拙劣啊 虽然还是看test case 错误想出来的。。。
大家都是 result = result * 10 + x %10; 而我是奇葩的 result = (result + x %10)*10
关键其实是可以改变result的type 这样就避免了在运算过程中的overflow从而只要最后check就好 改成long long
但是如果是64 bit的机器呢,long long也不管用了 所以提前判断也不是一个差的选择
class Solution { public: int reverse(int x) { long long result = 0; bool flag = false; if (x < 0) { x = -x; flag = true; } while (x != 0) { result = result * 10 + x % 10; x = x / 10; } //result += (x % 10); if (flag == true) { result = result * -1; } if ((result >= 2147483647) || (result <= -2147483648) ) { return 0; } return result; } };
或者是方法二如果overflow 比较就会不同 但是check for
public int reverse(int x) { int result = 0; while (x != 0) { int tail = x % 10; int newResult = result * 10 + tail; if ((newResult - tail) / 10 != result) { return 0; } result = newResult; x = x / 10; } return result; }
3. 写数字显得很不专业啊 要用0xf7777777 INT_MIN
4.为什么我要特别handle 负数的情况
其实根本不需要啊!!!
Rethink
for a while. And actually, we don't need to use special logic to handle '+' and '-', because the sign can be kept during calculating.
good link : https://leetcode.com/discuss/18785/my-accepted-15-lines-of-code-for-java https://leetcode.com/discuss/18523/shortest-code-possible-in-c
class Solution { public: int reverse(int x) { long long res = 0; while(x) { res = res*10 + x%10; x /= 10; } return (res<INT_MIN || res>INT_MAX) ? 0 : res; } };
相关文章推荐
- Js实现浏览器下标签页间切换触发的事件
- Java序列化详解
- 智慧云停车场的业务
- JAVA Exception理解
- 跨平台.NET Core--微软开源方向
- ]Android ADT SDK API 说明
- android中实现多个apk文件。
- 鸟哥的Linux私房菜(服务器)- 第十二章、网络参数控管者: DHCP 服务器
- 【持续更新】把.net代码转换为java代码的注意事项
- jsp详细说明
- 鸟哥的Linux私房菜(服务器)- 第十一章、远程联机服务器SSH / XDMCP / VNC / RDP
- CUDA issue:cudaGetDeviceCount()错误
- 集合的检索:位图法
- 当当网登录
- LeetCode -- Unique Paths II
- Canvas画布的一些实用技巧
- 鸟哥的Linux私房菜(服务器)- 第十章、申请合法的主机名
- iOS颜色渐变效果
- the difference between imperative style and functional style
- spark源码学习(一):sparkContext的初始化分析(一)