leetcode-7-Reverse Integer
来源:互联网 发布:大嘴猴天鹅绒知乎 编辑:程序博客网 时间:2024/06/06 08:40
#include <iostream>#include <climits>using namespace std;/* 整数的倒转。 要考虑几个比较特殊的输入: 1.最后一到多位是0. 2.负数 3.倒转之后会溢出,这个每次求和之前判断一次就可以了。 */class Solution {public: int reverse(int x) { //这种正数和负数的数字部分处理一样的,符号部分应该要单独处理 bool pos = x >= 0? true : false; int sum = 0; int xx = x >= 0? x : -x; while (xx > 0) { int bitx = xx % 10; //一旦溢出就返回0 if (sum > INT_MAX / 10 || (sum == INT_MAX / 10 && bitx > INT_MAX % 10)) { return 0; } sum = sum * 10 + bitx; xx /= 10; } return pos? sum : -sum; }};int main(int argc, const char * argv[]) { Solution s; int test = 1534236469; cout << s.reverse(test) << endl; cout << INT_MAX << endl; return 0;}
0 0
- LeetCode 7 - Reverse Integer
- LeetCode(7) Reverse Integer
- leetcode 7REVERSE INTEGER
- [leetcode 7] Reverse Integer
- [Leetcode] 7 - Reverse Integer
- LeetCode 7:《Reverse Integer》
- [leetcode 7] Reverse Integer
- LeetCode | #7 Reverse Integer
- leetcode.7-----------Reverse Integer
- LeetCode 7: Reverse Integer
- leetcode-7 Reverse Integer
- leetcode(7)Reverse Integer
- LeetCode 7 - Reverse Integer
- leetcode 7 Reverse Integer
- leetcode #7 Reverse Integer
- leetcode 7 -- Reverse Integer
- LeetCode 7: Reverse Integer
- Leetcode[7]-Reverse Integer
- 手机淘宝
- 织梦如何去掉首页的后缀
- URL传递参数长度限制
- nginx源码初读(10)--让烦恼从数据结构开始(ngx_cycle)
- linux set ff unix
- leetcode-7-Reverse Integer
- Block使用介绍(-)
- android开发环境搭建(Studio版)
- linux下释放cache内存
- iOS就九宫格实现
- 大数问题
- python print
- 97.View the Exhibit and evaluate the structure and data in the CUST_STATUS table.
- 奇异值分解及几何意义