LeetCode 7 - Reverse Integer
来源:互联网 发布:aocu2879vf接mac 编辑:程序博客网 时间:2024/05/17 05:18
Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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.
My Code
class Solution {public: int reverse(int x) { int flag = x >= 0 ? 1 : -1; int input = x >= 0 ? x : -x; int output = 0; while (input != 0) { int lastOutput = output; output *= 10; output += input % 10; if ((output - input % 10) / 10 != lastOutput) return 0; input /= 10; } output *= flag; return output; }};Runtime: 8 ms
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
- 【慕课网笔记】Java 反射机制 1 2 Class类的使用 动态加载类
- cocos2d-x 数学函数、常用宏粗整理(CC_SYNTHESIZE)
- java概述
- JS的关键字和保留字
- android开发之service详解
- LeetCode 7 - Reverse Integer
- oracle 数据库创建表空间和用户
- 操作系统之进程(一)
- Python subprocess模块使用
- JS设置、获取和取消Cookie
- 详解JVM工作原理和特点
- 网上最新的devstack安装教程【一键部署openstack测试环境】
- Java并发编程的艺术——互动出版网
- 关于cocos2dx里面的onKeyDown返回按钮的问题