Skip to content

Commit fb3d91f

Browse files
authored
Merge pull request #3 from CoderLiuJixin/master
07_整数反转
2 parents c964de4 + 95fd173 commit fb3d91f

File tree

6 files changed

+142
-1
lines changed

6 files changed

+142
-1
lines changed

0007-整数反转/C++/reverseInt.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
// 07_整数反转_C++
3+
//非好方法,小伙伴们有好的方法请补充,并在下方添加,谢谢!
4+
5+
class Solution {
6+
public:
7+
int reverse(int inputInt) {
8+
int result = 0;
9+
while (inputInt != 0) {
10+
int temp = inputInt % 10; //获得最后一位
11+
inputInt /= 10; //去除最后一位
12+
if (result > 214748364 || (result == 214748364 && temp > 7)) return 0;//正数溢出
13+
if (result < -214748364 || (result == -214748364 && temp < -8)) return 0;//负数溢出
14+
15+
result = result * 10 + temp;//反转
16+
}
17+
return result;
18+
}
19+
};

0007-整数反转/C/reverseInt.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// LJXReverseInt.c
3+
// LJXDemo
4+
//
5+
// Created by 刘吉新 on 2019/5/24.
6+
// Copyright © 2019年 com.auvgo.tmc. All rights reserved.
7+
//
8+
9+
// 07_整数反转_C
10+
11+
//每次拿出整数的最后一位,放到新的整数变量 result 后面
12+
//非好方法,小伙伴们有好的方法请补充,并在下方添加,谢谢!
13+
14+
#define INT_MAX 2147483647 //INT_MAX = 2^31-1 = 2147483647
15+
#define INT_MIN -2147483648 //INT_MIN = -2^31=-2147483648
16+
17+
int reverse(int inputInt) {
18+
int result = 0;
19+
while(inputInt != 0)
20+
{
21+
int temp = inputInt % 10;
22+
inputInt /= 10;
23+
//当result > INT_MAX/10,那么这个反转整数必然溢出
24+
//同理result < INT_MIN/10,那么这个反转整数也溢出
25+
//如果result = INT_MAX/10 或 INT_MIN/10,那么我们只需要比较最后一位。正数:temp > 7就溢出;负数:temp < -8就溢出
26+
if(result > INT_MAX / 10 || (result == INT_MAX / 10 && temp > 7))
27+
return 0;
28+
if(result < INT_MIN / 10 || (result == INT_MIN / 10 && temp < -8))
29+
return 0;
30+
result = result * 10 + temp;
31+
}
32+
return result;
33+
}
34+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
// 07_整数反转_Java
3+
4+
5+
class Solution {
6+
public int reverse(int inputInt) {
7+
int result = 0;
8+
while (inputInt != 0) {
9+
int temp = inputInt % 10; //获得最后一位
10+
inputInt /= 10; //去除最后一位
11+
if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && temp > 7)) { //正数溢出检查
12+
return 0;
13+
}
14+
if (result < Integer.MIN_VALUE / 10 || (result == Integer.MIN_VALUE / 10 && temp < -8)) { //负数溢出时检查
15+
return 0;
16+
}
17+
result = result * 10 + temp; //反转
18+
}
19+
return result;
20+
21+
}
22+
}

0007-整数反转/OC/reverseInt.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// LJXReverseInt.m
3+
// LJXDemo
4+
//
5+
// Created by 刘吉新 on 2019/5/24.
6+
// Copyright © 2019年 com.auvgo.tmc. All rights reserved.
7+
//
8+
9+
#define INT_MAX 2147483647 //INT_MAX = 2^31-1 = 2147483647
10+
#define INT_MIN -2147483648 //INT_MIN = -2^31=-2147483648
11+
12+
#pragma mark - 07_整数反转_OC
13+
//每次拿出整数的最后一位,放到新的整数变量 result 后面
14+
//非好方法,小伙伴们有好的方法请补充,并在下方添加,谢谢!
15+
- (NSInteger)reverse:(NSInteger)inputInt {
16+
NSInteger result = 0;
17+
while (inputInt != 0) {
18+
//获得最后一位
19+
NSInteger temp = inputInt % 10;
20+
//去除最后一位
21+
inputInt /= 10;
22+
//正数溢出检查
23+
if (result > INT_MAX / 10 || (result == INT_MAX / 10 && temp > 7)) {
24+
return 0;
25+
}
26+
//负数溢出时检查
27+
if (result < INT_MIN / 10 || (result == INT_MIN / 10 && temp < -8)) {
28+
return 0;
29+
}
30+
result = result * 10 + temp; //反转
31+
}
32+
return result;
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// LJXReverseInt.swift
3+
// AuvGo
4+
//
5+
// Created by 刘吉新 on 2019/5/24.
6+
// Copyright © 2019 com.auvgo.tmc. All rights reserved.
7+
//
8+
9+
//写的比较low,有好方法的小伙伴欢迎指导,在该方法的下方,另写一个好的方法或指导意见,谢谢!
10+
11+
func reverse(_ x: Int) -> Int {
12+
var num = x
13+
var result = 0;
14+
while num != 0 {
15+
// 获得最后一个数
16+
let temp = num % 10
17+
// 去除最后一位
18+
num /= 10
19+
// 正数溢出检查
20+
if result > 214748364 || (result == 214748364 && temp > 7) {
21+
return 0
22+
}
23+
// 负数溢出检查
24+
if result < -214748364 || (result == -214748364 && temp < -8) {
25+
return 0
26+
}
27+
// 反转
28+
result = result * 10 + temp
29+
}
30+
return result
31+
}
32+
33+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
| 4 | [寻找两个有序数组的中位数](https://leetcode-cn.com/problems/median-of-two-sorted-arrays) | 困难 | | | | | |
1717
| 5 | [最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring) | 中等 | | | | | |
1818
| 6 | [Z 字形变换](https://leetcode-cn.com/problems/zigzag-conversion) | 中等 | | | | | |
19-
| 7 | [整数反转](https://leetcode-cn.com/problems/reverse-integer) | 简单 | | | | | |
19+
| 7 | [整数反转](https://leetcode-cn.com/problems/reverse-integer) | 简单 | [Swift](0007-整数反转/Swift) | [Java](0007-整数反转/Java) | [C](0007-整数反转/C) | [C++](0007-整数反转/C++) | [OC](0007-整数反转/OC) | |
2020
| 8 | [字符串转换整数 (atoi)](https://leetcode-cn.com/problems/string-to-integer-atoi) | 中等 | | | | | |
2121
| 9 | [回文数](https://leetcode-cn.com/problems/palindrome-number) | 简单 | [Swift](0009-回文数/Swift) | | | | |
2222
| 10 | [正则表达式匹配](https://leetcode-cn.com/problems/regular-expression-matching) | 困难 | | | | | |

0 commit comments

Comments
 (0)