Reverse Integer [LeetCode]
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?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
Solution: be careful about corner case, like INT_MIN, 0, and -INT_MIN is not equals to INT_MAX. Here is the definition of them in limits.h
#define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX 2147483647 /* maximum (signed) int value */
int reversePostiveInt(int x){
vector<int> digits;
while(true) {
if(x < ){
digits.push_back(x);
break;
}else {
int remainder = x % ;
int quotient = x / ;
digits.push_back(remainder);
x = quotient;
}
}
// reverse output
long long int ret = ;
for(int i = ; i < digits.size(); i ++) {
ret = ret * + digits[i];
if(ret > INT_MAX){
ret = INT_MAX;
break;
}
}
return (int) ret;
}
int reverse(int x) {
if(x == )
return ;
if(x > ) {
return reversePostiveInt(x);
}else if( x == INT_MIN){
//overflow
return INT_MIN;
}else if ( x > INT_MIN) {
return -reversePostiveInt(-x);
}
}
Reverse Integer [LeetCode]的更多相关文章
- Reverse Integer LeetCode Java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 public cl ...
- Reverse Integer ---- LeetCode 007
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Solution ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
随机推荐
- 关于stl string find 容易犯的一个错误
有时候经常会判断一个字符串a中是否有子字符串b,那么有人会调用 string::find这个函数 这个函数返回子字符串首次出现的位置,那么有人会这样写 string str1 = "&qu ...
- Create XO Checker Game With Oracle Forms
Created XO Checker game in Oracle Forms and sharing its FMB (source code) for reference so that you ...
- CNV
CNV: 人类主要是二倍体.如果有些区域出现3个.4个拷贝,那就是扩增了,如果只出现1个拷贝,就是缺失.所以CNV分析是依靠特定位置的测序深度来估算的,先在染色体上划窗,然后看每个窗口的平均测序深度, ...
- OB命令大全
CALC : 判断表达式 WATCH : 添加监视表达式 AT : 在指定地址进行反汇编 FOLLOW : 跟随命令 ORIG : ...
- CUBRID学习笔记 12防火墙设置 linux
这玩意是linux上用的. 如果你的数据库不是装在linux下可以飘过了 iptables -I INPUT -p tcp --dport 8001 -j ACCEPT iptables -I INP ...
- C语言程序设计现代方法1,2,3章
1:浮点型(float)运算比int慢,并且可能存在舍入误差 如float存储0.1,以后使用可能会变成0.099999999987 2:宏定义只用大写,这是大多数C程序猿遵循的规范! C语言区分大小 ...
- hdu 4712 Hamming Distance 随机
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- SVN 的使用
文件夹上的符号有什么意义?? 黄色感叹号(有冲突):--这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许 ...
- iOS - Swift NSKeyedArchiver 数据归档
前言 public class NSKeyedArchiver : NSCoder public class NSKeyedUnarchiver : NSCoder 在 OC 语言中,归档是一个过程, ...
- SAP研究贴之--发票校验提示移动平均价为负
近日,应付岗密集出现发票校验时移动平均价为负值导致过账失败的情况,采购经理又是拍桌子.又是摔杯子的.财务经理安排任务彻底清查,找出问题原因.哎,毫无头绪啊...测试机模拟业务吧流程:合同(系统外)-采 ...