feat: 7_reverse_integer
This commit is contained in:
parent
fdc67c8348
commit
15fd24ee3a
60
7_reverse_integer/main.cpp
Normal file
60
7_reverse_integer/main.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool isLessThan(string a, string b) {
|
||||
bool aIsNeg = false, bIsNeg = false;
|
||||
|
||||
if (a[0] == '-') {
|
||||
aIsNeg = true;
|
||||
a = a.substr(1);
|
||||
}
|
||||
if (b[0] == '-') {
|
||||
bIsNeg = true;
|
||||
b = b.substr(1);
|
||||
}
|
||||
|
||||
if (aIsNeg && !bIsNeg) return true;
|
||||
if (!aIsNeg && bIsNeg) return false;
|
||||
|
||||
if (a.length() < b.length()) {
|
||||
return !aIsNeg;
|
||||
}
|
||||
if (a.length() > b.length()) {
|
||||
return aIsNeg;
|
||||
}
|
||||
|
||||
for (int i = 0; i < a.length(); i++) {
|
||||
if (a[i] < b[i]) return !aIsNeg;
|
||||
if (a[i] > b[i]) return aIsNeg;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int reverse(int x) {
|
||||
bool isNeg = x < 0;
|
||||
|
||||
string s = to_string(x);
|
||||
|
||||
if (isNeg) {
|
||||
s = s.substr(1);
|
||||
}
|
||||
|
||||
std::reverse(s.begin(), s.end());
|
||||
|
||||
if (isNeg) {
|
||||
s = "-" + s;
|
||||
if (isLessThan(s, to_string(INT32_MIN))) {
|
||||
s = "0";
|
||||
}
|
||||
} else {
|
||||
if (isLessThan(to_string(INT32_MAX), s)) {
|
||||
s = "0";
|
||||
}
|
||||
}
|
||||
|
||||
return atoi(s.c_str());
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user