题目:

Reverse digits of an integer.

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

click to show spoilers.

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?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):
Test cases had been added to test the overflow behavior.

思路:

  • 题意:反转整数
  • 注意两点:1.负数的情况2.溢出返回0(绝对值大于Integer.MAX_VALUE)

代码:

public class Solution {
    public int reverse(int x) {
        boolean flag = true;
        if(x < 0){
            flag = false;
            x = x*-1;
        }
        long res = 0;
        while(x > 0){
            res = res *10 + x%10;
            x = x / 10;
        }
        if(res > Integer.MAX_VALUE){
            return 0;
        }
        if(flag){
            return (int)res;
        }else{
            return (int)res * -1;
        }
    }
}

leetCode(62)-Reverse Integer的更多相关文章

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

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

  2. leetcode:Reverse Integer 及Palindrome Number

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

  3. LeetCode 7 Reverse Integer & int

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

  4. Leetcode 7. Reverse Integer(水)

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

  5. leetcode:Reverse Integer(一个整数反序输出)

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

  6. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

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

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

  8. LeetCode 7. Reverse Integer (倒转数字)

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

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

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

随机推荐

  1. memcached实战系列(三)memcached命令使用

    memcached命令的使用,在这里我们最好了解一下命令的含义,对命令有一个大致的了解,在了解的基础上进行使用.这里的命名是常用的crud命令的演示. 1.1.1. memcached命令的格式 标准 ...

  2. linux下字节对齐

    一,内存地址对齐的概念    计算机内存中排列.访问数据的一种方式,包含基本数据对齐和结构体数据对齐.    32位系统中,数据总线宽度为32,每次能够读取4字节数据.地址总线为32,最大寻址空间为4 ...

  3. Android计时器Chronometer-android学习之旅(二十一)

    Chronometer简介 Chronometer和DigitalColok都继承与TextView,但是Chronometer不是显示的当前时间,而是从某个时间开始又过去了多少时间,是一个时间差. ...

  4. 创建一个QT for Android的传感器应用应用程序(摘自笔者2015年将出的《QT5权威指南》,本文为试读篇)

     这个手册描述了使用Qt Quick面访的方式在Android和ios设备上开发QtQuick应用程序的方法.我们使用Qt Creator实现一个QtQuick应用程序,这个应用程序基于加速器的值 ...

  5. 开源项目——小Q聊天机器人V1.1

    小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...

  6. INV_TXN_MANAGER_PUB.PROCESS_TRANSACTIONS

    For Interface Transactions,INV_TXN_MANAGER_PUB.PROCESS_TRANSACTIONS DOES below things: 1)validate_gr ...

  7. Android进阶(二十五)setTextColor()的参数设置方式

    setTextColor()的参数设置方式 查了下资料发现setTextColor()的参数可以写成以下形式: 直接使用颜色值 setTextColor(0xFF0000FF);//0xFF0000F ...

  8. Unity UGUI基础之InputField

    InputField(输入域):为文本输入控件,等同于NGUI的Input. 一.InputField组件: Text Component(文本组件):此输入域的文本显示组件,需带有Text组件. T ...

  9. UNIX环境高级编程——进程关系

    一.终端的概念 在UNIX系统中,用户通过终端登录系统后得到一个Shell进程,这个终端成为Shell进程的控制终端(Controlling Terminal),控制终端是保存在PCB中的信息,而我们 ...

  10. Eclipse插件 - FindBugs 检查代码隐藏的 Bug

    简介         FindBugs 是一个在 Java 程序中查找 bug 的程序,它可以查找可能出错的代码,注意 FindBugs 是检查 Java 字节码,也就是*.class文件.其实准确的 ...