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).

数字逆转。

可转成字符数组。再处理。可是这样要考虑的情况比較多,并且使用了新的空间,效率不高。

能够通过求模运算获得依次低位,再把原乘以10加上新取得的数。

	public static int reverse(int x) {
int ret = 0;
while (x != 0) {
ret = ret * 10 + x % 10;
x /= 10;
}
return ret;
}

比方:输入-192,计算步骤例如以下,经过了三轮循环:

ret x

---------------

0 -192

-2 -192

-2 -19

---------------

-2 -19

-29 -19

-29 -1

---------------

-29 -1

-291 -1

-291 0

---------------

-291

LeetCode——Reverse Integer的更多相关文章

  1. LeetCode: Reverse Integer 解题报告

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

  2. [LeetCode] Reverse Integer 翻转整数

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

  3. C++ leetcode::Reverse Integer

    第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...

  4. [Leetcode] reverse integer 反转整数

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

  5. LeetCode——Reverse Integer(逆置一个整数)

    问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321   Ha ...

  6. Leetcode: Reverse Integer 正确的思路下-要考虑代码简化

    题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have ...

  7. leetcode:Reverse Integer【Python版】

    1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...

  8. [Leetcode]Reverse Integer

      核心思想:原数对10取余数赋值给新数后降一位,再把新数升一位加上下一次原数取余值,直到原数降为0. 解法如下: int reverse(int x) { bool minus = false; ) ...

  9. leetcode reverse integer&&Palindrome Number

    public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...

随机推荐

  1. django基础(web框架,http协议,django安装)

    学习Django之前我们先来看什么是OSI七层模型: 应用层 表示层       应用层(五层模型中把这三层合成一个应用层) http协议 会话层 传输层                  提供端口对 ...

  2. Python开发:网络编程

    Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的全部方法. 高级别的网络 ...

  3. 关于面试总结-python笔试题

    关于面试总结4-python笔试题 前言 现在面试测试岗位,一般会要求熟悉一门语言(python/java),为了考验求职者的基本功,一般会出2个笔试题,这些题目一般不难,主要考察基本功. 要是给你一 ...

  4. Python常用操作符

    Python常用操作符 1.成员关系操作符in 显示的数字前面填充'0'代替空格 6.转义字符 符号 含义 \' 单引号\" 双引号\a 发出系统响铃声\b 退格符\n 换行符\t 横向制表 ...

  5. hdu2074

    我先求出交叉的gird,然后再一行一行求得.感觉还可以吧.思路比较清晰,开始想的是数是第几行然后从每一行的前后开始控制,好麻烦的感觉,我就先求出来了框架再做就好做多啦!后来PE,突然发现我特殊处理n= ...

  6. XML文件中<return_code><![CDATA[SUCCESS]]></return_code>中CDATA的用法

    转义字符不合法的XML字符必须被替换为相应的实体. 如果在XML文档中使用类似"<" 的字符, 那么解析器将会出现错误,因为解析器会认为这是一个新元素的开始.所以不应该象下面 ...

  7. CSS相对布局和绝对布局

    relative 相对布局,正常的,从上到下.绝对布局absolute,就像不占位置,透明了一样,会和别的重合

  8. xmpp 与服务器连接并身份验证成功

    *  XMPP的特点,所有的请求都是通过代理的方式实现的 * *  因为xmpp是经由网络服务器进行数据通讯的,因此所有的,因此所有的请求都是提交给服务器处理 * *  服务器处理完毕止呕,以代理的方 ...

  9. HackerRank# Fibonacci Modified

    原题地址 竟然64位都要爆,这是要大整数乘法的节奏吗?我才不要写大整数乘法呢,用Ruby干掉 代码: # Enter your code here. Read input from STDIN. Pr ...

  10. 刷题总结——spoj1812(后缀自动机+DP)

    题目: A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is t ...