From 15fd24ee3af428e503c04995927c8c4a0b624528 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Fri, 14 Feb 2025 00:34:34 +0800 Subject: [PATCH] feat: 7_reverse_integer --- 7_reverse_integer/main.cpp | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 7_reverse_integer/main.cpp diff --git a/7_reverse_integer/main.cpp b/7_reverse_integer/main.cpp new file mode 100644 index 0000000..8048cdf --- /dev/null +++ b/7_reverse_integer/main.cpp @@ -0,0 +1,60 @@ +#include +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()); + } +}; \ No newline at end of file