LeetCode:Reverse Integer
来源:互联网 发布:js两个数组合并去重 编辑:程序博客网 时间:2024/06/06 10:00
问题描述:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
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.
Update (2014-11-10):
Test cases had been added to test the overflow behavior.
示例代码:
class Solution {public: int reverse(int x) { if(x == 0) { return 0; } const int max = 0x7fffffff; //int最大值 const int min = 0x80000000; //int最小值 long long key = 0; while(x) { key = key * 10 + x % 10; if(key > max || key < min) { return 0; } x /= 10; } return key; }};
0 0
- LeetCode:Reverse Integer
- LeetCode: Reverse Integer
- [Leetcode] Reverse Integer
- LeetCode : Reverse Integer
- [LeetCode] Reverse Integer
- Leetcode: Reverse Integer
- LeetCode 7 - Reverse Integer
- leetcode 104: Reverse Integer
- Leetcode : Reverse Integer
- 【leetcode】Reverse Integer
- [LeetCode]Reverse Integer
- LeetCode-Reverse Integer
- [leetcode] Reverse Integer
- LeetCode - Reverse Integer
- leetcode之Reverse Integer
- LeetCode | Reverse Integer
- LeetCode: Reverse Integer
- Leetcode: Reverse Integer
- 从K近邻算法、距离度量谈到KD树、SIFT+BBF算法
- build Variants创建不同配置的app(图文详解)
- 开始我的技术博客生涯
- Java一步一脚印—HashMap
- c++学习笔记(十二):C++编译器多态实现原理
- LeetCode:Reverse Integer
- Codeforces Round #327 (Div. 2) C - Median Smoothing
- 使用echarts时遇到的 function(){}()
- Windows下INF驱动安装文件内容分析
- Ubuntu关于build-essential
- getHibernateTemplate
- MyEclipse多Maven module导入问题以及思路分享
- JDBC 2 封装
- Linux下取代top的进程管理工具 htop