diff --git a/7_reverse_integer/main.cpp b/7_reverse_integer/main.cpp index 8048cdf..207a9ca 100644 --- a/7_reverse_integer/main.cpp +++ b/7_reverse_integer/main.cpp @@ -15,18 +15,16 @@ class Solution { b = b.substr(1); } - if (aIsNeg && !bIsNeg) return true; - if (!aIsNeg && bIsNeg) return false; + bool aIsPos = !aIsNeg, bIsPos = !bIsNeg; - if (a.length() < b.length()) { - return !aIsNeg; - } - if (a.length() > b.length()) { - return aIsNeg; - } + if (aIsNeg && bIsPos) return true; + if (aIsPos && bIsNeg) return false; + + if (a.length() < b.length()) return aIsPos; + 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 aIsPos; if (a[i] > b[i]) return aIsNeg; } @@ -37,24 +35,16 @@ class Solution { bool isNeg = x < 0; string s = to_string(x); - - if (isNeg) { - s = s.substr(1); - } - + 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"; - } + if (isLessThan(s, to_string(INT32_MIN))) s = "0"; } else { - if (isLessThan(to_string(INT32_MAX), s)) { - s = "0"; - } + if (isLessThan(to_string(INT32_MAX), s)) s = "0"; } return atoi(s.c_str()); } -}; \ No newline at end of file +};