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(一个整数反序输出)的更多相关文章

  1. LeetCode 7. Reverse Integer 一个整数倒叙输出

    潜在问题:(1)随着求和可能精度会溢出int 范围,需要使用long 来辅助判断是否溢出,此时返回 0 Assume we are dealing with an environment which ...

  2. [Leetcode] reverse integer 反转整数

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  3. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  4. 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。

    namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...

  5. 九度oj 题目1058:反序输出

    题目1058:反序输出 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:9677 解决:3495 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可 ...

  6. rev---将文件中的每行内容以字符为单位反序输出

    rev命令将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,依次类推.

  7. 九度OJ 1058:反序输出 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8454 解决:3042 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可能包含多组用例,每组用例 ...

  8. python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数。如果不存在这样一对整数,则输入一条消息进行说明。

    python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数.如果不存在这样一对整数,则输入一条消息 ...

  9. 输入一个整数n,输出契波那契数列的第n项

    package bianchengti; /* * 输入一个整数n,输出契波那契数列的第n项 * 斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89... */ p ...

随机推荐

  1. android sqlite 怎么写入存储时间

    字符串类型2013-12-10 12:12:12 SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd"); ...

  2. jboss内存查看管理 .

    jboss内存查看管理 标签: jbossjavagenerationjvmclassjar 2009-04-09 14:47 4248人阅读 评论(2) 收藏 举报 本文章已收录于:   // ' ...

  3. Effective C++条款01: 视C++为一个语言联邦

    一开始C++定义为:C with Classes. 如今的C++已经是一个多重范型编程语言,可以把C++视为有四个次语言组成的联邦语言. C.C++任然以C为基础.区块.语句.预处理.内置语言类型.数 ...

  4. java异常——RuntimeException和User Define Exception

    1.RuntimeException public class RuntimeException { public static void main(String[] args) { // TODO ...

  5. hive报错 Another instance of Derby may have already booted the database

    刚装好hive后,启动之后showtables;等正常,退出之后再进入,就发现会报错 Caused by: ERROR XSDB6: Another instance ofDerbymay have ...

  6. Vi Usage

    标签: linux 编辑工具 md 快捷键以及常用命令(前面带:的是命令) h -> 左移一个字符 j -> 下移一行 k -> 上移一行 l -> 右移一个字符 w或Shif ...

  7. django 外键 ,django __

    data sqlite> select * from author; id name age 1 jim 12 2 tom 11 sqlite> select * from book; i ...

  8. Gumshoe - Microsoft Code Coverage Test Toolset

    Gumshoe - Microsoft Code Coverage Test Toolset 2014-07-17 What is Gumshoe? How to instrument a binar ...

  9. 在DirectX 中进行2D渲染

    http://flcstudio.blog.163.com/blog/static/756035392008115111123672/ 最近,我看到很多关于DirectX8在最新的API中摒弃Dire ...

  10. UVa 1609 (博弈) Foul Play

    姑且把它归类为一道博弈吧,毕竟这也是在找必胜方案. 十分有意思的一道题目,设计一种方案让你支持的1队获胜. 题目给出了两个很重要的条件: 1队能打败至少一半的队伍 对于1队不能打败的黑队,一定存在一个 ...