leetcode:Reverse Integer(一个整数反序输出)
Question: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).
整数反转:例子输入x=123,输出x=321;输入x=-123,输出x=-321
注意事项:如果这个整数的个位数是0,我们应该输出什么呢?比如1000,我们需要输出1,比如-1000,我们需要输出-1;你注意到整数反序可能会发生溢出了吗?假定你输入32位二进制整数,然后 1000000003的反转就会溢出,你怎么样去处理这样的异常呢?
算法设计思路:对于这个整数x首先把他赋值给xx,xx其实就是x的绝对值,依次对xx从低位到高位判断读取,得到sum为反序的绝对值,最后经过判断x如果是正数,返回sum,如果是负数,返回-sum;至于整数溢出问题,我没有考虑,但是leetcode案例全部通过,没有报错,下面是代码:
class Solution {
public int reverse(int x) {
int sum=0;//需要返回的数
int xx=x;//x的绝对值
int i;//xx除以10的余数
if(x<0){
xx=-x;
}
while(xx/10>0){
//System.out.println(xx);
i=xx%10;//余数
//System.out.println("i="+i);
sum=(sum+i)*10;
xx=xx/10;
}
sum+=xx;
if(x<0)
sum=-sum;
return sum;
}
}
leetcode:Reverse Integer(一个整数反序输出)的更多相关文章
- LeetCode 7. Reverse Integer 一个整数倒叙输出
潜在问题:(1)随着求和可能精度会溢出int 范围,需要使用long 来辅助判断是否溢出,此时返回 0 Assume we are dealing with an environment which ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。
namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...
- 九度oj 题目1058:反序输出
题目1058:反序输出 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:9677 解决:3495 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可 ...
- rev---将文件中的每行内容以字符为单位反序输出
rev命令将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,依次类推.
- 九度OJ 1058:反序输出 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8454 解决:3042 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可能包含多组用例,每组用例 ...
- python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数。如果不存在这样一对整数,则输入一条消息进行说明。
python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数.如果不存在这样一对整数,则输入一条消息 ...
- 输入一个整数n,输出契波那契数列的第n项
package bianchengti; /* * 输入一个整数n,输出契波那契数列的第n项 * 斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89... */ p ...
随机推荐
- apache-hadoop-1.2.1、hbase、hive、mahout、nutch、solr安装教程
1 软件环境: VMware8.0 Ubuntu-12.10-desktop-i386 jdk-7u40-linux-i586.tar.gz hadoop-1.2.1.tar.gz eclipse-d ...
- Case 架构的实际应用-1
We use testlink to manage cases, and the frame is below: Project Name -All Features(Modules) -Featur ...
- jenkins-slave的搭建和使用
一 什么是Jenkins的分布式构建和部署 Jenkins的分布式构建,在Jenkins的配置中叫做节点,分布式构建能够让同一套代码或项目在不同的环境(如:Windows和Linux系统)中编译.部署 ...
- cmake的使用二:链接第三方静态库
cmake的使用二:链接第三方静态库
- Ext的正则表达式
http://www.cnblogs.com/azai/archive/2010/12/31/1923140.html 今天看到一篇关于Extjs正则表达式比较系统的总结. 使用extJs时能常用 ...
- 【Android】 PopupWindow使用小结
PopupWindow的很多用法网上比较多,我就不做过多解释了,只说下可能会遇到的问题,以及解决办法: 1.PopupWindow中的listview无响应 这个主要是因为show写在了set ...
- EntityFramework:支持同一事务提交的批量删除数据实现思路
一切从一段代码说起... 下面一段代码是最近我在对一EF项目进行重构时发现的. protected override void DoRemove(T entity) { this.dbContext. ...
- js设置与获取Cookie
/*设置与获取Cookie*/ var Cookie ={} Cookie.write = function(key, value, duration){ var d = new Date(); d. ...
- [Swift系列]003- 函数
[基础] Swift函数格式: 1.定义格式: func 函数名(参数名1:数据类型,... ,参数名n:数据类型) -> (返回值类型1,...,返回值类型n){ ///函数体内语句 } ...
- BZOJ 1556 墓地秘密
2333333333333333333333333333333333333333333333 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 辣鸡出题人辣鸡出题人辣鸡出题人辣鸡出题人辣鸡 ...