Skip to content

updated code #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ target/

#Ipython Notebook
.ipynb_checkpoints
.qodo
58 changes: 21 additions & 37 deletions python/008_String_to_Integer(atoi).py
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
class Solution(object):
def myAtoi(self, str):
def convert(self, s, numRows):
"""
:type str: str
:rtype: int
:type s: str
:type numRows: int
:rtype: str
"""
sign = 1
max_int, min_int = 2147483647, -2147483648
result, pos = 0, 0
ls = len(str)
while pos < ls and str[pos] == ' ':
pos += 1
if pos < ls and str[pos] == '-':
sign = -1
pos += 1
elif pos < ls and str[pos] == '+':
pos += 1
while pos < ls and ord(str[pos]) >= ord('0') and ord(str[pos]) <= ord('9'):
num = ord(str[pos]) - ord('0')
if result > max_int / 10 or ( result == max_int / 10 and num >= 8):
if sign == -1:
return min_int
return max_int
result = result * 10 + num
pos += 1
return sign * result
if numRows == 1:
return s

# def myAtoi(self, s):
# #https://leetcode.com/discuss/83626/line-python-solution-eafp-instead-lbyl-easier-logic-beats-24%25
# try:
# s = s.lstrip() + '$' # remove leading spaces and append an end mark
# for i, ch in enumerate(s):
# if not (ch in '+-' or '0' <= ch <= '9'):
# result = int(s[:i])
# return -2 ** 31 if result < -2 ** 31 else 2 ** 31 - 1 if result > 2 ** 31 - 1 else result
# except:
# return 0
rows = [''] * numRows
row_index = 0
direction = 1 # 1 for down, -1 for up

for char in s:
rows[row_index] += char

if __name__ == '__main__':
# begin
s = Solution()
print s.myAtoi("+-2")
row_index += direction

if row_index == numRows:
row_index = numRows - 2
direction = -1
elif row_index == -1:
row_index = 1
direction = 1

return ''.join(rows)
6 changes: 1 addition & 5 deletions python/009_Palindrome_Number.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# """

class Solution(object):
def isPalindrome(self, x: int) -> bool:
def isPalindrome(self, x):
x = str(x)
if (x == x[::-1]):
return True
Expand Down Expand Up @@ -61,7 +61,3 @@ def isPalindrome(self, x: int) -> bool:
# return False


if __name__ == '__main__':
# begin
s = Solution()
print s.isPalindrome(1001)