您的位置:首页 > 其它

LeetCode007 Reverse Integer

2017-03-26 20:29 393 查看
详细见:leetcode.com/problems/reverse-integer/

Java Solution:
github

package leetcode;

/*
* 7. Reverse Integer QuestionEditorial Solution My Submissions
Total Accepted: 156921
Total Submissions: 659315
Difficulty: Easy
Reverse digits of an integer.

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

public class P007_ReverseInteger {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(new Solution4().reverse(21474));
}
/*
* 代码最快,最低效的解法
* 10.28%
* 6 ms
*/
static class Solution {
public int reverse(int x) {
boolean isNegative = false;
if (x < 0) {
x = -x;
isNegative = true;
}
StringBuilder st = new StringBuilder(String.valueOf(x));
try {
return isNegative ? -Integer.parseInt(st.reverse().toString()) : Integer.parseInt(st.reverse().toString());
} catch (Exception e) {
return 0;
}
}
}
/*
* 20.05%
* 3ms
*/
static class Solution2 {
public int reverse(int x) {
boolean isNegative = false;
if (x < 0) {
x = -x;
isNegative = true;
}
int ans = 0, tmp = ans;
while (x > 0) {
if (ans > 214748364 || (ans == 214748364 && (x % 10) > 7))
return 0;
tmp = ans * 10 + (x % 10);
ans = tmp;
x = x / 10;
}
return isNegative ? -ans : ans;
}
}
/*
* 判断溢出的方法更改为轮子哥的。
* 20.05%
* 3ms
*/
static class Solution3 {
public int reverse(int x) {
boolean isNegative = false;
if (x < 0) {
x = -x;
isNegative = true;
}
int ans = 0, tmp = ans;
while (x > 0) {
tmp = ans * 10 + (x % 10);
if ((tmp - (x % 10)) % 10 == 0 && (tmp - (x % 10)) / 10 == ans)
ans = tmp;
else
return 0;
x = x / 10;
}
return isNegative ? -ans : ans;
}
}
/*
* 人生苦短,何不用long
* 20.05%
* 3ms
* 再也不优化了
*/
static class Solution4 {
public int reverse(int x) {
boolean isNegative = false;
if (x < 0) {
x = -x;
isNegative = true;
}
long ans = 0;
while (x > 0) {
ans = ans * 10 + (x % 10);
x = x / 10;
}
if ( (ans >> 31) != 0 )
return 0;
return isNegative ? -(int)ans : (int)ans;
}
}
}


C Solution:
github
/*
url: leetcode.com/problems/reverse-integer
22ms 17.10%
*/

#include <stdio.h>
#include <stdlib.h>

int reverse(int x) {
int arr[12];
int i;
int ans = 0;
int j = 0;
int pre = 0;
int max = 2147483647;
if (x == -2147483648)
return 0;
if (x < 0)
return -reverse(-x);
for (i = 0; i < 12 && x != 0; i ++) {
arr[i] = x % 10;
x = x / 10;
}
for (j = 0; j < i; j ++) {
pre = ans;
if (ans > max / 10 || (ans == max / 10 && arr[j] > 7))
return 0;
ans = ans * 10 + arr[j];
}
return ans;
}

int main() {
printf("%d\r\n", reverse(1534236469));
}

Python Solution:
github
#coding=utf-8

'''
url: leetcode.com/problems/reverse-integer/
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年3月26日
@details: Solution: AC 58ms 66.96%
'''

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x < 0: return -self.reverse(-x)
s = str(x)
i, j = 0, len(s) - 1
l = []
for i in range(j, -1, -1):
l.append(s[i])
v = int("".join(l))
if v > 2147483647 or v < -2147483648:
return 0
return v

if __name__ == "__main__":
v = -247483647
s = Solution()
print(s.reverse(v))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode