Table of Contents 0:00 Problem Statement 0:35 Solution 4:50 Pseudo Code 7:30 Code - Python 8:26 Code - C++
@mathematics339814 күн бұрын
class Solution: def myAtoi(self, s): i = 0 INT_MAX = 2147483647 INT_MIN = -2147483648 ans = 0 is_negative = False while i < len(s) and s[i] == ' ': i += 1 if s[i] == '-': is_negative = True i += 1 while i < len(s) and (s[i] >= '0' and s[i] INT_MAX/10 or (ans == INT_MAX/10 and digit > 7): return INT_MIN if is_negative else INT_MAX ans = ans*10 + digit i += 1 if is_negative: return -ans return ans
@mathematics339814 күн бұрын
class Solution { public: int myAtoi(char *s) { int ans = 0; bool is_negative = false; int i = 0; while (s[i] != '\0' and s[i] == ' ') i++; if (s[i] == '-') { is_negative = true; i++; } while (s[i] != '\0' and (s[i] >= '0' and s[i] INT_MAX/10 or (ans == INT_MAX/10 and digit > 7)) return is_negative?INT_MIN:INT_MAX; ans = ans*10 + digit; //cout