## Reverse Integer -- LeetCode 原

Reverse digits of an integer.

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

click to show spoilers.

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):

[解题思路]

Init Code : RunTime 308ms

``````public int reverse(int x) {
boolean negative = false;
String str = String.valueOf(x);
if(x<0){
negative = true;
str = str.substring(1);
}
char[] strArray = str.toCharArray();
int length = strArray.length;

for(int i=0;i<length/2;i++){
char temp = strArray[i];
strArray[i] = strArray[length-1-i];
strArray[length-1-i] = temp;
}
str = String.valueOf(strArray);
int reverseX = 0;
try{
reverseX = Integer.parseInt(str);
}catch(Exception e){
return reverseX;
}
if(negative){
return -reverseX;
}
return reverseX;
}``````

Improved Code : RunTime :296ms

``````public int reverse(int x) {
if(x==Integer.MIN_VALUE)
return 0;
int num = Math.abs(x);
int res = 0;
while(num!=0){
if(res > (Integer.MAX_VALUE-num%10)/10){
return 0;
}
res = res*10+num%10;
num /= 10;
}
return x>0?res:-res;
}`````` ### 勤奋的桑尼

[leetcode] Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the int......

jdflyfly
2014/06/24
0
0
leetcode -- Reverse Integer

Reverse Integer Description Given a 32-bit signed integer, reverse digits of an integer. Example 1: Example 2: Example 3: Note: Assume we are dealing with an environment which c......

leiline
2017/11/07
0
0
[leetcode] Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus......

jdflyfly
2014/06/24
0
1
LeetCode：Reverse Integer - 翻转数字

1、题目名称 Reverse Integer（翻转数字） 2、题目地址 https://leetcode.com/problems/reverse-integer/ 3、题目内容 英文：Reverse digits of an integer. 中文：翻转一个正整数的各位，形...

2015/09/20
408
0
LeetCode：Palindrome Number - 回文数

1、题目名称 Palindrome Number（回文数） 2、题目地址 https://leetcode.com/problems/palindrome-number 3、题目内容 英文：Determine whether an integer is a palindrome. Do this witho......

2015/09/24
109
0

idea下springboot 项目在static目录下添加文件不生效

idea下springboot 项目在static目录下添加文件不生效 问题描述 是这样子的，我的项目目录结构如下： 我在static目录下，创建了index.html和aaaa.jpg这两个文件。然后，启动服务访问 http://l...

wotrd

5
0
k8s1.14 一、环境

1. 4台虚拟机 (CentOS Linux release 7.2.1511 (Core) ） 192.168.130.211 master 192.168.130.212 node1 192.168.130.213 node2 192.168.130.214 node3 2. 设置服务器hostname 2.1 设置本机......

ThomasCheng

4
0

linuxCool

1
0

go4it

14
0
HyperLogLog简介

（1）HyperLogLog简介 　　 　　在Redis 在 2.8.9 版本才添加了 HyperLogLog，HyperLogLog算法是用于基数统计的算法，每个 HyperLogLog 键只需要花费 12 KB 内存，就可以计算接近 2^64 个...

SEOwhywhy

1
0 