post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/reverse-integer/discuss/1626765/Pop-%2B-push-digits-before-and-after-overflow.
class Solution: def reverse(self, x: int) -> int: rev = 0 mul = 1 if x < 0: mul = -1 x = abs(x) # pop while x != 0: pop = x % 10 x = int(x/10) rev = rev * 10 + pop # check if reversed value is in given range of x if -(2**31) <= mul * rev and mul * rev <= (2**31)-1: return mul * rev # else return 0
reverse-integer
Pop + push digits before and after overflow.
AmrinderKaur1
1
118
reverse integer
7
0.273
Medium
300
https://leetcode.com/problems/reverse-integer/discuss/1626765/Pop-%2B-push-digits-before-and-after-overflow.
class Solution: def reverse(self, x: int) -> int: rev = 0 mul = 1 if x < 0: mul = -1 x = abs(x) # pop while x != 0: pop = x % 10 x = int(x/10) rev = rev * 10 + pop # check overflow if -(2**31) > mul * rev or mul * rev > (2**31)-1: return 0 return mul * rev
reverse-integer
Pop + push digits before and after overflow.
AmrinderKaur1
1
118
reverse integer
7
0.273
Medium
301
https://leetcode.com/problems/reverse-integer/discuss/1385160/much-simpler-and-easy-to-understand
class Solution: def reverse(self, x: int) -> int: b = 0 y = abs(x) while int(y): a = int(y%10) y = y/10 b = b*10 + a if x<0: return -b if int(b)<=2**31 else 0 else: return b if int(b)<=2**31 else 0
reverse-integer
much simpler and easy to understand
jamil117
1
461
reverse integer
7
0.273
Medium
302
https://leetcode.com/problems/reverse-integer/discuss/1319071/Python-28-ms-89-faster
class Solution: def reverse(self, x: int) -> int: #convert input to string an reverse ostring = str(abs(x))[::-1] #check if value is > 32bit int range if int(ostring) > (2)**31: ostring = '0' # next check if the original input was negative elif x < 0: ostring = '-' + ostring return(int(ostring))
reverse-integer
Python 28 ms, 89% faster
ovidaure
1
458
reverse integer
7
0.273
Medium
303
https://leetcode.com/problems/reverse-integer/discuss/1279172/Easy-Python-Code-Without-Loop-Beats-99.83
class Solution(object): def reverse(self, x): a=int(str(x).strip("-")[::-1]) if x<0: a=-a if -2147483648<=a<=2147483647: return a else: return 0
reverse-integer
Easy Python Code Without Loop Beats 99.83%
Horatio32112
1
178
reverse integer
7
0.273
Medium
304
https://leetcode.com/problems/reverse-integer/discuss/1249803/Python-Solution
class Solution(object): def reverse(self, x): INTMAX=2**31-1 INTMIN=-1*(2**31) reverse=0 while x!=0: d=int(x/10) pop=x-(d*10) x=d if reverse>INTMAX/10 or (reverse==INTMAX/10 and pop>7): return 0 if reverse<int(INTMIN/10) or (reverse==int(INTMIN/10) and pop<-8): return 0 reverse=reverse*10+pop return reverse
reverse-integer
Python Solution
ParathaCoder
1
346
reverse integer
7
0.273
Medium
305
https://leetcode.com/problems/reverse-integer/discuss/1249803/Python-Solution
class Solution(object): def reverse(self, x): result = 0 sign= 1 isNegative=x<0 if isNegative: sign= -1 x = -x while x: result = result * 10 + x % 10 x /= 10 return 0 if result > pow(2,31) else result * sign
reverse-integer
Python Solution
ParathaCoder
1
346
reverse integer
7
0.273
Medium
306
https://leetcode.com/problems/reverse-integer/discuss/1062774/Python-Optimal-Solution
class Solution: def reverse(self, x: int) -> int: reverse = 0 multiplicator = -1 if x < 0 else 1 x = abs(x) while x != 0: last_digit = x % 10 reverse = int(reverse * 10 + last_digit) x = int(x/10) reverse = reverse * multiplicator if (-2**31 <= reverse < 2**31) == False: return 0 return reverse
reverse-integer
Python Optimal Solution
eduardohattorif
1
297
reverse integer
7
0.273
Medium
307
https://leetcode.com/problems/reverse-integer/discuss/999369/Easiest-Solution-in-10-lines-with-comments-included
class Solution: def reverse(self, x: int) -> int: # store the sign sign = '-' if x<0 else '+' # if the sign is negative, slice the string, reverse, concatenate the '-' and convert back to int if sign == '-': i = str(x)[1:] reverse_i = i[::-1] reverse_i = int('-'+reverse_i) # convert to string, reverse and convert back to int else: i = str(x) reverse_i = i[::-1] reverse_i = int(reverse_i) # overflow checking if -2**31 <= reverse_i <= 2**31 - 1: return reverse_i else: return 0
reverse-integer
Easiest Solution in 10 lines with comments included
nitin_bommi
1
326
reverse integer
7
0.273
Medium
308
https://leetcode.com/problems/reverse-integer/discuss/827686/Python3-solution-satisfies-32-bit-signed-integer-condition
class Solution: def reverse(self, x: int) -> int: result = 0 limit = 2147483647 if x>=0 else -2147483648 sign = 1 if x>=0 else -1 for i, s in enumerate(str(abs(x))): if x > 0: if result+int(s)*pow(10, i) > limit: return 0 elif x < 0: if result-int(s)*pow(10, i) < limit: return 0 # Addition is guaranteed to be in the range: [-2147483648, 2147483647] result = result + sign*int(s)*pow(10, i) return result
reverse-integer
Python3 solution satisfies 32-bit signed integer condition
ecampana
1
768
reverse integer
7
0.273
Medium
309
https://leetcode.com/problems/reverse-integer/discuss/654121/python3-solution-with-comments-with-for-humans
class Solution: def reverse(self, x: int) -> int: # store and take out negation if present sign = -1 if x<0 else 1 x = abs(x) rev = 0 # reverse digit by dividing by 10 while x: x, mod = divmod(x, 10) rev = rev*10 + mod # check if result is outside the contraint if rev >= 2**31-1 or rev <= -2**31: return 0 # otherwise return return rev*sign
reverse-integer
python3 solution with comments with for humans
nandita727
1
144
reverse integer
7
0.273
Medium
310
https://leetcode.com/problems/reverse-integer/discuss/357717/Python-With-Overflow-Check-During-Computation
class Solution: def reverse(self, x: int) -> int: if x < 0: # Sign ommited as we do all ops unsigned INT_LIMIT_QUOTIENT, INT_LIMIT_REMAINDER= divmod(2**31, 10) sign = -1 else: #positive INT_LIMIT_QUOTIENT, INT_LIMIT_REMAINDER= divmod(2**31 - 1, 10) sign = 1 x = x * sign reverse = 0 while x: x, digit = divmod(x, 10) if (reverse > INT_LIMIT_QUOTIENT or (reverse == INT_LIMIT_QUOTIENT and digit > INT_LIMIT_REMAINDER)): return 0 reverse = reverse * 10 + digit return sign * reverse
reverse-integer
Python With Overflow Check During Computation
amchoukir
1
448
reverse integer
7
0.273
Medium
311
https://leetcode.com/problems/reverse-integer/discuss/2846818/python-solution
class Solution: def reverse(self, x: int) -> int: st = str(x) st = st[-1::-1] if len(st)>1: st = st.strip('0') if st[-1] =='-': st = st.split('-')[0] ans = int(st)*-1 else : ans = int(st) else : ans = int(st) if ans <= (pow(2,31)-1) and ans >= (pow(2,31)*-1): return ans else : return 0
reverse-integer
python solution
HARMEETSINGH0
0
1
reverse integer
7
0.273
Medium
312
https://leetcode.com/problems/reverse-integer/discuss/2846801/Python-3-String-conversion-method
class Solution: def reverse(self, x: int) -> int: s ="" sign =1 for i in str(x): if i.isdigit(): s=i+s else: sign =-1 return sign * int(s) if sign * int(s) < 2**31 -1 and sign * int(s) > -2**31 else 0
reverse-integer
Python 3 - String conversion method
vishnusuresh1995
0
2
reverse integer
7
0.273
Medium
313
https://leetcode.com/problems/reverse-integer/discuss/2846364/Python-easy-solution-(RT-95.81-Memory-97.44)
class Solution: def reverse(self, x: int) -> int: def help(x): if x[0]=='-': return (x[0]+str(int(x[(len(x)-1):0:-1]))) else: return (str(int(x[::-1]))) x=str(x) y=int(help(x)) if y<=(pow(2,31)-1) and y>=-(pow(2,31)): return y else: return 0
reverse-integer
Python easy solution (RT-95.81% , Memory - 97.44%)
itachi112
0
2
reverse integer
7
0.273
Medium
314
https://leetcode.com/problems/reverse-integer/discuss/2846248/Python-3-simple-Solution
class Solution: def reverse(self, x: int) -> int: negative = False if x < 0: negative = True x = str(x)[::-1] x = x[:len(x)-1] x = int(x) else: x = int(str(x)[::-1]) if negative and -x >= -(2**31): return -x elif x <= (2**31)-1: return x return 0
reverse-integer
Python 3 simple Solution
user0106Ez
0
2
reverse integer
7
0.273
Medium
315
https://leetcode.com/problems/reverse-integer/discuss/2844125/i-tried-my-level-best
class Solution: def reverse(self, x: int) -> int: x=str(x) o=2**31-1 n=-2**31 if x[0]=='-': b=x[1:] b=str(int(b[::-1])) b=int('-'+b) if (o> b >n): return b else: return 0 else: m=x[::-1] if m[0]=='0' : m=int(m) if (o> m >n): return m else: return 0 else: m=int(m) if (o> m >n): return m else: return 0
reverse-integer
i tried my level best
venkatesh402
0
3
reverse integer
7
0.273
Medium
316
https://leetcode.com/problems/reverse-integer/discuss/2841734/Python-oror-Easily-Understandable
class Solution: def reverse(self, x: int) -> int: MINVALUE = -(pow(2,31)) MAXVALUE = (pow(2,31)) - 1 numstr = str(x) numstr = numstr.rstrip('0') if(len(numstr) == 0): return 0 if(numstr[0].isdigit()): reverse = numstr[::-1] else: rev = numstr[:0:-1] reverse = numstr[0]+rev if(int(reverse) > MINVALUE and int(reverse) < MAXVALUE): return reverse else: return 0
reverse-integer
Python || Easily Understandable
user6374IX
0
5
reverse integer
7
0.273
Medium
317
https://leetcode.com/problems/reverse-integer/discuss/2838284/Approach-beats-83.3-of-Other-solutions-in-Runtime
class Solution: def reverse(self, x: int) -> int: if x==0: return 0 elif x>0: k=str(x) a=k[::-1] if a[0]=='0' and int(a[1:]) in range(-2**31,(2**31)-1): return int(a[1:]) elif int(a) in range(-2**31,(2**31)-1): return int(a) else: return 0 else: b=str(x) k=b[1:] a=k[::-1] if a[0]=='0' and int(a[1:]) in range(-2**31,(2**31)-1): return int("-"+a[1:]) elif int(a) in range(-2**31,(2**31)-1): return int("-"+a) else: return 0
reverse-integer
Approach beats 83.3% of Other solutions in Runtime
G_KUSHWANTH
0
1
reverse integer
7
0.273
Medium
318
https://leetcode.com/problems/reverse-integer/discuss/2837799/Python-Simple
class Solution: def reverse(self, x: int) -> int: if x < 0: a = int('-' + str(x)[1::][::-1]) if a < (2**31) * -1: return 0 else: return a else: a = int(str(x)[::-1]) if a > (2**31) - 1: return 0 else: return a
reverse-integer
Python Simple
n555s77
0
5
reverse integer
7
0.273
Medium
319
https://leetcode.com/problems/reverse-integer/discuss/2831140/Simple-Solution-in-python
class Solution: def reverse(self, x: int) -> int: reverse_num = x if (reverse_num > 0): reverse_num = int((str(x)[::-1]).replace('-',"")) else: reverse_num = int('-' + ((str(x)[::-1]).replace('-',""))) return reverse_num if (reverse_num > -2**31 and reverse_num < 2**31-1) else 0
reverse-integer
Simple Solution in python
BattagliaJ
0
6
reverse integer
7
0.273
Medium
320
https://leetcode.com/problems/reverse-integer/discuss/2831120/Python3-greaterEasy-and-understandable
class Solution: def reverse(self, x: int) -> int: if x >= 0: xstr = str(x) y = xstr[::-1] x = int(y) if -2**31 <= x < 2**31: return x else: return 0 else: xstr = str(-x) y = xstr[::-1] x = int(y) if -2**31 <= x < 2**31: return -x else: return 0
reverse-integer
Python3-->Easy and understandable
Silvanus20
0
2
reverse integer
7
0.273
Medium
321
https://leetcode.com/problems/reverse-integer/discuss/2830738/Cool-and-fast-python-solution-(long)
class Solution: def reverse(self, x: int) -> int: if (x) == 0: return x count = 0 digit_position = 1 answer = [] negative = False if abs(x) != x: negative = True x = abs(x) #Code basically does this: #Number = 531 # Removes first digit position(1) 1 times and adds "1" to a list # Removes second digit position(3) 3 times and adds "3" to a list # Removes thidre digit position(5) 5 times and adds "5" to a list # Which means answer = ['1', '3', '5'] # list items are made int and joined # If the given number was negative the answer is multiplied by -1 while x != 0: while x % (10*digit_position) != 0: x -= digit_position count += 1*digit_position answer.append(str(int((count/digit_position)))) count = 0 digit_position *= 10 answer = (''.join(answer)) if negative: answer = int(answer)* -1 if abs(int(answer)) >= 2147483647: return 0 return int(answer)
reverse-integer
Cool and fast python solution (long)
OSTERIE
0
1
reverse integer
7
0.273
Medium
322
https://leetcode.com/problems/reverse-integer/discuss/2825332/Simple-Solution-with-Proper-Limit-Handling-The-Best
class Solution: def reverse(self, x: int) -> int: MININT = -2**31 MAXINT = 2**31 - 1 MAXINTD10 = MAXINT // 10 if x == MININT: return 0 is_negative = x < 0 if is_negative: x = -x y = 0 while x != 0: x, r = divmod(x, 10) if y > MAXINTD10: return 0 y *= 10 y += r if is_negative: y = -y return y
reverse-integer
Simple Solution with Proper Limit Handling, The Best
Triquetra
0
3
reverse integer
7
0.273
Medium
323
https://leetcode.com/problems/reverse-integer/discuss/2819863/Python-Code-40ms-with-explanation.-Simple-to-understand.
class Solution: def reverse(self, x: int) -> int: for n in range(-2**31,(2**31)-1): if x == 0: return 0 elif x > 0: n = int(str(x)[::-1]) #slicing here it will be reversing the number which mean last no will be first and viceversa. else: n = int('-'+str(x)[:0:-1]) # small change here from above is that it adds - at 0th index before starting the number. if abs(n) > 2**31: #abs is absolute which means if a = -5 i write abs(a) then o/p will be 5 return 0 else: return n
reverse-integer
Python Code [40ms] with explanation. Simple to understand.
Guru_Srinivasula_Reddy
0
2
reverse integer
7
0.273
Medium
324
https://leetcode.com/problems/reverse-integer/discuss/2818478/Simple-and-Faster-than-98.
class Solution: def reverse(self, x: int) -> int: num = 0 flag = False if x < 0: flag = True x *= -1 while x: num = (num*10) + x%10 x //= 10 if flag: num *= -1 if num > 2**31 or num < -2**31 - 1: return 0 return num
reverse-integer
Simple and Faster than 98%.
Sudhanshu344
0
2
reverse integer
7
0.273
Medium
325
https://leetcode.com/problems/reverse-integer/discuss/2817185/reverse-integer
class Solution: def reverse(self, x: int) -> int: try: o=int(str(x)[::-1]) except ValueError: o=-1*int(str(x)[::-1].replace("-","")) if 2**31-1<o or o<(-2)**31: return 0 return o
reverse-integer
reverse integer
emresvd
0
2
reverse integer
7
0.273
Medium
326
https://leetcode.com/problems/reverse-integer/discuss/2816291/Python-Solution-without-storing-64-integer
class Solution: def reverse(self, x: int) -> int: re_string = str(x) if re_string[0]=='-': sign = -1 re_string = re_string[1:] max_possible = str( (2**31)) else: sign = 1 re_string = re_string max_possible = str( (2**31)-1) if len(re_string)<len(max_possible): return sign*int(re_string[::-1]) for i in range(len(re_string)): if int(re_string[-i-1]) < int(max_possible[i]): return sign*int(re_string[::-1]) if int(re_string[-i-1]) > int(max_possible[i]): return 0 return sign*int(re_string[::-1])
reverse-integer
Python Solution without storing 64 integer
mgaber6
0
4
reverse integer
7
0.273
Medium
327
https://leetcode.com/problems/reverse-integer/discuss/2810940/Python-O(n)-solution-using-strings
class Solution: def reverse(self, x: int) -> int: s=str(x) if s[0]!='-': s1=s[::-1] else: s1=s[1::] s1=s[0]+s1[::-1] y=int(s1) low=-(2**31) high=(2**31)-1 if y < low or y>high: y=0 return y
reverse-integer
Python O(n) solution using strings
SnehaGanesh
0
1
reverse integer
7
0.273
Medium
328
https://leetcode.com/problems/reverse-integer/discuss/2807688/Python-Easy
class Solution: def reverse(self, x: int) -> int: if x == 0: return 0 original_num = str(x) for i in str(x)[::-1]: if i != "0": break else: original_num = original_num[0:-1] ans = int(original_num[::-1]) if x > 0 else int(original_num[::-1][:len(original_num)-1]) * -1 return ans if (-2 ** 31 <= ans <= (2 ** 31) -1) else 0
reverse-integer
Python - Easy
D_zh10
0
10
reverse integer
7
0.273
Medium
329
https://leetcode.com/problems/reverse-integer/discuss/2806463/Python-less-not-converting-to-stringgreater-using-math-Easy-Vizzy
class Solution: def reverse(self, x: int) -> int: reverse = 0 num, x = x, abs(x) while x: last = x % 10 x //= 10 reverse = reverse * 10 + last if not (reverse <= 2 ** 31 and reverse >= -2 ** 31): return 0 return reverse if num > 0 else -reverse
reverse-integer
Python < not converting to string> using math -- Easy Vizzy
nehavari
0
6
reverse integer
7
0.273
Medium
330
https://leetcode.com/problems/string-to-integer-atoi/discuss/1510014/Python-Simple-Solution-without-Strip-beats-95
class Solution: def myAtoi(self, s: str) -> int: if not s: return 0 sign = 1 integer = 0 i = 0 while i < len(s) and s[i] == ' ': i+=1 #skipping leading white space if i < len(s) and (s[i] == '-' or s[i] == '+'): if s[i] == '-': sign = -1 i+=1 while(i < len(s) and s[i].isdigit()): integer = integer * 10 + int(s[i]) i+=1 integer = sign*integer ans = self.limit(integer) return ans def limit(self, num): if num > pow(2, 31) -1: return pow(2, 31) -1 if num < -1*pow(2, 31): return -1*pow(2, 31) return num
string-to-integer-atoi
Python Simple Solution without Strip beats 95%
emerald19
7
790
string to integer (atoi)
8
0.166
Medium
331
https://leetcode.com/problems/string-to-integer-atoi/discuss/2716487/Python-Very-Intuitive-with-Comments
class Solution: def myAtoi(self, s: str) -> int: if not s: return 0 # remove leading and trailing whitespace s = s.strip() # save sign if one exists pos = True if s and s[0] == '-': pos = False s = s[1:] elif s and s[0] == '+': s = s[1:] # ignore leading zeros i = 0 while i < len(s) and s[i] == '0': i += 1 # apply relevant digits res = None while i < len(s) and s[i] in '0123456789': if res is None: res = int(s[i]) else: res = (res * 10) + int(s[i]) i += 1 res = 0 if res is None else res # apply sign res = res if pos else -res # clip result res = max(res, -2**31) res = min(res, (2**31)-1) return res
string-to-integer-atoi
Python Very Intuitive with Comments
jonathanbrophy47
4
1,000
string to integer (atoi)
8
0.166
Medium
332
https://leetcode.com/problems/string-to-integer-atoi/discuss/1688575/Python3-Not-an-interesting-problem-but-here-is-my-O(n)-Time-or-O(1)-Space-solution
class Solution: def myAtoi(self, s: str) -> int: i = res = 0 op = 1 while i < len(s) and s[i] == ' ': i += 1 if i < len(s) and s[i] in '+-': op = 1 if s[i] == '+' else -1 i += 1 MAX_RES = (1 << 31) - 1 if op == 1 else 1 << 31 while i < len(s) and s[i].isdigit() and res <= MAX_RES: res = res * 10 + int(s[i]) i += 1 return min(res, MAX_RES) * op
string-to-integer-atoi
[Python3] Not an interesting problem, but here is my O(n) Time | O(1) Space solution
PatrickOweijane
4
322
string to integer (atoi)
8
0.166
Medium
333
https://leetcode.com/problems/string-to-integer-atoi/discuss/1504610/Python-3-oror-30ms-oror-Simple-Logic-oror-Fast-using-edge-cases
class Solution: def myAtoi(self, s: str) -> int: sign=1 s=s.strip() if s=="": return 0 char=s[0] if char=="-" or char=="+": s=s[1:] if char=="-": sign=-1 ans=0 for ch in s: if '0'<=ch<='9': ans=ans*10+(int(ch)) else: break ans = ans*sign if ans<-2147483648: return -2147483648 elif ans>2147483647: return 2147483647 return ans
string-to-integer-atoi
Python 3 || 30ms || Simple Logic || Fast using edge cases
ana_2kacer
3
371
string to integer (atoi)
8
0.166
Medium
334
https://leetcode.com/problems/string-to-integer-atoi/discuss/1720228/Python-90-Faster
class Solution: def myAtoi(self, s: str) -> int: acceptable = ['1','2','3','4','5','6','7','8','9','0'] output = '' #iterate throgh string, break when character not found for idx,i in enumerate(s.lstrip()): if idx == 0 and (i == '-' or i == '+'): output +=i elif i in acceptable: output+=i else: #stop when a non-number is fond break #check for a number in the right range if len(output) == 0 or output == '+' or output == '-': return(0) elif int(output) < -(2**31): return(-2147483648) elif int(output) > (2**31)-1: return(2147483647) else: return(int(output))
string-to-integer-atoi
Python 90% Faster
ovidaure
2
226
string to integer (atoi)
8
0.166
Medium
335
https://leetcode.com/problems/string-to-integer-atoi/discuss/1009518/python3-solution-or-faster-than-98
class Solution: def myAtoi(self, s: str) -> int: # pointer denoting current index of traversal i = 0 # ignoring whitespaces while i < len(s) and s[i] == ' ': i += 1 # if string consists of only whitespaces if i == len(s): return 0 # value to be returned num = 0 # 1 if num is nonnegative, -1 if num is negative sign = 1 # checking for a sign (+/-) character if s[i] == '-': sign = -1 i += 1 elif s[i] == '+': i += 1 # handling numeric characters until we see a non-numeric character while i < len(s) and s[i].isdigit(): num *= 10 num += ord(s[i]) - ord('0') i += 1 # if we need to make num negative num *= sign # handling out-of-range values if num >= 2 ** 31: num = 2 ** 31 - 1 elif num < -(2 ** 31): num = -(2 ** 31) return num
string-to-integer-atoi
python3 solution | faster than 98%
catherinehuang82
2
253
string to integer (atoi)
8
0.166
Medium
336
https://leetcode.com/problems/string-to-integer-atoi/discuss/2319833/Simple-Python-solution-using-try-except-and-array-slicing
class Solution: def myAtoi(self, s: str) -> int: char_list = list(s.lstrip()) # If the string was all whitespace or empty then we have no work to do if len(char_list) == 0: return 0 # Store the sign if it was present sign = char_list[0] if char_list[0] in ['-','+'] else '' # Skip over the sign if its present lbound = 1 if sign != '' else 0 # Assume that we wil have integers to the end of the string rbound = len(char_list) # Set limits min_output = -2 ** 31 max_output = (2 ** 31) -1 for i in range(lbound, len(char_list)): # Assume we can cast the value to an int try: int(char_list[i]) # If we get an error it must not have been an int # Adjust the right boundary to be used with string slicing # Remember that slice is exclsuve of the high value except ValueError: rbound = i break # If we had no int values return zero # Otherwise convert the array to an int # If we have a negative sign then convert to negative # Otherwise just leave the value result = 0 if lbound == rbound else int(''.join(char_list[lbound:rbound])) * -1 if sign == '-' else int(''.join(char_list[lbound:rbound])) # Check against limits if result < min_output: return min_output elif result > max_output: return max_output else: return result
string-to-integer-atoi
Simple Python solution using try except and array slicing
bradleyjwilliams567
1
59
string to integer (atoi)
8
0.166
Medium
337
https://leetcode.com/problems/string-to-integer-atoi/discuss/2076881/Python3-Regular-Expression-Solution
class Solution: def myAtoi(self, s: str) -> int: res = ( int(match.group(0)) if (match := re.match(r"\s*[+-]?\d+", s)) else 0 ) return min(max(res, -2**31), 2**31 - 1)
string-to-integer-atoi
[Python3] Regular Expression Solution
Lindelt
1
122
string to integer (atoi)
8
0.166
Medium
338
https://leetcode.com/problems/string-to-integer-atoi/discuss/1865039/5-Lines-Python-Solution-(Regex)-oror-90-Faster-oror-Memory-less-than-85
class Solution: def myAtoi(self, s: str) -> int: s=s.strip() ; s=re.findall('(^[\+\-0]*\d+)\D*', s) try: ans=int(''.join(s)) return -2**31 if ans<-2**31 else 2**31-1 if ans>2**31-1 else ans except: return 0
string-to-integer-atoi
5-Lines Python Solution (Regex) || 90% Faster || Memory less than 85%
Taha-C
1
189
string to integer (atoi)
8
0.166
Medium
339
https://leetcode.com/problems/string-to-integer-atoi/discuss/1865039/5-Lines-Python-Solution-(Regex)-oror-90-Faster-oror-Memory-less-than-85
class Solution: def myAtoi(self, s: str) -> int: s=s.strip() if len(s)==0 : return 0 sgn=-1 if s[0]=='-' else 1 if s[0] in '-+': s=s.replace(s[0],'',1) ans=0 ; i=0 while i<len(s) and s[i].isdigit(): ans=ans*10+int(s[i]) ; i+=1 return -2**31 if sgn*ans<-2**31 else 2**31-1 if sgn*ans>2**31-1 else sgn*ans
string-to-integer-atoi
5-Lines Python Solution (Regex) || 90% Faster || Memory less than 85%
Taha-C
1
189
string to integer (atoi)
8
0.166
Medium
340
https://leetcode.com/problems/string-to-integer-atoi/discuss/1814584/Python-3-Clean-Simple-String-Parsing-O(N)-or-Beats-87
class Solution: def myAtoi(self, s: str) -> int: digits = "0123456789+-" if s == "": return 0 n = len(s) for i in range(n): if s[i] != " ": s = s[i:] break num = "" for ch in s: if ch not in digits: break num += ch if num == "": return 0 num = int(num) return 2**31-1 if num >= 2**31-1 else (-2)**31 if num <= (-2)**31 else num
string-to-integer-atoi
[Python 3] Clean Simple String Parsing O(N) | Beats 87%
hari19041
1
155
string to integer (atoi)
8
0.166
Medium
341
https://leetcode.com/problems/string-to-integer-atoi/discuss/1688718/Python3-Simple-and-easy-solution
class Solution: def myAtoi(self, s: str) -> int: s = s.strip() neg = 0 numbers = "" for i, c in enumerate(s): if i == 0: if c == "+": continue if c == "-": neg = 1 continue if c.isdigit(): numbers += c elif not c.isdigit(): break number = 0 if not numbers else int(numbers) if neg == 1: return max(-2**31, -number) return min(2**31 - 1, number)
string-to-integer-atoi
[Python3] Simple and easy solution
tushar_prajapati
1
87
string to integer (atoi)
8
0.166
Medium
342
https://leetcode.com/problems/string-to-integer-atoi/discuss/1601517/Python-straightforward-solution
class Solution: def myAtoi(self, s: str) -> int: s = s.strip() # delete blank spaces sign = 1 # check wherther the initial conditiones starts as a number if s == '': return(0) elif s[0] in '+-': if len(s) < 2: return(0) elif not s[1].isdigit(): return(0) elif s[0] == '-': sign = -1 start = 1 elif s[0].isdigit(): start = 0 else: return(0) i = start # find out the number of digits while i < len(s) and s[i].isdigit(): i += 1 num = int(s[start:i]) * sign num = max(-2**31, min(2**31 - 1, num)) return(num)
string-to-integer-atoi
Python straightforward solution
user5983s
1
282
string to integer (atoi)
8
0.166
Medium
343
https://leetcode.com/problems/string-to-integer-atoi/discuss/1419853/Python-or-28ms-(greater93.93)-14.5-MB-or-Overflow-secure-WO-regex
class Solution: def myAtoi(self, s: str) -> int: number = 0 found_valid_chars = False negative = None i = 0 while i < len(s): c = s[i] if '0' <= c <= '9': found_valid_chars = True if not negative: if number <= (2**31-1-int(c))/10: number = number*10 + int(c) else: return 2**31-1 else: if number >= (-2**31+int(c))/10: number = number *10 - int(c) else: return -2**31 else: if found_valid_chars: # Didn't recognized a number and already removed leading ' ' so break it break elif c == '+': negative = False found_valid_chars = True elif c == '-': negative = True found_valid_chars = True elif c == ' ': pass else: break i+=1 return number
string-to-integer-atoi
Python | 28ms (>93.93%), 14.5 MB | Overflow secure WO regex
zenodallavalle
1
164
string to integer (atoi)
8
0.166
Medium
344
https://leetcode.com/problems/string-to-integer-atoi/discuss/1300257/Python-soln-takes-into-account-edge-cases-of-spaces
class Solution: def myAtoi(self, s: str) -> int: if(len(s)==0): return(0) else: l=list(s.strip()) if(len(l)==0): return(0) n=1 if(l[0]=='-'): n=-1 if(l[0]=='-' or l[0]=='+'): del l[0] ans=0 i=0 while(i<len(l) and l[i].isdigit()): ans=ans*10+(ord(l[i])-ord('0')) i+=1 ans*=n sol=min(ans,2**31-1) return(max(-2**31,sol))
string-to-integer-atoi
Python soln, takes into account edge cases of spaces
quikslvr21
1
207
string to integer (atoi)
8
0.166
Medium
345
https://leetcode.com/problems/string-to-integer-atoi/discuss/651960/Python3-two-lines-using-regex
class Solution: def myAtoi(self, str: str) -> int: str = "".join(re.findall('^[\+|\-]?\d+', str.lstrip())) return 0 if not str else min(2**31-1, max(-2**31, int(str)))
string-to-integer-atoi
[Python3] two lines using regex
ye15
1
165
string to integer (atoi)
8
0.166
Medium
346
https://leetcode.com/problems/string-to-integer-atoi/discuss/435498/Python3-one-pass-with-explanation-(93.59)
class Solution: def myAtoi(self, s: str) -> int: ii = -1 for i in range(len(s)): if ii == -1: if s[i] in "+-" or s[i].isdigit(): ii = i elif not s[i].isspace(): return 0 elif not s[i].isdigit(): break else: i = len(s) ans = 0 if 0 <= ii and (ii+1 < i or s[ii].isdigit()): ans = int(s[ii:i]) return max(-(1<<31), min((1<<31)-1, ans))
string-to-integer-atoi
Python3 one-pass with explanation (93.59%)
ye15
1
203
string to integer (atoi)
8
0.166
Medium
347
https://leetcode.com/problems/string-to-integer-atoi/discuss/435498/Python3-one-pass-with-explanation-(93.59)
class Solution: def myAtoi(self, str: str) -> int: str = "".join(re.findall('^[\+|\-]?\d+', str.lstrip())) return 0 if not str else min(2**31-1, max(-2**31, int(str)))
string-to-integer-atoi
Python3 one-pass with explanation (93.59%)
ye15
1
203
string to integer (atoi)
8
0.166
Medium
348
https://leetcode.com/problems/string-to-integer-atoi/discuss/2846351/python-%2B-comments-easy-to-understand
class Solution: def myAtoi(self, s: str) -> int: #delete the space from both sides s = s.strip() if not s: return 0 tmp = list(s) nums = ['0','1','2','3','4','5','6','7','8','9'] min = -2**31 max = 2**31-1 #assume that the sign is '+',so use 1 to express '+' #the first charcter maybe a sign,so traversal should start from second item, so i = 1. sign,res,i = 1,0,1 #use -1 express '-' if tmp[0] == '-': sign = -1 #If the first charcter is not a sign, so traversal start from the first item. i=0 elif tmp[0] != '+': i = 0 for j in tmp[i:]: if j not in nums: break #for example, 413 = 41*10+3. 4135 = 413*10+5..... res = res*10+int(j) #don't forget sign res = res*sign if res>max: return max if res<min: return min return res
string-to-integer-atoi
python + comments, easy to understand
xiaolaotou
0
3
string to integer (atoi)
8
0.166
Medium
349
https://leetcode.com/problems/string-to-integer-atoi/discuss/2842998/python-solution-with-clear-explanation-with-comments-time-O(n)-and-space-O(1)
class Solution: def myAtoi(self, s: str) -> int: i=0 j=len(s) if j==0: return 0 neg=False num_index=-1 #checking if the first place is not a white space and not symboles or digits if s[i]!=' ' and s[i] not in [' ' , '+' , '-' , '.'] and not 48<=ord(s[i])<=57: return 0 # print(i) int_max=2**31-1 int_min=-1*(int_max+1) #going over the spaces while(i<j and s[i]==' '): i+=1 #checking the sign if i<j and s[i]=='-': neg=True i+=1 elif i<j and s[i]=='+': i+=1 #getting the number if i<j and 48<=ord(s[i])<=57: num_index=i k=i while(k<j and 48<=ord(s[k])<=57): k+=1 if k==j: k+=1 #assigning sign based on neg value if neg: #checking if out of range if yes returning min_int else value return int_min if -1*int(s[num_index:k])<=int_min else -1*int(s[num_index:k]) else: return int_max if 1*int(s[num_index:k])>=int_max else 1*int(s[num_index:k]) return 0
string-to-integer-atoi
python solution with clear explanation with comments time O(n) and space O(1)
sintin1310
0
2
string to integer (atoi)
8
0.166
Medium
350
https://leetcode.com/problems/string-to-integer-atoi/discuss/2833221/Easy-Python-3-or-95.37-Runtime-993-Memory-or-With-Explanation
class Solution: def myAtoi(self, s: str) -> int: length = len(s) if length == 0: return 0 res = "" sign = 1 i = 0 # skip all of the whitespaces while s[i] == ' ': i += 1 if i >= length: return 0 # get the sign if s[i] == '-': sign = -1 i += 1 elif s[i] == '+': sign = 1 i += 1 if i >= length: return 0 # if the character after the sign is not a digit, return 0 if s[i] not in "0123456789": return 0 # otherwise it is a digit and the number can be read else: while s[i] in "0123456789": res += s[i] i += 1 if i >= length: return self.clampResult(int(res) * sign) return self.clampResult(int(res) * sign) def clampResult(self, res) -> int: # clamp the result between -2^31 and 2^31-1 return max(min(2147483647, res), -2147483648)
string-to-integer-atoi
Easy Python 3 | 95.37% Runtime, 99,3% Memory | With Explanation
Leyonad
0
4
string to integer (atoi)
8
0.166
Medium
351
https://leetcode.com/problems/string-to-integer-atoi/discuss/2818846/python-solution(its-correct-but-some-test-cases-doesn't-have-proper-output)
class Solution: def myAtoi(self, s: str) -> int: res=0 c=1 for i in range(len(s)): if(s[i]=="-"): c=c*(-1) elif(s[i]=="0"): res=res*10+0 elif(s[i]=="1"): res=res*10+1 elif(s[i]=="2"): res=res*10+2 elif(s[i]=="3"): res=res*10+3 elif(s[i]=="4"): res=res*10+4 elif(s[i]=="5"): res=res*10+5 elif(s[i]=="6"): res=res*10+6 elif(s[i]=="7"): res=res*10+7 elif(s[i]=="8"): res=res*10+8 elif(s[i]=="9"): res=res*10+9 res=res*c return res
string-to-integer-atoi
python solution(its correct but some test cases doesn't have proper output)
sreyanshbaranwal
0
1
string to integer (atoi)
8
0.166
Medium
352
https://leetcode.com/problems/string-to-integer-atoi/discuss/2803308/Python-simple-solution-or-Beats-92-in-time-79-in-space
class Solution: def myAtoi(self, s: str) -> int: i, sign, out = 0, 1, "" # going pass the leading spaces while i < len(s): if s[i] == " ": i += 1 else: break # checking for symbols if i < len(s): if s[i] == "-": sign = -1 i += 1 elif s[i] == "+": i += 1 # checking for digits and adding them to out and stopping completely if no digit is encountered while i < len(s) and s[i].isdigit(): out += s[i] i += 1 # checking if any digit has been read if out == "": return 0 # formalitites else: out = sign * int(out) if out < -(2 ** 31): out = - (2 ** 31) elif out > (2 ** 31) - 1: out = (2 ** 31) - 1 return out
string-to-integer-atoi
Python simple solution | Beats 92% in time, 79 % in space
gkpani97
0
3
string to integer (atoi)
8
0.166
Medium
353
https://leetcode.com/problems/string-to-integer-atoi/discuss/2794514/Python-easy-solution
class Solution: def myAtoi(self, s: str) -> int: if not s: return 0 i = 0 sign = 1 res = 0 while i < len(s) and s[i] == ' ': i += 1 if(i == len(s)): return 0 if(s[i] == '+' or s[i] == '-'): sign = -1 if s[i] == '-' else 1 i += 1 max = 2 ** 31-1 min = -2 ** 31 while(i < len(s)): c = s[i] if(not c.isdigit()): return res digit = sign*(int(c)) # print(res == int(min/10) and digit < min % 10, digit, min % 10) if(res > max/10 or (res == int(max/10) and digit > max % 10)): return max if(res < min/10 or (res == int(min/10) and digit < min % -10)): return min res = res*10+digit i += 1 return res
string-to-integer-atoi
Python easy solution
welin
0
3
string to integer (atoi)
8
0.166
Medium
354
https://leetcode.com/problems/string-to-integer-atoi/discuss/2788330/python-solution
class Solution: def myAtoi(self, s: str) -> int: i = 0 sign = 1 while i < len(s) and s[i] == ' ': i += 1 if i < len(s) and s[i] == '-': sign = -1 i += 1 elif i < len(s) and s[i] == '+': sign = 1 i += 1 num = 0 while i < len(s): if s[i].isdigit(): num = (num * 10) + int(s[i]) else: break i += 1 if sign < 0: num *= sign if num < -2**31:return -2**31 elif num > 2**31-1:return 2**31-1 else:return num
string-to-integer-atoi
python solution
shingnapure_shilpa17
0
3
string to integer (atoi)
8
0.166
Medium
355
https://leetcode.com/problems/string-to-integer-atoi/discuss/2787147/Easy-Python-Solution
class Solution: def myAtoi(self, s: str) -> int: s = s.strip() s = s.split(" ") if s[0] == "": return 0 if s[0].isnumeric(): pass else: if s[0] == "": return 0 elif s[0][0] == '-' or s[0][0] == '+' or s[0][0].isnumeric(): for i in range(1,len(s[0])): if not s[0][i].isnumeric(): s[0] = s[0][:i] break else: return 0 try: k = int(float(s[0])) if k >= -2**(31) and k <= 2**(31)-1: return k elif k < -2**(31): return -2**(31) else: return 2**(31)-1 except: return 0
string-to-integer-atoi
Easy Python Solution
urmil_kalaria
0
4
string to integer (atoi)
8
0.166
Medium
356
https://leetcode.com/problems/string-to-integer-atoi/discuss/2786081/Stuff
class Solution: def myAtoi(self, s: str) -> int: s = s.strip(' ') positive, s = self.PositiveNumber(s) num = self.StringToNum(s) if not positive: num = num * -1 num = self.CheckNumBoundries(num) return num @staticmethod def PositiveNumber(s:str) -> tuple[bool, str]: if s and s[0] == '-': return False, s[1:] if s and s[0] == '+': return True, s[1:] return True, s @staticmethod def StringToNum(s: str) -> int: hit_non_zero = False tmp = '' for num in s: if not hit_non_zero and num == '0': continue hit_non_zero = True try: int(num) except ValueError: break tmp += num if not tmp: return 0 return int(tmp) @staticmethod def CheckNumBoundries(num): if (n:=(2**31 -1)) < num: return n if (n:=(-2**31)) > num: return n return num
string-to-integer-atoi
Stuff
kylegsmith
0
3
string to integer (atoi)
8
0.166
Medium
357
https://leetcode.com/problems/string-to-integer-atoi/discuss/2780905/Python3-81.62-runtime-beats-and-78.94-memory-beats-solution.
class Solution: def myAtoi(self, s: str) -> int: def clamp(num: int): if num > 2 **31 - 1: num = 2 **31 - 1 elif num < - 2 **31: num = - 2 **31 return num def plusminus(num: int, Flag: bool) ->int: if Flag == False: return -num else: return num flag = True Num = 0 sin = s.strip() if sin == '': return 0 if sin[0].isdigit() or sin[0] == '+' or sin[0] == '-': if sin[0] =='-': flag = False sin = sin[1:len(sin)] elif sin[0] =='+': flag = True sin = sin[1:len(sin)] else: return 0 for i in range(0, len(sin)): if sin[i].isdigit(): Num = Num * 10 + int(sin[i]) i += 1 else: break res = plusminus(Num, flag) return clamp(res)
string-to-integer-atoi
[Python3] 81.62 % runtime beats & 78.94 % memory beats solution.
jungledowmtown55
0
9
string to integer (atoi)
8
0.166
Medium
358
https://leetcode.com/problems/string-to-integer-atoi/discuss/2770730/Python-oror-Straight-Forward
class Solution: def myAtoi(self, s: str) -> int: n = len(s) nums = set() for i in range(10): nums.add(str(i)) sign, i = 1, 0 while i < n and s[i] == ' ': i += 1 if i<n and s[i] == '-': sign = -sign i += 1 elif i< n and s[i] == '+': i += 1 digits = [] while i < n and s[i] in nums: digits.append(int(s[i])) i += 1 m, num = len(digits), 0 for i,v in enumerate(digits): if sign == -1: num -= v*(10**(m-i-1)) else: num += v*(10**(m-i-1)) if num <= -2**31: return -(2**31) if num >= 2**31 - 1: return 2**31 - 1 return num
string-to-integer-atoi
Python || Straight Forward
morpheusdurden
0
19
string to integer (atoi)
8
0.166
Medium
359
https://leetcode.com/problems/string-to-integer-atoi/discuss/2752167/Python-Solution.
class Solution: def myAtoi(self, s: str) -> int: s=s.lstrip(" ") if s is None or len(s)==0: return 0 INT_MAX=2**31-1 INT_MIN=-(2**31) possign = len(s)>1 and s[0]=="+" negsigne = len(s)>1 and s[0]=="-" i = 0 res = 0 if s[0]=="+" or s[0]=="-": i += 1 while i < len(s): if '0'<=s[i]<='9': res = res*10 + (ord(s[i]) - ord('0')) else: break i += 1 #print(res) if possign: res*=1 if negsigne: res*=-1 if res>INT_MAX: return INT_MAX elif res<INT_MIN: return INT_MIN else: return res
string-to-integer-atoi
Python Solution.
abhay147
0
6
string to integer (atoi)
8
0.166
Medium
360
https://leetcode.com/problems/string-to-integer-atoi/discuss/2725054/Please-give-your-thoughts-on-my-approach.-Can-it-be-further-optimized
class Solution: def myAtoi(self, s: str) -> int: digit = [] isPos = True digitMet = False signMet = False for char in s: if not digitMet and not char.isdigit(): # if not signMet: if char == "-" and not signMet: isPos = False signMet = True elif char == "+" and not signMet: isPos = True signMet = True elif char == " " and not signMet: continue else: return 0 if char.isdigit(): digit.append(char) digitMet = True if digitMet and not char.isdigit(): break if len(digit) == 0: return 0 digit = int("".join(digit)) if not isPos: digit *= -1 if digit > 2**31-1: return 2**31-1 if digit < -2**31: return -2**31 return digit
string-to-integer-atoi
Please, give your thoughts on my approach. Can it be further optimized?
Alex_Gr
0
2
string to integer (atoi)
8
0.166
Medium
361
https://leetcode.com/problems/string-to-integer-atoi/discuss/2699493/doesn't-need-regex-solution
class Solution: def myAtoi(self, s: str) -> int: m ='' if s.strip() =='': return 0 if s.strip()[0]=='-'or s.strip()[0]=='+' or s.strip()[0].isdigit(): try: for i in s.strip()[1:]: if i.isdigit(): m+=i else: break m = int(s.strip()[0]+m) return -2**31 if m <-2**31 else 2**31-1 if m>2**31-1 else m except: return 0 else: return 0
string-to-integer-atoi
doesn't need regex solution
Juju_Ren
0
3
string to integer (atoi)
8
0.166
Medium
362
https://leetcode.com/problems/string-to-integer-atoi/discuss/2676273/Python-Beats-100-Solution-with-full-working-explanation
class Solution: # Time: O(n) and Space(1) def myAtoi(self, s: str) -> int: ls = list(s.strip()) if len(ls) == 0: return 0 sign = -1 if ls[0] == '-' else 1 if ls[0] in ['-', '+']: # removing the sign as we already captured the integer type in sign variable del ls[0] ret, i = 0, 0 while i < len(ls) and ls[i].isdigit(): # run till ls-1 and if ls[i] is only a digit ret = ret*10 + ord(ls[i]) - ord('0') i += 1 # if the sign * ret is out of bounds, than we will return MIN for -ve OOB and MAX for +ve OOB Integer return max(-2**31, min(sign * ret, 2**31 - 1))
string-to-integer-atoi
Python Beats 100% Solution with full working explanation
DanishKhanbx
0
94
string to integer (atoi)
8
0.166
Medium
363
https://leetcode.com/problems/string-to-integer-atoi/discuss/2671771/Ekomobong-Archibong-Solution
class Solution: def myAtoi(self, s: str) -> int: final = [] digits_reached = False r = [-2**31, 2**31 - 1] # ignore any leading whitespace s = s.lstrip() # check if next character is - or + (if not at the end of string) # [this affects the final result] if len(s) > 0: if s[0] == "+" or s[0] == "-": final.append(s[0]) if len(final) == 0: s = s else: s = s[1:] # read in the next characters until the non-digit char / end of the input is reached. for i in range(len(s)): val = s[i] if val.isdigit(): final.append(val) digits_reached = True else: break # if no digits is reached, then the integer is 0 if digits_reached is False: return 0 # should be in the integer range [-2**31, 2**31 - 1] else: final = int("".join(final)) if final < r[0]: return r[0] elif final > r[1]: return r[1] else: return final
string-to-integer-atoi
Ekomobong Archibong Solution👍
ekomboy012
0
2
string to integer (atoi)
8
0.166
Medium
364
https://leetcode.com/problems/string-to-integer-atoi/discuss/2657153/Short-and-Easy-Python-solution-with-explanation
class Solution: def myAtoi(self, s: str) -> int: # First get rid of any leading or trailing spaces in the string. Initiate an empty final answer list s = s.strip() ; new_list = [] # If the string is blank or was only containing spaces, proceed no more and return null if len(s) == 0: return 0 else: # If the 1st char of the string = "-", then the output has to be negative so lets store this value in a 'sign' variable sign = [-1 if s[0] == "-" else 1][0] # If the 1st char is "-" or "+", get rid of it as we have already stored our sign value now if s[0] == "-" or s[0] == "+": s = s[1:] for i in s: # Check if each element of the string is numeric or not. If yes, then keep appending it in the final answer list if i.isnumeric(): new_list.append(i) else: # As soon as a non numeric character is obtained, proceed no more and exit the for loop break # Create a function which will 1st convert a list of string elements into a string, then be converted to int only to be multiplied by sign, later. ret_val = lambda sign_value, num_str_list: sign_value * int(''.join(num_str_list)) if len(new_list) > 0: # Spit the lambda function's output if its under int ranges else the range cap itself return [2 ** 31 - 1 if ret_val(sign, new_list) > 2 ** 31 - 1 else -2 ** 31 if ret_val(sign, new_list) < -2 ** 31 else ret_val(sign, new_list)][0] else: # If there is no numeric character present in the input or if the 1st character itself is a non digit, return null then return 0
string-to-integer-atoi
Short and Easy Python solution with explanation
code_snow
0
83
string to integer (atoi)
8
0.166
Medium
365
https://leetcode.com/problems/string-to-integer-atoi/discuss/2563535/medium
class Solution: def myAtoi(self, s: str) -> int: s = s.strip() print(s) if s=='': return 0 leading =s[0] if leading == '-': sign = 'neg' s = s[1:] elif leading == '+': sign = 'pos' s = s[1:] else: sign = 'pos' out = '' if s =='': return 0 elif not s[0].isdigit() : return 0 for i in s: if i.isdigit(): out += i else: break if sign == 'pos': return min(int(out),2**31-1) else: return max(-int(out),-2**31)
string-to-integer-atoi
这题为什么是medium啊?
kkljlklkjl
0
22
string to integer (atoi)
8
0.166
Medium
366
https://leetcode.com/problems/string-to-integer-atoi/discuss/2502017/Runtime%3A-41-ms-faster-than-85.79-Memory-Usage%3A-13.9-MB-less-than-79.42
class Solution: def myAtoi(self, s: str) -> int: isNegative, hasSign, hasLeadingZero = False, False, False startI = 0 endI = len(s)-1 while startI < len(s): if s[startI] == '+': if hasSign or hasLeadingZero: return 0 else: hasSign = True startI += 1 elif s[startI] == '-': if hasSign or hasLeadingZero: return 0 else: hasSign = True isNegative = True startI += 1 elif s[startI] == '0': hasLeadingZero = True startI += 1 elif s[startI] == ' ': if hasSign or hasLeadingZero: return 0 startI += 1 continue elif not s[startI].isnumeric(): return 0 else: break while endI > -1 and not s[endI].isnumeric(): if endI < startI: return 0 endI -= 1 if startI == len(s) or endI < startI: return 0 integer = 0 decimal = 0 offset = 0 while startI <= endI: """ if s[startI] == '.': startI += 1 offset = 0 break """ if not s[startI].isnumeric(): break integer = integer * 10 + int(s[startI]) offset += 1 startI += 1 """ print('integer =', integer) while startI <= endI: decimal = decimal * 10 + int(s[startI]) offset += 1 startI += 1 print('decimal =', decimal) """ res = integer # + (decimal/(10**offset)) res = res * -1 if isNegative else res if isNegative and res < -2**31: res = -2**31 elif not isNegative and res > 2**31 - 1: res = 2**31 - 1 return res
string-to-integer-atoi
Runtime: 41 ms, faster than 85.79%; Memory Usage: 13.9 MB, less than 79.42%
GizDave
0
73
string to integer (atoi)
8
0.166
Medium
367
https://leetcode.com/problems/string-to-integer-atoi/discuss/2420768/Python-Accurate-and-Faster-Solution-oror-Documented
class Solution: def myAtoi(self, s: str) -> int: MIN_INT, MAX_INT = -2**31, 2**31 - 1 i, ans, sign = 0, 0, '+' s = s + '\0' # append terminator to prevent index out of range # skip spaces while s[i] == ' ': i += 1 # read sign if s[i] in '-+': sign = s[i]; i += 1 # read digits while s[i] in '1234567890': ans = ans*10 + ord(s[i]) - ord('0') i += 1 if sign == '-': ans = -ans if ans <= MIN_INT: return MIN_INT if ans >= MAX_INT: return MAX_INT return ans
string-to-integer-atoi
[Python] Accurate and Faster Solution || Documented
Buntynara
0
80
string to integer (atoi)
8
0.166
Medium
368
https://leetcode.com/problems/string-to-integer-atoi/discuss/2375874/Python%3A-43ms-13.8MB-(faster-than-80.60-memory-usage-less-than-98.80-of-Python3-submissions)
class Solution: def assign_sign(self, sign): # verify that we haven't already got a sign #&nbsp;"+42-" -> we don't want to return -42; hence check if not self.is_neg and not self.is_pos: # no sign has been set yet if sign=="+": self.is_pos = True elif sign=="-": self.is_neg = True return def add_to_int(self, num): if not self.num: self.num = num else: self.num = (self.num*10) + num def myAtoi(self, s: str) -> int: #&nbsp;remove the leading and trailing spaces self.is_neg = False self.is_pos = False self.num = None s=s.strip() for i in s: # ignore the rest of the string if a non digit character is read if i in ("+","-"): #&nbsp;only read the first symbol; break if second symbol is read if self.is_pos or self.is_neg or isinstance(self.num, int): #&nbsp;one of the two symbols is read or a number is read break self.assign_sign(i) continue try: i = int(i) self.add_to_int(i) except ValueError: # it's neither a sign, nor a number; terminate break # outside the loop; compile the result if not self.num: return 0 upper_limit = 2**31 - 1 if self.is_pos or (not self.is_pos and not self.is_neg): if self.num > upper_limit: self.num = upper_limit elif self.is_neg: if self.num > upper_limit+1: self.num = upper_limit+1 self.num = -1 * self.num return self.num
string-to-integer-atoi
Python: 43ms, 13.8MB (faster than 80.60%, memory usage less than 98.80% of Python3 submissions)
mohak0
0
188
string to integer (atoi)
8
0.166
Medium
369
https://leetcode.com/problems/string-to-integer-atoi/discuss/2373674/Python-Easy-to-Understand-and-Fast-Solution
class Solution: def myAtoi(self, s: str) -> int: s = s.lstrip() if not s: return 0 if s[0]=='-': sign = -1 s = s[1:] elif s[0]=='+': sign = 1 s = s[1:] else: sign = 1 res = '' for ch in s: if ord(ch)>47 and ord(ch)<58: res+=ch else: break res = sign*int(res) if res else 0 if res<(-2**31): return -2**31 elif res> 2**31 - 1: return 2**31 - 1 else: return res
string-to-integer-atoi
Python Easy to Understand and Fast Solution
harsh30199
0
176
string to integer (atoi)
8
0.166
Medium
370
https://leetcode.com/problems/string-to-integer-atoi/discuss/2371459/python-solution-first-solution
class Solution: def myAtoi(self, s: str) -> int: i = 0; n = len(s); ans = 0; sign = 1 while i < n and s[i] == ' ': i += 1 if i < n and s[i] == '-': sign = -1 i += 1 elif i < n and s[i] == '+': i += 1 while i < n and s[i].isdigit(): ans = ans * 10 + int(s[i]) i += 1 ans *= sign MAX = 2 ** 31 - 1 MIN = -2 ** 31 if ans > MAX: return MAX if ans < MIN: return MIN return ans
string-to-integer-atoi
python solution first solution
Nikhilcode123
0
65
string to integer (atoi)
8
0.166
Medium
371
https://leetcode.com/problems/string-to-integer-atoi/discuss/2356756/Python-easiest-solution-or-step-by-step
class Solution: def myAtoi(self, s: str) -> int: i = 0; n = len(s); ans = 0; sign = 1 # Step 1 while i < n and s[i] == ' ': i += 1 # Step 2 if i < n and s[i] == '-': sign = -1 i += 1 elif i < n and s[i] == '+': i += 1 # Step 3 while i < n and s[i].isdigit(): ans = ans * 10 + int(s[i]) i += 1 # Step 4 ans *= sign # step 5 MAX = 2 ** 31 - 1 MIN = -2 ** 31 if ans > MAX: return MAX if ans < MIN: return MIN #step 6 return ans
string-to-integer-atoi
✅ Python easiest solution | step by step
dhananjay79
0
188
string to integer (atoi)
8
0.166
Medium
372
https://leetcode.com/problems/string-to-integer-atoi/discuss/2319136/Simple-python-OOP-solution
class OnlyWhitespaceError(ValueError): """Input string have only whitespace characters""" class EmptyStringError(ValueError): """String doesn't have digits-characters""" class Solution: def __init__(self): self.s = None def myAtoi(self, s: str) -> int: """ Method which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function) It passes the input string through four method: 1) _ignore_whitespace(ignore whitespace on start string) 2) _is_negative(checks whether the number will be negative) 3) _parse_digit_characters(read next digit-sumbol) 4) _correct_int( * check if the integer is out of the 32-bit signed * if necessary, will bring the number to a negative charge) Used try-except syntax to catch error (OnlyWhitespaceError, EmptyStringError) Example: >>> a = Solution() >>> a.myAtoi(" -56.8 sdlfkj 8") -56 >>> a.myAtoi(" +-56.8 sdlfkj 8") #uncorrect syntax 0 """ self.s = s try: pos = self._ignore_whitespace negative, status = self._is_negative(start=pos) parse_int = int(self._parse_digit_characters(start=pos + status)) return self._correct_int(parse_int, negative) except (OnlyWhitespaceError, EmptyStringError): return 0 @property def _ignore_whitespace(self) -> int: for i, x in enumerate(self.s): if x != " ": return i raise OnlyWhitespaceError def _is_negative(self, start: int) -> tuple[bool, bool]: return "-" == self.s[start], self.s[start] in "-+" def _parse_digit_characters(self, start: int) -> str: digits = __import__("string").digits rez = "" if start < self.s.__len__(): for ch in self.s[start:]: if ch in digits: rez += ch else: break if rez: return rez raise EmptyStringError @staticmethod def _correct_int(number: int, negative: bool) -> int: bit_max = 2147483648 if bit_max <= number: return -bit_max if negative else bit_max - 1 return number * (-1 if negative else 1)
string-to-integer-atoi
Simple python OOP solution ✔️
Van4o
0
96
string to integer (atoi)
8
0.166
Medium
373
https://leetcode.com/problems/string-to-integer-atoi/discuss/2314764/Simple-Python-Solution
class Solution: def myAtoi(self, s: str) -> int: s=s.strip() d=0 if(len(s)==0 or ord(s[0])>=65 or ord(s[0])>=97 or s[0]=='.'): return 0 if(s[0]=='-'): d=-1 s=s[1:] elif(s[0]=='+'): s=s[1:] else: s=s n="" lrange=-(2**31) rrange= 2**31-1 for s in s: try: if(ord(s)==32 or s=='.' or ord(s)>=65 or s=='-' or s=='+'): break elif(int(s)>=0 and int(s)<=9): n+=s except: continue if(d<0): try: n=-int(n) except: return 0 else: try: n=int(n) except: return 0 if(n<lrange): return lrange elif(n>rrange): return rrange else: return n
string-to-integer-atoi
Simple Python Solution
dinesh211
0
36
string to integer (atoi)
8
0.166
Medium
374
https://leetcode.com/problems/string-to-integer-atoi/discuss/2194341/Python-easy-solutionor-41-ms-faster-than-80.78
class Solution: def myAtoi(self, s: str) -> int: if s=="": return 0 cnt,cnt2=0,0 ``` counting leading white space and removing it by slicing``` for i in range(0, len(s)): if s[i] == ' ': cnt+=1 elif s[i]!=' ':break s= s[cnt:] ```counting leading +/-``` for i in range(0, len(s)): if s[i]=='+' or s[i]=='-': cnt2+=1 if s[i]>='0' and s[i]<='9':break ```Reading next the characters until the next non-digit character. The rest of the string is ignored.``` for i in range(cnt2, len(s)): if s[i]<'0' or s[i]> '9': s=s[:i] break if s=="": return 0 ```If it contains more than 1 '+' / '-' or single sign, return 0``` if cnt2>1: return 0 elif cnt2==1 and len(s)==1: return 0 else: num = int(s) if num<-2147483648: num=-2147483648 elif num>2147483647: num=2147483647 return num
string-to-integer-atoi
Python easy solution| 41 ms, faster than 80.78%
Sadika12
0
104
string to integer (atoi)
8
0.166
Medium
375
https://leetcode.com/problems/string-to-integer-atoi/discuss/1917046/Simple-Python-Walk
class Solution: def myAtoi(self, s: str) -> int: sign = None number = "0" # if no integer were read, the integer is 0 i, l = 0, len(s) # Max and min ints MAX = 2**31 - 1 MIN = -2**31 # Algorithm from description # 1. Read and ignore any leading whitespace while i < l and s[i] == ' ': i += 1 # 2. Check if the next character is - or + # If the sign is None we assign it but the following character is a sign, we want to return 0 (case: "+-12") if i < l and s[i] == '+': if not sign: sign = 1 i += 1 else: return 0 if i < l and s[i] == '-': if not sign: sign = -1 i += 1 else: return 0 # 3. Read the next characters as long as it is an int while i < l and s[i].isdigit(): number += s[i] i += 1 # If no sign symbol was given. Assume positive sign if not sign: sign = 1 # 4. Convert into integer with the correct sign number = int(number) * sign # 5. If the integer is out of range, clamp it number = min(number, MAX) number = max(number, MIN) # 6. Return return number
string-to-integer-atoi
Simple Python Walk
emjames
0
30
string to integer (atoi)
8
0.166
Medium
376
https://leetcode.com/problems/string-to-integer-atoi/discuss/1911918/Python-straightforward-solution-runtime-O(n)-with-comments
class Solution: def myAtoi(self, s: str) -> int: # first remove leading spaces if any space = 0 for c in s: if c == ' ': space += 1 else: break s = s[space:] # determine if number is neg if not s: return 0 neg, res = False, 0 if s[0] == '-': neg = True # iterate string till we find one none digit char for i, c in enumerate(s): if i == 0 and (c == '-' or c == '+'): continue if not c.isdigit(): break res = res * 10 + int(c) # assign sign to our result if neg: res = -res # compare our number with max_int and min_int res = max(res, -2 ** 31) res = min(res, 2 ** 31 - 1) return res
string-to-integer-atoi
Python straightforward solution runtime O(n) with comments
v0vbs
0
27
string to integer (atoi)
8
0.166
Medium
377
https://leetcode.com/problems/string-to-integer-atoi/discuss/1889790/4-lines-using-regex-Python3
class Solution: def myAtoi(self, s: str) -> int: if (r := re.search(r'^ *([-+]?)0*([0-9]{1,11})', s)) is None: return 0 MIN_INT, MAX_INT, r = -2**31, 2**31 - 1, int(r.group(2)) * (r.group(1)=='-' and -1 or 1) return (r < MIN_INT and MIN_INT) or (r > MAX_INT and MAX_INT) or r
string-to-integer-atoi
4 lines using regex [Python3]
dracdrac
0
130
string to integer (atoi)
8
0.166
Medium
378
https://leetcode.com/problems/string-to-integer-atoi/discuss/1840713/Python-solution-O(n)-32ms-beats-95
class Solution: def myAtoi(self, s: str) -> int: s=list(s) isNegative = False i = 0 while i<len(s) and s[i]==" ": i+=1 if i<len(s) and s[i] == '-': isNegative = True i+=1 elif i<len(s) and s[i] == '+': isNegative = False i+=1 total = 0 while i<len(s) and s[i].isdigit(): total = total*10 + int(s[i]) i+=1 if not isNegative and total>2147483647: total = 2147483647 break elif isNegative and total>2147483648: total = 2147483648 break if isNegative: return -total else: return total
string-to-integer-atoi
Python solution - O(n), 32ms, beats 95%
rgsj90
0
204
string to integer (atoi)
8
0.166
Medium
379
https://leetcode.com/problems/string-to-integer-atoi/discuss/1833221/Simple-oror-EZ-oror-beats-~94
class Solution: def myAtoi(self, s: str) -> int: arr=s.strip().split() ans="" for i in arr: if len(i)==1 and (i=='+' or i=='-'): ans+=i break elif len(i)==1 and (i=='0' or i=='1' or i=='2' or i=='3' or\ i=='4' or i=='5' or i=='6' or i=='7' or\ i=='8' or i=='9'): ans+=i break else: for j in range(0, len(i)): if i[j]=='+' and j==0: ans+=i[j] elif i[j]=='-' and j==0: ans+=i[j] elif i[j]=='0' or i[j]=='1' or i[j]=='2' or i[j]=='3' or\ i[j]=='4' or i[j]=='5' or i[j]=='6' or i[j]=='7' or\ i[j]=='8' or i[j]=='9': ans+=i[j] else: break break if len(ans)==0: return 0 elif len(ans)==1: if ans[0]=='+' or ans[0]=='-': return 0 return int(ans) else: if ans[0]=='+': v=int(ans[1:]) elif ans[0]=='-': v=-1*int(ans[1:]) else: v=int(ans) if v<-1*(2**31): return -1*(2**31) elif v>(2**31-1): return 2**31-1 return v
string-to-integer-atoi
Simple || EZ || beats ~94 %
ashu_py22
0
27
string to integer (atoi)
8
0.166
Medium
380
https://leetcode.com/problems/string-to-integer-atoi/discuss/1726518/Beats-99-of-memory-use
class Solution: def myAtoi(self, s: str) -> int: s= s.strip(" ") if len(s) == 0:return 0 s = list(s) sign = 1 if s[0] == "-": sign = -1 s.pop(0) # Removing the sign from the first term elif s[0] == "+": sign = 1 s.pop(0) else: pass if len(s) > 2 and not s[0].isdigit(): return 0 _ = "0" for i in s: if i.isdigit(): _+=i else: break return_val = int(_) * sign if return_val < -2147483648: return -2147483648 elif return_val > 2147483647: return 2147483647 else: return return_val
string-to-integer-atoi
Beats 99% of memory use
funnybacon
0
98
string to integer (atoi)
8
0.166
Medium
381
https://leetcode.com/problems/palindrome-number/discuss/2797115/Easy-Python-Solution-with-O(1)-space
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0: return False res = 0 temp = x while temp: temp, n = divmod(temp, 10) res = (res * 10) + n return res == x
palindrome-number
Easy Python Solution with O(1) space
tragob
11
1,900
palindrome number
9
0.53
Easy
382
https://leetcode.com/problems/palindrome-number/discuss/1338150/Palindrome-Number-or-Python-solution-without-String-or-Reverse
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0: return False length, temp = -1, x while temp: temp = temp // 10 length += 1 temp = x while temp: left, right = temp // 10**length, temp % 10 if left != right: return False temp = (temp % 10**length) // 10 length -= 2 return True
palindrome-number
Palindrome Number | Python solution without String or Reverse
yiseboge
5
442
palindrome number
9
0.53
Easy
383
https://leetcode.com/problems/palindrome-number/discuss/1943490/Python-one-line-solutution-faster-than-98
class Solution: def isPalindrome(self, x: int) -> bool: return str(x) == str(x)[::-1]
palindrome-number
Python one line solutution faster than 98%
scarletta
4
364
palindrome number
9
0.53
Easy
384
https://leetcode.com/problems/palindrome-number/discuss/2258280/Python-Fastest-and-Shortest-or-One-Line
class Solution: def isPalindrome(self, x: int) -> bool: return str(x) == str(x)[::-1]
palindrome-number
✅Python Fastest & Shortest | One Line
Skiper228
3
255
palindrome number
9
0.53
Easy
385
https://leetcode.com/problems/palindrome-number/discuss/2199886/Python-3-or-2-Solutions
class Solution: def __init__(self): self.revNum = 0 def isPalindrome(self, x: int) -> bool: if x < 0: return False def revTheGivenNumber(num): # num = 54321 if num == 0: # False return self.revNum lastVal = num%10 # 54321 --> 1 self.revNum = self.revNum*10+lastVal return(revTheGivenNumber(num//10)) return x == revTheGivenNumber(x)
palindrome-number
Python 3 | 2 Solutions
yashpurohit763
3
379
palindrome number
9
0.53
Easy
386
https://leetcode.com/problems/palindrome-number/discuss/1643248/Python-Solution-to-palindrome-problem-statement-%3A-29ms
class Solution: def isPalindrome(self, x: int) -> bool: rev_num = 0 dup_copy_of_original_int = x if(x<0): return False while(x>0): rev_num = rev_num*10+x%10 x = x//10 return dup_copy_of_original_int==rev_num
palindrome-number
Python Solution to palindrome problem statement : 29ms
aisimrand
3
648
palindrome number
9
0.53
Easy
387
https://leetcode.com/problems/palindrome-number/discuss/1364282/90.06-faster-O(digits)-Python-Beginner-friendly
class Solution: def isPalindrome(self, x: int) -> bool: if(x>=0): return x == int(str(x)[::-1]) return False
palindrome-number
90.06% faster, O(digits), Python, Beginner friendly
unKNOWN-G
3
725
palindrome number
9
0.53
Easy
388
https://leetcode.com/problems/palindrome-number/discuss/1057636/Python.-ONE-LINER-cool-solution.
class Solution: def isPalindrome(self, x: int) -> bool: return str(x) == str(x)[::-1]
palindrome-number
Python. ONE-LINER cool solution.
m-d-f
3
314
palindrome number
9
0.53
Easy
389
https://leetcode.com/problems/palindrome-number/discuss/2765616/Python-1-line-Solution-Runtime%3A-57-ms-faster-than-98.06
class Solution: def isPalindrome(self, x: int) -> bool: return True if str(x)==str(x)[::-1] else False
palindrome-number
[ Python ] 🐍🐍1 line Solution -Runtime: 57 ms, faster than 98.06%
sourav638
2
16
palindrome number
9
0.53
Easy
390
https://leetcode.com/problems/palindrome-number/discuss/2439785/Python3-Simple-one-liner-with-explanation
class Solution: def isPalindrome(self, x: int) -> bool: # Turn x into a string, then compare it to itself reversed # Note: in python, a string can be traversed using [] # here, we're telling it to traverse the whole string [::] in increments of -1 # this effectively reverses the string return str(x) == str(x)[::-1]
palindrome-number
[Python3] Simple one liner with explanation
connorthecrowe
2
222
palindrome number
9
0.53
Easy
391
https://leetcode.com/problems/palindrome-number/discuss/1621745/Python-recursive-solution-faster-than-96.07
class Solution: def isPalindrome(self, x: int) -> bool: x = str(x) if x[0] != x[-1]: return False elif len(x) <= 2 and x[0] == x[-1]: return True else: return self.isPalindrome(x[1:-1])
palindrome-number
Python recursive solution, faster than 96.07%
cookm353
2
532
palindrome number
9
0.53
Easy
392
https://leetcode.com/problems/palindrome-number/discuss/1136585/Python3-without-string-conversion
class Solution: def isPalindrome(self, x: int) -> bool: if x == 0: return True if x < 0 or x % 10 == 0: return False rev = 0 temp = x while temp > 0: rev = (rev * 10) + (temp % 10) temp = temp // 10 return rev == x
palindrome-number
Python3 without string conversion
thalesbruno
2
224
palindrome number
9
0.53
Easy
393
https://leetcode.com/problems/palindrome-number/discuss/2748565/Python-or-solution-faster-than-94.29-of-python3-submissions-or-Explained
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0: return False rev = str(x)[::-1] return rev == str(x)
palindrome-number
Python | solution faster than 94.29 % of python3 submissions | Explained
sahil193101
1
18
palindrome number
9
0.53
Easy
394
https://leetcode.com/problems/palindrome-number/discuss/2671562/Python-One-Liner
class Solution: def isPalindrome(self, x): return str(x) == "".join(reversed(list(str(x))))
palindrome-number
Python One-Liner
keioon
1
229
palindrome number
9
0.53
Easy
395
https://leetcode.com/problems/palindrome-number/discuss/2664686/Solution-without-converting-integer-to-string
class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False if x<10: return True i = 10 start = 0 lst = [] while i >= 0: digit = x//(10**i) if digit == 0 and start == 0: i -= 1 continue else: if start == 0: n = i start = 1 lst.append(digit) x-= digit*(10**i) i -= 1 i, j = 0, n while i<=j: if lst[i] == lst[j]: i += 1 j -= 1 continue else: return False return True
palindrome-number
Solution without converting integer to string
makwanajigar17
1
174
palindrome number
9
0.53
Easy
396
https://leetcode.com/problems/palindrome-number/discuss/2510328/One-line-simple-solution-python3
class Solution: def isPalindrome(self, x: int) -> bool: return str(x) == str(x)[::-1]
palindrome-number
One line simple solution python3
khushie45
1
205
palindrome number
9
0.53
Easy
397
https://leetcode.com/problems/palindrome-number/discuss/2424127/Simple-enough-solution-in-python
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0: #takes care of negative integers which would always be False return False digit = [int(i) for i in str(x)] if len(digit) == 1 and 0 <= x <= 9: return True elif len(digit) > 1 and digit[:] == digit[::-1]: return True return False
palindrome-number
Simple enough solution in python
charlsony
1
88
palindrome number
9
0.53
Easy
398
https://leetcode.com/problems/palindrome-number/discuss/2420834/Python-Accurate-and-Faster-Solution-wo-String-conversion-oror-Documented
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0: return False # if negative, return False (a negative num can't be palindrome) copy, reverse = x, 0 # take backup of x, initilize reversed number in reverse to reversed number # calculate the reverse number and store into reverse while copy: # if copy has still non-zero value reverse = 10*reverse + copy % 10 # add last digit from copy to reverse copy //= 10 # integer devision to remove last digit from copy return x == reverse # return true if both are equal, else false
palindrome-number
[Python] Accurate and Faster Solution w/o String conversion || Documented
Buntynara
1
93
palindrome number
9
0.53
Easy
399