From 9bc7eab122176e72003b8dd3826631b13958df7b Mon Sep 17 00:00:00 2001 From: changyoucong Date: Wed, 22 Feb 2023 16:57:09 +0800 Subject: [PATCH] =?UTF-8?q?leetcode=20=E5=88=B7=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 + .idea/inspectionProfiles/Project_Default.xml | 150 ++++++++++++++++++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/leetcode.iml | 10 ++ .idea/misc.xml | 4 + .idea/modules.xml | 8 + 150to200/__init__.py | 0 .../\346\234\200\345\244\247\346\225\260.py" | 14 ++ ...73\350\275\254\351\223\276\350\241\250.py" | 33 ++++ 1to50/__init__.py | 0 ...11\346\225\260\344\271\213\345\222\214.py" | 20 +++ ...55\347\232\204\350\212\202\347\202\271.py" | 14 ++ ...44\346\225\260\344\271\213\345\222\214.py" | 22 +++ ...44\346\225\260\347\233\270\345\212\240.py" | 19 +++ ...22\346\263\241\346\216\222\345\272\217.py" | 7 + ...73\351\231\244\346\260\264\345\215\260.py" | 16 ++ ...07\345\272\217\351\223\276\350\241\250.py" | 24 +++ .../\345\233\236\346\226\207\346\225\260.py" | 13 ++ ...215\242\346\225\264\346\225\260 (atoi).py" | 12 ++ "1to50/\345\277\253\346\216\222.py" | 13 ++ ...71\347\232\204\344\270\213\346\240\207.py" | 11 ++ ...64\346\225\260\345\217\215\350\275\254.py" | 15 ++ ...27\351\251\254\346\225\260\345\255\227.py" | 13 ++ ...00\351\225\277\345\255\220\344\270\262.py" | 14 ++ ...11\346\225\260\344\271\213\345\222\214.py" | 29 ++++ ...54\345\205\261\345\211\215\347\274\200.py" | 15 ++ ...36\346\226\207\345\255\220\344\270\262.py" | 17 ++ ...10\347\232\204\346\213\254\345\217\267.py" | 7 + ...76\345\274\217\345\214\271\351\205\215.py" | 0 ...27\346\257\215\347\273\204\345\220\210.py" | 26 +++ ...64\347\232\204\345\256\271\345\231\250.py" | 14 ++ ...73\351\231\244\345\205\203\347\264\240.py" | 22 +++ Readme.md | 2 + main.py | 16 ++ ...15\347\232\204\345\255\220\344\270\262.py" | 3 + ...04\351\207\215\345\244\215\351\241\271.py" | 21 +++ ...04\344\270\255\344\275\215\346\225\260.py" | 14 ++ 37 files changed, 627 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/leetcode.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 150to200/__init__.py create mode 100644 "150to200/\346\234\200\345\244\247\346\225\260.py" create mode 100644 "1to50/K \344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250.py" create mode 100644 1to50/__init__.py create mode 100644 "1to50/\344\270\211\346\225\260\344\271\213\345\222\214.py" create mode 100644 "1to50/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.py" create mode 100644 "1to50/\344\270\244\346\225\260\344\271\213\345\222\214.py" create mode 100644 "1to50/\344\270\244\346\225\260\347\233\270\345\212\240.py" create mode 100644 "1to50/\345\206\222\346\263\241\346\216\222\345\272\217.py" create mode 100644 "1to50/\345\216\273\351\231\244\346\260\264\345\215\260.py" create mode 100644 "1to50/\345\220\210\345\271\266K\344\270\252\345\215\207\345\272\217\351\223\276\350\241\250.py" create mode 100644 "1to50/\345\233\236\346\226\207\346\225\260.py" create mode 100644 "1to50/\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260 (atoi).py" create mode 100644 "1to50/\345\277\253\346\216\222.py" create mode 100644 "1to50/\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.py" create mode 100644 "1to50/\346\225\264\346\225\260\345\217\215\350\275\254.py" create mode 100644 "1to50/\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227.py" create mode 100644 "1to50/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" create mode 100644 "1to50/\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214.py" create mode 100644 "1to50/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" create mode 100644 "1to50/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" create mode 100644 "1to50/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" create mode 100644 "1to50/\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.py" create mode 100644 "1to50/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.py" create mode 100644 "1to50/\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.py" create mode 100644 "1to50/\347\247\273\351\231\244\345\205\203\347\264\240.py" create mode 100644 Readme.md create mode 100644 main.py create mode 100644 "\344\270\262\350\201\224\346\211\200\346\234\211\345\215\225\350\257\215\347\232\204\345\255\220\344\270\262.py" create mode 100644 "\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.py" create mode 100644 "\345\257\273\346\211\276\344\270\244\344\270\252\346\255\243\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.py" diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..45750c6 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,150 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/leetcode.iml b/.idea/leetcode.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/leetcode.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..15764f7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..619d2f7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/150to200/__init__.py b/150to200/__init__.py new file mode 100644 index 0000000..e69de29 diff --git "a/150to200/\346\234\200\345\244\247\346\225\260.py" "b/150to200/\346\234\200\345\244\247\346\225\260.py" new file mode 100644 index 0000000..37a0914 --- /dev/null +++ "b/150to200/\346\234\200\345\244\247\346\225\260.py" @@ -0,0 +1,14 @@ +from typing import List + + +class Solution: + def largestNumber(self, nums: List[int]) -> str: + + for i in range(len(nums) - 1): + for j in range(i + 1, len(nums)): + if str(nums[i]) + str(nums[j]) < str(nums[j]) + str(nums[i]): + nums[i], nums[j] = nums[j], nums[i] + s = "" + for num in nums: + s += str(num) + return str(int(s)) \ No newline at end of file diff --git "a/1to50/K \344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250.py" "b/1to50/K \344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250.py" new file mode 100644 index 0000000..0348f26 --- /dev/null +++ "b/1to50/K \344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250.py" @@ -0,0 +1,33 @@ +# Definition for singly-linked list. +from typing import Optional +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: + dummy = ListNode() + p = dummy + while True: + count = k + stack = [] + tmp = head + while count and tmp: + stack.append(tmp) + tmp = tmp.next + count -= 1 + if count: + p.next = head + break + while stack: + p.next = stack.pop() + p = p.next + p.next = tmp + head = tmp + return dummy.next + + +if __name__ == '__main__': + print(Solution().reverseKGroup([1, 2, 3, 4, 5], 2)) diff --git a/1to50/__init__.py b/1to50/__init__.py new file mode 100644 index 0000000..e69de29 diff --git "a/1to50/\344\270\211\346\225\260\344\271\213\345\222\214.py" "b/1to50/\344\270\211\346\225\260\344\271\213\345\222\214.py" new file mode 100644 index 0000000..47f4a64 --- /dev/null +++ "b/1to50/\344\270\211\346\225\260\344\271\213\345\222\214.py" @@ -0,0 +1,20 @@ +from itertools import combinations +from typing import List + + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + L = [] + for i in combinations(nums, 3): + if sum(list(i)) == 0: + L.append(list(i)) + L1 = [] + for j in L: + j.sort() + L1.append(j) + b = list(set([tuple(j) for j in L1])) + return [list(i) for i in b] + + +nums = [-1, 0, 1, 2, -1, -4] +print(Solution().threeSum(nums)) diff --git "a/1to50/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.py" "b/1to50/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.py" new file mode 100644 index 0000000..04de2a7 --- /dev/null +++ "b/1to50/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.py" @@ -0,0 +1,14 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: + if not head or not head.next: + return head + + tail = head.next + head.next = self.swapPairs(tail.next) + tail.next = head + return tail \ No newline at end of file diff --git "a/1to50/\344\270\244\346\225\260\344\271\213\345\222\214.py" "b/1to50/\344\270\244\346\225\260\344\271\213\345\222\214.py" new file mode 100644 index 0000000..6c7e5a1 --- /dev/null +++ "b/1to50/\344\270\244\346\225\260\344\271\213\345\222\214.py" @@ -0,0 +1,22 @@ +from typing import List + + +class Solution: + + @staticmethod + def twoSum(nums: List[int], target: int) -> List[int]: + length = len(nums) + if length < 2: + return [] + for k, v in enumerate(nums): + + for j in range(k + 1, length): + if v + nums[j] == target: + return [k, j] + return [] + + +if __name__ == '__main__': + nums = [3, 2, 4] + target = 6 + print(Solution.twoSum(nums, target)) diff --git "a/1to50/\344\270\244\346\225\260\347\233\270\345\212\240.py" "b/1to50/\344\270\244\346\225\260\347\233\270\345\212\240.py" new file mode 100644 index 0000000..8dc6b62 --- /dev/null +++ "b/1to50/\344\270\244\346\225\260\347\233\270\345\212\240.py" @@ -0,0 +1,19 @@ +# Definition for singly-linked list. + +from typing import Optional + + +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def addTwoNumbers(self, l1, l2) -> Optional[ListNode]: + l1.val += l2.val + if l1.val > 10: + l1.next = self.addTwoNumbers(ListNode(l1.val // 10), l1.next) + l1.val %= 10 + l1.next = self.addTwoNumbers(l1.next, l2.next) + return l1 diff --git "a/1to50/\345\206\222\346\263\241\346\216\222\345\272\217.py" "b/1to50/\345\206\222\346\263\241\346\216\222\345\272\217.py" new file mode 100644 index 0000000..ff05b45 --- /dev/null +++ "b/1to50/\345\206\222\346\263\241\346\216\222\345\272\217.py" @@ -0,0 +1,7 @@ +def bubbleSort(data: list): + length = len(data) + for i in range(length - 1): + for j in range(length - i - 1): + if data[j] > data[j + 1]: + data[j], data[j + 1] = data[j + 1], data[j] + return data diff --git "a/1to50/\345\216\273\351\231\244\346\260\264\345\215\260.py" "b/1to50/\345\216\273\351\231\244\346\260\264\345\215\260.py" new file mode 100644 index 0000000..54fb378 --- /dev/null +++ "b/1to50/\345\216\273\351\231\244\346\260\264\345\215\260.py" @@ -0,0 +1,16 @@ +from PyPDF2 import PdfReader, PdfWriter + +output = PdfWriter() + +reader = PdfReader(r"D:\CU_76a18490a1c94c20bda9ccfc81acabd6 (1).pdf") +# 获取总页数 +page_count = reader.getNumPages() + +# with open(,'rb') as pf: +# pin = PdfReader(pf) +# for i in range(2): +# page = pin.getPage(pin.pages[0]) +# print(page) +# # output.addPage(page) +# # with open("XXXX/out.pdf",'wb') as ouf: +# # output.write(ouf) \ No newline at end of file diff --git "a/1to50/\345\220\210\345\271\266K\344\270\252\345\215\207\345\272\217\351\223\276\350\241\250.py" "b/1to50/\345\220\210\345\271\266K\344\270\252\345\215\207\345\272\217\351\223\276\350\241\250.py" new file mode 100644 index 0000000..5e41555 --- /dev/null +++ "b/1to50/\345\220\210\345\271\266K\344\270\252\345\215\207\345\272\217\351\223\276\350\241\250.py" @@ -0,0 +1,24 @@ +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: + heap = [] + for sub_list in lists: + while sub_list: + heap.append(sub_list.val) + sub_list = sub_list.next + heap.sort(reverse=True) + + head = ListNode(None) + curr_list = head + while heap: + # 从heap列表的尾部取最小数加入到链表中 + temp_list = ListNode(heap.pop()) + curr_list.next = temp_list + curr_list = curr_list.next + return head.next diff --git "a/1to50/\345\233\236\346\226\207\346\225\260.py" "b/1to50/\345\233\236\346\226\207\346\225\260.py" new file mode 100644 index 0000000..b562ca3 --- /dev/null +++ "b/1to50/\345\233\236\346\226\207\346\225\260.py" @@ -0,0 +1,13 @@ +class Solution: + def isPalindrome(self, x: int) -> bool: + if x < 0: + return False + x1 = str(x)[::-1] + if int(x1) == x: + return True + return False + + +if __name__ == '__main__': + x = -123 + print(str(x)[::-1].isalnum()) diff --git "a/1to50/\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260 (atoi).py" "b/1to50/\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260 (atoi).py" new file mode 100644 index 0000000..bd33b8a --- /dev/null +++ "b/1to50/\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260 (atoi).py" @@ -0,0 +1,12 @@ +import re + + +class Solution: + def myAtoi(self, s: str) -> int: + INT_MAX = 2147483647 + INT_MIN = -2147483648 + str = s.lstrip() # 清除左边多余的空格 + num_re = re.compile(r'^[\+\-]?\d+') # 设置正则规则 + num = num_re.findall(str) # 查找匹配的内容 + num = int(*num) # 由于返回的是个列表,解包并且转换成整数 + return max(min(num, INT_MAX), INT_MIN) # 返回值 diff --git "a/1to50/\345\277\253\346\216\222.py" "b/1to50/\345\277\253\346\216\222.py" new file mode 100644 index 0000000..c5214c0 --- /dev/null +++ "b/1to50/\345\277\253\346\216\222.py" @@ -0,0 +1,13 @@ +def quick_sort(data: list): + if len(data) < 2: + return data.sort() + mid = data[len(data) // 2] + left = [] + right = [] + data.remove(mid) + for num in data: + if num > mid: + right.append(num) + else: + left.append(num) + return quick_sort(left) + [mid] + quick_sort(right) diff --git "a/1to50/\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.py" "b/1to50/\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.py" new file mode 100644 index 0000000..df4a463 --- /dev/null +++ "b/1to50/\346\211\276\345\207\272\345\255\227\347\254\246\344\270\262\344\270\255\347\254\254\344\270\200\344\270\252\345\214\271\351\205\215\351\241\271\347\232\204\344\270\213\346\240\207.py" @@ -0,0 +1,11 @@ +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + if not haystack or not needle: + return -1 + for i in range(len(haystack)): + if haystack[i] == needle[0]: + if haystack[i: i + len(needle)] == needle: + return i + return -1 + + diff --git "a/1to50/\346\225\264\346\225\260\345\217\215\350\275\254.py" "b/1to50/\346\225\264\346\225\260\345\217\215\350\275\254.py" new file mode 100644 index 0000000..78654e9 --- /dev/null +++ "b/1to50/\346\225\264\346\225\260\345\217\215\350\275\254.py" @@ -0,0 +1,15 @@ +class Solution: + + @staticmethod + def reverse(x: int) -> int: + s = str(x) + INT_MIN, INT_MAX = -2 ** 31, 2 ** 31 - 1 + if s.startswith('-'): + l = s[1:][::-1] + return -int("".join(l)) if INT_MIN < int("".join(l)) < INT_MAX else 0 + else: + l = s[::-1] + return int("".join(l)) if INT_MIN < int("".join(l)) < INT_MAX else 0 + + + diff --git "a/1to50/\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227.py" "b/1to50/\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227.py" new file mode 100644 index 0000000..487001e --- /dev/null +++ "b/1to50/\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227.py" @@ -0,0 +1,13 @@ +class Solution: + def intToRoman(self, num: int) -> str: + # 使用哈希表,按照从大到小顺序排列 + hashmap = {1000:'M', 900:'CM', 500:'D', 400:'CD', 100:'C', 90:'XC', 50:'L', 40:'XL', 10:'X', 9:'IX', 5:'V', 4:'IV', 1:'I'} + res = '' + for key in hashmap: + if num // key != 0: + count = num // key # 比如输入4000,count 为 4 + res += hashmap[key] * count + num %= key + return res + + diff --git "a/1to50/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" "b/1to50/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" new file mode 100644 index 0000000..e3426ac --- /dev/null +++ "b/1to50/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" @@ -0,0 +1,14 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + queue = [] + length = 0 + for i in list(s): + if i in queue: + length = max(length, len(queue)) + while i in queue: + queue.pop(0) + queue.append(i) + + return max(length, len(queue)) + + diff --git "a/1to50/\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214.py" "b/1to50/\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214.py" new file mode 100644 index 0000000..e5aaf34 --- /dev/null +++ "b/1to50/\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214.py" @@ -0,0 +1,29 @@ +class Solution: + def threeSumClosest(self, nums: List[int], target: int) -> int: + nums.sort() + length = len(nums) + temp = 0 + for i in range(length): + if i > 0 and nums[i] == nums[i - 1]: + continue + left = i + 1 + right = length - 1 + while left < right: + temp = nums[i] + nums[left] + nums[right] + if temp == target: + return temp + if temp > target: + # 如果和大于 target,移动 c 对应的指针 + k0 = right - 1 + # 移动到下一个不相等的元素 + while left < k0 and nums[k0] == nums[right]: + k0 -= 1 + right = k0 + else: + # 如果和小于 target,移动 b 对应的指针 + j0 = left + 1 + # 移动到下一个不相等的元素 + while j0 < right and nums[j0] == nums[left]: + j0 += 1 + left = j0 + return temp diff --git "a/1to50/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" "b/1to50/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" new file mode 100644 index 0000000..66848eb --- /dev/null +++ "b/1to50/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" @@ -0,0 +1,15 @@ +from typing import List + + +def longestCommonPrefix(strs: List[str]) -> str: + if not str: + return "" + s1 = min(strs) + s2 = max(strs) + for i, x in enumerate(s1): + if x != s2[i]: + return s2[:i] + return s1 + + + diff --git "a/1to50/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" "b/1to50/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" new file mode 100644 index 0000000..f8a4205 --- /dev/null +++ "b/1to50/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" @@ -0,0 +1,17 @@ +class Solution: + def expandAroundCenter(self, s, left, right): + while left >= 0 and right < len(s) and s[left] == s[right]: + left -= 1 + right += 1 + return left + 1, right - 1 + + def longestPalindrome(self, s: str) -> str: + start, end = 0, 0 + for i in range(len(s)): + left1, right1 = self.expandAroundCenter(s, i, i) + left2, right2 = self.expandAroundCenter(s, i, i + 1) + if right1 - left1 > end - start: + start, end = left1, right1 + if right2 - left2 > end - start: + start, end = left2, right2 + return s[start: end + 1] diff --git "a/1to50/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" "b/1to50/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" new file mode 100644 index 0000000..2e960a0 --- /dev/null +++ "b/1to50/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" @@ -0,0 +1,7 @@ +class Solution: + def isValid(self, s): + while '{}' in s or '()' in s or '[]' in s: + s = s.replace('{}', '') + s = s.replace('[]', '') + s = s.replace('()', '') + return s == '' \ No newline at end of file diff --git "a/1to50/\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.py" "b/1to50/\346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215.py" new file mode 100644 index 0000000..e69de29 diff --git "a/1to50/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.py" "b/1to50/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.py" new file mode 100644 index 0000000..95f8554 --- /dev/null +++ "b/1to50/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.py" @@ -0,0 +1,26 @@ +class Solution: + def letterCombinations(self, digits: str) -> List[str]: + KEY = {'2': ['a', 'b', 'c'], + '3': ['d', 'e', 'f'], + '4': ['g', 'h', 'i'], + '5': ['j', 'k', 'l'], + '6': ['m', 'n', 'o'], + '7': ['p', 'q', 'r', 's'], + '8': ['t', 'u', 'v'], + '9': ['w', 'x', 'y', 'z']} + + if len(digits) == 0: + return [] + + res_list = [] + + def dfs(digits: str, res: str): + if len(digits) == 0: + res_list.append(res) # 搜索叶子结点,返回上一级 + else: + for val in KEY[digits[0]]: # 搜索子节点 + dfs(digits[1:], res + val) + + dfs(digits, '') + + return res_list diff --git "a/1to50/\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.py" "b/1to50/\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.py" new file mode 100644 index 0000000..5ad71c6 --- /dev/null +++ "b/1to50/\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.py" @@ -0,0 +1,14 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + # 时间复杂度 o(n) + ans = 0 + left = 0 + right = len(height) - 1 + while left < right: + area = (right - left) * min(height[left], height[right]) + ans = max(ans, area) + if height[left] < height[right]: + left += 1 + else: + right -= 1 + return ans diff --git "a/1to50/\347\247\273\351\231\244\345\205\203\347\264\240.py" "b/1to50/\347\247\273\351\231\244\345\205\203\347\264\240.py" new file mode 100644 index 0000000..5ca9459 --- /dev/null +++ "b/1to50/\347\247\273\351\231\244\345\205\203\347\264\240.py" @@ -0,0 +1,22 @@ +from typing import List + + +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + ix = 0 + while True: + if ix + 1 > len(nums): + break + + a = nums[ix] + + if a == val: + nums.pop(ix) + else: + ix += 1 + + return len(nums) + + +if __name__ == '__main__': + print(Solution().removeElement([3, 2, 2, 3], 3)) diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..ed39599 --- /dev/null +++ b/Readme.md @@ -0,0 +1,2 @@ +# leetcode +leetcode刷题记录Python版本 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..5596b44 --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press Shift+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git "a/\344\270\262\350\201\224\346\211\200\346\234\211\345\215\225\350\257\215\347\232\204\345\255\220\344\270\262.py" "b/\344\270\262\350\201\224\346\211\200\346\234\211\345\215\225\350\257\215\347\232\204\345\255\220\344\270\262.py" new file mode 100644 index 0000000..a8d917a --- /dev/null +++ "b/\344\270\262\350\201\224\346\211\200\346\234\211\345\215\225\350\257\215\347\232\204\345\255\220\344\270\262.py" @@ -0,0 +1,3 @@ +class Solution: + def findSubstring(self, s: str, words: List[str]) -> List[int]: + ... \ No newline at end of file diff --git "a/\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.py" "b/\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.py" new file mode 100644 index 0000000..4a501e5 --- /dev/null +++ "b/\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271.py" @@ -0,0 +1,21 @@ +from typing import List + + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + if not nums: + return 0 + n = len(nums) + slow = 1 # slow在等fast给他数据 + fast = 1 # fast用来找不同给slow + while fast < n: # fast遍历完就结束 + if nums[fast] != nums[fast - 1]: # fast 找到不同的数 + nums[slow] = nums[fast] + slow += 1 # slow获得fast的数后去下一位等待 + fast += 1 + + return slow + + +if __name__ == '__main__': + print(Solution().removeDuplicates([1, 1, 2])) diff --git "a/\345\257\273\346\211\276\344\270\244\344\270\252\346\255\243\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.py" "b/\345\257\273\346\211\276\344\270\244\344\270\252\346\255\243\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.py" new file mode 100644 index 0000000..2257dde --- /dev/null +++ "b/\345\257\273\346\211\276\344\270\244\344\270\252\346\255\243\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.py" @@ -0,0 +1,14 @@ +from typing import List + + +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + num = nums1 + nums2 + num.sort() + print(num) + length = len(num) + + if length % 2 == 1: + return num[int(length / 2)] + else: + return (num[int(length / 2)] + num[int(length / 2) - 1]) / 2