Reverse Integer

转载 2015年07月07日 23:51:43

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.

class Solution {
    
    	public int reverse(Integer x) {
		
		long result=0;
		long factor = (x>=0)?1:-1;
		long temp = x*factor;
		
		while (temp>0) {
			long a = temp%10;
			result = result*10+a;
			temp=temp/10;			
		}
		
		if(result>Integer.MAX_VALUE){
			return 0;
		}
		return (int)(result*factor);
    }
    
};


相关文章推荐

【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
  • 345

关于LeetCode中Reverse Integer一题的理解

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

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

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

7.Reverse Integer

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

LeetCode Reverse Integer

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

Leetcode 7 Reverse Integer

Reverse Integer Total Accepted: 89449 Total Submissions: 373087 Reverse digits of an intege...
  • javays1
  • javays1
  • 2015年08月16日 23:07
  • 284

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
  • 167
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Reverse Integer
举报原因:
原因补充:

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