From ce123c30c0d608013cd6db79da9a73f3c72f6271 Mon Sep 17 00:00:00 2001 From: "vishwak.p" <23211a04j4@bvrit.ac.in> Date: Wed, 2 Apr 2025 11:27:11 +0530 Subject: [PATCH 1/2] . --- .gitignore | 1 + python/008_String_to_Integer(atoi).py | 58 ++++++++++----------------- 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 683d8b0..006d1bc 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,4 @@ target/ #Ipython Notebook .ipynb_checkpoints +.qodo diff --git a/python/008_String_to_Integer(atoi).py b/python/008_String_to_Integer(atoi).py index ac167ad..cfd5086 100644 --- a/python/008_String_to_Integer(atoi).py +++ b/python/008_String_to_Integer(atoi).py @@ -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") \ No newline at end of file + 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) \ No newline at end of file From efc046a2f8b2427872e82613eaa54c7c720a7978 Mon Sep 17 00:00:00 2001 From: "vishwak.p" <23211a04j4@bvrit.ac.in> Date: Wed, 2 Apr 2025 11:44:20 +0530 Subject: [PATCH 2/2] Update 009_Palindrome_Number.py --- python/009_Palindrome_Number.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/python/009_Palindrome_Number.py b/python/009_Palindrome_Number.py index 8dc7ce5..3bc9020 100644 --- a/python/009_Palindrome_Number.py +++ b/python/009_Palindrome_Number.py @@ -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 @@ -61,7 +61,3 @@ def isPalindrome(self, x: int) -> bool: # return False -if __name__ == '__main__': - # begin - s = Solution() - print s.isPalindrome(1001)