LeetCode 7. Reverse Integer(整数反转)
2018-03-16 17:44
836 查看
题目描述: Given a 32-bit signed integer, reverse digits of an integer.
例一:Input:123Output: 321例二:Input:-123Output:-321例三:Input:120Output:21注意:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
分析:
题意:把一个整型的数进行反转,保持原来的符号并返回(去掉前置0)。如果反转之后上溢出或者下溢出,则返回0。
思路一:由于反转之后存在溢出的问题,所以我们先把整型(int,取值范围-2^31~2^31-1)转化为长整型(long long int,取值范围-2^63-2^63-1),这样确保在长整型范围内不会溢出。最后把结果和整型的最大、最小值进行对比,溢出返回0,不溢出返回自身。
思路二:如果不使用长整型(long long int)呢?我们知道整型的取值范围为-2^31~2^31-1,即-2147483648~2147483647,那么我们先判断输入整数的正负号,把对应最大的数和输入的整数逆序保存在数组中,同时得到了输入的位数和逆序绝对值。如果位数小于10位,则必然不会溢出,返回逆序绝对值和符号的乘积即可;如果等于10位,则我们从数字的高位开始逐位比较:①输入的该位更大,则溢出;②两位一样大,则跳过;③输入的该位更小,则不会溢出,返回乘积即可。
代码(思路一):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
class Solution {
public:
int reverse(int x) {
// Exceptional Case:
if(x == 0){
return 0;
}
LL xx = (LL)x, yy = 0;
int sign = 1;
if(xx < 0){
sign = -1;
xx = -xx;
}
while(xx){
yy = yy * 10 + (xx % 10);
xx /= 10;
}
if(sign == -1){
yy = -yy;
}
if((sign == 1 && yy <= INT_MAX) || (sign == -1 && yy >= INT_MIN)){
return (int)yy;
}
return 0;
}
};代码(思路二):
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int reverse(int x) {
vector<int> maxNum = {7, 4, 6, 3, 8, 4, 7, 4, 1, 2};
vector<int> num;
int sign = (x >= 0)? 1: -1;
if(sign == -1){
maxNum[0] = 8;
}
int ans = 0;
while(x){
int digit = (abs)(x % 10);
num.insert(num.begin(), digit);
ans = 10 * ans + digit;
x /= 10;
}
int n = num.size();
// n: 1 - 9
if(n < 10){
return ans * sign;
}
// n: 10
int i = 9;
while(i >= 0){
if(num[i] < maxNum[i]){
return ans * sign;
}
else if(num[i] == maxNum[i]){
i--;
continue;
}
else {
return 0;
}
}
return ans * sign;
}
};
例一:Input:123Output: 321例二:Input:-123Output:-321例三:Input:120Output:21注意:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
分析:
题意:把一个整型的数进行反转,保持原来的符号并返回(去掉前置0)。如果反转之后上溢出或者下溢出,则返回0。
思路一:由于反转之后存在溢出的问题,所以我们先把整型(int,取值范围-2^31~2^31-1)转化为长整型(long long int,取值范围-2^63-2^63-1),这样确保在长整型范围内不会溢出。最后把结果和整型的最大、最小值进行对比,溢出返回0,不溢出返回自身。
思路二:如果不使用长整型(long long int)呢?我们知道整型的取值范围为-2^31~2^31-1,即-2147483648~2147483647,那么我们先判断输入整数的正负号,把对应最大的数和输入的整数逆序保存在数组中,同时得到了输入的位数和逆序绝对值。如果位数小于10位,则必然不会溢出,返回逆序绝对值和符号的乘积即可;如果等于10位,则我们从数字的高位开始逐位比较:①输入的该位更大,则溢出;②两位一样大,则跳过;③输入的该位更小,则不会溢出,返回乘积即可。
代码(思路一):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
class Solution {
public:
int reverse(int x) {
// Exceptional Case:
if(x == 0){
return 0;
}
LL xx = (LL)x, yy = 0;
int sign = 1;
if(xx < 0){
sign = -1;
xx = -xx;
}
while(xx){
yy = yy * 10 + (xx % 10);
xx /= 10;
}
if(sign == -1){
yy = -yy;
}
if((sign == 1 && yy <= INT_MAX) || (sign == -1 && yy >= INT_MIN)){
return (int)yy;
}
return 0;
}
};代码(思路二):
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int reverse(int x) {
vector<int> maxNum = {7, 4, 6, 3, 8, 4, 7, 4, 1, 2};
vector<int> num;
int sign = (x >= 0)? 1: -1;
if(sign == -1){
maxNum[0] = 8;
}
int ans = 0;
while(x){
int digit = (abs)(x % 10);
num.insert(num.begin(), digit);
ans = 10 * ans + digit;
x /= 10;
}
int n = num.size();
// n: 1 - 9
if(n < 10){
return ans * sign;
}
// n: 10
int i = 9;
while(i >= 0){
if(num[i] < maxNum[i]){
return ans * sign;
}
else if(num[i] == maxNum[i]){
i--;
continue;
}
else {
return 0;
}
}
return ans * sign;
}
};
相关文章推荐
- LeetCode 7. Reverse Integer 整数反转
- LeetCode 7. Reverse Integer(反转整数)
- leetcode_7. Reverse Integer 反转整数的各个数字
- 【leetcode】Reverse Integer整数反转----Java代码实现
- [leetcode] Reverse Integer 反转一个整数
- [Leetcode] reverse integer 反转整数
- leetcode 7. Reverse Integer(C语言,翻转一个整数,判断是否溢出)19
- leetcode reverse Integer(反转整数)
- [LeetCode-7] Reverse Integer(反转整数)
- [leetcode 7] Reverse Integer(整数反转) c函数实现
- Leetcode Reverse Integer 反转整数
- 7. Reverse Integer(反转整数)
- (LeetCode)Reverse Integer --- 反转整数
- [LeetCode] 7. Reverse Integer 翻转整数
- [置顶] [LeetCode] Reverse Integer 整数反转
- LeetCode 7 :Reverse Integer ---- 反转int整数
- Leetcode:Reverse Integer ,整数反转
- 【LeetCode】- Reverse Integer(将一个整数反转)
- leetCode 7. Reverse Integer (数字反转) 解题思路与方法
- leetcode——Reverse Integer 反转整数数字(AC)