LeetCode 0007
来源:互联网 发布:微信加粉软件 编辑:程序博客网 时间:2024/06/03 09:26
原题链接
我的解法:
直接转置即可
class Solution {public: int reverse(int x) { const int max = 0x7fffffff; const int min = 0x80000000; long long res = 0; while(x) { res *= 10; res += (x % 10); if(res > max || res < min) { return 0; } x /= 10; } return res; }};
最快答案的解法:
大概思路是一致的,不过多了一些预处理
class Solution {public: int reverse(int x) { bool isNegative = false; int revX = 0, temp = x, count=1; int limitX = 0x7fffffff/10, limitY=0x7fffffff%10; if (x < 0) {isNegative = true; temp = x * -1;} while(temp > 0) { if ((count == 10) && ((revX > limitX) || ((revX == limitX) && (temp > limitY)))) return 0; revX = (revX * 10) + temp % 10; temp = temp/10; count++; } if (isNegative) revX = revX * -1; return revX; }};
阅读全文
0 0
- LeetCode 0007
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- Retrofit+Rxjava
- iOS 枚举位移
- ros catkin project
- java学习应知道的 7 个 Java 新工具
- alertdialog创建对话框的几个小点
- LeetCode 0007
- ios app上架app store审核催促加速
- h5大转盘,圆盘抽奖,正方形抽奖
- 你如何对网站的文件和资源进行优化?
- 棋盘问题(简单深搜)
- spring boot(19)-拦截器
- 使用hexo+github搭建免费个人博客详细教程
- 史上最简单的SpringCloud教程 | 第十一篇: docker部署spring cloud项目
- Java线程池execute()方法源码解析