标签:nbsp 标签 http 题目 this ret time while -o
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 hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Java Solution:
Runtime beats 80.84%
完成日期:06/12/2017
关键词:reverse int
关键点:% 10; / 10
1 class Solution 2 { 3 public int reverse(int x) 4 { 5 int res = 0; 6 7 while(x != 0) 8 { 9 int tail = x % 10; 10 int newRes = res * 10 + tail; 11 12 if((newRes - tail) / 10 != res) // check overflow 13 return 0; 14 15 res = newRes; 16 x = x / 10; 17 } 18 19 20 return res; 21 } 22 }
参考资料:https://discuss.leetcode.com/topic/6104/my-accepted-15-lines-of-code-for-java
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 7. Reverse Integer (倒转数字)
标签:nbsp 标签 http 题目 this ret time while -o
原文地址:http://www.cnblogs.com/jimmycheng/p/8016130.html