LeetCode Algorithm 07_Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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) {
bool isNegative = false;
if(x<){
x=-x;
isNegative = true;
}
unsigned long result = ;
while(x){
result = result* + x%;
x = x/;
}
if(result > INT_MAX) return ;
return (isNegative)? int(-result) : int(result) ; }
};
LeetCode Algorithm 07_Reverse Integer的更多相关文章
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
- LeetCode Algorithm 05_Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
随机推荐
- jquery正则匹配URL地址
JQuery代码: var regexp = /((http|ftp|https|file):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\% ...
- mysql中group by和order by混用 结果不是理想结果
在使用mysql排序的时候会想到按照降序分组来获得一组数据,而使用order by往往得到的不是理想中的结果,那么怎么才能使用group by 和order by得到理想中的数据结果呢? 例如 有一个 ...
- CSUOJ 1638 Continued Fraction
1638: Continued Fraction Time Limit: 1 Sec Memory Limit: 128 MB Description Input Output Sample Inp ...
- java.lang.IllegalArgumentException: The observer is null.终于解决方式
java.lang.IllegalArgumentException: The observer is null.终于解决方式 在使用数据适配的时候的问题: java.lang.IllegalArgu ...
- Unity3D:粒子系统Particle System
1. GameObject → Create Other → Particle System. 2. 选中 Particle System,可看到下列屬性: 3.Particle System: ...
- SQl 行转列,列转行 (PIVOT,UPIVOT)
1.列转行 表t_pivot 转后效果 方法 1 (常用方法 case when) SELECT buydate, SUM(CASE WHEN type = '生活' THEN typec ...
- .Net MVC小尝试
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 67.nodejs取参四种方法req.body,req.params,req.param,req.body
转自:http://www.cnblogs.com/jkingdom/p/8065202.html 摘要: nodejs取参四种方法req.body,req.params,req.param,req. ...
- Domino系统从UNIX平台到windows平台的迁移及备份
单位机房的一台服务机器到折旧期了,换成了新购IBM机器X3950,而且都预装了windows 2003 server 标准版,所以只有把以前在Unix平台下跑的OA系统迁移到新的windows 200 ...
- javap 命令 反编译代码
javap 命令 javap -c classname 一个好的分析class二进制文件的 链接 http://blog.csdn.net/pwlazy/article/details/7954169