LeetCode:Reverse Integer

原创 2015年11月20日 18:01:57

Reverse Integer

Total Accepted: 108334 Total Submissions: 462300 Difficulty: Easy

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.

Subscribe to see which companies asked this question

Hide Tags
 Math
Hide Similar Problems
 (E) String to Integer (atoi)



































code:

class Solution {
public:
    int reverse(int x) {
        if(0 == x) return 0;
        int ret;
        long long sum = 0;
        while(0 == x % 10) x /= 10;
        while(x) {
            sum = sum * 10 + x % 10;
            x /= 10;
        }
        ret = sum;
        if(ret == sum) return ret;
        else return 0;
    }
};


版权声明:本文为博主原创文章,转载请注明出处。

【LeetCode7】【Reverse Integer】java中整形正负表示的一些思考

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

Reverse Integer leetcode

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

LeetCode | 7)Reverse Integer

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

【leetcode c++】Reverse Integer

题目很短,也很简单。 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -3...
  • hqq39
  • hqq39
  • 2015年06月07日 19:44
  • 374

LeetCode 7 — Reverse Integer(C++ Java Python)

题目:http://oj.leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 1...

LeetCode Reverse Integer

Reverse Integer Total Accepted: 61132 Total Submissions: 219035 My Submissions Question Solution R...

LeetCode OJ - Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 c...
  • cinrry
  • cinrry
  • 2016年01月08日 18:13
  • 182

leetcode Reverse Integer java基本编程学习笔记(一)

时隔一个月,再次拾起leetcode开始刷题。 我这种人吧,总是做一段时间,某天因为某个原因给搁置了,然后就没有然后了,等在想起来总能给自己那段时间寻找"合理"的借口,这种人是不是要一辈子成为lose...

leetcode 2---Reverse Integer

题记:        由于临近毕业要找工作,虽然看了一遍数据结构和算法,但是总感觉心里发虚。而且项目中所接触到得编程都是基于其他方面得改进算法,总之本人编程能力有限,经常被人鄙视,所以下定决心,...

LeetCode07:Reverse Integer

原题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -32...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode:Reverse Integer
举报原因:
原因补充:

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