数字倒序
2017-05-16 17:05
706 查看
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
代码如下:
public class ReverseInteger {
public int reverse(int x){
int result = 0;
while(x!=0){
int tail = x%10;
int newResult = result*10 + tail;
if((newResult - tail)/10 != result){
return 0;
}
result = newResult;
x = x/10;
}
return x>=0 ? result : -result;//考虑符号问题
}
}
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
代码如下:
public class ReverseInteger {
public int reverse(int x){
int result = 0;
while(x!=0){
int tail = x%10;
int newResult = result*10 + tail;
if((newResult - tail)/10 != result){
return 0;
}
result = newResult;
x = x/10;
}
return x>=0 ? result : -result;//考虑符号问题
}
}
相关文章推荐
- 运用递归给定一个int型的整数,倒序输出他的每一位上的数字
- python学习习题总结(1)——求数字位数,正序倒序输出
- [算法]最简单的数字倒序输出
- 输入一个数字,将其倒序输出,并输出其各个位上的乘积
- 输入一组数字要求倒序输出,例如输入为123输出是321
- 数字倒序输出
- [program] ReverseDigits ( 即: 倒序输出int类型的数字,比如1792,输出为2971)
- 求一个数字的倒序数
- 《Java程序》将数字进行倒序输出
- 四个整数字加密解密实现.每位数字都加上5,相加后除以10的余数代替该数字再倒序
- 华为机试数字倒序
- 第十二周oj项目A运用递归倒序输出数字
- Reverse Integer:将给定数字倒序返回
- 华为机试之数字倒序输出
- 键位对应的数字以及倒序排列
- 数字排序 键盘输入多个整数,输入quite后结束,把所有的整数倒序排列打印
- 倒序输出数字,计算一年之中的某一天是那一天
- 链表 编写链表将数字倒序输出的C语言程序。(我们C语言老师讲的!)
- 使用递归倒序输出数字,并说明递归耗费了大量的栈空间
- c语言入门—数字倒序