题目: 

  Reverse digits of an integer.

  Example1: x = 123, return 321
  Example2: x = -123, return -321

思路:不断取最低位加到先前值得后面,如下:

        while(x!=0){
          res=res*10+x%10;
          x/=10;
        }

   还要注意反转后可能会出现溢出的情况。

public class Solution {
public int reverse(int x) {
long res=0;
while(x!=0){
res=res*10+x%10;
x/=10;
} if(res>Integer.MAX_VALUE||res<Integer.MIN_VALUE){
return 0;
}else{
return (int)res;
}
}
}

  

【LeetCode】7. Reverse Integer 整型数反转的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  3. leetcode题解||Reverse Integer 问题

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

  4. leetcode:Reverse Integer 及Palindrome Number

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

  5. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  6. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  7. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  8. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  9. [leetcode]7. Reverse Integer反转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

随机推荐

  1. 【Hadoop测试程序】编写MapReduce测试Hadoop环境

    我们使用之前搭建好的Hadoop环境,可参见: <[Hadoop环境搭建]Centos6.8搭建hadoop伪分布模式>http://www.cnblogs.com/ssslinppp/p ...

  2. Python 迭代删除重复项,集合删除重复项

    1. 迭代删除重复项:先排序列表项,然后通过新迭代(not in)去除重复项,分片打印 def sanitize(time_string): if '-' in time_string: splitt ...

  3. nsq的erlang客户端

    nsq是基于golang开发的分布式消息系统,这里仅仅贴个和erlang之间的通信demo rebar-creator create-app test_nsq rebar.config % -*- e ...

  4. QueryRunner的API

    org.apache.commons.dbutils Class QueryRunner java.lang.Object org.apache.commons.dbutils.AbstractQue ...

  5. 从1970年1月1日00:00:00 GMT以来此时间对象表示的毫秒数转化为Datetime

    1970年1月1日(00:00:00 GMT)Unix 时间戳(Unix Timestamp)对时间转换 将Long类型转换为DateTime类型 /// <summary> /// 将L ...

  6. 黄聪:MYSQL5.6缓存性能优化my.ini文件配置方案

    使用MYSQL版本:5.6 [client] …… default-character-set=gbk default-storage-engine=MYISAM max_connections=10 ...

  7. (C/C++) memset

    C语言: memset   extern void *memset(void *buffer,int c,int count);   #include <string.h>   功能:把b ...

  8. (C#) 发布程序,包含某些配置文件或数据文件。

    在VS2012里面,右击需要发布的Project,选择“Properties“, 在弹出的窗口里面点选”Publish“, 再点击”Application Files“, 将默认的Publish St ...

  9. .net关于httpModules的应用示例

    这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时 候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用. HttpModule:Http模块,可以在页面处理前后. ...

  10. 把vector中的string对象导入到字符指针数组中

    #include <iostream>#include <string>#include <vector>//#include <cctype>#inc ...