数字逆序 leetcode
来源:互联网 发布:迈阿密大学知乎 编辑:程序博客网 时间:2024/06/06 03:07
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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?
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).
public class Solution { public int reverse(int x) { // if(x==0) return 0 //处理0,可不用单独考虑 boolean negative = false; if(x<0) negative = true; x = Math.abs(x); // while(x%10==0){ //处理尾部的0,可不用单独考虑 // x/=10; //} int now = 0; while(x>0){ now*=10; now+=x%10; x/=10; } if(now<0) return -1; // 溢出啦!!!题目要求不用exception if(negative) now = -now; return now; }}
- 数字逆序 leetcode
- 数字逆序
- 【C】逆序排列数字
- 数字逆序输出
- 逆序输出单个数字
- 逆序打印数字
- 1060: 逆序数字
- 【LeetCode】整数逆序输出
- 数字字符串的逆序输出
- 求数字的逆序表示
- 输入十个数字,逆序输出
- zzuli OJ 1060: 逆序数字
- python逆序打印各位数字
- 实现数字逆序的方法
- leetcode----数字
- leetcode | 数组中的逆序对
- 逆序输出数字并求最大最小值
- C++中逆序输出字符串和数字
- android自定义实现自己需要seekbar
- 一些常用的API
- 倍压整流电路
- Linux中__init、__devinit等初始化宏
- 路由器对数据的转发操作
- 数字逆序 leetcode
- 直接插入排序三种实现
- 一个IT男的职业发展全景图
- 单链表中通过某一结点指针删除该结点
- asp.net 动态操作配置文件 web.config
- JSP导出excel的方法
- OAuth 2.0 Authorization Server 代码copy
- 递归实现汉诺塔
- Hi3531用SPI FLASH启动 使用Nand做文件系统