From eac4dc96cfc31f9cbce480d99ae473afa03cb7c2 Mon Sep 17 00:00:00 2001 From: anianuzhny Date: Thu, 5 Aug 2021 14:01:46 +0300 Subject: [PATCH 1/4] add two linked lists --- leetcode/add two numbers.py | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 leetcode/add two numbers.py diff --git a/leetcode/add two numbers.py b/leetcode/add two numbers.py new file mode 100644 index 0000000..e1123fe --- /dev/null +++ b/leetcode/add two numbers.py @@ -0,0 +1,54 @@ + +from typing import List + +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: + """ + :type l1: ListNode + :type l2: ListNode + :rtype: ListNode + """ + + carry = 0 + head = first_head = ListNode() + + while l1 or l2 or carry: + result = 0 + if l1: + result += l1.val + l1 = l1.next + if l2: + result += l2.val + l2 = l2.next + if carry: + result += carry + + carry = result // 10 + val = result % 10 + head.next = ListNode(val) + head = head.next + return first_head.next + + def stringToListNode(self,number_string: List[int]): + previousNode = None + first = None + for i in number_string: + currentNode = ListNode(i) + if first is None: + first = currentNode + if previousNode is not None: + previousNode.next = currentNode + previousNode = currentNode + return first + +if __name__ == '__main__': + s = Solution() + l1 = s.stringToListNode([2,4,3]) + l2 = s.stringToListNode([5,6,4]) + s.addTwoNumbers(l1, l2) \ No newline at end of file From 864b58797a45710e21637de062dfb01f647c03eb Mon Sep 17 00:00:00 2001 From: anianuzhny Date: Thu, 5 Aug 2021 16:30:36 +0300 Subject: [PATCH 2/4] zigzag_conversion --- leetcode/zigzag_conversion.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 leetcode/zigzag_conversion.py diff --git a/leetcode/zigzag_conversion.py b/leetcode/zigzag_conversion.py new file mode 100644 index 0000000..33592fc --- /dev/null +++ b/leetcode/zigzag_conversion.py @@ -0,0 +1,27 @@ +class Solution: + def convert(self, s: str, numRows: int) -> str: + m = [[''] * len(s) for i in range(numRows)] + global_idx = 0 + column = 0 + while global_idx < len(s): + + for row_idx in range(numRows): + m[row_idx][column] = s[global_idx] + global_idx += 1 + if global_idx >= len(s): + break + if global_idx < len(s): + for rox_idx_up in range(numRows - 2): + column += 1 + m[numRows - rox_idx_up - 2][column] = s[global_idx] + global_idx += 1 + if global_idx >= len(s): + break + column += 1 + + return ''.join([m[r][c] for r in range(len(m)) for c in range(len(m[0]))]) + + +if __name__ == '__main__': + s = Solution() + print(s.convert("PAYPALISHIRING", 3)) \ No newline at end of file From a865257aa7ba39a7ba5807fa41e71d7f0e8e9548 Mon Sep 17 00:00:00 2001 From: anianuzhny Date: Thu, 5 Aug 2021 17:37:25 +0300 Subject: [PATCH 3/4] str to int myAtoi --- leetcode/str_to_int_atoi.py | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 leetcode/str_to_int_atoi.py diff --git a/leetcode/str_to_int_atoi.py b/leetcode/str_to_int_atoi.py new file mode 100644 index 0000000..8ad8bab --- /dev/null +++ b/leetcode/str_to_int_atoi.py @@ -0,0 +1,53 @@ +class Solution: + def myAtoi(self, s: str) -> int: + result = s.strip() + + sign = '' + + if not result: + return 0 + if result[0] in ['-', '+']: + sign = result[0] + if len(result) > 1 and not str.isdigit(result[1]): + return 0 # '+-12' case + + digit: str = result[1:] + elif not str.isdigit(result[0]): + return 0 + elif str.isdigit(result[0]): + + digit: str = result + + + digits = ''.join(list(filter(lambda s: str.isdigit(s) or s == '.', digit))) + if digits not in digit: + first_alpha = list(filter(lambda s: str.isalpha(s) or s in ['+', ' ', '-'], digit)) + if first_alpha: + first_alpha = first_alpha[0] + + idx = digit.index(first_alpha) + digit = digit[0:idx].strip().split()[0] + else: + digit = digit + # return 0 + else: + digit = digits + try: + output = int(sign + digit) + except ValueError: + try: + output = int(float(sign + digit)) + except ValueError: + return 0 + + if output >= 2 ** 32 // 2: + return 2 ** 32 // 2 -1 + elif output < -2 ** 32 // 2: + return -2 ** 32 // 2 + else: + return output + + +if __name__ == '__main__': + s = Solution() + print(s.myAtoi(" 123 456")) \ No newline at end of file From 8ce91fd0be89ff85f04af541913b3f85b200f70b Mon Sep 17 00:00:00 2001 From: anianuzhny Date: Fri, 6 Aug 2021 12:54:49 +0300 Subject: [PATCH 4/4] longest palindrome --- leetcode/is_palindrome.py | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 leetcode/is_palindrome.py diff --git a/leetcode/is_palindrome.py b/leetcode/is_palindrome.py new file mode 100644 index 0000000..7219510 --- /dev/null +++ b/leetcode/is_palindrome.py @@ -0,0 +1,69 @@ +from typing import Tuple, List + + +class Solution: + def longestPalindrome(self, s: str) -> str: + # max_result_length = 0 + # max_result_idx = 0 + # d = {} + # times = 0 + # for idx in range(len(s)): + # if len(s) - idx < max_result_length: + # break + # for count_letters in range(1, len(s) + 1): + # if count_letters < max_result_length: + # continue + # substring_to_check = s[idx:count_letters] + # if substring_to_check not in d: + # is_palindrom, n = self.is_palindrom(substring_to_check) + # times += 1 + # if is_palindrom: + # if n > max_result_length: + # max_result_length = n + # max_result_idx = idx + # d[substring_to_check] = (True, idx, n) + # else: + # d[substring_to_check] = (False, idx, n) + + # # is_palindrom, n = self.is_palindrom(substring_to_check) + # # times += 1 + # # if is_palindrom: + # # if n > max_result_length: + # # max_result_length = n + # # max_result_idx = idx + # # d[substring_to_check] = (True, n) + # output = s[max_result_idx: max_result_length + max_result_idx] + # return output + dp: List[List[bool]] = [[False] * len(s) for _ in range(len(s))] + length: int = 0 + result: str = '' + times = 0 + temp_results = [] + for end in range(len(s)): + for start in range(end+1): + if start == end: + dp[start][end] = True + elif start + 1 == end: + dp[start][end] = s[start] == s[end] + else: + dp[start][end] = s[start] == s[end] and dp[start+1][end-1] + + if dp[start][end] and end-start+1 > length: + times += 1 + length = end-start+1 + result = s[start:end+1] + temp_results.append(result) + + return result + def is_palindrom(self, s: str) -> Tuple[bool, int]: + if s == s[::-1]: + return True, len(s) + else: + return False, 0 + +if __name__ == '__main__': + s = Solution() + # print(s.longestPalindrome("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")) + # print(s.longestPalindrome('forgeeksskeegfor')) + # print(s.longestPalindrome('a')) + print(s.longestPalindrome('twbiqwtafgjbtolwprpdnymaatlbuacrmzzwhkpvuwdtyfjsbsqxrlxxtqkjlpkvpxmlajdmnyepsmczmmfdtjfbyybotpoebilayqzvqztqgddpcgpelwmriwmoeeilpetbxoyktizwcqeeivzgxacuotnlzutdowiudwuqnghjgoeyojikjhlmcsrctvnahnoapmkcrqmwixpbtirkasbyajenknuccojooxfwdeflmxoueasvuovcayisflogtpxtbvcxfmydjupwihnxlpuxxcclbhvutvvshcaikuedhyuksbwwjsnssizoedjkbybwndxpkwcdxaexagazztuiuxphxcedqstahmevkwlayktrubjypzpaiwexkwbxcrqhkpqevhxbyipkmlqmmmogrnexsztsbkvebjgybrolttvnidnntpgvsawgaobycfaaviljsvyuaanguhohsepbthgjyqkicyaxkytshqmtxhilcjxdpcbmvnpippdrpggyohwyswuydyrhczlxyyzregpvxyfwpzvmjuukswcgpenygmnfwdlryobeginxwqjhxtmbpnccwdaylhvtkgjpeyydkxcqarkwvrmwbxeetmhyoudfuuwxcviabkqyhrvxbjmqcqgjjepmalyppymatylhdrazxytixtwwqqqlrcusxyxzymrnryyernrxbgrphsioxrxhmxwzsytmhnosnrpwtphaunprdtbpwapgjjqcnykgspjsxslxztfsuflijbeebwyyowjzpsbjcdabxmxhtweppffglvhfloprfavduzbgkw')) \ No newline at end of file