-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Coder_iOS
authored and
Coder_iOS
committed
May 24, 2019
1 parent
c964de4
commit cdcd3d7
Showing
6 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters