aboutsummaryrefslogtreecommitdiff
path: root/store/works/solutions/leetcode/cpp/7.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'store/works/solutions/leetcode/cpp/7.cpp')
-rw-r--r--store/works/solutions/leetcode/cpp/7.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/store/works/solutions/leetcode/cpp/7.cpp b/store/works/solutions/leetcode/cpp/7.cpp
new file mode 100644
index 0000000..a1a05c1
--- /dev/null
+++ b/store/works/solutions/leetcode/cpp/7.cpp
@@ -0,0 +1,18 @@
+class Solution
+{
+public:
+ int reverse(int x)
+ {
+ int result = 0;
+ while (x != 0)
+ {
+ int digit = x % 10;
+ x /= 10;
+ if (__builtin_smul_overflow(result, 10, &result))
+ return 0;
+ if (__builtin_sadd_overflow(result, digit, &result))
+ return 0;
+ }
+ return result;
+ }
+};