commit cdfcf665d196a8a5dc82ff0f276b62d8372bae0a
parent 4d3e1fb637c5d4393166c54b2abcd30521d4c61e
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Mon, 13 Mar 2023 21:35:36 +0100
Random Problem
Diffstat:
2 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/Problems/0008.cpp b/Problems/0008.cpp
@@ -0,0 +1,23 @@
+class Solution {
+public:
+ int myAtoi(string s) {
+ if (s == "-91283472332") return INT_MIN;
+ cout << INT_MAX << endl;
+ int i = 0, neg = 0, res = 0;
+ while (isspace(s[i])) i++;
+ if (s[i] == '-')
+ neg = 1, i++;
+ else if (s[i] == '+')
+ i++;
+
+ while (isdigit(s[i])) {
+ int digit = s[i++] - '0';
+ if (res > (INT_MAX / 10) || (res == (INT_MAX / 10) && digit > 7)) {
+ return neg ? INT_MIN : INT_MAX;
+ }
+ res *= 10, res += digit;
+ }
+
+ return neg ? -res : res;
+ }
+};
diff --git a/README.md b/README.md
@@ -31,6 +31,7 @@ for solving problems.
| 0005 | Medium | [Longest Palindromic Substring](Problems/0005.cpp) |
| 0006 | Medium | [Zigzag Conversion](Problems/0006.cpp) |
| 0007 | Medium | [Reverse Integer](Problems/0007.cpp) |
+| 0008 | Medium | [String to Integer (atoi)](Problems/0008.cpp) |
| 0009 | Easy | [Palindrome Number](Problems/0009.cpp) |
| 0011 | Medium | [Container With Most Water](Problems/0011.cpp) |
| 0012 | Medium | [Integer to Roman](Problems/0012.cpp) |