Skip to content

Commit

Permalink
07_整数反转
Browse files Browse the repository at this point in the history
  • Loading branch information
Coder_iOS authored and Coder_iOS committed May 24, 2019
1 parent c964de4 commit cdcd3d7
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 1 deletion.
19 changes: 19 additions & 0 deletions 0007-整数反转/C++/reverseInt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

// 07_整数反转_C++
//非好方法,小伙伴们有好的方法请补充,并在下方添加,谢谢!

class Solution {
public:
int reverse(int inputInt) {
int result = 0;
while (inputInt != 0) {
int temp = inputInt % 10; //获得最后一位
inputInt /= 10; //去除最后一位
if (result > 214748364 || (result == 214748364 && temp > 7)) return 0;//正数溢出
if (result < -214748364 || (result == -214748364 && temp < -8)) return 0;//负数溢出

result = result * 10 + temp;//反转
}
return result;
}
};
34 changes: 34 additions & 0 deletions 0007-整数反转/C/reverseInt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// LJXReverseInt.c
// LJXDemo
//
// Created by 刘吉新 on 2019/5/24.
// Copyright © 2019年 com.auvgo.tmc. All rights reserved.
//

// 07_整数反转_C

//每次拿出整数的最后一位,放到新的整数变量 result 后面
//非好方法,小伙伴们有好的方法请补充,并在下方添加,谢谢!

#define INT_MAX 2147483647 //INT_MAX = 2^31-1 = 2147483647
#define INT_MIN -2147483648 //INT_MIN = -2^31=-2147483648

int reverse(int inputInt) {
int result = 0;
while(inputInt != 0)
{
int temp = inputInt % 10;
inputInt /= 10;
//当result > INT_MAX/10,那么这个反转整数必然溢出
//同理result < INT_MIN/10,那么这个反转整数也溢出
//如果result = INT_MAX/10 或 INT_MIN/10,那么我们只需要比较最后一位。正数:temp > 7就溢出;负数:temp < -8就溢出
if(result > INT_MAX / 10 || (result == INT_MAX / 10 && temp > 7))
return 0;
if(result < INT_MIN / 10 || (result == INT_MIN / 10 && temp < -8))
return 0;
result = result * 10 + temp;
}
return result;
}

22 changes: 22 additions & 0 deletions 0007-整数反转/Java/reverseInt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

// 07_整数反转_Java


class Solution {
public int reverse(int inputInt) {
int result = 0;
while (inputInt != 0) {
int temp = inputInt % 10; //获得最后一位
inputInt /= 10; //去除最后一位
if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && temp > 7)) { //正数溢出检查
return 0;
}
if (result < Integer.MIN_VALUE / 10 || (result == Integer.MIN_VALUE / 10 && temp < -8)) { //负数溢出时检查
return 0;
}
result = result * 10 + temp; //反转
}
return result;

}
}
33 changes: 33 additions & 0 deletions 0007-整数反转/OC/reverseInt.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// LJXReverseInt.m
// LJXDemo
//
// Created by 刘吉新 on 2019/5/24.
// Copyright © 2019年 com.auvgo.tmc. All rights reserved.
//

#define INT_MAX 2147483647 //INT_MAX = 2^31-1 = 2147483647
#define INT_MIN -2147483648 //INT_MIN = -2^31=-2147483648

#pragma mark - 07_整数反转_OC
//每次拿出整数的最后一位,放到新的整数变量 result 后面
//非好方法,小伙伴们有好的方法请补充,并在下方添加,谢谢!
- (NSInteger)reverse:(NSInteger)inputInt {
NSInteger result = 0;
while (inputInt != 0) {
//获得最后一位
NSInteger temp = inputInt % 10;
//去除最后一位
inputInt /= 10;
//正数溢出检查
if (result > INT_MAX / 10 || (result == INT_MAX / 10 && temp > 7)) {
return 0;
}
//负数溢出时检查
if (result < INT_MIN / 10 || (result == INT_MIN / 10 && temp < -8)) {
return 0;
}
result = result * 10 + temp; //反转
}
return result;
}
33 changes: 33 additions & 0 deletions 0007-整数反转/Swift/reverseInt.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// LJXReverseInt.swift
// AuvGo
//
// Created by 刘吉新 on 2019/5/24.
// Copyright © 2019 com.auvgo.tmc. All rights reserved.
//

//写的比较low,有好方法的小伙伴欢迎指导,在该方法的下方,另写一个好的方法或指导意见,谢谢!

func reverse(_ x: Int) -> Int {
var num = x
var result = 0;
while num != 0 {
// 获得最后一个数
let temp = num % 10
// 去除最后一位
num /= 10
// 正数溢出检查
if result > 214748364 || (result == 214748364 && temp > 7) {
return 0
}
// 负数溢出检查
if result < -214748364 || (result == -214748364 && temp < -8) {
return 0
}
// 反转
result = result * 10 + temp
}
return result
}


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

0 comments on commit cdcd3d7

Please sign in to comment.