refactor: using pos variable
This commit is contained in:
parent
15fd24ee3a
commit
5f9464b16e
@ -15,18 +15,16 @@ class Solution {
|
|||||||
b = b.substr(1);
|
b = b.substr(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (aIsNeg && !bIsNeg) return true;
|
bool aIsPos = !aIsNeg, bIsPos = !bIsNeg;
|
||||||
if (!aIsNeg && bIsNeg) return false;
|
|
||||||
|
|
||||||
if (a.length() < b.length()) {
|
if (aIsNeg && bIsPos) return true;
|
||||||
return !aIsNeg;
|
if (aIsPos && bIsNeg) return false;
|
||||||
}
|
|
||||||
if (a.length() > b.length()) {
|
if (a.length() < b.length()) return aIsPos;
|
||||||
return aIsNeg;
|
if (a.length() > b.length()) return aIsNeg;
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < a.length(); i++) {
|
for (int i = 0; i < a.length(); i++) {
|
||||||
if (a[i] < b[i]) return !aIsNeg;
|
if (a[i] < b[i]) return aIsPos;
|
||||||
if (a[i] > b[i]) return aIsNeg;
|
if (a[i] > b[i]) return aIsNeg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,22 +35,14 @@ class Solution {
|
|||||||
bool isNeg = x < 0;
|
bool isNeg = x < 0;
|
||||||
|
|
||||||
string s = to_string(x);
|
string s = to_string(x);
|
||||||
|
if (isNeg) s = s.substr(1);
|
||||||
if (isNeg) {
|
|
||||||
s = s.substr(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::reverse(s.begin(), s.end());
|
std::reverse(s.begin(), s.end());
|
||||||
|
|
||||||
if (isNeg) {
|
if (isNeg) {
|
||||||
s = "-" + s;
|
s = "-" + s;
|
||||||
if (isLessThan(s, to_string(INT32_MIN))) {
|
if (isLessThan(s, to_string(INT32_MIN))) s = "0";
|
||||||
s = "0";
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (isLessThan(to_string(INT32_MAX), s)) {
|
if (isLessThan(to_string(INT32_MAX), s)) s = "0";
|
||||||
s = "0";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return atoi(s.c_str());
|
return atoi(s.c_str());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user