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... | 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
... | 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 negati... | 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
... | 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 * sig... | 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) ... | 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[... | 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
... | 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
... | 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)
... | 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 :
... | 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)):
... | 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
... | 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
e... | 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:
... | 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):
... | 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:
re... | 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-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]... | 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:... | 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... | 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.
... | 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:
... | 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... | 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 e... | 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
re... | 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] == ... | 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] ==... | 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
... | 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':
... | 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 == '+'):
... | 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
... | 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 ['-','+'... | 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 e... | 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 ... | 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
... | 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():
... | 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 ... | 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... | 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)
... | 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 '... | 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<=o... | 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
#... | 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"):
... | 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] == ... | 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] ... | 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 = ... | 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].isn... | 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) -... | 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 Fl... | 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:
dig... | 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... | 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:
... | 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
... | 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... | 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 r... | 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:
... | 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'
... | 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
... | 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
... | 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
# "+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=="... | 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
... | 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 = an... | 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... | 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 conve... | 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... | 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[c... | 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 igno... | 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
i... | 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 = Fals... | 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==... | 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
... | 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
... | 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.rev... | 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 rever... | 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
... | 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... | 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 ... | palindrome-number | [Python] Accurate and Faster Solution w/o String conversion || Documented | Buntynara | 1 | 93 | palindrome number | 9 | 0.53 | Easy | 399 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.