LeetCode 7. Reverse Integer(反转整数)

原创 2018年04月17日 20:21:17

题目网址:https://leetcode.com/problems/reverse-integer/description/


题目要求:

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1].

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

算法思路:

  • 定义一个整数变量result = 0
  • 通过取余,每次获取一个个位数(tail)
  • tail*10+result 即为反转后的数字
  • 为了防止反转后的数字越界要判断反转后的数字/10 是否和原数相等

代码:

    /**
     * 反转整数
     * @param x 整数
     * @return 反转
     */
    public int reverse(int x) {
        int result =0;
        while (x != 0)
        {
            int tail = x%10;
            int newResult = result*10+tail;
            if((newResult - tail)/10 != result) {return 0;}
            result = newResult;
            x = x/10;
        }
        return result;
    }



[C++]LeetCode 7:Reverse Integer(翻转整数)

Problem: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321...
  • emmaalways
  • emmaalways
  • 2015-05-02 14:23:48
  • 1434

7. Reverse Integer [easy] (Python)

题目链接https://leetcode.com/problems/reverse-integer/题目原文 Reverse digits of an integer. Example1: ...
  • coder_orz
  • coder_orz
  • 2016-07-26 21:50:37
  • 3042

[leetcode 7] Reverse Integer(整数反转) c函数实现

[leetcode 7] Description: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x =...
  • banana1006034246
  • banana1006034246
  • 2017-03-22 18:58:22
  • 823

【LeetCode-面试算法经典-Java实现】【007-Reverse Integer(翻转整数)】

【007-Reverse Integer(翻转整数)】Reverse digits of an integer. Example1: x = 123, return 321 ,Example2: x ...
  • DERRANTCM
  • DERRANTCM
  • 2015-07-18 06:25:38
  • 3712

【LeetCode-Algorithm】【7-Reverse Integer】【Python】

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click ...
  • IFollowRivers
  • IFollowRivers
  • 2016-09-20 19:04:47
  • 1057

【leetcode】Reverse Integer整数反转----Java代码实现

用Java实现反转整数 Reverse Integer
  • yxb09501bin
  • yxb09501bin
  • 2016-11-17 17:00:34
  • 2488

7. Reverse Integer 反转int

7. Reverse Integer问题:反转int,当有负号时需要保留负号。解决思路:1、先将int类型转换为string,按照之前写过的string类型做好反转,再转为int类型。2、不做类型转换...
  • u010223431
  • u010223431
  • 2016-05-06 09:48:57
  • 2569

LeetCode7——Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 大意 反转i...
  • booirror
  • booirror
  • 2015-01-26 11:21:12
  • 2868

leetcode 7. Reverse Integer(C语言,翻转一个整数,判断是否溢出)19

leetcode 7. Reverse Integer(C语言,反向一个整数,判断溢出)19C语言解析及代码,另附判断溢出总结...
  • m0_37454852
  • m0_37454852
  • 2017-10-01 21:04:41
  • 147

LeetCode: Reverse Integer (JavaScript)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 本地测试通过...
  • notail_woo
  • notail_woo
  • 2015-04-23 20:41:39
  • 849
收藏助手
不良信息举报
您举报文章:LeetCode 7. Reverse Integer(反转整数)
举报原因:
原因补充:

(最多只允许输入30个字)