【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
总结:处理整数溢出的方法
①用数据类型转换long 或 long long
②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好)
③整体恢复,看数字是否相等。
思路:注意30000这样以0结尾的数字,注意越界时返回0.
我检查越界是通过把翻转的数字再翻转回去,看是否相等。
int reverse(int x) {
int xx = abs(x);
int ans = ;
int zeros = ; //如果x = 2000 这种后面有很多0 那翻转后是2 再翻转还是2 需要乘以1000才能恢复成2000
while(xx != )
{
int r = xx % ;
xx /= ;
ans = ans * + r;
if(ans == ) zeros *= ;
}
ans = (x < ) ? -ans : ans;
//检测是否溢出 思路把数字重新翻转回去,看结果是否相同
int anss = abs(ans), check = ;
while(anss != )
{
int r = anss % ;
anss /= ;
check = check * + r;
}
check = (x < ) ? -check : check;
check *= zeros;
if(check != x) //溢出了
return ;
else
return ans;
}
基于②的判断:
public int reverse(int x)
{
int result = ; while (x != )
{
int tail = x % ;
int newResult = result * + tail;
if ((newResult - tail) / != result)
{ return ; }
result = newResult;
x = x / ;
} return result;
}
基于①的判断:
int reverse(int x) {
long num = abs((long)x);
long new_num = ;
while(num) {
new_num = new_num* + num%;
num /= ;
}
if (new_num > INT_MAX) {
return ;
}
return (x< ? -*new_num : new_num);
}
【leetcode】Reverse Integer(middle)☆的更多相关文章
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Dungeon Game (middle)
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- [歪谈]拽一个贵人出来给你"当炮架子"
我们在古装神话剧中经常会听到某个“先知”对前来算命的人说:你会在某某时刻遇到你的贵人.而这个贵人会在事业上助你一臂之力. 这里有个问题:贵人到底是什么?我们怎样去寻找我们的贵人. 前几天有个网友来咨询 ...
- 【CISP笔记】数据库及应用安全
数据库安全特性检查工具美国应用安全公司的App Detective英国下一代软件公司的NGS SQuirrel 常见应用安全威胁 网络层面拒绝服务.电子欺骗.嗅探.……系统层面Web服务漏洞.配置错误 ...
- 如何在CentOS 7上安装EPEL源
EPEL 是什么? EPEL (Extra Packages for Enterprise Linux,企业版Linux的额外软件包) 是Fedora小组维护的一个软件仓库项目,为RHEL/CentO ...
- centos bad ELF interpreter: No such file or directory
sudo yum install glibc.i686
- mongodb certification
又偷懒 也有学到不少东西 这个东西算是小结啦 给2013年的碌碌无为挽回点面子 哈哈~
- int (*p)[4] 与 int* p[4]
碰到一道题: ][] = {,,,,,,,,,,,}; ]; ] = (a+); cout<<*(p+)<<endl; cout<<(*ptr+)[]<< ...
- 如何让JQuery报错-遁地龙卷风
0.解决的问题 a.当选择器语法没有问题,找不到元素时,让jquery报错 b.选择器语法有问题,程序无法继续执行时,让jquery报错 主要针对传递字符串,尝试前请备份jquery库,最好改变名字加 ...
- BZOJ1212——L语言
题目大意:每一个字符串都可以分解成一些个单词组成,现在给你一些单词,再给你一个字符串, dp吧,设f[i]为从0开始,到i结束的字符串前缀是否可以被分解,因为单词长度很小,所以,这就T了, (什么逻辑 ...
- 编写Delphi控件属性Stored和Default的理解及应用
property ButtonSize: Integer read FButtonSize write SetButtonSize default 0; property Color: TCol ...
- [翻译]opengl扩展教程2
[翻译]opengl扩展教程2 原文地址https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/extensions_part2.php [ ...