diff --git a/.DS_Store b/.DS_Store index 5f8901c..cef69cc 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git "a/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.js" "b/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.js" new file mode 100644 index 0000000..9a970c0 --- /dev/null +++ "b/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.js" @@ -0,0 +1,12 @@ +const twoSum = (nums, target) => { + const prevNums = {}; // 存放出现过的数字,和对应的索引 + for (let i = 0; i < nums.length; i++) { // 遍历每一项 + const curNum = nums[i]; // 当前项 + const targetNum = target - curNum; // 希望从过去的数字中找到的呼应项 + const targetNumIndex = prevNums[targetNum];// 在prevNums中找targetNum的索引 + if (targetNumIndex !== undefined) { // 如果能找到 + return [targetNumIndex, i]; // 直接返回targetNumIndex和当前的i + } // 如果找不到,说明之前没出现过targetNum + prevNums[curNum] = i; // 往prevNums存当前curNum和对应的i + } +} \ No newline at end of file diff --git "a/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.py" "b/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.py" index 8b92177..2201fce 100644 --- "a/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.py" +++ "b/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.py" @@ -9,4 +9,4 @@ def twoSum(self, nums, target): for i, num in enumerate(nums): if target - num in dic: return [dic[target - num], i] - dic[num] = i \ No newline at end of file + dic[num] = i diff --git "a/0002.\344\270\244\346\225\260\347\233\270\345\212\240/0002-\344\270\244\346\225\260\347\233\270\345\212\240 2.py" "b/0002.\344\270\244\346\225\260\347\233\270\345\212\240/0002-\344\270\244\346\225\260\347\233\270\345\212\240 2.py" new file mode 100644 index 0000000..92afc5c --- /dev/null +++ "b/0002.\344\270\244\346\225\260\347\233\270\345\212\240/0002-\344\270\244\346\225\260\347\233\270\345\212\240 2.py" @@ -0,0 +1,39 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def addTwoNumbers(self, l1, l2): + """ + :type l1: ListNode + :type l2: ListNode + :rtype: ListNode + """ + if self.getLength(l1) < self.getLength(l2): + l1, l2 = l2, l1 + head = l1 + while(l2): + l1.val += l2.val + l1 = l1.next + l2 = l2.next + + p = head + while(p): + if p.val > 9: + p.val -= 10 + if p.next: + p.next.val += 1 + else: + p.next = ListNode(1) + p = p.next + return head + + + def getLength(self, l): + tmp = 0 + while(l): + tmp += 1 + l = l.next + return tmp \ No newline at end of file diff --git "a/0003.\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/0003-\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/0003.\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/0003-\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" index 7fcc3ef..8a1f7f4 100644 --- "a/0003.\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/0003-\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/0003.\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/0003-\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" @@ -4,13 +4,13 @@ def lengthOfLongestSubstring(self, s): :type s: str :rtype: int """ - record = {} - start, res = 0, 0 - for end in range(len(s)): - if s[end] in record: - start = max(start, record[s[end]] + 1) - - record[s[end]] = end - res = max(res, end - start + 1) - + left, right = 0, 0 + dic = dict() + res = 0 + while right < len(s): + if s[right] in dic: + left = max(left, dic[s[right]] + 1) + dic[s[right]] = right + res = max(res, right - left + 1) + right += 1 return res \ No newline at end of file diff --git "a/0004.\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/0004-\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/0004.\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/0004-\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..b500112 --- /dev/null +++ "b/0004.\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/0004-\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,24 @@ +class Solution(object): + def findMedianSortedArrays(self, nums1, nums2): + """ + :type nums1: List[int] + :type nums2: List[int] + :rtype: float + """ + res = [] + p1, p2 = 0, 0 + while p1 < len(nums1) and p2 < len(nums2): + if nums1[p1] <= nums2[p2]: + res.append(nums1[p1]) + p1 += 1 + else: + res.append(nums2[p2]) + p2 += 1 + while p1 < len(nums1): + res.append(nums1[p1]) + p1 += 1 + while p2 < len(nums2): + res.append(nums2[p2]) + p2 += 1 + print res + return res[len(res) // 2] if len(res) % 2 == 1 else (res[len(res) // 2 - 1] + res[len(res) // 2]) * 1.0 / 2 \ No newline at end of file diff --git "a/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/0005-\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" "b/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/0005-\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" index d1f7847..8033cf5 100644 --- "a/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/0005-\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" +++ "b/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262/0005-\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" @@ -4,30 +4,21 @@ def longestPalindrome(self, s): :type s: str :rtype: str """ - left, right = 0, 0 - res, string = 0, "" + res = "" for i in range(len(s)): - left, right = i, i - while(left >= 0 and right < len(s) and s[left] == s[right]): - left -= 1 - right += 1 - left += 1 - right -= 1 - - if right - left + 1 > res: - res = right - left + 1 - string = s[left:right + 1] - - for i in range(1, len(s)): - left, right = i - 1, i - while(left >= 0 and right < len(s) and s[left] == s[right]): - left -= 1 - right += 1 - left += 1 - right -= 1 - - if right - left + 1 > res: - res = right - left + 1 - string = s[left:right + 1] - return string - \ No newline at end of file + tmp = self.centralSpread(i, i, s) + if len(tmp) > len(res): + res = tmp + tmp = self.centralSpread(i, i + 1, s) + if len(tmp) > len(res): + res = tmp + return res + + def centralSpread(self, left, right, s): + res = "" + while left >= 0 and right < len(s) and s[left] == s[right]: + res = s[left: right + 1] + left -= 1 + right += 1 + # print res, left, right + return res \ No newline at end of file diff --git "a/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260 2.py" "b/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260 2.py" new file mode 100644 index 0000000..fc986ad --- /dev/null +++ "b/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260 2.py" @@ -0,0 +1,17 @@ +class Solution(object): + def isPalindrome(self, x): + """ + :type x: int + :rtype: bool + """ + #2019.6.1 + xx = x + if x < 0: + return False + + reverse = 0 + while x > 0: + x, tmp = divmod(x, 10) + reverse = reverse * 10 + tmp + + return reverse == xx \ No newline at end of file diff --git "a/0012.\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227/0012-\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227.py" "b/0012.\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227/0012-\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227.py" index f49d843..b0d9569 100644 --- "a/0012.\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227/0012-\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227.py" +++ "b/0012.\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227/0012-\346\225\264\346\225\260\350\275\254\347\275\227\351\251\254\346\225\260\345\255\227.py" @@ -4,13 +4,16 @@ def intToRoman(self, num): :type num: int :rtype: str """ - digit = [1000,900,500,400,100,90,50,40,10,9,5,4,1] - mapping = {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"} + l = [(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")][::-1] res = "" - for i in digit: - res += (num / i) * mapping[i] - num -= i * (num / i) - if num == 0: - break - return res - + while num: + for value, ch in l[::-1]: + if num >= value: + res += ch + num -= value + break + else: + l.pop() + return res \ No newline at end of file diff --git "a/0013.\347\275\227\351\251\254\346\225\260\345\255\227\350\275\254\346\225\264\346\225\260/0013-\347\275\227\351\251\254\346\225\260\345\255\227\350\275\254\346\225\264\346\225\260.py" "b/0013.\347\275\227\351\251\254\346\225\260\345\255\227\350\275\254\346\225\264\346\225\260/0013-\347\275\227\351\251\254\346\225\260\345\255\227\350\275\254\346\225\264\346\225\260.py" index 2caa879..c0f930a 100644 --- "a/0013.\347\275\227\351\251\254\346\225\260\345\255\227\350\275\254\346\225\264\346\225\260/0013-\347\275\227\351\251\254\346\225\260\345\255\227\350\275\254\346\225\264\346\225\260.py" +++ "b/0013.\347\275\227\351\251\254\346\225\260\345\255\227\350\275\254\346\225\264\346\225\260/0013-\347\275\227\351\251\254\346\225\260\345\255\227\350\275\254\346\225\264\346\225\260.py" @@ -4,21 +4,15 @@ def romanToInt(self, s): :type s: str :rtype: int """ - dic = {"I": 1, "V":5, "X": 10, "L":50, "C":100, "D": 500, "M": 1000} - stack = [] + dic = {"I":1, "V": 5, "X":10, "L":50, "C":100, "D":500, "M":1000} res = 0 - for inx, item in enumerate(s): - res += dic[item] - # print res - # s.append(item) - if item == "V" or item == "X": - if stack and stack[-1] == "I": - res -= 2 - elif item == "L" or item == "C": - if stack and stack[-1] == "X": - res -= 20 - elif item == "D" or item == "M": - if stack and stack[-1] == "C": - res -= 200 - stack.append(item) - return res \ No newline at end of file + pre_value = None + for ch in s: + if (ch in ["V", "X"] and pre_value == 1) or \ + (ch in ["L", "C"] and pre_value == 10) or \ + (ch in ["D", "M"] and pre_value == 100): + res += dic[ch] - 2 * pre_value + else: + res += dic[ch] + pre_value = dic[ch] + return res diff --git "a/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271/0019-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271.py" "b/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271/0019-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271.py" new file mode 100644 index 0000000..30dac97 --- /dev/null +++ "b/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271/0019-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + l = 0 + p = head + while p: + l += 1 + p = p.next + + count = l - n + 1 + + dummy = ListNode(-1) + dummy.next = head + cur = -1 + p = dummy + while p: + cur += 1 + if cur == count - 1: + node_to_be_deleted = p.next + p.next = node_to_be_deleted.next + break + p = p.next + + return dummy.next \ No newline at end of file diff --git "a/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267 2.py" "b/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267 2.py" new file mode 100644 index 0000000..563ad92 --- /dev/null +++ "b/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267 2.py" @@ -0,0 +1,16 @@ +class Solution(object): + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + dic = {")": "(", "]":"[", "}":"{"} + stack = [] + for ch in s: + if ch in ["(", "[", "{"]: + stack.append(ch) + else: + if not stack or dic[ch] != stack[-1]: + return False + stack.pop() + return len(stack) == 0 \ No newline at end of file diff --git "a/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" "b/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" index 4a34f9b..563ad92 100644 --- "a/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" +++ "b/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" @@ -4,14 +4,13 @@ def isValid(self, s): :type s: str :rtype: bool """ - mapping = {")":"(", "]":"[", "}":"{"} + dic = {")": "(", "]":"[", "}":"{"} stack = [] - for i, char in enumerate(s): - if char not in mapping:#left - stack.append(char) + for ch in s: + if ch in ["(", "[", "{"]: + stack.append(ch) else: - if not stack or stack[-1] != mapping[char]: + if not stack or dic[ch] != stack[-1]: return False stack.pop() - - return len(stack) == 0 \ No newline at end of file + return len(stack) == 0 \ No newline at end of file diff --git "a/21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/21-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" "b/0021.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/0021-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250 2.py" similarity index 100% rename from "21.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/21-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" rename to "0021.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/0021-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250 2.py" diff --git "a/0022.\346\213\254\345\217\267\347\224\237\346\210\220/0022-\346\213\254\345\217\267\347\224\237\346\210\220 2.py" "b/0022.\346\213\254\345\217\267\347\224\237\346\210\220/0022-\346\213\254\345\217\267\347\224\237\346\210\220 2.py" new file mode 100644 index 0000000..326c241 --- /dev/null +++ "b/0022.\346\213\254\345\217\267\347\224\237\346\210\220/0022-\346\213\254\345\217\267\347\224\237\346\210\220 2.py" @@ -0,0 +1,22 @@ +class Solution(object): + def generateParenthesis(self, n): + """ + :type n: int + :rtype: List[str] + """ + + res = [] + + def dfs(tmp, left, right): + if len(tmp) == 2 * n: + res.append(tmp) + + if left: + dfs(tmp + "(", left - 1, right) + if right > left: + dfs(tmp + ")", left, right - 1) + + + dfs("", n, n) + return res + \ No newline at end of file diff --git "a/0024.\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/0024-\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 2.py" "b/0024.\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/0024-\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 2.py" new file mode 100644 index 0000000..85dc922 --- /dev/null +++ "b/0024.\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/0024-\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 2.py" @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def swapPairs(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + if not head or not head.next: + return head + dummy = ListNode(1) + dummy.next = head + + first = head + second = head.next + + tail = second.next + first.next = self.swapPairs(tail) + second.next = first + dummy.next = second + + return dummy.next \ No newline at end of file diff --git "a/0026.\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/0026-\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/0026.\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/0026-\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..5f77e5e --- /dev/null +++ "b/0026.\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/0026-\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,10 @@ +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + visited = set() + index = 0 + for num in nums: + if num not in visited: + visited.add(num) + nums[index] = num + index += 1 + return index \ No newline at end of file diff --git "a/0027.\347\247\273\351\231\244\345\205\203\347\264\240/0027-\347\247\273\351\231\244\345\205\203\347\264\240 2.py" "b/0027.\347\247\273\351\231\244\345\205\203\347\264\240/0027-\347\247\273\351\231\244\345\205\203\347\264\240 2.py" new file mode 100644 index 0000000..7f0c216 --- /dev/null +++ "b/0027.\347\247\273\351\231\244\345\205\203\347\264\240/0027-\347\247\273\351\231\244\345\205\203\347\264\240 2.py" @@ -0,0 +1,21 @@ +class Solution(object): + def removeElement(self, nums, val): + """ + :type nums: List[int] + :type val: int + :rtype: int + """ + nums.sort() + for i, num in enumerate(nums): + if num == val: + j = i + 1 + while(j < len(nums) and nums[j] == num): + j += 1 + t = j + while(j < len(nums)): + nums[i] = nums[j] + i += 1 + j += 1 + return i + + \ No newline at end of file diff --git "a/0028.\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/0028-\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/0028.\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/0028-\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..781ae1a --- /dev/null +++ "b/0028.\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/0028-\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,5 @@ +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + if needle not in haystack: + return -1 + return haystack.index(needle) \ No newline at end of file diff --git "a/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/0034-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256 2.py" "b/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/0034-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256 2.py" new file mode 100644 index 0000000..6916af6 --- /dev/null +++ "b/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/0034-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256 2.py" @@ -0,0 +1,56 @@ +class Solution(object): + def searchRange(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + lo, hi = 0, len(nums) - 1 + + while lo <= hi: + + mid = (lo + hi) // 2 + # print lo, hi, mid, nums + if nums[mid] == target: + break + elif nums[mid] < target: + lo = mid + 1 + else: + hi = mid - 1 + + if lo > hi: + return [-1, -1] + midposition = mid + leftside, rightside = midposition, midposition + #߽ + # print 1 + lo, hi = 0, midposition + while lo <= hi: + # print lo, hi, mid + mid = (lo + hi) // 2 + if nums[mid] < target: + lo = mid + 1 + elif nums[mid] == target: + if mid == 0 or (mid - 1 >= 0 and nums[mid - 1] < target): + leftside = mid + break + else: + hi = mid - 1 + # print 1 + #ұ߽ + lo, hi = midposition, len(nums) - 1 + while lo <= hi: + mid = (lo + hi) // 2 + if nums[mid] > target: + hi = mid - 1 + elif nums[mid] == target: + if mid == len(nums) - 1 or (mid + 1 < len(nums) and nums[mid + 1] > target): + rightside = mid + break + else: + lo = mid + 1 + + return [leftside, rightside] + + + \ No newline at end of file diff --git "a/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256 2.py" "b/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256 2.py" new file mode 100644 index 0000000..af9dcf0 --- /dev/null +++ "b/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256 2.py" @@ -0,0 +1,18 @@ +class Solution(object): + def searchInsert(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: int + """ + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + + if nums[mid] == target: + return mid + elif nums[mid] > target: + right = mid - 1 + elif nums[mid] < target: + left = mid + 1 + return left \ No newline at end of file diff --git "a/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.py" "b/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.py" index 521a7c3..af9dcf0 100644 --- "a/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.py" +++ "b/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.py" @@ -5,13 +5,14 @@ def searchInsert(self, nums, target): :type target: int :rtype: int """ - lo,hi = 0, len(nums) - 1 - while (lo <= hi): - mid = lo + (hi - lo) / 2 + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target: return mid elif nums[mid] > target: - hi = mid - 1 - else: - lo = mid + 1 - return lo \ No newline at end of file + right = mid - 1 + elif nums[mid] < target: + left = mid + 1 + return left \ No newline at end of file diff --git "a/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254 2.py" "b/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254 2.py" new file mode 100644 index 0000000..29ea49c --- /dev/null +++ "b/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254 2.py" @@ -0,0 +1,15 @@ +class Solution: + def isValidSudoku(self, board: List[List[str]]) -> bool: + row = defaultdict(set) + col = defaultdict(set) + square = defaultdict(set) + for i in range(9): + for j in range(9): + if board[i][j].isdigit(): + if board[i][j] in row[i] or board[i][j] in col[j] or board[i][j] in square[(i // 3, j // 3)]: + return False + else: + row[i].add(board[i][j]) + col[j].add(board[i][j]) + square[(i // 3, j // 3)].add(board[i][j]) + return True diff --git "a/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.py" "b/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.py" index ed34e1b..29ea49c 100644 --- "a/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.py" +++ "b/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.py" @@ -1,19 +1,15 @@ -class Solution(object): - def isValidSudoku(self, board): - """ - :type board: List[List[str]] - :rtype: bool - """ - from collections import defaultdict - row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set) +class Solution: + def isValidSudoku(self, board: List[List[str]]) -> bool: + row = defaultdict(set) + col = defaultdict(set) + square = defaultdict(set) for i in range(9): for j in range(9): if board[i][j].isdigit(): - if board[i][j] in row[i] or board[i][j] in column[j] or board[i][j] in squre[(i//3, j//3)]: + if board[i][j] in row[i] or board[i][j] in col[j] or board[i][j] in square[(i // 3, j // 3)]: return False else: row[i].add(board[i][j]) - column[j].add(board[i][j]) - squre[(i//3, j //3)].add(board[i][j]) + col[j].add(board[i][j]) + square[(i // 3, j // 3)].add(board[i][j]) return True - \ No newline at end of file diff --git "a/0037.\350\247\243\346\225\260\347\213\254/0037-\350\247\243\346\225\260\347\213\254.py" "b/0037.\350\247\243\346\225\260\347\213\254/0037-\350\247\243\346\225\260\347\213\254.py" index cfbd885..f121617 100644 --- "a/0037.\350\247\243\346\225\260\347\213\254/0037-\350\247\243\346\225\260\347\213\254.py" +++ "b/0037.\350\247\243\346\225\260\347\213\254/0037-\350\247\243\346\225\260\347\213\254.py" @@ -1,48 +1,66 @@ -class Solution(object): - def solveSudoku(self, board): +from collections import defaultdict +import copy +class Solution: + def solveSudoku(self, board: List[List[str]]) -> None: """ - :type board: List[List[str]] - :rtype: None Do not return anything, modify board in-place instead. + Do not return anything, modify board in-place instead. """ - from collections import defaultdict - row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set) - + row = defaultdict(set) + col = defaultdict(set) + square = defaultdict(set) + for i in range(9): + for j in range(9): + if board[i][j].isdigit(): + row[i].add(board[i][j]) + col[j].add(board[i][j]) + square[(i // 3, j // 3)].add(board[i][j]) self.res = [] def dfs(x, y): - - if x == 8 and y == 9: - # print board - for roww in board: - self.res.append(roww[:]) - # print self.res + if x == 9 and y == 0: + if self.isValidSudoku(board): + self.res = copy.deepcopy(board) return - if y == 9: - dfs(x + 1, 0) - return - if board[x][y].isdigit(): - dfs(x, y + 1) - return - - for k in range(1,10): - if str(k) not in row[x] and str(k) not in column[y] and str(k) not in squre[(x // 3, y // 3)]: - board[x][y] = str(k) - row[x].add(str(k)) - column[y].add(str(k)) - squre[(x // 3, y // 3)].add(str(k)) - - dfs(x, y + 1) - - board[x][y] = "." - row[x].remove(str(k)) - column[y].remove(str(k)) - squre[(x // 3, y // 3)].remove(str(k)) + if not self.res: + if board[x][y] != ".": + if y == 8: + dfs(x + 1, 0) + else: + dfs(x, y + 1) + return + + for num in range(1, 10): + num = str(num) + if num not in row[x] and num not in col[y] and num not in square[(x // 3, y // 3)]: + board[x][y] = num + + row[x].add(num) + col[y].add(num) + square[(x // 3, y // 3)].add(num) + if y == 8: + dfs(x + 1, 0) + else: + dfs(x, y + 1) + board[x][y] = "." + row[x].remove(num) + col[y].remove(num) + square[(x // 3, y // 3)].remove(num) + + dfs(0, 0) + board[:] = self.res + + + def isValidSudoku(self, board: List[List[str]]) -> bool: + row = defaultdict(set) + col = defaultdict(set) + square = defaultdict(set) for i in range(9): for j in range(9): if board[i][j].isdigit(): - row[i].add(board[i][j].encode("utf-8")) - column[j].add(board[i][j].encode("utf-8")) - squre[(i // 3, j // 3)].add(board[i][j].encode("utf-8")) - - dfs(0, 0) - board[:] = self.res \ No newline at end of file + if board[i][j] in row[i] or board[i][j] in col[j] or board[i][j] in square[(i // 3, j // 3)]: + return False + else: + row[i].add(board[i][j]) + col[j].add(board[i][j]) + square[(i // 3, j // 3)].add(board[i][j]) + return True \ No newline at end of file diff --git "a/0045.\350\267\263\350\267\203\346\270\270\346\210\217II/0045-\350\267\263\350\267\203\346\270\270\346\210\217II.py" "b/0045.\350\267\263\350\267\203\346\270\270\346\210\217II/0045-\350\267\263\350\267\203\346\270\270\346\210\217II.py" new file mode 100644 index 0000000..5ad82f4 --- /dev/null +++ "b/0045.\350\267\263\350\267\203\346\270\270\346\210\217II/0045-\350\267\263\350\267\203\346\270\270\346\210\217II.py" @@ -0,0 +1,9 @@ +class Solution: + def jump(self, nums: List[int]) -> int: + dp = [float("inf") for _ in nums] + dp[0] = 0 + for i, num in enumerate(nums): + for j in range(1, 1 + num): + if i + j < len(nums): + dp[i + j] = min(dp[i + j], dp[i] + 1) + return dp[-1] \ No newline at end of file diff --git "a/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227 2.py" "b/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227 2.py" new file mode 100644 index 0000000..f2a991c --- /dev/null +++ "b/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227 2.py" @@ -0,0 +1,16 @@ +class Solution(object): + def permute(self, nums): + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + res = [] + def dfs(tmp, nums): + if not nums: + res.append(tmp) + + for i, x in enumerate(nums): + dfs(tmp + [x], nums[:i] + nums[i + 1:]) + + dfs([], nums) + return res \ No newline at end of file diff --git "a/0048.\346\227\213\350\275\254\345\233\276\345\203\217/0048-\346\227\213\350\275\254\345\233\276\345\203\217 2.py" "b/0048.\346\227\213\350\275\254\345\233\276\345\203\217/0048-\346\227\213\350\275\254\345\233\276\345\203\217 2.py" new file mode 100644 index 0000000..e97f740 --- /dev/null +++ "b/0048.\346\227\213\350\275\254\345\233\276\345\203\217/0048-\346\227\213\350\275\254\345\233\276\345\203\217 2.py" @@ -0,0 +1,20 @@ +class Solution(object): + def rotate(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: None Do not return anything, modify matrix in-place instead. + """ + #תҶԳƷת + if not matrix or not matrix[0]: + return matrix + n = len(matrix) + + for i in range(n): + for j in range(i + 1, n): + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + + for row in matrix: + for i in range(n // 2): + row[i], row[n - 1 - i] = row[n - 1 - i], row[i] + + return matrix \ No newline at end of file diff --git "a/0049.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/0049-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204 2.py" "b/0049.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/0049-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204 2.py" new file mode 100644 index 0000000..6998b4a --- /dev/null +++ "b/0049.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/0049-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204 2.py" @@ -0,0 +1,18 @@ +class Solution(object): + def groupAnagrams(self, strs): + """ + :type strs: List[str] + :rtype: List[List[str]] + """ + record = dict() + + for word in strs: + tmp = tuple(sorted(word)) + # print tmp + if tmp in record: + record[tmp].append(word) + else: + record[tmp] = [word] + return [val for key, val in record.items()] + + \ No newline at end of file diff --git "a/0054.\350\236\272\346\227\213\347\237\251\351\230\265/0054-\350\236\272\346\227\213\347\237\251\351\230\265 2.py" "b/0054.\350\236\272\346\227\213\347\237\251\351\230\265/0054-\350\236\272\346\227\213\347\237\251\351\230\265 2.py" new file mode 100644 index 0000000..33e693b --- /dev/null +++ "b/0054.\350\236\272\346\227\213\347\237\251\351\230\265/0054-\350\236\272\346\227\213\347\237\251\351\230\265 2.py" @@ -0,0 +1,42 @@ +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + if not matrix or not matrix[0]: + return [] + m, n = len(matrix), len(matrix[0]) + i, j = 0, 0 + state = "right" + cnt = 0 + res = [] + while(cnt < m * n): + cnt += 1 + res.append(matrix[i][j]) + matrix[i][j] = "X" + if state == "right": + j += 1 + if j == n or matrix[i][j] == "X": + i += 1 + j -= 1 + state = "down" + elif state == "down": + i += 1 + if i == m or matrix[i][j] == "X": + i -= 1 + j -= 1 + state = "left" + elif state == "left": + j -= 1 + if j == -1 or matrix[i][j] == "X": + j += 1 + i -= 1 + state = "up" + elif state == "up": + i -= 1 + if i == -1 or matrix[i][j] == "X": + i += 1 + j += 1 + state = "right" + return res \ No newline at end of file diff --git "a/0055.\350\267\263\350\267\203\346\270\270\346\210\217/0055-\350\267\263\350\267\203\346\270\270\346\210\217 2.py" "b/0055.\350\267\263\350\267\203\346\270\270\346\210\217/0055-\350\267\263\350\267\203\346\270\270\346\210\217 2.py" new file mode 100644 index 0000000..b6283c9 --- /dev/null +++ "b/0055.\350\267\263\350\267\203\346\270\270\346\210\217/0055-\350\267\263\350\267\203\346\270\270\346\210\217 2.py" @@ -0,0 +1,12 @@ +class Solution(object): + def canJump(self, nums): + """ + :type nums: List[int] + :rtype: bool + """ + start, end = 0, 0 + + while start <= end and end < len(nums): + end = max(end, start + nums[start]) + start += 1 + return end >= len(nums) - 1 \ No newline at end of file diff --git "a/0058.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/0058-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246 2.py" "b/0058.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/0058-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246 2.py" new file mode 100644 index 0000000..2ef7de1 --- /dev/null +++ "b/0058.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/0058-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246 2.py" @@ -0,0 +1,14 @@ +class Solution(object): + def lengthOfLastWord(self, s): + """ + :type s: str + :rtype: int + """ + cnt = 0 + for i in range(len(s) - 1, -1, -1): + if s[i] != " ": + while i >= 0 and s[i] != " ": + cnt += 1 + i -= 1 + break + return cnt \ No newline at end of file diff --git "a/0064.\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/0064-\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.py" "b/0064.\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/0064-\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.py" index d6a6897..421b9bd 100644 --- "a/0064.\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/0064-\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.py" +++ "b/0064.\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/0064-\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.py" @@ -7,20 +7,15 @@ def minPathSum(self, grid): if not grid or not grid[0]: return 0 m, n = len(grid), len(grid[0]) - dp = [[0 for _ in range(n)] for _ in range(m)] - for i in range(m): - for j in range(n): - # print i, j - dp[i][j] = grid[i][j] - - dp[0][0] = grid[0][0] - for i in range(m): - for j in range(n): - if i - 1 >= 0 and j - 1 >= 0: - dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]) - elif i - 1 >= 0: - dp[i][j] += dp[i - 1][j] - elif j - 1 >= 0: - dp[i][j] += dp[i][j - 1] - return dp[-1][-1] \ No newline at end of file + for j in range(1, n): + grid[0][j] += grid[0][j - 1] + + for i in range(1, m): + grid[i][0] += grid[i - 1][0] + + for i in range(1, m): + for j in range(1, n): + grid[i][j] += min(grid[i - 1][j], grid[i][j - 1]) + + return grid[-1][-1] \ No newline at end of file diff --git "a/69.x\347\232\204\345\271\263\346\226\271\346\240\271/69-x\347\232\204\345\271\263\346\226\271\346\240\271.py" "b/0069.x\347\232\204\345\271\263\346\226\271\346\240\271/0069-x\347\232\204\345\271\263\346\226\271\346\240\271 2.py" similarity index 100% rename from "69.x\347\232\204\345\271\263\346\226\271\346\240\271/69-x\347\232\204\345\271\263\346\226\271\346\240\271.py" rename to "0069.x\347\232\204\345\271\263\346\226\271\346\240\271/0069-x\347\232\204\345\271\263\346\226\271\346\240\271 2.py" diff --git "a/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257 2.py" "b/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257 2.py" new file mode 100644 index 0000000..88ab0fc --- /dev/null +++ "b/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257 2.py" @@ -0,0 +1,19 @@ +class Solution(object): + def climbStairs(self, n): + """ + :type n: int + :rtype: int + """ + if n <= 2: + return [1, 2][n - 1] + first = 1 + second = 2 + cnt = 2 + while cnt < n: + cnt += 1 + cur = first + second + if cnt == n: + return cur + first = second + second = cur + \ No newline at end of file diff --git "a/0071.\347\256\200\345\214\226\350\267\257\345\276\204/0071-\347\256\200\345\214\226\350\267\257\345\276\204 2.py" "b/0071.\347\256\200\345\214\226\350\267\257\345\276\204/0071-\347\256\200\345\214\226\350\267\257\345\276\204 2.py" new file mode 100644 index 0000000..d008010 --- /dev/null +++ "b/0071.\347\256\200\345\214\226\350\267\257\345\276\204/0071-\347\256\200\345\214\226\350\267\257\345\276\204 2.py" @@ -0,0 +1,15 @@ +class Solution(object): + def simplifyPath(self, path): + """ + :type path: str + :rtype: str + """ + l = path.split("/") + stack = [] + for item in l: + if item != "." and item != ".." and item: + stack.append(item) + elif item == ".." and stack: + stack.pop() + + return "/" + "/".join(stack) \ No newline at end of file diff --git "a/0073.\347\237\251\351\230\265\347\275\256\351\233\266/0073-\347\237\251\351\230\265\347\275\256\351\233\266 2.py" "b/0073.\347\237\251\351\230\265\347\275\256\351\233\266/0073-\347\237\251\351\230\265\347\275\256\351\233\266 2.py" new file mode 100644 index 0000000..f33d6de --- /dev/null +++ "b/0073.\347\237\251\351\230\265\347\275\256\351\233\266/0073-\347\237\251\351\230\265\347\275\256\351\233\266 2.py" @@ -0,0 +1,24 @@ +class Solution(object): + def setZeroes(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: None Do not return anything, modify matrix in-place instead. + """ + if not matrix or not matrix[0]: + return matrix + m, n = len(matrix), len(matrix[0]) + + for i in range(m): + for j in range(n): + if matrix[i][j] == 0: + for t in range(m):#ͬһ + if matrix[t][j] != 0: + matrix[t][j] = "0" + for t in range(n):#ͬһ + if matrix[i][t] != 0: + matrix[i][t] = "0" + for i in range(m): + for j in range(n): + if matrix[i][j] == "0": + matrix[i][j] = 0 + \ No newline at end of file diff --git "a/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206 2.py" "b/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206 2.py" new file mode 100644 index 0000000..6012ad0 --- /dev/null +++ "b/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206 2.py" @@ -0,0 +1,14 @@ +class Solution(object): + def subsets(self, nums): + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + res = [[]] + for num in nums: + tmp = res[:] + for item in res: + tmp.append(item + [num]) + res = tmp[:] + + return res \ No newline at end of file diff --git "a/0079.\345\215\225\350\257\215\346\220\234\347\264\242/0079-\345\215\225\350\257\215\346\220\234\347\264\242.py" "b/0079.\345\215\225\350\257\215\346\220\234\347\264\242/0079-\345\215\225\350\257\215\346\220\234\347\264\242.py" index 77cc5f1..d3cdcde 100644 --- "a/0079.\345\215\225\350\257\215\346\220\234\347\264\242/0079-\345\215\225\350\257\215\346\220\234\347\264\242.py" +++ "b/0079.\345\215\225\350\257\215\346\220\234\347\264\242/0079-\345\215\225\350\257\215\346\220\234\347\264\242.py" @@ -7,34 +7,31 @@ def exist(self, board, word): """ if not board or not board[0]: return False - if not word: - return True - - self.res = False + + m, n = len(board), len(board[0]) dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] - def dfs(start, x0, y0): - if start == len(word) - 1: + self.res = False + def dfs(word_idx, x0, y0): + # print word_idx + if word_idx >= len(word): self.res = True - return - visited.add((x0, y0)) - - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - # print x, y - if 0 <= x < m and 0 <= y < n and (x, y) not in visited and board[x][y] == word[start + 1] and not self.res: - visited.add((x, y)) - dfs(start + 1, x, y) - visited.remove((x, y)) - - m, n = len(board), len(board[0]) - # print m * n, len(word) + return + if not self.res: + for k in range(len(dx)): + x1 = x0 + dx[k] + y1 = y0 + dy[k] + + if 0 <= x1 < m and 0 <= y1 < n and board[x1][y1] == word[word_idx]: + temp = board[x1][y1] + board[x1][y1] = -1 + dfs(word_idx + 1, x1, y1) + board[x1][y1] = temp for i in range(m): for j in range(n): if board[i][j] == word[0]: - visited = set() - dfs(0, i, j) - if self.res: - return True - return False \ No newline at end of file + temp = board[i][j] + board[i][j] = 0 + dfs(1, i, j) + board[i][j] = temp + return self.res \ No newline at end of file diff --git "a/0080.\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\271II/0080-\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\271II.py" "b/0080.\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\271II/0080-\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\271II.py" new file mode 100644 index 0000000..b9415ef --- /dev/null +++ "b/0080.\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\271II/0080-\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\271II.py" @@ -0,0 +1,17 @@ +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + prev, prev_count = nums[0], 1 + res = 1 + for num in nums[1:]: + if num == prev: + if prev_count == 1: + nums[res] = num + res += 1 + prev_count += 1 + else: + nums[res] = num + res += 1 + prev = num + prev_count = 1 + return res + \ No newline at end of file diff --git "a/0082.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II/0082-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II 2.py" "b/0082.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II/0082-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II 2.py" new file mode 100644 index 0000000..2f4b37b --- /dev/null +++ "b/0082.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II/0082-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II 2.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def deleteDuplicates(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + if not head or not head.next: + return head + newhead = ListNode(-1) + newhead.next = head + if head.val != head.next.val: + head.next = self.deleteDuplicates(head.next) + else: + p = head + while p and p.val == head.val: + p = p.next + newhead.next = self.deleteDuplicates(p) + + return newhead.next + + + \ No newline at end of file diff --git "a/0083.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/0083-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 2.py" "b/0083.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/0083-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 2.py" new file mode 100644 index 0000000..a601135 --- /dev/null +++ "b/0083.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/0083-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240 2.py" @@ -0,0 +1,20 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def deleteDuplicates(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + if not head or not head.next: + return head + p = head + while p and p.val == head.val: + p = p.next + head.next = self.deleteDuplicates(p) + + return head \ No newline at end of file diff --git "a/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/0084-\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.py" "b/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/0084-\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.py" index 465d8f7..0a6ac26 100644 --- "a/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/0084-\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.py" +++ "b/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/0084-\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.py" @@ -1,15 +1,12 @@ -class Solution(object): - def largestRectangleArea(self, heights): - """ - :type heights: List[int] - :rtype: int - """ +class Solution: + def largestRectangleArea(self, heights: List[int]) -> int: heights = [0] + heights + [0] - stack = [] res = 0 - for i, num in enumerate(heights): - while stack and heights[stack[-1]] > num: - top = stack.pop() - res = max(res, (i - stack[-1] - 1) * heights[top]) + stack = [] + for i in range(len(heights)): + while stack and heights[stack[-1]] > heights[i]: + top = stack.pop() + res = max(res, (i - stack[-1] - 1) * heights[top]) + stack.append(i) return res \ No newline at end of file diff --git "a/0088.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/0088-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204 2.py" "b/0088.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/0088-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204 2.py" new file mode 100644 index 0000000..2f0a252 --- /dev/null +++ "b/0088.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/0088-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204 2.py" @@ -0,0 +1,12 @@ +class Solution(object): + def merge(self, nums1, m, nums2, n): + """ + :type nums1: List[int] + :type m: int + :type nums2: List[int] + :type n: int + :rtype: None Do not return anything, modify nums1 in-place instead. + """ + nums1[:] = (nums1[:m] + nums2) + nums1.sort() + # return sorted(nums) \ No newline at end of file diff --git "a/0091.\350\247\243\347\240\201\346\226\271\346\263\225/0091-\350\247\243\347\240\201\346\226\271\346\263\225.py" "b/0091.\350\247\243\347\240\201\346\226\271\346\263\225/0091-\350\247\243\347\240\201\346\226\271\346\263\225.py" new file mode 100644 index 0000000..61ea284 --- /dev/null +++ "b/0091.\350\247\243\347\240\201\346\226\271\346\263\225/0091-\350\247\243\347\240\201\346\226\271\346\263\225.py" @@ -0,0 +1,39 @@ +class Solution: + def numDecodings(self, s: str) -> int: + if not s or s[0] == "0": + return 0 + + dp = [0]*(len(s) + 1) # dp[i] represents ways of s[:i + 1] + dp[1] = dp[0] = 1 + + for i in range(2, len(s) + 1): + if s[i - 1] == "0": + if s[i - 2] in ["1", "2"]: + dp[i] = dp[i - 2] + else: + return 0 + elif s[i - 2] == "1" or (s[i - 2] == "2" and "1" <= s[i - 1] <= "6"): + dp[i] = dp[i - 1] + dp[i - 2] + else: + dp[i] = dp[i - 1] + return dp[-1] + + # dp = [0]*(len(s) + 1) + # if s[0] == '0': + # return 0 + # dp[0] = 1 + # dp[1] = 1 + # for i in range(2, len(s)+1): + # if s[i-1] == '0' : + # if s[i-2] in ['1', '2']: + # dp[i] = dp[i-2] + # else: + # return 0 + # elif s[i-2] == '1' or (s[i-2] == '2' and '1' <= s[i-1] <= '6'): + # dp[i] = dp[i-1] + dp[i-2] + # else: + # dp[i] = dp[i-1] + # return dp[-1] + + + diff --git "a/0092.\345\217\215\350\275\254\351\223\276\350\241\250II/0092-\345\217\215\350\275\254\351\223\276\350\241\250II 2.py" "b/0092.\345\217\215\350\275\254\351\223\276\350\241\250II/0092-\345\217\215\350\275\254\351\223\276\350\241\250II 2.py" new file mode 100644 index 0000000..4edf755 --- /dev/null +++ "b/0092.\345\217\215\350\275\254\351\223\276\350\241\250II/0092-\345\217\215\350\275\254\351\223\276\350\241\250II 2.py" @@ -0,0 +1,51 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def reverseBetween(self, head, m, n): + """ + :type head: ListNode + :type m: int + :type n: int + :rtype: ListNode + """ + newhead = ListNode(-1) + newhead.next = head + #ҵm - 1͵nڵ + cnt = 1 + slow = head + while cnt < m - 1: + print slow.val + slow = slow.next + cnt += 1 + cnt = 1 + fast = head + while cnt < n: + # print fast.val, n + fast = fast.next + cnt += 1 + # print fast.val, cnt + print slow.val, fast.val + tail = fast.next + fast.next = None + if m != 1: + slow.next = self.reverseLL(slow.next) + else: + newhead.next = self.reverseLL(slow) + p = slow + while p and p.next: + p = p.next + p.next = tail + return newhead.next + + def reverseLL(self, head): + if not head or not head.next: + return head + + p = self.reverseLL(head.next) + head.next.next = head + head.next = None + return p \ No newline at end of file diff --git "a/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0096-\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0096-\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" index b13ef7a..7ba4079 100644 --- "a/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0096-\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" +++ "b/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0096-\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -4,12 +4,11 @@ def numTrees(self, n): :type n: int :rtype: int """ - dic = {0:1, 1:1} - + res = [0] * (n+1) + res[0] = 1 + res[1] = 1 for i in range(2, n + 1): - cnt = 0 - for l in range(0, i): - cnt += dic[l] * dic[i - 1 - l] - dic[i] = cnt - - return dic[n] \ No newline at end of file + for j in range(i): + res[i] += res[j] * res[i-j-1] + + return res[n] \ No newline at end of file diff --git "a/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221 2.py" "b/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221 2.py" new file mode 100644 index 0000000..e3be02d --- /dev/null +++ "b/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221 2.py" @@ -0,0 +1,18 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: + if not p and not q: + return True + + if not p and q: + return False + + if p and not q: + return False + + return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) \ No newline at end of file diff --git "a/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221.py" "b/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221.py" index 981c048..e3be02d 100644 --- "a/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221.py" +++ "b/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221.py" @@ -1,19 +1,18 @@ # Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: + if not p and not q: + return True + + if not p and q: + return False + + if p and not q: + return False -class Solution(object): - def isSameTree(self, p, q): - """ - :type p: TreeNode - :type q: TreeNode - :rtype: bool - """ - if not p: - return not q - if not q: - return not p return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) \ No newline at end of file diff --git "a/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221/0101-\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.py" "b/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221/0101-\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.py" index 1ae0835..9caa639 100644 --- "a/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221/0101-\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.py" +++ "b/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221/0101-\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.py" @@ -11,11 +11,14 @@ def isSymmetric(self, root): :type root: TreeNode :rtype: bool """ - def helper(node1, node2): - if not node1: - return not node2 - if not node2: - return not node1 - return node1.val == node2.val and helper(node1.left, node2.right) and helper(node1.right, node2.left) - return helper(root, root) \ No newline at end of file + def isSame(node1, node2): + if not node1 and not node2: + return True + if not node1 and node2: + return False + if node1 and not node2: + return False + return node1.val == node2.val and isSame(node1.left, node2.right) and isSame(node1.right, node2.left) + + return isSame(root, root) \ No newline at end of file diff --git "a/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" "b/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..204ca41 --- /dev/null +++ "b/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + res.append(cur_level) + queue = next_queue + return res \ No newline at end of file diff --git "a/0103.\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206/0103-\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206.py" "b/0103.\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206/0103-\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..6e4ed82 --- /dev/null +++ "b/0103.\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206/0103-\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + queue = [root] + res = [] + flag = 1 + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + if flag: + res.append(cur_level) + flag = 0 + else: + flag = 1 + res.append(cur_level[::-1]) + queue = next_queue + return res \ No newline at end of file diff --git "a/0105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0105-\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221 2.py" "b/0105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0105-\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221 2.py" new file mode 100644 index 0000000..c9439fe --- /dev/null +++ "b/0105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0105-\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221 2.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def buildTree(self, preorder, inorder): + """ + :type preorder: List[int] + :type inorder: List[int] + :rtype: TreeNode + """ + if not preorder: + return None + + root = TreeNode(preorder[0]) + idx = inorder.index(root.val) + + root.left = self.buildTree(preorder[1:idx + 1], inorder[:idx]) + root.right = self.buildTree(preorder[idx + 1:], inorder[idx + 1:]) + return root \ No newline at end of file diff --git "a/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0106-\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221 2.py" "b/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0106-\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221 2.py" new file mode 100644 index 0000000..c67ac3d --- /dev/null +++ "b/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0106-\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221 2.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def buildTree(self, inorder, postorder): + """ + :type inorder: List[int] + :type postorder: List[int] + :rtype: TreeNode + """ + if not inorder: + return None + + root = TreeNode(postorder[-1]) + idx = inorder.index(root.val) + + root.left = self.buildTree(inorder[:idx], postorder[:idx]) + root.right = self.buildTree(inorder[idx + 1:], postorder[idx:-1]) + return root \ No newline at end of file diff --git "a/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II.py" "b/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II.py" new file mode 100644 index 0000000..e2a4c93 --- /dev/null +++ "b/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + res.append(cur_level) + queue = next_queue + return res[::-1] \ No newline at end of file diff --git "a/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0108-\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 2.py" "b/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0108-\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 2.py" new file mode 100644 index 0000000..da3ec6e --- /dev/null +++ "b/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0108-\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 2.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def sortedArrayToBST(self, nums): + """ + :type nums: List[int] + :rtype: TreeNode + """ + if not nums: + return None + rootIdx = len(nums)//2 + rootVal = nums[rootIdx] + + root = TreeNode(rootVal) + root.left = self.sortedArrayToBST(nums[:rootIdx]) + root.right = self.sortedArrayToBST(nums[rootIdx + 1:]) + + return root \ No newline at end of file diff --git "a/0109.\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0109-\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 2.py" "b/0109.\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0109-\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 2.py" new file mode 100644 index 0000000..2d2a03a --- /dev/null +++ "b/0109.\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0109-\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221 2.py" @@ -0,0 +1,39 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def sortedListToBST(self, head): + """ + :type head: ListNode + :rtype: TreeNode + """ + if not head: + return None + if not head.next: + return TreeNode(head.val) + slow, fast = head, head + pre = head + while fast and fast.next: + pre = slow + slow = slow.next + fast = fast.next.next + # print slow.val, fast.val, pre.val + pre.next = None + part1 = head + part2 = slow.next + + root = TreeNode(slow.val) + root.left = self.sortedListToBST(part1) + root.right = self.sortedListToBST(part2) + return root + \ No newline at end of file diff --git "a/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246/0111-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246 2.py" "b/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246/0111-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246 2.py" new file mode 100644 index 0000000..15bab42 --- /dev/null +++ "b/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246/0111-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246 2.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def minDepth(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if not root: + return 0 + if not root.left and not root.right: + return 1 + elif not root.left: + return 1 + self.minDepth(root.right) + elif not root.right: + return 1 + self.minDepth(root.left) + else: + return 1 + min(self.minDepth(root.left), self.minDepth(root.right)) \ No newline at end of file diff --git "a/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214 2.py" "b/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214 2.py" new file mode 100644 index 0000000..274aed8 --- /dev/null +++ "b/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214 2.py" @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def hasPathSum(self, root, sum): + """ + :type root: TreeNode + :type sum: int + :rtype: bool + """ + def dfs(node, s): + if not node: + return False + + s += node.val + if not node.left and not node.right: + return s == sum + return dfs(node.left, s) or dfs(node.right, s) + + return dfs(root, 0) \ No newline at end of file diff --git "a/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214.py" "b/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214.py" index ce9a472..274aed8 100644 --- "a/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214.py" +++ "b/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214.py" @@ -12,8 +12,13 @@ def hasPathSum(self, root, sum): :type sum: int :rtype: bool """ - if not root: - return False - if not root.left and not root.right: - return root.val == sum - return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val) \ No newline at end of file + def dfs(node, s): + if not node: + return False + + s += node.val + if not node.left and not node.right: + return s == sum + return dfs(node.left, s) or dfs(node.right, s) + + return dfs(root, 0) \ No newline at end of file diff --git "a/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II 2.py" "b/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II 2.py" new file mode 100644 index 0000000..79538f5 --- /dev/null +++ "b/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II 2.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def pathSum(self, root, sum): + """ + :type root: TreeNode + :type sum: int + :rtype: List[List[int]] + """ + res = [] + def dfs(node, path, s): + if not node: + return [] + s += node.val + if not node.left and not node.right: + if s == sum: + res.append(path + [node.val]) + + dfs(node.left, path + [node.val], s) + dfs(node.right, path + [node.val], s) + + dfs(root, [], 0) + return res \ No newline at end of file diff --git "a/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II.py" "b/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II.py" index b03107d..79538f5 100644 --- "a/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II.py" +++ "b/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II.py" @@ -6,25 +6,23 @@ # self.right = None class Solution(object): - def pathSum(self, root, s): + def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ res = [] - def dfs(node, path): + def dfs(node, path, s): if not node: - return + return [] + s += node.val + if not node.left and not node.right: + if s == sum: + res.append(path + [node.val]) - path += [node.val] - if not node.left and not node.right and sum(path) == s: - res.append(path[:]) - - dfs(node.left, path) - dfs(node.right, path) - - path.pop() - - dfs(root, []) + dfs(node.left, path + [node.val], s) + dfs(node.right, path + [node.val], s) + + dfs(root, [], 0) return res \ No newline at end of file diff --git "a/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250 2.py" "b/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250 2.py" new file mode 100644 index 0000000..c08d3f9 --- /dev/null +++ "b/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250 2.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution(object): + def flatten(self, root): + """ + :type root: TreeNode + :rtype: None Do not return anything, modify root in-place instead. + """ + if not root or (not root.left and not root.right): + return root + + self.flatten(root.left) + self.flatten(root.right) + + tmp = root.right + root.right = root.left + root.left = None + + while root and root.right: + root = root.right + + root.right = tmp \ No newline at end of file diff --git "a/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250.py" "b/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250.py" index 8858e81..c08d3f9 100644 --- "a/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250.py" +++ "b/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250.py" @@ -1,27 +1,26 @@ # Definition for a binary tree node. # class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution(object): def flatten(self, root): """ :type root: TreeNode :rtype: None Do not return anything, modify root in-place instead. """ - if not root: + if not root or (not root.left and not root.right): return root + self.flatten(root.left) self.flatten(root.right) - - tmp = root.right + + tmp = root.right root.right = root.left - root.left = None - - node = root - while node.right: - node = node.right - node.right = tmp - \ No newline at end of file + root.left = None + + while root and root.right: + root = root.right + + root.right = tmp \ No newline at end of file diff --git "a/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/0116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210 2.py" "b/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/0116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210 2.py" new file mode 100644 index 0000000..2ec9db4 --- /dev/null +++ "b/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/0116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210 2.py" @@ -0,0 +1,40 @@ +""" +# Definition for a Node. +class Node(object): + def __init__(self, val, left, right, next): + self.val = val + self.left = left + self.right = right + self.next = next +""" +class Solution(object): + def connect(self, root): + """ + :type root: Node + :rtype: Node + """ + if not root: + return + wait = None + if root.left and root.right: + root.left.next = root.right + wait = root.right + elif root.left: + wait = root.left + elif root.right: + wait = root.right + + p = root.next + while p: + if p.left: + wait.next = p.left + break + elif p.right: + wait.next = p.right + break + else: + p = p.next + + self.connect(root.left) + self.connect(root.right) + return root \ No newline at end of file diff --git "a/0117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/0117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II 2.py" "b/0117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/0117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II 2.py" new file mode 100644 index 0000000..3cf5013 --- /dev/null +++ "b/0117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/0117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II 2.py" @@ -0,0 +1,42 @@ +""" +# Definition for a Node. +class Node(object): + def __init__(self, val, left, right, next): + self.val = val + self.left = left + self.right = right + self.next = next +""" +class Solution(object): + def connect(self, root): + """ + :type root: Node + :rtype: Node + """ + if not root: + return + wait = None + if root.left and root.right: + root.left.next = root.right + wait = root.right + elif root.left: + wait = root.left + elif root.right: + wait = root.right + else: + return root + p = root.next + while p: + if p.left: + wait.next = p.left + break + elif p.right: + wait.next = p.right + break + else: + p = p.next + + + self.connect(root.right) + self.connect(root.left) + return root \ No newline at end of file diff --git "a/121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/121-\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.py" "b/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/0121-\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 2.py" similarity index 100% rename from "121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/121-\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.py" rename to "0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/0121-\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 2.py" diff --git "a/0125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/0125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262 2.py" "b/0125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/0125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262 2.py" new file mode 100644 index 0000000..ff3a1f2 --- /dev/null +++ "b/0125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/0125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262 2.py" @@ -0,0 +1,12 @@ +class Solution(object): + def isPalindrome(self, s): + """ + :type s: str + :rtype: bool + """ + tmp = "" + for char in s.lower(): + if char.isalpha() or char.isdigit(): + tmp += char + print tmp + return tmp == tmp[::-1] \ No newline at end of file diff --git "a/0129.\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214/0129-\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.py" "b/0129.\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214/0129-\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.py" new file mode 100644 index 0000000..8f580e1 --- /dev/null +++ "b/0129.\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214/0129-\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -0,0 +1,22 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: Optional[TreeNode]) -> int: + self.res = 0 + def dfs(node, cur_sum): + if not node: + return + + cur_sum = cur_sum * 10 + node.val + if not node.left and not node.right: + self.res += cur_sum + else: + dfs(node.left, cur_sum) + dfs(node.right, cur_sum) + + dfs(root, 0) + return self.res \ No newline at end of file diff --git "a/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276 2.py" "b/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276 2.py" new file mode 100644 index 0000000..ebffc5b --- /dev/null +++ "b/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276 2.py" @@ -0,0 +1,49 @@ +class Solution(object): + def cloneGraph(self, node): + """ + :type node: Node + :rtype: Node + """ + from collections import defaultdict, deque + neighborList = defaultdict(list) #ϣڼ¼ϽھӵĹϵ, keyÿϽ㣬valneighbors + mapping = dict() #ϣڼ¼ϽӦ½ڵ + + def bfs(queue): + if not queue: + return + newqueue = deque() + + while queue: + cur = queue.popleft() + mapping[cur] = Node(cur.val, []) #ΪÿϽ㴴һӦ½ڵ + for nei in cur.neighbors: + neighborList[cur].append(nei) #¼µǰϽھ + if nei not in visited: + visited.add(nei) + newqueue.append(nei) + bfs(newqueue) #BFSһ + + visited = {node} + q = deque() + q.append(node) + bfs(q) + visited = {node} + + def generate(queue): + while queue: + newqueue = [] + for node in queue: + if node: + if not neighborList[node]: #ûھ + return + + for nei in neighborList[node]: #ÿھ + mapping[node].neighbors.append(mapping[nei]) #µĽǡϽھӶӦġ½ + if nei not in visited: + visited.add(nei) + newqueue.append(nei) + queue = newqueue[:] + + generate([node]) + + return mapping[node] #Ӧ½ڵ \ No newline at end of file diff --git "a/0138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/0138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250 2.py" "b/0138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/0138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250 2.py" new file mode 100644 index 0000000..7e115f1 --- /dev/null +++ "b/0138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/0138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250 2.py" @@ -0,0 +1,31 @@ +""" +# Definition for a Node. +class Node(object): + def __init__(self, val, next, random): + self.val = val + self.next = next + self.random = random +""" +class Solution(object): + def copyRandomList(self, head): + """ + :type head: Node + :rtype: Node + """ + #133⣬ ͼ + mapping = dict() + + p = head + while p: + mapping[p] = Node(p.val, None, None) + p = p.next + + for key, val in mapping.items(): #keyϽ㣬 val½ڵ + if key.next: + val.next = mapping[key.next] + if key.random and key.random in mapping: + val.random = mapping[key.random] + + return mapping[head] if head else head + + \ No newline at end of file diff --git "a/0139.\345\215\225\350\257\215\346\213\206\345\210\206/0139-\345\215\225\350\257\215\346\213\206\345\210\206.py" "b/0139.\345\215\225\350\257\215\346\213\206\345\210\206/0139-\345\215\225\350\257\215\346\213\206\345\210\206.py" index 3adaf59..9ada5e0 100644 --- "a/0139.\345\215\225\350\257\215\346\213\206\345\210\206/0139-\345\215\225\350\257\215\346\213\206\345\210\206.py" +++ "b/0139.\345\215\225\350\257\215\346\213\206\345\210\206/0139-\345\215\225\350\257\215\346\213\206\345\210\206.py" @@ -1,17 +1,13 @@ -class Solution(object): - def wordBreak(self, s, wordDict): - """ - :type s: str - :type wordDict: List[str] - :rtype: bool - """ - dp = [0] - - for j in range(len(s) + 1): - for i in dp: - if s[i:j] in wordDict: - dp.append(j) +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + from collections import deque + wordDict = set(wordDict) + record = [0] + + for i in range(len(s) + 1): + for j in record: + if s[j:i] in wordDict: + record.append(i) break - # print dp - return dp[-1] == len(s) - \ No newline at end of file + # print (record) + return record[-1] == len(s) \ No newline at end of file diff --git "a/0140.\345\215\225\350\257\215\346\213\206\345\210\206II/0140-\345\215\225\350\257\215\346\213\206\345\210\206II.py" "b/0140.\345\215\225\350\257\215\346\213\206\345\210\206II/0140-\345\215\225\350\257\215\346\213\206\345\210\206II.py" new file mode 100644 index 0000000..74c4db8 --- /dev/null +++ "b/0140.\345\215\225\350\257\215\346\213\206\345\210\206II/0140-\345\215\225\350\257\215\346\213\206\345\210\206II.py" @@ -0,0 +1,22 @@ +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> List[str]: + + def helper(s, memo): + if s in memo: + return memo[s] + if not s: + return [] + res = [] + for word in wordDict: + if not s.startswith(word): + continue + if len(word) == len(s): + res.append(word) + else: + resultOfTheRest = helper(s[len(word):], memo) + for item in resultOfTheRest: + item = word + ' ' + item + res.append(item) + memo[s] = res + return res + return helper(s, {}) \ No newline at end of file diff --git "a/0141.\347\216\257\345\275\242\351\223\276\350\241\250/0141-\347\216\257\345\275\242\351\223\276\350\241\250 2.py" "b/0141.\347\216\257\345\275\242\351\223\276\350\241\250/0141-\347\216\257\345\275\242\351\223\276\350\241\250 2.py" new file mode 100644 index 0000000..568e4df --- /dev/null +++ "b/0141.\347\216\257\345\275\242\351\223\276\350\241\250/0141-\347\216\257\345\275\242\351\223\276\350\241\250 2.py" @@ -0,0 +1,24 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def hasCycle(self, head): + """ + :type head: ListNode + :rtype: bool + """ + if not head or not head.next: + return False + slow, fast = head, head + + while fast: + slow = slow.next + fast = fast.next + if fast: + fast = fast.next + if slow == fast: + return True + return False \ No newline at end of file diff --git "a/0142.\347\216\257\345\275\242\351\223\276\350\241\250II/0142-\347\216\257\345\275\242\351\223\276\350\241\250II 2.py" "b/0142.\347\216\257\345\275\242\351\223\276\350\241\250II/0142-\347\216\257\345\275\242\351\223\276\350\241\250II 2.py" new file mode 100644 index 0000000..3fbb11e --- /dev/null +++ "b/0142.\347\216\257\345\275\242\351\223\276\350\241\250II/0142-\347\216\257\345\275\242\351\223\276\350\241\250II 2.py" @@ -0,0 +1,34 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def detectCycle(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + if not head or not head.next: + return None + + slow, fast = head, head + while fast: + slow = slow.next + fast = fast.next + if fast: + fast = fast.next + + if slow == fast: + break + if slow != fast: + return None + + fast = head + while slow: + if slow == fast: + return slow + slow = slow.next + fast = fast.next + \ No newline at end of file diff --git "a/0146.LRU\347\274\223\345\255\230\346\234\272\345\210\266/0146-LRU\347\274\223\345\255\230\346\234\272\345\210\266.py" "b/0146.LRU\347\274\223\345\255\230\346\234\272\345\210\266/0146-LRU\347\274\223\345\255\230\346\234\272\345\210\266.py" index 265117e..c2c9139 100644 --- "a/0146.LRU\347\274\223\345\255\230\346\234\272\345\210\266/0146-LRU\347\274\223\345\255\230\346\234\272\345\210\266.py" +++ "b/0146.LRU\347\274\223\345\255\230\346\234\272\345\210\266/0146-LRU\347\274\223\345\255\230\346\234\272\345\210\266.py" @@ -1,25 +1,33 @@ -class LRUCache(object): +class DLLNode(object): + def __init__(self, key, val, pre, nxt): + self.key = key + self.val = val + self.pre = pre + self.nxt = next +class LRUCache(object): def __init__(self, capacity): """ :type capacity: int """ - from collections import OrderedDict self.capacity = capacity - self.record = OrderedDict() + self.size = 0 + self.head = DLLNode(-1, -1, None, None) + self.tail = DLLNode(-1, -1, self.head, None) + self.head.next = self.tail + self.dic = dict() def get(self, key): """ :type key: int :rtype: int """ - if key in self.record: - tmp = self.record.pop(key) - self.record[key] = tmp - return tmp - else: + if key not in self.dic: return -1 - + else: + value = self.dic[key].val + self.moveToHead(self.dic[key]) + return value def put(self, key, value): """ @@ -27,15 +35,40 @@ def put(self, key, value): :type value: int :rtype: None """ - if key in self.record: - self.record.pop(key) - else: - if self.capacity > 0: - self.capacity -= 1 + if key not in self.dic: + node = DLLNode(key, value, None, None) + self.dic[key] = node + if self.size < self.capacity: + self.size += 1 else: - self.record.popitem(last = False) - self.record[key] = value + self.removeLastElement() + self.insertToHead(node) + else: + self.dic[key].val = value + self.moveToHead(self.dic[key]) + + def insertToHead(self, node): + + pre_first = self.head.next + self.head.next = node + node.pre = self.head + pre_first.pre = node + node.next = pre_first + # print (node.key, node.pre.key, node.next.key) + + def removeLastElement(self): + pre_last = self.tail.pre + pre_second_last = pre_last.pre + pre_second_last.next = self.tail + self.tail.pre = pre_second_last + self.dic.pop(pre_last.key) + + def moveToHead(self, node): + node.pre.next = node.next + node.next.pre = node.pre + + self.insertToHead(node) # Your LRUCache object will be instantiated and called as such: diff --git "a/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/0150-\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274 2.py" "b/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/0150-\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274 2.py" new file mode 100644 index 0000000..d0ff710 --- /dev/null +++ "b/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/0150-\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274 2.py" @@ -0,0 +1,27 @@ +class Solution(object): + def evalRPN(self, tokens): + """ + :type tokens: List[str] + :rtype: int + """ + stack = [] + for i, x in enumerate(tokens): + # print stack + if x not in ["+", "-", "*", "/"]: + stack.append(int(x)) + else: + if x == "+": + tmp = stack[-1] + stack[-2] + stack = stack[:-2] + elif x == "-": + tmp = stack[-2] - stack[-1] + stack = stack[:-2] + elif x == "*": + tmp = stack[-1] * stack[-2] + stack = stack[:-2] + elif x == "/": + tmp = int( stack[-2] * 1.0 / stack[-1]) + stack = stack[:-2] + stack.append(tmp) + + return stack[0] \ No newline at end of file diff --git "a/0151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215/0151-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215.py" "b/0151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215/0151-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215.py" new file mode 100644 index 0000000..6b07361 --- /dev/null +++ "b/0151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215/0151-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215.py" @@ -0,0 +1,3 @@ +class Solution: + def reverseWords(self, s: str) -> str: + return " ".join(s.split()[::-1]) \ No newline at end of file diff --git "a/155.\346\234\200\345\260\217\346\240\210/155-\346\234\200\345\260\217\346\240\210.py" "b/0155.\346\234\200\345\260\217\346\240\210/0155-\346\234\200\345\260\217\346\240\210 2.py" similarity index 100% rename from "155.\346\234\200\345\260\217\346\240\210/155-\346\234\200\345\260\217\346\240\210.py" rename to "0155.\346\234\200\345\260\217\346\240\210/0155-\346\234\200\345\260\217\346\240\210 2.py" diff --git "a/0160.\347\233\270\344\272\244\351\223\276\350\241\250/0160-\347\233\270\344\272\244\351\223\276\350\241\250 2.py" "b/0160.\347\233\270\344\272\244\351\223\276\350\241\250/0160-\347\233\270\344\272\244\351\223\276\350\241\250 2.py" new file mode 100644 index 0000000..c3b380b --- /dev/null +++ "b/0160.\347\233\270\344\272\244\351\223\276\350\241\250/0160-\347\233\270\344\272\244\351\223\276\350\241\250 2.py" @@ -0,0 +1,37 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def getIntersectionNode(self, headA, headB): + """ + :type head1, head1: ListNode + :rtype: ListNode + """ + pa, pb = headA, headB + la, lb = 0, 0 + while pa: + la += 1 + pa = pa.next + + while pb: + lb += 1 + pb = pb.next + + if la < lb: + la, lb, headA, headB = lb, la, headB, headA + + n = la - lb + pa, pb = headA, headB + while n: + pa = pa.next + n -= 1 + + while pa: + if pa == pb: + return pa + pa = pa.next + pb = pb.next + return None \ No newline at end of file diff --git "a/169.\345\244\232\346\225\260\345\205\203\347\264\240/169-\345\244\232\346\225\260\345\205\203\347\264\240.py" "b/0169.\345\244\232\346\225\260\345\205\203\347\264\240/0169-\345\244\232\346\225\260\345\205\203\347\264\240 2.py" similarity index 100% rename from "169.\345\244\232\346\225\260\345\205\203\347\264\240/169-\345\244\232\346\225\260\345\205\203\347\264\240.py" rename to "0169.\345\244\232\346\225\260\345\205\203\347\264\240/0169-\345\244\232\346\225\260\345\205\203\347\264\240 2.py" diff --git "a/0174.\345\234\260\344\270\213\345\237\216\346\270\270\346\210\217/0174-\345\234\260\344\270\213\345\237\216\346\270\270\346\210\217.py" "b/0174.\345\234\260\344\270\213\345\237\216\346\270\270\346\210\217/0174-\345\234\260\344\270\213\345\237\216\346\270\270\346\210\217.py" new file mode 100644 index 0000000..4d7ab41 --- /dev/null +++ "b/0174.\345\234\260\344\270\213\345\237\216\346\270\270\346\210\217/0174-\345\234\260\344\270\213\345\237\216\346\270\270\346\210\217.py" @@ -0,0 +1,20 @@ +class Solution(object): + def calculateMinimumHP(self, dungeon): + """ + :type dungeon: List[List[int]] + :rtype: int + """ + if len(dungeon[0])==0: + return 1 + dungeon[-1][-1] = max(1,1-dungeon[-1][-1]) + # 边界 + for i in range(len(dungeon)-2,-1,-1): + dungeon[i][-1] = max(1,dungeon[i+1][-1]-dungeon[i][-1]) + for j in range(len(dungeon[0])-2,-1,-1): + dungeon[-1][j] = max(1,dungeon[-1][j+1]-dungeon[-1][j]) + + for i in range(len(dungeon)-2,-1,-1): + for j in range(len(dungeon[0])-2,-1,-1): + dungeon[i][j] = max(1,min(dungeon[i][j+1]-dungeon[i][j],dungeon[i+1][j]-dungeon[i][j])) + # print(dungeon) + return dungeon[0][0] \ No newline at end of file diff --git "a/0189.\350\275\256\350\275\254\346\225\260\347\273\204/0189-\350\275\256\350\275\254\346\225\260\347\273\204.py" "b/0189.\350\275\256\350\275\254\346\225\260\347\273\204/0189-\350\275\256\350\275\254\346\225\260\347\273\204.py" new file mode 100644 index 0000000..0ea399b --- /dev/null +++ "b/0189.\350\275\256\350\275\254\346\225\260\347\273\204/0189-\350\275\256\350\275\254\346\225\260\347\273\204.py" @@ -0,0 +1,15 @@ +class Solution: + def rotate(self, nums: List[int], k: int) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def reverse(left, right): + while left < right: + nums[left], nums[right] = nums[right], nums[left] + left += 1 + right -= 1 + k = k % len(nums) + reverse(0, len(nums) - 1) + reverse(0, k - 1) + reverse(k, len(nums) - 1) + \ No newline at end of file diff --git "a/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215 2.py" "b/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215 2.py" new file mode 100644 index 0000000..3d94e44 --- /dev/null +++ "b/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215 2.py" @@ -0,0 +1,14 @@ +class Solution(object): + def rob(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if len(nums) == 0: + return 0 + if len(nums) <= 2: + return max(nums) + nums[1] = max(nums[0], nums[1]) + for i in range(2, len(nums)): + nums[i] = max(nums[i - 2] + nums[i], nums[i - 1]) + return max(nums) if nums else 0 \ No newline at end of file diff --git "a/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215.py" "b/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215.py" index 515cd58..3d94e44 100644 --- "a/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215.py" +++ "b/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215.py" @@ -4,15 +4,11 @@ def rob(self, nums): :type nums: List[int] :rtype: int """ - # return 0 - if not nums: + if len(nums) == 0: return 0 - dp = [0 for _ in nums] - dp[0] = nums[0] - for i in range(1, len(nums)): - if i == 1: - dp[i] = max(dp[0], nums[i]) - else: - dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]) - return dp[-1] - \ No newline at end of file + if len(nums) <= 2: + return max(nums) + nums[1] = max(nums[0], nums[1]) + for i in range(2, len(nums)): + nums[i] = max(nums[i - 2] + nums[i], nums[i - 1]) + return max(nums) if nums else 0 \ No newline at end of file diff --git "a/0199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/0199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276 2.py" "b/0199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/0199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276 2.py" new file mode 100644 index 0000000..eb22eed --- /dev/null +++ "b/0199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/0199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276 2.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def rightSideView(self, root): + """ + :type root: TreeNode + :rtype: List[int] + """ + from collections import deque + if not root: + return [] + queue = deque([root]) + res = [] + while queue: + tmp = None + for _ in range(len(queue)): + cur = queue.popleft() + if cur: + tmp = cur.val + queue += [cur.left, cur.right] + if tmp: + res.append(tmp) + return res diff --git "a/0200.\345\262\233\345\261\277\346\225\260\351\207\217/0200-\345\262\233\345\261\277\346\225\260\351\207\217.py" "b/0200.\345\262\233\345\261\277\346\225\260\351\207\217/0200-\345\262\233\345\261\277\346\225\260\351\207\217.py" index 0da7d96..fc2adf0 100644 --- "a/0200.\345\262\233\345\261\277\346\225\260\351\207\217/0200-\345\262\233\345\261\277\346\225\260\351\207\217.py" +++ "b/0200.\345\262\233\345\261\277\346\225\260\351\207\217/0200-\345\262\233\345\261\277\346\225\260\351\207\217.py" @@ -1,29 +1,31 @@ -class Solution(object): - def numIslands(self, grid): - """ - :type grid: List[List[str]] - :rtype: int - """ +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + from collections import deque if not grid or not grid[0]: return 0 + m, n = len(grid), len(grid[0]) + dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] - def dfs(x, y): - for k in range(4): - xx = x + dx[k] - yy = y + dy[k] - - if 0 <= xx < m and 0 <= yy < n and grid[xx][yy] == "1": - grid[xx][yy] = "0" - dfs(xx, yy) - - + res = 0 for i in range(m): for j in range(n): if grid[i][j] == "1": grid[i][j] = "0" - dfs(i, j) res += 1 + + queue = deque([(i, j)]) + + while queue: + x0, y0 = queue.popleft() + for k in range(4): + x = x0 + dx[k] + y = y0 + dy[k] + + if 0 <= x < m and 0 <= y < n and grid[x][y] == "1": + grid[x][y] = "0" + queue.append((x, y)) + return res \ No newline at end of file diff --git "a/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260 2.py" "b/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260 2.py" new file mode 100644 index 0000000..b376561 --- /dev/null +++ "b/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260 2.py" @@ -0,0 +1,20 @@ +class Solution(object): + def isHappy(self, n): + """ + :type n: int + :rtype: bool + """ + def happy(num): + res = 0 + while num: + num, tmp = divmod(num, 10) + res += tmp ** 2 + return res + visited = set() + while n and n not in visited: + visited.add(n) + tmp = happy(n) + if tmp == 1: + return True + n = tmp + return False \ No newline at end of file diff --git "a/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262/0205-\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262 2.py" "b/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262/0205-\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262 2.py" new file mode 100644 index 0000000..101e467 --- /dev/null +++ "b/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262/0205-\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262 2.py" @@ -0,0 +1,17 @@ +class Solution(object): + def isIsomorphic(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + mapping = dict() + for i, char in enumerate(s): + if char in mapping: + if mapping[char] != t[i]: #һԶ + return False + else: + if t[i] in mapping.values(): #һ + return False + mapping[char] = t[i] + return True \ No newline at end of file diff --git "a/0207.\350\257\276\347\250\213\350\241\250/0207-\350\257\276\347\250\213\350\241\250.py" "b/0207.\350\257\276\347\250\213\350\241\250/0207-\350\257\276\347\250\213\350\241\250.py" index b535743..23b832c 100644 --- "a/0207.\350\257\276\347\250\213\350\241\250/0207-\350\257\276\347\250\213\350\241\250.py" +++ "b/0207.\350\257\276\347\250\213\350\241\250/0207-\350\257\276\347\250\213\350\241\250.py" @@ -1,35 +1,29 @@ -class Solution(object): - def canFinish(self, numCourses, prerequisites): - """ - :type numCourses: int - :type prerequisites: List[List[int]] - :rtype: bool - """ - from collections import deque - if not prerequisites: #ûǰÿεҪ - return True - - indegree = [0 for _ in range(numCourses)] - adj = [set() for _ in range(numCourses)] - - for end, start in prerequisites: - indegree[end] += 1 - adj[start].add(end) - - queue = deque() - for i, x in enumerate(indegree): - if not x: #Ϊ0Ľ - queue.append(i) - - cnt = 0 +class Solution: + def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: + # 1. find all node/course with indegree 0, let them enter a queue + from collections import defaultdict, deque + indegree = defaultdict(int) + children = defaultdict(set) + all_courses = set() + for cur, pre in prerequisites: + indegree[cur] += 1 + children[pre].add(cur) + all_courses.add(cur) + all_courses.add(pre) + + queue = deque([]) + for course in all_courses: + if indegree[course] == 0: + queue.append(course) + # 2. BFS, let course with indegree 0 leave a queue, and let its children with indegree 0 into the queue + studied_course = 0 while queue: cur = queue.popleft() - cnt += 1 #ǰcur - - for neighbor in adj[cur]: - indegree[neighbor] -= 1 - if not indegree[neighbor]: - queue.append(neighbor) - - return cnt == numCourses - \ No newline at end of file + + studied_course += 1 + for child in children[cur]: + indegree[child] -= 1 + if indegree[child] == 0: + queue.append(child) + + return studied_course == len(all_courses) \ No newline at end of file diff --git "a/0210.\350\257\276\347\250\213\350\241\250II/0210-\350\257\276\347\250\213\350\241\250II.py" "b/0210.\350\257\276\347\250\213\350\241\250II/0210-\350\257\276\347\250\213\350\241\250II.py" index 1ba3321..bdf7200 100644 --- "a/0210.\350\257\276\347\250\213\350\241\250II/0210-\350\257\276\347\250\213\350\241\250II.py" +++ "b/0210.\350\257\276\347\250\213\350\241\250II/0210-\350\257\276\347\250\213\350\241\250II.py" @@ -1,35 +1,33 @@ -class Solution(object): - def findOrder(self, numCourses, prerequisites): - """ - :type numCourses: int - :type prerequisites: List[List[int]] - :rtype: List[int] - """ - if not prerequisites: - return [i for i in range(numCourses)] - - indegree = [0 for _ in range(numCourses)] - adj = [set() for _ in range(numCourses)] - - for end, start in prerequisites: - indegree[end] += 1 - adj[start].add(end) - - from collections import deque - queue = deque() - - for i, x in enumerate(indegree): - if not x: - queue.append(i) - +class Solution: + def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: + from collections import defaultdict, deque + indegree = defaultdict(int) + children = defaultdict(set) + all_courses = set() + for cur, pre in prerequisites: + indegree[cur] += 1 + children[pre].add(cur) + all_courses.add(cur) + all_courses.add(pre) + + queue = deque([]) + for course in all_courses: + if indegree[course] == 0: + queue.append(course) + # 2. BFS, let course with indegree 0 leave a queue, and let its children with indegree 0 into the queue res = [] + while queue: cur = queue.popleft() + res.append(cur) - - for neighbor in adj[cur]: - indegree[neighbor] -= 1 - if indegree[neighbor] == 0: - queue.append(neighbor) - - return res if len(res) == numCourses else [] \ No newline at end of file + for child in children[cur]: + indegree[child] -= 1 + if indegree[child] == 0: + queue.append(child) + if len(res) != len(all_courses): + return [] + for course in range(numCourses): + if course not in all_courses: + res.append(course) + return res \ No newline at end of file diff --git "a/0213.\346\211\223\345\256\266\345\212\253\350\210\215II/0213-\346\211\223\345\256\266\345\212\253\350\210\215II 2.py" "b/0213.\346\211\223\345\256\266\345\212\253\350\210\215II/0213-\346\211\223\345\256\266\345\212\253\350\210\215II 2.py" new file mode 100644 index 0000000..ca4716c --- /dev/null +++ "b/0213.\346\211\223\345\256\266\345\212\253\350\210\215II/0213-\346\211\223\345\256\266\345\212\253\350\210\215II 2.py" @@ -0,0 +1,26 @@ +class Solution(object): + def rob(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if len(nums) == 1: + return nums[0] + return max(self.rob2(nums[1:]), self.rob2(nums[:-1])) + + def rob2(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + # return 0 + if not nums: + return 0 + dp = [0 for _ in nums] + dp[0] = nums[0] + for i in range(1, len(nums)): + if i == 1: + dp[i] = max(dp[0], nums[i]) + else: + dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]) + return dp[-1] \ No newline at end of file diff --git "a/0215.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240/0215-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240 2.py" "b/0215.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240/0215-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240 2.py" new file mode 100644 index 0000000..6c62036 --- /dev/null +++ "b/0215.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240/0215-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240 2.py" @@ -0,0 +1,30 @@ +class Solution(object): + def findKthLargest(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: int + """ + left, right, target = 0, len(nums) - 1, k - 1 + while True: + pos = self.partition(nums, left, right) + if pos == target: + return nums[pos] + elif pos > k: #Ҫ + right = pos - 1 + elif pos < k: #Ҫ + left = pos + 1 + + def partition(self, nums, left, right): + import random + k = random.randint(left, right) + pivot = nums[k] + nums[left], nums[k] = nums[k], nums[left] + index = left + + for i in range(left + 1, right + 1): + if nums[i] > pivot: + index += 1 + nums[i], nums[index] = nums[index], nums[i] + nums[left], nums[index] = nums[index], nums[left] + return index #ʱindexֵnums[index] Ҳֵnums[index]С \ No newline at end of file diff --git "a/0219.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II/0219-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II 2.py" "b/0219.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II/0219-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II 2.py" new file mode 100644 index 0000000..5d2aa7a --- /dev/null +++ "b/0219.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II/0219-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II 2.py" @@ -0,0 +1,14 @@ +class Solution(object): + def containsNearbyDuplicate(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: bool + """ + dic = dict() + for i, num in enumerate(nums): + if num in dic: + if i - dic[num] <= k: + return True + dic[num] = i + return False \ No newline at end of file diff --git "a/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260 2.py" "b/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260 2.py" new file mode 100644 index 0000000..0cf67da --- /dev/null +++ "b/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260 2.py" @@ -0,0 +1,18 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def countNodes(self, root): + """ + :type root: TreeNode + :rtype: int + """ + def dfs(node): + if not node: + return 0 + return 1 + dfs(node.left) + dfs(node.right) + return dfs(root) \ No newline at end of file diff --git "a/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.py" "b/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.py" index 58e9408..0cf67da 100644 --- "a/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.py" +++ "b/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.py" @@ -11,26 +11,8 @@ def countNodes(self, root): :type root: TreeNode :rtype: int """ - if not root: - return 0 - - self.leavesCnt = 0 - self.height = 0 - self.flag = 0 - def dfs(node, layer): - if not node or self.flag: - return - if not node.left and not node.right: - - self.height = max(self.height, layer) - if layer < self.height: - self.flag = 1 - else: - self.leavesCnt += 1 - return - dfs(node.left, layer + 1) - dfs(node.right, layer + 1) - - dfs(root, 0) - # print self.leavesCnt - return self.leavesCnt + sum([2 ** i for i in range(self.height)] ) \ No newline at end of file + def dfs(node): + if not node: + return 0 + return 1 + dfs(node.left) + dfs(node.right) + return dfs(root) \ No newline at end of file diff --git "a/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0226-\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" "b/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0226-\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" index 59cd84a..30d3e64 100644 --- "a/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0226-\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" +++ "b/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0226-\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" @@ -13,8 +13,9 @@ def invertTree(self, root): """ if not root: return root - left = root.left - right = root.right - root.left = self.invertTree(right) - root.right = self.invertTree(left) + left = self.invertTree(root.left) + right = self.invertTree(root.right) + + root.left = right + root.right = left return root \ No newline at end of file diff --git "a/0228.\346\261\207\346\200\273\345\214\272\351\227\264/0228-\346\261\207\346\200\273\345\214\272\351\227\264 2.py" "b/0228.\346\261\207\346\200\273\345\214\272\351\227\264/0228-\346\261\207\346\200\273\345\214\272\351\227\264 2.py" new file mode 100644 index 0000000..30c6d84 --- /dev/null +++ "b/0228.\346\261\207\346\200\273\345\214\272\351\227\264/0228-\346\261\207\346\200\273\345\214\272\351\227\264 2.py" @@ -0,0 +1,25 @@ +class Solution(object): + def summaryRanges(self, nums): + """ + :type nums: List[int] + :rtype: List[str] + """ + if not nums: + return nums + start, end = nums[0], nums[0] + res = [] + for i, num in enumerate(nums): + if i != 0: + if num == end + 1: + end += 1 + else: + if end - start == 0: + res.append(str(end)) + else: + res.append(str(start) + "->" + str(end)) + start, end = num, num + if end - start == 0: + res.append(str(end)) + else: + res.append(str(start) + "->" + str(end)) + return res \ No newline at end of file diff --git "a/0230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/0230-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240 2.py" "b/0230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/0230-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240 2.py" new file mode 100644 index 0000000..bff7e15 --- /dev/null +++ "b/0230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/0230-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240 2.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def kthSmallest(self, root, k): + """ + :type root: TreeNode + :type k: int + :rtype: int + """ + cnt = 0 + cur, stack = root, [] + while cur or stack: + if cur: + stack.append(cur) + cur = cur.left + else: + cnt += 1 + cur = stack.pop() + if cnt == k: + return cur.val + cur = cur.right \ No newline at end of file diff --git "a/0231.2\347\232\204\345\271\202/0231-2\347\232\204\345\271\202 2.py" "b/0231.2\347\232\204\345\271\202/0231-2\347\232\204\345\271\202 2.py" new file mode 100644 index 0000000..5faa721 --- /dev/null +++ "b/0231.2\347\232\204\345\271\202/0231-2\347\232\204\345\271\202 2.py" @@ -0,0 +1,7 @@ +class Solution(object): + def isPowerOfTwo(self, n): + """ + :type n: int + :rtype: bool + """ + return n > 0 and not (n & (n - 1)) \ No newline at end of file diff --git "a/0237.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/0237-\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271 2.py" "b/0237.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/0237-\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271 2.py" new file mode 100644 index 0000000..b0924b0 --- /dev/null +++ "b/0237.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/0237-\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271 2.py" @@ -0,0 +1,15 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def deleteNode(self, node): + """ + :type node: ListNode + :rtype: void Do not return anything, modify node in-place instead. + """ + node.val = node.next.val + node.next = node.next.next + \ No newline at end of file diff --git "a/0238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/0238-\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257 2.py" "b/0238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/0238-\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257 2.py" new file mode 100644 index 0000000..f51bc1f --- /dev/null +++ "b/0238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/0238-\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257 2.py" @@ -0,0 +1,14 @@ +class Solution(object): + def productExceptSelf(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + res = [1] * len(nums) + for i in range(1, len(nums)): + res[i] = res[i - 1] * nums[i - 1] + tmp = 1 + for i in range(len(nums) - 1, -1, -1): + res[i] *= tmp + tmp *= nums[i] + return res \ No newline at end of file diff --git "a/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/0242-\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215 2.py" "b/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/0242-\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215 2.py" new file mode 100644 index 0000000..469e1cd --- /dev/null +++ "b/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/0242-\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215 2.py" @@ -0,0 +1,8 @@ +class Solution(object): + def isAnagram(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + return sorted(s) == sorted(t) \ No newline at end of file diff --git "a/0256.\347\262\211\345\210\267\346\210\277\345\255\220/0256-\347\262\211\345\210\267\346\210\277\345\255\220.py" "b/0256.\347\262\211\345\210\267\346\210\277\345\255\220/0256-\347\262\211\345\210\267\346\210\277\345\255\220.py" index 8a86c0f..c9ec3f2 100644 --- "a/0256.\347\262\211\345\210\267\346\210\277\345\255\220/0256-\347\262\211\345\210\267\346\210\277\345\255\220.py" +++ "b/0256.\347\262\211\345\210\267\346\210\277\345\255\220/0256-\347\262\211\345\210\267\346\210\277\345\255\220.py" @@ -10,4 +10,4 @@ def minCost(self, costs): costs[i][0] += min(costs[i - 1][1], costs[i - 1][2]) costs[i][1] += min(costs[i - 1][0], costs[i - 1][2]) costs[i][2] += min(costs[i - 1][0], costs[i - 1][1]) - return min(costs[-1]) \ No newline at end of file + return min(costs[-1]) \ No newline at end of file diff --git "a/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204/0257-\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204 2.py" "b/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204/0257-\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204 2.py" new file mode 100644 index 0000000..23bc926 --- /dev/null +++ "b/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204/0257-\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204 2.py" @@ -0,0 +1,30 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def binaryTreePaths(self, root): + """ + :type root: TreeNode + :rtype: List[str] + """ + if not root: + return [] + self.res = [] + + def dfs(node, tmp): + if not node: + return + if not node.left and not node.right: + self.res.append(tmp + str(node.val)) + return + + dfs(node.left, tmp + str(node.val) + "->") + dfs(node.right, tmp + str(node.val) + "->") + + dfs(root, "") + return self.res + \ No newline at end of file diff --git "a/0258.\345\220\204\344\275\215\347\233\270\345\212\240/0258-\345\220\204\344\275\215\347\233\270\345\212\240 2.py" "b/0258.\345\220\204\344\275\215\347\233\270\345\212\240/0258-\345\220\204\344\275\215\347\233\270\345\212\240 2.py" new file mode 100644 index 0000000..cc52a39 --- /dev/null +++ "b/0258.\345\220\204\344\275\215\347\233\270\345\212\240/0258-\345\220\204\344\275\215\347\233\270\345\212\240 2.py" @@ -0,0 +1,7 @@ +class Solution(object): + def addDigits(self, num): + """ + :type num: int + :rtype: int + """ + return 1 + (num - 1) % 9 if num > 9 else num \ No newline at end of file diff --git "a/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II 2.py" "b/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II 2.py" new file mode 100644 index 0000000..533b3c1 --- /dev/null +++ "b/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II 2.py" @@ -0,0 +1,24 @@ +class Solution(object): + def nthUglyNumber(self, n): + """ + :type n: int + :rtype: int + """ + from heapq import * + l = [1] + heapify(l) + cnt = 1 + used = set([1]) + while cnt < n: + cur = heappop(l) + if cur * 2 not in used: + heappush(l, cur * 2) + used.add(cur * 2) + if cur * 3 not in used: + heappush(l, cur * 3) + used.add(cur * 3) + if cur * 5 not in used: + used.add(cur * 5) + heappush(l, cur * 5) + cnt += 1 + return heappop(l) \ No newline at end of file diff --git "a/0268.\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227/0268-\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227.py" "b/0268.\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227/0268-\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..92349c3 --- /dev/null +++ "b/0268.\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227/0268-\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,4 @@ +class Solution: + def missingNumber(self, nums: List[int]) -> int: + n = len(nums) + return (1 + n) * n // 2 - sum(nums) \ No newline at end of file diff --git "a/0269.\347\201\253\346\230\237\350\257\215\345\205\270/0269-\347\201\253\346\230\237\350\257\215\345\205\270.py" "b/0269.\347\201\253\346\230\237\350\257\215\345\205\270/0269-\347\201\253\346\230\237\350\257\215\345\205\270.py" new file mode 100644 index 0000000..c556b44 --- /dev/null +++ "b/0269.\347\201\253\346\230\237\350\257\215\345\205\270/0269-\347\201\253\346\230\237\350\257\215\345\205\270.py" @@ -0,0 +1,45 @@ +class Solution: + def alienOrder(self, words: List[str]) -> str: + from collections import defaultdict, deque + + indegree = defaultdict(int) + children = defaultdict(set) + all_chars = set() + for word in words: + for ch in word: + all_chars.add(ch) + + for i in range(1, len(words)): + # find the first different char in words[i] and words[i - 1] + j = 0 + while j < len(words[i]) and j < len(words[i - 1]): + if words[i][j] != words[i - 1][j]: + if words[i][j] not in children[words[i - 1][j]]: + indegree[words[i][j]] += 1 + children[words[i - 1][j]].add(words[i][j]) + break + if j == len(words[i]) - 1 and j < len(words[i - 1]) - 1: + return "" + j += 1 + + # t -> f, w -> e, r -> t, e -> r + queue = deque() + # print (indegree) + # print (children) + for ch in all_chars: + if indegree[ch] == 0: + queue.append(ch) + # print (queue) + res = "" + while queue: + cur = queue.popleft() + + res += cur + for child in children[cur]: + indegree[child] -= 1 + if indegree[child] == 0: + queue.append(child) + + return res if len(res) == len(all_chars) else "" + + \ No newline at end of file diff --git "a/0272.\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II/0272-\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II 2.py" "b/0272.\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II/0272-\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II 2.py" new file mode 100644 index 0000000..45f45cf --- /dev/null +++ "b/0272.\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II/0272-\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II 2.py" @@ -0,0 +1,34 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +from heapq import * +class Solution(object): + def closestKValues(self, root, target, k): + """ + :type root: TreeNode + :type target: float + :type k: int + :rtype: List[int] + """ + def inOrder(node): + if not node: + return [] + return inOrder(node.left) + [node.val] + inOrder(node.right) + + l = inOrder(root) + subs = [] + heapify(subs) + for num in l: + sub = abs(target - num) + heappush(subs, (-sub, num)) + if len(subs) > k: + heappop(subs) + + res = [] + for sub, num in subs: + res.append(num) + return res + \ No newline at end of file diff --git "a/0286.\345\242\231\344\270\216\351\227\250/0286-\345\242\231\344\270\216\351\227\250.py" "b/0286.\345\242\231\344\270\216\351\227\250/0286-\345\242\231\344\270\216\351\227\250.py" index 826b6d0..0c0707e 100644 --- "a/0286.\345\242\231\344\270\216\351\227\250/0286-\345\242\231\344\270\216\351\227\250.py" +++ "b/0286.\345\242\231\344\270\216\351\227\250/0286-\345\242\231\344\270\216\351\227\250.py" @@ -1,32 +1,29 @@ -class Solution(object): - def wallsAndGates(self, rooms): +class Solution: + def wallsAndGates(self, rooms: List[List[int]]) -> None: """ - :type rooms: List[List[int]] - :rtype: None Do not return anything, modify rooms in-place instead. + Do not return anything, modify rooms in-place instead. """ from collections import deque + if not rooms or not rooms[0]: return rooms - m, n = len(rooms), len(rooms[0]) - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] INF = 2147483647 - queue = deque() - def bfs(queue): - while queue: - pos = queue.popleft() - x0, y0 = pos - - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - if 0 <= x < m and 0 <= y < n and rooms[x][y] == INF: - rooms[x][y] = rooms[x0][y0] + 1 - queue.append((x, y)) - + + m, n = len(rooms), len(rooms[0]) + queue = deque() # (x_pos, y_pos, step from a gate) for i in range(m): for j in range(n): - if rooms[i][j] == 0: #ڴÿų - queue.append((i, j)) - bfs(queue) - return rooms \ No newline at end of file + if rooms[i][j] == 0: + queue.append((i, j, 0)) + + dx = [1, -1, 0, 0] + dy = [0, 0, 1, -1] + while queue: + x_pos, y_pos, step = queue.popleft() + for k in range(4): + x = x_pos + dx[k] + y = y_pos + dy[k] + + if 0 <= x < m and 0 <= y < n and rooms[x][y] == INF: + rooms[x][y] = step + 1 + queue.append((x, y, step + 1)) \ No newline at end of file diff --git "a/0289.\347\224\237\345\221\275\346\270\270\346\210\217/0289-\347\224\237\345\221\275\346\270\270\346\210\217 2.py" "b/0289.\347\224\237\345\221\275\346\270\270\346\210\217/0289-\347\224\237\345\221\275\346\270\270\346\210\217 2.py" new file mode 100644 index 0000000..4cf3306 --- /dev/null +++ "b/0289.\347\224\237\345\221\275\346\270\270\346\210\217/0289-\347\224\237\345\221\275\346\270\270\346\210\217 2.py" @@ -0,0 +1,37 @@ +class Solution(object): + def gameOfLife(self, board): + """ + :type board: List[List[int]] + :rtype: None Do not return anything, modify board in-place instead. + """ + if not board or not board[0]: + return + m, n = len(board), len(board[0]) + dx = [1, -1, 0, 0, 1, 1, -1, -1] + dy = [0, 0, 1, -1, 1, -1, 1, -1] + + def countLiveCells(x0, y0): + cnt = 0 + for k in range(8): + x = x0 + dx[k] + y = y0 + dy[k] + + if 0 <= x < m and 0 <= y < n and board[x][y] == 1: + cnt += 1 + + return cnt + + liveCellSet = set() + for i in range(m): + for j in range(n): + if board[i][j] == 1 and countLiveCells(i, j) in [2, 3]: + liveCellSet.add((i, j)) + elif board[i][j] == 0 and countLiveCells(i, j) == 3: + liveCellSet.add((i, j)) + + for i in range(m): + for j in range(n): + if (i, j) in liveCellSet: + board[i][j] = 1 + else: + board[i][j] = 0 \ No newline at end of file diff --git "a/0290.\345\215\225\350\257\215\350\247\204\345\276\213/0290-\345\215\225\350\257\215\350\247\204\345\276\213 2.py" "b/0290.\345\215\225\350\257\215\350\247\204\345\276\213/0290-\345\215\225\350\257\215\350\247\204\345\276\213 2.py" new file mode 100644 index 0000000..93a230e --- /dev/null +++ "b/0290.\345\215\225\350\257\215\350\247\204\345\276\213/0290-\345\215\225\350\257\215\350\247\204\345\276\213 2.py" @@ -0,0 +1,20 @@ +class Solution(object): + def wordPattern(self, pattern, s): + """ + :type pattern: str + :type str: str + :rtype: bool + """ + s = s.split(" ") + if len(pattern) != len(s): + return False + dic = {} + for i, char in enumerate(pattern): + if char not in dic: + dic[char] = s[i] + else: + if dic[char] != s[i]: + return False + + return len(set(dic.values())) == len(dic.values()) + \ No newline at end of file diff --git "a/0292.Nim\346\270\270\346\210\217/0292-Nim\346\270\270\346\210\217 2.py" "b/0292.Nim\346\270\270\346\210\217/0292-Nim\346\270\270\346\210\217 2.py" new file mode 100644 index 0000000..ef4b15d --- /dev/null +++ "b/0292.Nim\346\270\270\346\210\217/0292-Nim\346\270\270\346\210\217 2.py" @@ -0,0 +1,7 @@ +class Solution(object): + def canWinNim(self, n): + """ + :type n: int + :rtype: bool + """ + return n % 4 != 0 \ No newline at end of file diff --git "a/0297.\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/0297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226.py" "b/0297.\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/0297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226.py" new file mode 100644 index 0000000..c619231 --- /dev/null +++ "b/0297.\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/0297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226.py" @@ -0,0 +1,51 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Codec: + def serialize(self, root): + """Encodes a tree to a single string. + + :type root: TreeNode + :rtype: str + """ + if not root: + return "" + + s = "" + def preorder(node): + if not node: + return "#" + + return str(node.val) + "," + preorder(node.left) + "," +preorder(node.right) + s = preorder(root) + return s + + def deserialize(self, data): + """Decodes your encoded data to tree. + + :type data: str + :rtype: TreeNode + """ + if not data or data == "#": + return None + queue = deque(data.split(",")) + return self.helper(queue) + + def helper(self, queue): + cur = queue.popleft() + if cur == "#": + return None + root = TreeNode(cur) + root.left = self.helper(queue) + root.right = self.helper(queue) + + return root + +# Your Codec object will be instantiated and called as such: +# ser = Codec() +# deser = Codec() +# ans = deser.deserialize(ser.serialize(root)) \ No newline at end of file diff --git "a/0312.\346\210\263\346\260\224\347\220\203/0312-\346\210\263\346\260\224\347\220\203.py" "b/0312.\346\210\263\346\260\224\347\220\203/0312-\346\210\263\346\260\224\347\220\203.py" new file mode 100644 index 0000000..154c828 --- /dev/null +++ "b/0312.\346\210\263\346\260\224\347\220\203/0312-\346\210\263\346\260\224\347\220\203.py" @@ -0,0 +1,18 @@ +class Solution(object): + def maxCoins(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if not nums: + return 0 + + nums = [1] + nums + [1] + n = len(nums) + dp = [[0 for _ in range(n)] for _ in range(n)] + + for i in range(n-2, -1, -1): + for j in range(i+1, n): + for k in range(i+1, j): + dp[i][j] = max(dp[i][j], dp[i][k] + nums[i]*nums[k]*nums[j] + dp[k][j]) + return dp[0][-1] \ No newline at end of file diff --git "a/0313.\350\266\205\347\272\247\344\270\221\346\225\260/0313-\350\266\205\347\272\247\344\270\221\346\225\260.py" "b/0313.\350\266\205\347\272\247\344\270\221\346\225\260/0313-\350\266\205\347\272\247\344\270\221\346\225\260.py" new file mode 100644 index 0000000..6fb1177 --- /dev/null +++ "b/0313.\350\266\205\347\272\247\344\270\221\346\225\260/0313-\350\266\205\347\272\247\344\270\221\346\225\260.py" @@ -0,0 +1,16 @@ +class Solution: + def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int: + import heapq + min_heap = [1] # find the smallest ugly number + visited = set() + cnt = n + while cnt: + cur = heappop(min_heap) + cnt -= 1 + if not cnt: + return cur + for multiple in primes: + nxt = cur * multiple + if nxt not in visited: + visited.add(nxt) + heappush(min_heap, nxt) \ No newline at end of file diff --git "a/0314.\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\347\233\264\351\201\215\345\216\206/0314-\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\347\233\264\351\201\215\345\216\206.py" "b/0314.\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\347\233\264\351\201\215\345\216\206/0314-\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\347\233\264\351\201\215\345\216\206.py" new file mode 100644 index 0000000..4fc5d0e --- /dev/null +++ "b/0314.\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\347\233\264\351\201\215\345\216\206/0314-\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\347\233\264\351\201\215\345\216\206.py" @@ -0,0 +1,32 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def verticalOrder(self, root): + """ + :type root: TreeNode + :rtype: List[List[int]] + """ + if not root: + return None + + from collections import defaultdict, deque + + queue = deque([(0, root)]) + res = defaultdict(list) # key is column idx in the result, value is all elements that have that idx + while queue: + col_idx, node = queue.popleft() + + res[col_idx].append(node.val) + + if node.left: + queue.append((col_idx - 1, node.left)) + if node.right: + queue.append((col_idx + 1, node.right)) + + return [val for idx, val, in sorted(res.items(), key=lambda x: x[0])] + diff --git "a/0315.\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260/0315-\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260.py" "b/0315.\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260/0315-\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260.py" index 4abc475..663b691 100644 --- "a/0315.\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260/0315-\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260.py" +++ "b/0315.\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260/0315-\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260.py" @@ -11,7 +11,6 @@ def countSmaller(self, nums): :type nums: List[int] :rtype: List[int] """ - # 从左往右处理,右边是未知的,所以不好弄 # 从右往左处理,则对于每个数,其右边的数都已知 res = [0 for _ in nums] @@ -21,15 +20,13 @@ def countSmaller(self, nums): return res[::-1] def insert(self, root, val, i, res): - if not root: + if not root: #如果当前root为空 root = TreeNode(val) - elif root.val >= val: + elif root.val >= val: # 如果应该插入左子树 root.left_subtree_cnt += 1 root.left = self.insert(root.left, val, i, res) - elif root.val < val: - res[i] += root.left_subtree_cnt + 1 + elif root.val < val: # 如果应该插入右子树 + res[i] += root.left_subtree_cnt + 1 # 1 代表当前root root.right = self.insert(root.right, val, i, res) - return root - - \ No newline at end of file + return root \ No newline at end of file diff --git "a/0316.\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215/0316-\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215.py" "b/0316.\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215/0316-\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215.py" new file mode 100644 index 0000000..0b190ed --- /dev/null +++ "b/0316.\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215/0316-\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215.py" @@ -0,0 +1,13 @@ +class Solution: + def removeDuplicateLetters(self, s: str) -> str: + from collections import Counter + c = Counter(s) + stack = [] # incresing stack + for char in s: + c[char] -= 1 + if char in stack: + continue + while stack and stack[-1] > char and c[stack[-1]] >= 1: + stack.pop() + stack.append(char) + return "".join(stack) \ No newline at end of file diff --git "a/0325.\345\222\214\347\255\211\344\272\216k\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204\351\225\277\345\272\246/0325-\345\222\214\347\255\211\344\272\216k\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204\351\225\277\345\272\246.py" "b/0325.\345\222\214\347\255\211\344\272\216k\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204\351\225\277\345\272\246/0325-\345\222\214\347\255\211\344\272\216k\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204\351\225\277\345\272\246.py" new file mode 100644 index 0000000..8e8dfe6 --- /dev/null +++ "b/0325.\345\222\214\347\255\211\344\272\216k\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204\351\225\277\345\272\246/0325-\345\222\214\347\255\211\344\272\216k\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204\351\225\277\345\272\246.py" @@ -0,0 +1,18 @@ +class Solution: + def maxSubArrayLen(self, nums: List[int], k: int) -> int: + if not nums: + return 0 + dic = {} # key is prefix_sum and val is the smallest idx + dic[0] = -1 + res = 0 + for i, num in enumerate(nums): + if i > 0: + nums[i] += nums[i - 1] + if nums[i] not in dic: + dic[nums[i]] = i + target = nums[i] - k + # print(dic, nums[i]) + if target in dic: + res = max(res, i - dic[target]) + + return res \ No newline at end of file diff --git "a/0342.4\347\232\204\345\271\202/0342-4\347\232\204\345\271\202.py" "b/0342.4\347\232\204\345\271\202/0342-4\347\232\204\345\271\202.py" index d6d0e82..2a5e42a 100644 --- "a/0342.4\347\232\204\345\271\202/0342-4\347\232\204\345\271\202.py" +++ "b/0342.4\347\232\204\345\271\202/0342-4\347\232\204\345\271\202.py" @@ -4,8 +4,9 @@ def isPowerOfFour(self, num): :type num: int :rtype: bool """ - i = 1 - while(i= 4: + if num % 4: + return False + num //= 4 + + return num == 1 \ No newline at end of file diff --git "a/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/0344-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262 2.py" "b/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/0344-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262 2.py" new file mode 100644 index 0000000..ceb97e9 --- /dev/null +++ "b/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/0344-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262 2.py" @@ -0,0 +1,15 @@ +class Solution(object): + def reverseString(self, s): + """ + :type s: List[str] + :rtype: None Do not return anything, modify s in-place instead. + """ + if not s or len(s) == 0: + return s + left, right = 0, len(s) - 1 + while(left < right): + s[left], s[right] = s[right], s[left] + left += 1 + right -= 1 + return s + \ No newline at end of file diff --git "a/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/0347-\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.py" "b/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/0347-\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.py" index a0efe08..5188c16 100644 --- "a/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/0347-\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.py" +++ "b/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/0347-\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.py" @@ -1,8 +1,20 @@ -class Solution(object): - def topKFrequent(self, nums, k): - """ - :type nums: List[int] - :type k: int - :rtype: List[int] - """ - return [digit for digit, fre in collections.Counter(nums).most_common(k)] \ No newline at end of file +from heapq import * +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + from collections import Counter + + dic = Counter(nums) + bucket = [[] for _ in range(len(nums) + 1)] + for num, fre in dic.items(): + bucket[fre].append(num) + # print (bucket) + + res = [] + for i in range(len(bucket) - 1, -1, -1): + if bucket[i]: + if len(bucket[i]) <= k - len(res): + res += bucket[i] + else: + res += bucket[i][:k - len(res)] + break + return res diff --git "a/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/0349-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206 2.py" "b/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/0349-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206 2.py" new file mode 100644 index 0000000..530c02c --- /dev/null +++ "b/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/0349-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206 2.py" @@ -0,0 +1,8 @@ +class Solution(object): + def intersection(self, nums1, nums2): + """ + :type nums1: List[int] + :type nums2: List[int] + :rtype: List[int] + """ + return list(set(nums1) & set(nums2)) \ No newline at end of file diff --git "a/0350.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II/0350-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II.py" "b/0350.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II/0350-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II.py" index f2c8362..f84fde7 100644 --- "a/0350.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II/0350-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II.py" +++ "b/0350.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II/0350-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II.py" @@ -5,12 +5,11 @@ def intersect(self, nums1, nums2): :type nums2: List[int] :rtype: List[int] """ + from collections import Counter + dic1 = Counter(nums1) + dic2 = Counter(nums2) + res = [] - for i in range(0,len(nums1)): - if nums1[i] in nums2: - nums2.remove(nums1[i]) - # print nums1,nums2 - res.append(nums1[i]) - - # print nums1,nums2,res + for key, val in dic1.items(): + res += [key] * min(val, dic2[key]) return res \ No newline at end of file diff --git "a/0380.\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240/0380-\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.py" "b/0380.\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240/0380-\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.py" new file mode 100644 index 0000000..49b33ad --- /dev/null +++ "b/0380.\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240/0380-\345\270\270\346\225\260\346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.py" @@ -0,0 +1,67 @@ +import random +class RandomizedSet(object): + + def __init__(self): + """ + Initialize your data structure here. + """ + self.dic = dict() + self.l = [] + + def insert(self, val): + """ + Ins +from typing import ValuesViewerts a value to the set. Returns true if the set did not already contain the specified element. + :type val: int + :rtype: bool + """ + if val in self.dic: + return False + else: + self.l.append(val) + self.dic[val] = len(self.l) - 1 + return True + + + def remove(self, val): + """ + Removes a value from the set. Returns true if the set contained the specified element. + :type val: int + :rtype: bool + """ + + if val not in self.dic: + return False + else: + # get the index of the element to be delted + index = self.dic[val] + self.dic.pop(val) + + # swap the element with the last element + self.l[index], self.l[-1] = self.l[-1], self.l[index] + + if index != len(self.l) - 1: + # if swap happened, update the index of element that got swapped + self.dic[self.l[index]] = index + + self.l.pop() + + return True + + + + def getRandom(self): + """ + Get a random element from the set. + :rtype: int + """ + + return random.choice(self.l) + + + +# Your RandomizedSet object will be instantiated and called as such: +# obj = RandomizedSet() +# param_1 = obj.insert(val) +# param_2 = obj.remove(val) +# param_3 = obj.getRandom() \ No newline at end of file diff --git "a/0382.\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271/0382-\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271.py" "b/0382.\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271/0382-\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271.py" new file mode 100644 index 0000000..3b76846 --- /dev/null +++ "b/0382.\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271/0382-\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271.py" @@ -0,0 +1,22 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +import random +class Solution: + + def __init__(self, head: Optional[ListNode]): + self.list = [] + p = head + while p: + self.list.append(p.val) + p = p.next + + def getRandom(self) -> int: + return random.choice(self.list) + + +# Your Solution object will be instantiated and called as such: +# obj = Solution(head) +# param_1 = obj.getRandom() \ No newline at end of file diff --git "a/0383.\350\265\216\351\207\221\344\277\241/0383-\350\265\216\351\207\221\344\277\241 2.py" "b/0383.\350\265\216\351\207\221\344\277\241/0383-\350\265\216\351\207\221\344\277\241 2.py" new file mode 100644 index 0000000..8bbe897 --- /dev/null +++ "b/0383.\350\265\216\351\207\221\344\277\241/0383-\350\265\216\351\207\221\344\277\241 2.py" @@ -0,0 +1,19 @@ +class Solution(object): + def canConstruct(self, ransomNote, magazine): + """ + :type ransomNote: str + :type magazine: str + :rtype: bool + """ + + r = collections.Counter(ransomNote) + m = collections.Counter(magazine) + + for key in r: + if m.get(key, 0): + if m[key] < r[key]: + return False + else: + return False + + return True \ No newline at end of file diff --git "a/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227 2.py" "b/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227 2.py" new file mode 100644 index 0000000..715dff1 --- /dev/null +++ "b/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227 2.py" @@ -0,0 +1,14 @@ +class Solution(object): + def isSubsequence(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + i, j = 0, 0 + while i < len(s) and j < len(t): + if s[i] == t[j]: + i += 1 + j += 1 + # test + return i == len(s) \ No newline at end of file diff --git "a/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.py" "b/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.py" index fb0353b..715dff1 100644 --- "a/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.py" +++ "b/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.py" @@ -10,4 +10,5 @@ def isSubsequence(self, s, t): if s[i] == t[j]: i += 1 j += 1 - return i == len(s) \ No newline at end of file + # test + return i == len(s) \ No newline at end of file diff --git "a/0410.\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274/0410-\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274 2.py" "b/0410.\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274/0410-\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274 2.py" new file mode 100644 index 0000000..720f73f --- /dev/null +++ "b/0410.\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274/0410-\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274 2.py" @@ -0,0 +1,31 @@ +class Solution(object): + def splitArray(self, nums, m): + """ + :type nums: List[int] + :type m: int + :rtype: int + """ + # min(nums), sum(nums) + if len(nums) == m: + return max(nums) + lo, hi = max(nums), sum(nums) + while(lo < hi): + mid = (lo + hi) // 2 # + + temp, cnt = 0, 1 + for num in nums: + temp += num + # cnt += 1 + if temp > mid: + temp = num + cnt += 1 + # print temp, cnt, mid + + if cnt > m: #˼˵midӦüӴ + lo = mid + 1 + elif cnt <= m: + hi = mid + + + return lo + \ No newline at end of file diff --git a/0412.FizzBuzz/0412-FizzBuzz 2.py b/0412.FizzBuzz/0412-FizzBuzz 2.py new file mode 100644 index 0000000..e58e5ae --- /dev/null +++ b/0412.FizzBuzz/0412-FizzBuzz 2.py @@ -0,0 +1,17 @@ +class Solution(object): + def fizzBuzz(self, n): + """ + :type n: int + :rtype: List[str] + """ + res = [] + for i in range(1,n+1): + if i % 3 == 0 and i % 5 == 0: + res.append("FizzBuzz") + elif i%3 == 0: + res.append("Fizz") + elif i%5 == 0: + res.append("Buzz") + else: + res.append(str(i)) + return res \ No newline at end of file diff --git "a/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264/0435-\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.py" "b/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264/0435-\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.py" new file mode 100644 index 0000000..c05a4bf --- /dev/null +++ "b/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264/0435-\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.py" @@ -0,0 +1,18 @@ +class Solution(object): + def eraseOverlapIntervals(self, intervals): + """ + :type intervals: List[List[int]] + :rtype: int + """ + if not intervals or not intervals[0]: + return 0 + + intervals = sorted(intervals, key = lambda x:x[1]) + pre_end = intervals[0][1] + canAttendCnt = 1 + for i in range(1, len(intervals)): + if intervals[i][0] >= pre_end: # start later than previous one end + canAttendCnt += 1 + pre_end = intervals[i][1] + return len(intervals) - canAttendCnt + diff --git "a/0445.\344\270\244\346\225\260\347\233\270\345\212\240II/0445-\344\270\244\346\225\260\347\233\270\345\212\240II.py" "b/0445.\344\270\244\346\225\260\347\233\270\345\212\240II/0445-\344\270\244\346\225\260\347\233\270\345\212\240II.py" index 4fbe278..6ce5ec1 100644 --- "a/0445.\344\270\244\346\225\260\347\233\270\345\212\240II/0445-\344\270\244\346\225\260\347\233\270\345\212\240II.py" +++ "b/0445.\344\270\244\346\225\260\347\233\270\345\212\240II/0445-\344\270\244\346\225\260\347\233\270\345\212\240II.py" @@ -11,39 +11,68 @@ def addTwoNumbers(self, l1, l2): :type l2: ListNode :rtype: ListNode """ - s1, s2 = [], [] - while l1: - s1.append(l1.val) + def getLinkedListLength(l): + length = 0 + while l: + l = l.next + length += 1 + return length + + def printLL(node): + l = [] + while node: + l.append(node.val) + node = node.next + print(l) + + def reverseLL(node): + if not node or not node.next: + return node + p = reverseLL(node.next) + node.next.next = node + node.next = None + return p + + length1 = getLinkedListLength(l1) + length2 = getLinkedListLength(l2) + + if length1 < length2: + l1, l2 = l2, l1 + length1, length2 = length2, length1 + + dummy = ListNode(-1) + p = dummy + + n = length1 - length2 + while n: + p.next = ListNode(l1.val) l1 = l1.next - + p = p.next + n -= 1 + while l2: - s2.append(l2.val) + p.next = ListNode(l1.val + l2.val) + p = p.next + l1 = l1.next l2 = l2.next - + + + dummy.next = reverseLL(dummy.next) + + p = dummy.next carry = 0 - cur = ListNode(-1) - while s1 or s2: - value = carry - if s1: - value += s1.pop() - if s2: - value += s2.pop() - - carry = value > 9 - value %= 10 - - cur.val = value - pre = ListNode(-1) - pre.next = cur - cur = pre - - if carry: #ܵĽλ - pre.val = 1 - return pre - - return pre.next + pre = dummy + while p: + p.val += carry + if p.val > 9: + p.val -= 10 + carry = 1 + else: + carry = 0 + p = p.next + pre = pre.next - - + if carry: + pre.next = ListNode(1) - \ No newline at end of file + return reverseLL(dummy.next) diff --git "a/0451.\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217/0451-\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217 2.py" "b/0451.\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217/0451-\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217 2.py" new file mode 100644 index 0000000..880536d --- /dev/null +++ "b/0451.\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217/0451-\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217 2.py" @@ -0,0 +1,7 @@ +class Solution(object): + def frequencySort(self, s): + """ + :type s: str + :rtype: str + """ + return ''.join([char*freq for char,freq in collections.Counter(s).most_common()]) \ No newline at end of file diff --git "a/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203/0452-\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.py" "b/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203/0452-\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.py" index 64b8736..90c371e 100644 --- "a/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203/0452-\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.py" +++ "b/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203/0452-\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.py" @@ -7,14 +7,12 @@ def findMinArrowShots(self, points): if not points or not points[0]: return 0 - points.sort(key = lambda x:x[1]) - - arrow = points[0][1] - + points = sorted(points, key = lambda x: x[1]) res = 1 - for point in points: - if arrow < point[0]: + pre_end = points[0][1] + + for i in range(1, len(points)): + if points[i][0] > pre_end: res += 1 - arrow = point[1] - - return res \ No newline at end of file + pre_end = points[i][1] + return res diff --git "a/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/0459-\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.py" "b/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/0459-\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..a1e9d70 --- /dev/null +++ "b/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/0459-\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,10 @@ +class Solution(object): + def repeatedSubstringPattern(self, s): + """ + :type s: str + :rtype: bool + """ + for i in range(0, len(s) - 1): + if s[:i + 1] * (len(s) //(i + 1)) == s: + return True + return False \ No newline at end of file diff --git "a/496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" "b/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I 2.py" similarity index 100% rename from "496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" rename to "0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I 2.py" diff --git "a/0500.\351\224\256\347\233\230\350\241\214/0500-\351\224\256\347\233\230\350\241\214 2.py" "b/0500.\351\224\256\347\233\230\350\241\214/0500-\351\224\256\347\233\230\350\241\214 2.py" new file mode 100644 index 0000000..73d9e3b --- /dev/null +++ "b/0500.\351\224\256\347\233\230\350\241\214/0500-\351\224\256\347\233\230\350\241\214 2.py" @@ -0,0 +1,34 @@ +class Solution(object): + def findWords(self, words): + """ + :type words: List[str] + :rtype: List[str] + """ + # words = words.lower() + g1 = ["q","w","e","r","t","y","u","i","o","p"] + g2 = ["a","s","d","f","g","h","j","k","l"] + g3 = ["z","x","c","v","b","n","m"] + res = list() + for word in words: + temp = word.lower() + if temp[0] in g1: + flag = 1 + elif temp[0] in g2: + flag = 2 + elif temp[0] in g3: + flag = 3 + temp = set(temp) + for char in temp: + if flag == 1 and char not in g1: + flag = 0 + break + if flag == 2 and char not in g2: + flag = 0 + break + if flag == 3 and char not in g3: + flag = 0 + break + if flag: + res.append(word) + + return res \ No newline at end of file diff --git "a/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II/0503-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II 2.py" "b/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II/0503-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II 2.py" new file mode 100644 index 0000000..046530d --- /dev/null +++ "b/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II/0503-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II 2.py" @@ -0,0 +1,34 @@ +class Solution(object): + def nextGreaterElements(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + if not nums: + return nums + max_element = max(nums) + stack = list() + res = [-1 for i in range(len(nums))] + + for i, x in enumerate(nums): + + if not stack or nums[stack[-1]] >= x: + stack.append(i) + else: + # print stack, res + while(stack and nums[stack[-1]] < x): + res[stack[-1]] = x + stack.pop() + if x != max_element:#IJ÷ŽջֱĬ-1ͺ + stack.append(i) + + # print stack, res + if stack: #ҪѭԪ + for i, x in enumerate(nums): + if not stack: + break + while stack and x > nums[stack[-1]]: + res[stack[-1]] = x + stack.pop() + + return res \ No newline at end of file diff --git "a/0506.\347\233\270\345\257\271\345\220\215\346\254\241/0506-\347\233\270\345\257\271\345\220\215\346\254\241.py" "b/0506.\347\233\270\345\257\271\345\220\215\346\254\241/0506-\347\233\270\345\257\271\345\220\215\346\254\241.py" new file mode 100644 index 0000000..4dbfe25 --- /dev/null +++ "b/0506.\347\233\270\345\257\271\345\220\215\346\254\241/0506-\347\233\270\345\257\271\345\220\215\346\254\241.py" @@ -0,0 +1,21 @@ +class Solution: + def findRelativeRanks(self, score: List[int]) -> List[str]: + p = [[score[i], i] for i in range(len(score))] + + p.sort(key = lambda x: -x[0]) + + res = [0 for _ in score] + for index, pair in enumerate(p): + score, original_index = pair[0], pair[1] + + if index == 0: + val = "Gold Medal" + elif index == 1: + val = "Silver Medal" + elif index == 2: + val = "Bronze Medal" + else: + val = str(index + 1) + + res[original_index] = val + return res \ No newline at end of file diff --git "a/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274/0513-\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274 2.py" "b/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274/0513-\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274 2.py" new file mode 100644 index 0000000..c9fbab3 --- /dev/null +++ "b/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274/0513-\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274 2.py" @@ -0,0 +1,29 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def findBottomLeftValue(self, root): + """ + :type root: TreeNode + :rtype: int + """ + #层序遍历返回最后一层第一个 + from collections import deque + if not root: + return None + queue = deque([root]) + while queue: + res = [] + for _ in range(len(queue)): + cur = queue.popleft() + res.append(cur.val) + if cur.left: + queue.append(cur.left) + if cur.right: + queue.append(cur.right) + return res[0] + \ No newline at end of file diff --git "a/0515.\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274/0515-\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274 2.py" "b/0515.\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274/0515-\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274 2.py" new file mode 100644 index 0000000..4b14616 --- /dev/null +++ "b/0515.\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274/0515-\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274 2.py" @@ -0,0 +1,38 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def largestValues(self, root): + """ + :type root: TreeNode + :rtype: List[int] + """ + + if not root: + return [] + next_layer = [root] + result = [] + while(next_layer): + temp_next_layer = [] + layer_value = [] + for node in next_layer: + if not node: + continue + layer_value.append(node.val) + # print layer_value + if node.left: + temp_next_layer.append(node.left) + if node.right: + temp_next_layer.append(node.right) + + # print temp_next_layer[0].val + next_layer = temp_next_layer + result.append(max(layer_value)) + + + return result + \ No newline at end of file diff --git "a/0521.\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240/0521-\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240.py" "b/0521.\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240/0521-\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240.py" new file mode 100644 index 0000000..1304139 --- /dev/null +++ "b/0521.\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240/0521-\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240.py" @@ -0,0 +1,5 @@ +class Solution: + def findLUSlength(self, a: str, b: str) -> int: + if a == b: + return -1 + return max(len(a), len(b)) \ No newline at end of file diff --git "a/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/0538-\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221 2.py" "b/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/0538-\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221 2.py" new file mode 100644 index 0000000..4ce2343 --- /dev/null +++ "b/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/0538-\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221 2.py" @@ -0,0 +1,29 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def convertBST(self, root): + """ + :type root: TreeNode + :rtype: TreeNode + """ + #ı˳ + if not root: + return root + self.s = 0 + + def convert(node): + if not node: + return + + convert(node.right) + node.val += self.s + self.s = node.val + convert(node.left) + + convert(root) + return root \ No newline at end of file diff --git "a/0542.01\347\237\251\351\230\265/0542-01\347\237\251\351\230\265.py" "b/0542.01\347\237\251\351\230\265/0542-01\347\237\251\351\230\265.py" index b5e180f..d95fb0c 100644 --- "a/0542.01\347\237\251\351\230\265/0542-01\347\237\251\351\230\265.py" +++ "b/0542.01\347\237\251\351\230\265/0542-01\347\237\251\351\230\265.py" @@ -1,43 +1,35 @@ -from collections import deque class Solution(object): def updateMatrix(self, matrix): """ :type matrix: List[List[int]] :rtype: List[List[int]] """ + from collections import deque if not matrix or not matrix[0]: - return matrix + return matrix m, n = len(matrix), len(matrix[0]) - res = matrix[:] - - q = deque() - - for i in range(m): - for j in range(n): - if matrix[i][j] == 0: - q.append([[i, j], 0]) - dx = [1, -1, 0, 0] + dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] - visited = [[0 for _ in range(n + 1)] for _ in range(m + 1)] - - while q: - tmp, distance = q.popleft() - x0, y0 = tmp[0], tmp[1] - - if matrix[x0][y0] == 1: - res[x0][y0] = distance + res = [[0 for _ in range(n)] for _ in range (m)] + for i in range(m): + for j in range(n): + if matrix[i][j] == 1: + queue = deque([(i, j, 0)]) + visited = set((i, j)) + while queue: + x, y, dist = queue.popleft() - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] + if matrix[x][y] == 0: + res[i][j] = dist + break + else: + for k in range(4): + xx = x + dx[k] + yy = y + dy[k] - if 0 <= x < m and 0 <= y < n and visited[x][y] != 1: - q.append([[x, y], distance + 1]) - visited[x][y] = 1 - return res - - - - - \ No newline at end of file + if 0 <= xx < m and 0 <= yy < n and (xx, yy) not in visited: + visited.add((xx, yy)) + queue.append((xx, yy, dist + 1)) + + return res \ No newline at end of file diff --git "a/0557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/0557-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III 2.py" "b/0557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/0557-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III 2.py" new file mode 100644 index 0000000..baad7cf --- /dev/null +++ "b/0557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/0557-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III 2.py" @@ -0,0 +1,7 @@ +class Solution(object): + def reverseWords(self, s): + """ + :type s: str + :rtype: str + """ + return " ".join(word[::-1] for word in s.split(" ")) \ No newline at end of file diff --git "a/0589.N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0589-N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206 2.py" "b/0589.N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0589-N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206 2.py" new file mode 100644 index 0000000..d6ef697 --- /dev/null +++ "b/0589.N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0589-N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206 2.py" @@ -0,0 +1,20 @@ +""" +# Definition for a Node. +class Node(object): + def __init__(self, val=None, children=None): + self.val = val + self.children = children +""" +class Solution(object): + def preorder(self, root): + """ + :type root: Node + :rtype: List[int] + """ + if not root: + return [] + + res = [root.val] + for child in root.children: + res += self.preorder(child) + return res \ No newline at end of file diff --git "a/0590.N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0590-N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206 2.py" "b/0590.N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0590-N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206 2.py" new file mode 100644 index 0000000..bd35385 --- /dev/null +++ "b/0590.N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0590-N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206 2.py" @@ -0,0 +1,19 @@ +""" +# Definition for a Node. +class Node(object): + def __init__(self, val=None, children=None): + self.val = val + self.children = children +""" +class Solution(object): + def postorder(self, root): + """ + :type root: Node + :rtype: List[int] + """ + if not root: + return [] + res = [] + for child in root.children: + res += self.postorder(child) + return res + [root.val] \ No newline at end of file diff --git "a/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221/0617-\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221 2.py" "b/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221/0617-\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221 2.py" new file mode 100644 index 0000000..a708dc1 --- /dev/null +++ "b/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221/0617-\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221 2.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def mergeTrees(self, t1, t2): + """ + :type t1: TreeNode + :type t2: TreeNode + :rtype: TreeNode + """ + if not t1: + return t2 + if not t2: + return t1 + t1.val += t2.val + t1.left = self.mergeTrees(t1.left, t2.left) + t1.right = self.mergeTrees(t1.right, t2.right) + return t1 + \ No newline at end of file diff --git "a/0637.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274/0637-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274 2.py" "b/0637.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274/0637-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274 2.py" new file mode 100644 index 0000000..0f29e9f --- /dev/null +++ "b/0637.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274/0637-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274 2.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def averageOfLevels(self, root): + """ + :type root: TreeNode + :rtype: List[float] + """ + from collections import deque + if not root: + return [] + queue = deque([root]) + res = [] + while queue: + layer = [] + for _ in range(len(queue)): + cur = queue.popleft() + if cur: + layer.append(cur.val) + queue += [cur.left, cur.right] + if layer: + res.append(sum(layer) * 1.0 / len(layer)) + return res diff --git "a/0639.\350\247\243\347\240\201\346\226\271\346\263\2252/0639-\350\247\243\347\240\201\346\226\271\346\263\2252.py" "b/0639.\350\247\243\347\240\201\346\226\271\346\263\2252/0639-\350\247\243\347\240\201\346\226\271\346\263\2252.py" new file mode 100644 index 0000000..f965e09 --- /dev/null +++ "b/0639.\350\247\243\347\240\201\346\226\271\346\263\2252/0639-\350\247\243\347\240\201\346\226\271\346\263\2252.py" @@ -0,0 +1,44 @@ +class Solution: + def numDecodings(self, s: str) -> int: + if not s or s[0] == "0": + return 0 + + dp = [0]*(len(s) + 1) # dp[i] represents ways of s[:i + 1] + if s[0] == "*": + dp[0] = 1 + dp[1] = 9 + else: + dp[0] = dp[1] = 1 + + MOD = 10 ** 9 + 7 + + for i in range(2, len(s) + 1): + if s[i - 1] == "*": + if s[i - 2] == "1": + dp[i] = dp[i - 2] * 9 + elif s[i - 2] == "2": + dp[i] = dp[i - 2] * 6 + elif s[i - 2] == "*": + dp[i] = dp[i - 2] * 15 + dp[i] += 9 * dp[i - 1] + elif s[i - 1] == "0": + if s[i - 2] == "1" or s[i - 2] == "2": + dp[i] = dp[i - 2] + elif s[i - 2] == "*": + dp[i] = dp[i - 2] * 2 + else: + return 0 + else: + if s[i - 2] == "1" or (s[i - 2] == "2" and "1" <= s[i - 1] <= "6"): + dp[i] = dp[i - 2] + elif s[i - 2] == "*": + if "1" <= s[i - 1] <= "6": + dp[i] = dp[i - 2] * 2 + else: + dp[i] = dp[i - 2] + + dp[i] += dp[i - 1] + + dp[i] = dp[i] % MOD + # print (dp, i) + return dp[-1] \ No newline at end of file diff --git "a/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245BST/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245BST.py" "b/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245BST/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245BST.py" new file mode 100644 index 0000000..568998a --- /dev/null +++ "b/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245BST/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245BST.py" @@ -0,0 +1,30 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution(object): + def findTarget(self, root, k): + """ + :type root: TreeNode + :type k: int + :rtype: bool + """ + values = set() + + self.result = False + def inorderTraversal(node): + if not node: + return [] + if not self.result: + if k - node.val in values: + self.result = True + return + values.add(node.val) + + inorderTraversal(node.left) + inorderTraversal(node.right) + inorderTraversal(root) + return self.result + \ No newline at end of file diff --git "a/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..6005f43 --- /dev/null +++ "b/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def findTarget(self, root: Optional[TreeNode], k: int) -> bool: + visited = set() + self.res = False + + def inorder(node): + if not node: + return + + inorder(node.left) + + if k - node.val in visited: + self.res = True + visited.add(node.val) + + if not self.res: + inorder(node.right) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221/0654-\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221 2.py" "b/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221/0654-\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221 2.py" new file mode 100644 index 0000000..bacac72 --- /dev/null +++ "b/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221/0654-\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221 2.py" @@ -0,0 +1,20 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def constructMaximumBinaryTree(self, nums): + """ + :type nums: List[int] + :rtype: TreeNode + """ + if not nums: + return None + root = TreeNode(max(nums)) + root.left = self.constructMaximumBinaryTree(nums[:nums.index(root.val)]) + root.right = self.constructMaximumBinaryTree(nums[nums.index(root.val)+1:]) + return root + \ No newline at end of file diff --git "a/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271/0657-\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271 2.py" "b/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271/0657-\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271 2.py" new file mode 100644 index 0000000..8363e03 --- /dev/null +++ "b/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271/0657-\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271 2.py" @@ -0,0 +1,7 @@ +class Solution(object): + def judgeCircle(self, moves): + """ + :type moves: str + :rtype: bool + """ + return moves.count('U')==moves.count('D') and moves.count('R')==moves.count('L') \ No newline at end of file diff --git "a/0680.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II/0680-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II.py" "b/0680.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II/0680-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II.py" new file mode 100644 index 0000000..90330a6 --- /dev/null +++ "b/0680.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II/0680-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II.py" @@ -0,0 +1,20 @@ +class Solution: + def validPalindrome(self, s: str) -> bool: + delete = False + left, right = 0, len(s) - 1 + + while left < right: + if s[left] != s[right]: + if not delete: + delete = True + break + left += 1 + right -= 1 + + if delete: + s1 = s[:left] + s[left + 1:] + s2 = s[:right] + s[right + 1:] + return s1 == s1[::-1] or s2 == s2[::-1] + else: + return True + \ No newline at end of file diff --git "a/0682.\346\243\222\347\220\203\346\257\224\350\265\233/0682-\346\243\222\347\220\203\346\257\224\350\265\233 2.py" "b/0682.\346\243\222\347\220\203\346\257\224\350\265\233/0682-\346\243\222\347\220\203\346\257\224\350\265\233 2.py" new file mode 100644 index 0000000..7488253 --- /dev/null +++ "b/0682.\346\243\222\347\220\203\346\257\224\350\265\233/0682-\346\243\222\347\220\203\346\257\224\350\265\233 2.py" @@ -0,0 +1,19 @@ +class Solution(object): + def calPoints(self, ops): + """ + :type ops: List[str] + :rtype: int + """ + stack = list() + for i, op in enumerate(ops): + # print stack + if op == "+": + stack.append(stack[-1] + stack[-2]) + elif op == "D": + stack.append(2 * stack[-1]) + elif op == "C": + stack.pop() + else: + stack.append(int(op)) + + return sum(stack) \ No newline at end of file diff --git "a/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242/0700-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242 2.py" "b/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242/0700-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242 2.py" new file mode 100644 index 0000000..6576874 --- /dev/null +++ "b/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242/0700-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242 2.py" @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def searchBST(self, root, val): + """ + :type root: TreeNode + :type val: int + :rtype: TreeNode + """ + if not root or root.val == val: + return root + + if root.val > val: + return self.searchBST(root.left, val) + elif root.val < val: + return self.searchBST(root.right, val) \ No newline at end of file diff --git "a/0702.\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204/0702-\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204 2.py" "b/0702.\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204/0702-\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204 2.py" new file mode 100644 index 0000000..607236b --- /dev/null +++ "b/0702.\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204/0702-\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204 2.py" @@ -0,0 +1,19 @@ +class Solution(object): + def search(self, reader, target): + """ + :type reader: ArrayReader + :type target: int + :rtype: int + """ + i = 0 + tmp = reader.get(i) + if tmp > target: #targetȵһԪضСԲ + return -1 + while tmp != 2147483647: + if tmp == target: #ҵ + return i + if tmp > target: #ԪضtargetҲ + break + i += 1 + tmp = reader.get(i) + return -1 \ No newline at end of file diff --git "a/0703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240/0703-\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240.py" "b/0703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240/0703-\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240.py" new file mode 100644 index 0000000..4f478f0 --- /dev/null +++ "b/0703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240/0703-\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240.py" @@ -0,0 +1,22 @@ +import heapq +class KthLargest: + def __init__(self, k: int, nums: List[int]): + self.k = k + self.min_heap = [] + for num in nums: + if len(self.min_heap) < k: + heappush(self.min_heap, num) + else: + heappushpop(self.min_heap, num) + + def add(self, val: int) -> int: + if len(self.min_heap) < self.k: + heappush(self.min_heap, val) + else: + heappushpop(self.min_heap, val) + return self.min_heap[0] + + +# Your KthLargest object will be instantiated and called as such: +# obj = KthLargest(k, nums) +# param_1 = obj.add(val) \ No newline at end of file diff --git "a/0709.\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215/0709-\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215 2.py" "b/0709.\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215/0709-\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215 2.py" new file mode 100644 index 0000000..3ba9795 --- /dev/null +++ "b/0709.\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215/0709-\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215 2.py" @@ -0,0 +1,8 @@ +class Solution(object): + def toLowerCase(self, str): + """ + :type str: str + :rtype: str + """ + return str.lower() + \ No newline at end of file diff --git "a/0717.1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246/0717-1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246.py" "b/0717.1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246/0717-1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246.py" new file mode 100644 index 0000000..a5c61b3 --- /dev/null +++ "b/0717.1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246/0717-1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246.py" @@ -0,0 +1,15 @@ +class Solution: + def isOneBitCharacter(self, bits: List[int]) -> bool: + connected_with_prev = False + + for i, bit in enumerate(bits): + if connected_with_prev: + if i == len(bits) - 1: + return False + connected_with_prev = False + else: + if bit == 1: + connected_with_prev = True + else: + connected_with_prev = False + return True \ No newline at end of file diff --git "a/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204/0718-\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.py" "b/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204/0718-\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.py" new file mode 100644 index 0000000..90afcab --- /dev/null +++ "b/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204/0718-\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.py" @@ -0,0 +1,19 @@ +class Solution(object): + def findLength(self, A, B): + """ + :type A: List[int] + :type B: List[int] + :rtype: int + """ + # dp[i][j] represents A[:i + 1], B[:j + 1] longest common subarray length + dp = [[0 for _ in range(len(B) + 1)] for _ in range(len(A) + 1)] + + res = 0 + + for i in range(1, len(A) + 1): + for j in range(1, len(B) + 1): + if A[i - 1] == B[j - 1]: + dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1) + res = max(dp[i][j], res) + # print dp + return res \ No newline at end of file diff --git "a/0723.\347\262\211\347\242\216\347\263\226\346\236\234/0723-\347\262\211\347\242\216\347\263\226\346\236\234.py" "b/0723.\347\262\211\347\242\216\347\263\226\346\236\234/0723-\347\262\211\347\242\216\347\263\226\346\236\234.py" new file mode 100644 index 0000000..e624702 --- /dev/null +++ "b/0723.\347\262\211\347\242\216\347\263\226\346\236\234/0723-\347\262\211\347\242\216\347\263\226\346\236\234.py" @@ -0,0 +1,44 @@ +class Solution(object): + def candyCrush(self, board): + """ + :type board: List[List[int]] + :rtype: List[List[int]] + """ + if not board or not board[0]: + return None + + m, n = len(board), len(board[0]) + # 1. crush stage + + # flag all elements to be crushed + todo = 0 + for i in range(m): + for j in range(n - 2): + if board[i][j] and abs(board[i][j]) == abs(board[i][j + 1]) == abs(board[i][j + 2]): + board[i][j] = board[i][j + 1] = board[i][j + 2] = -abs(board[i][j]) + todo = 1 + + for j in range(n): + for i in range(m - 2): + if board[i][j] and abs(board[i][j]) == abs(board[i + 1][j]) == abs(board[i + 2][j]): + board[i][j] = board[i + 1][j] = board[i + 2][j] = -abs(board[i][j]) + todo = 1 + # print board, todo + # 2. gravity stage + for j in range(n): + lo, hi = m - 1, m - 1 + while hi >= 0: + while hi >= 0 and board[hi][j] < 0: + hi -= 1 + + if hi >= 0: + board[lo][j] = board[hi][j] + lo -= 1 + hi -= 1 + + while lo >= 0: + board[lo][j] = 0 + lo -= 1 + + # recursively call this function if more crush is necessary + return self.candyCrush(board) if todo else board \ No newline at end of file diff --git "a/0725.\345\210\206\351\232\224\351\223\276\350\241\250/0725-\345\210\206\351\232\224\351\223\276\350\241\250.py" "b/0725.\345\210\206\351\232\224\351\223\276\350\241\250/0725-\345\210\206\351\232\224\351\223\276\350\241\250.py" new file mode 100644 index 0000000..cc285bb --- /dev/null +++ "b/0725.\345\210\206\351\232\224\351\223\276\350\241\250/0725-\345\210\206\351\232\224\351\223\276\350\241\250.py" @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]: + dummy1 = ListNode(-1) + dummy2 = ListNode(-1) + + smaller_list = dummy1 + larger_list = dummy2 + p = head + while p: + if p.val < x: + new_node = ListNode(p.val) + smaller_list.next = new_node + smaller_list = smaller_list.next + else: + new_node = ListNode(p.val) + larger_list.next = new_node + larger_list = larger_list.next + p = p.next + + smaller_list.next = dummy2.next + return dummy1.next diff --git "a/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257/0746-\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257 2.py" "b/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257/0746-\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257 2.py" new file mode 100644 index 0000000..8d8e3ed --- /dev/null +++ "b/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257/0746-\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257 2.py" @@ -0,0 +1,18 @@ +class Solution(object): + def minCostClimbingStairs(self, cost): + """ + :type cost: List[int] + :rtype: int + """ + # dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i] + + l = len(cost) + + dp = [0 for _ in range(l + 1)] + + dp[0], dp[1] = cost[0], cost[1] + + for i in range(2, l): + dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i] + + return min(dp[l - 1], dp[l - 2]) \ No newline at end of file diff --git "a/0785.\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276/0785-\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.py" "b/0785.\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276/0785-\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.py" new file mode 100644 index 0000000..95cd2b5 --- /dev/null +++ "b/0785.\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276/0785-\345\210\244\346\226\255\344\272\214\345\210\206\345\233\276.py" @@ -0,0 +1,30 @@ +class Solution(object): + def isBipartite(self, graph): + """ + :type graph: List[List[int]] + :rtype: bool + """ + dic = {} + self.res = True + + def dfs(node): + if not self.res: + return + + for child in graph[node]: + # print node, child + if child in dic: + if dic[child] == dic[node]: + # print child, node, graph, dic + self.res = False + return + else: + dic[child] = not dic[node] + dfs(child) + + for node in range(len(graph)): + if node not in dic: + dic[node] = True + dfs(node) + return self.res + \ No newline at end of file diff --git "a/0786.\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/0786-\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260.py" "b/0786.\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/0786-\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260.py" new file mode 100644 index 0000000..bd10cee --- /dev/null +++ "b/0786.\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/0786-\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260.py" @@ -0,0 +1,10 @@ +class Solution: + def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]: + res = [] + + for i in range(len(arr)): + for j in range(i + 1, len(arr)): + res.append((arr[i], arr[j], arr[i] * 1.0 / arr[j])) + + res.sort(key = lambda x: x[2]) + return [res[k - 1][0], res[k - 1][1]] \ No newline at end of file diff --git "a/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204/0797-\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.py" "b/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204/0797-\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.py" index 7a1a2ea..7942ab6 100644 --- "a/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204/0797-\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.py" +++ "b/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204/0797-\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.py" @@ -4,19 +4,16 @@ def allPathsSourceTarget(self, graph): :type graph: List[List[int]] :rtype: List[List[int]] """ - res = list() n = len(graph) - def dfs(start, tmp): - if graph[start] == [] and start == n - 1:#ûһڵ - tmp += graph[start] - res.append(tmp[:]) + res = [] + def dfs(cur, path): + path.append(cur) + if cur == n - 1: + res.append(path[:]) return - - l = graph[start] - for node in l: - tmp.append(node) - dfs(node, tmp) - tmp.pop() - dfs(0, [0]) + for nxt in graph[cur]: + dfs(nxt, path[:]) + + dfs(0, []) return res \ No newline at end of file diff --git "a/0804.\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215/0804-\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215 2.py" "b/0804.\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215/0804-\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215 2.py" new file mode 100644 index 0000000..b6fab5d --- /dev/null +++ "b/0804.\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215/0804-\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215 2.py" @@ -0,0 +1,16 @@ +class Solution(object): + def uniqueMorseRepresentations(self, words): + """ + :type words: List[str] + :rtype: int + """ + moore = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] + queue = set() + + for word in words: + temp = "" + for char in word: + temp += moore[ord(str(char)) - ord("a")] + queue.add(temp) + + return len(queue) \ No newline at end of file diff --git "a/0807.\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277/0807-\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277 2.py" "b/0807.\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277/0807-\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277 2.py" new file mode 100644 index 0000000..3632801 --- /dev/null +++ "b/0807.\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277/0807-\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277 2.py" @@ -0,0 +1,22 @@ +class Solution(object): + def maxIncreaseKeepingSkyline(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + length = len(grid) + if length == 0: + return 0 + res = 0 + + for i in range(0, length): + for j in range(0, length): + rowMax = 0 + colomnMax = 0 + for t in range(0,length): + rowMax = max(grid[i][t],rowMax) + colomnMax = max(grid[t][j],colomnMax) + print rowMax, colomnMax + res += min(colomnMax,rowMax ) - grid[i][j] + return res + \ No newline at end of file diff --git "a/0811.\345\255\220\345\237\237\345\220\215\350\256\277\351\227\256\350\256\241\346\225\260/0811-\345\255\220\345\237\237\345\220\215\350\256\277\351\227\256\350\256\241\346\225\260.py" "b/0811.\345\255\220\345\237\237\345\220\215\350\256\277\351\227\256\350\256\241\346\225\260/0811-\345\255\220\345\237\237\345\220\215\350\256\277\351\227\256\350\256\241\346\225\260.py" index 6b74440..d4063c2 100644 --- "a/0811.\345\255\220\345\237\237\345\220\215\350\256\277\351\227\256\350\256\241\346\225\260/0811-\345\255\220\345\237\237\345\220\215\350\256\277\351\227\256\350\256\241\346\225\260.py" +++ "b/0811.\345\255\220\345\237\237\345\220\215\350\256\277\351\227\256\350\256\241\346\225\260/0811-\345\255\220\345\237\237\345\220\215\350\256\277\351\227\256\350\256\241\346\225\260.py" @@ -4,23 +4,19 @@ def subdomainVisits(self, cpdomains): :type cpdomains: List[str] :rtype: List[str] """ - resList = [] - resMap = {} - for s in cpdomains: - count, domains = s.split(' ') - n = domains.count('.') - tmp = domains - for i in range(n+1): - if resMap.has_key(tmp): - resMap[tmp] = resMap[tmp] + int(count) - else: - resMap[tmp] = int(count) - index = tmp.find('.') + 1 - if index == -1: - break - else: - tmp = tmp[index:] - # for key, value in resMap.items(): - # resList.append(str(value) + ' ' + key); - # return resList - return [str(resMap[key]) + ' ' + key for key in resMap] \ No newline at end of file + from collections import defaultdict + dic = defaultdict(int) + + for pair in cpdomains: + splitted_pair = pair.split() + cnt, domain = splitted_pair[0], splitted_pair[1] + cnt = int(cnt) + + for i in range(len(domain)): + if not i or domain[i] == ".": + dic[domain[i:].lstrip(".")] += cnt + + res = [] + for domain, frequency in dic.items(): + res.append(" ".join([str(frequency), domain])) + return res \ No newline at end of file diff --git "a/0832.\347\277\273\350\275\254\345\233\276\345\203\217/0832-\347\277\273\350\275\254\345\233\276\345\203\217 2.py" "b/0832.\347\277\273\350\275\254\345\233\276\345\203\217/0832-\347\277\273\350\275\254\345\233\276\345\203\217 2.py" new file mode 100644 index 0000000..9de6750 --- /dev/null +++ "b/0832.\347\277\273\350\275\254\345\233\276\345\203\217/0832-\347\277\273\350\275\254\345\233\276\345\203\217 2.py" @@ -0,0 +1,11 @@ +class Solution(object): + def flipAndInvertImage(self, A): + """ + :type A: List[List[int]] + :rtype: List[List[int]] + """ + res = list() + for a in A: + a.reverse() + res.append((1 - i) for i in a) + return res \ No newline at end of file diff --git "a/0852.\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225/0852-\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225 2.py" "b/0852.\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225/0852-\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225 2.py" new file mode 100644 index 0000000..8c8b2ba --- /dev/null +++ "b/0852.\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225/0852-\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225 2.py" @@ -0,0 +1,18 @@ +class Solution(object): + def peakIndexInMountainArray(self, A): + """ + :type A: List[int] + :rtype: int + """ + left = 0 + right = len(A) - 1 + while( left <= right): + mid = left + (right - left) / 2 + if A[mid - 1] < A[mid] < A[mid + 1]: + left = mid + 1 + elif A[mid - 1] > A[mid] > A[mid + 1]: + right = mid -1 + else: + break + print mid + return mid \ No newline at end of file diff --git "a/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266/0860-\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.py" "b/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266/0860-\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.py" index a0b77f2..15c9101 100644 --- "a/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266/0860-\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.py" +++ "b/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266/0860-\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.py" @@ -4,18 +4,22 @@ def lemonadeChange(self, bills): :type bills: List[int] :rtype: bool """ - five = ten = 0 - for num in bills: - if num == 5: - five += 1 - elif num == 10 and five: - ten += 1 - five -= 1 - elif num == 20 and five and ten: - five -= 1 - ten -= 1 - elif num == 20 and five >= 3: - five -= 3 + dic = {5:0, 10:0} + + for bill in bills: + if bill == 5: + dic[5] += 1 + elif bill == 10: + if dic[5] < 1: + return False + dic[5] -= 1 + dic[10] += 1 else: - return False - return True \ No newline at end of file + if dic[10] and dic[5]: + dic[10] -= 1 + dic[5] -= 1 + elif dic[5] >= 3: + dic[5] -= 3 + else: + return False + return True diff --git "a/0876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/0876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" "b/0876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/0876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" new file mode 100644 index 0000000..39ec0a9 --- /dev/null +++ "b/0876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/0876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" @@ -0,0 +1,13 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: + slow, fast = head, head + while fast and fast.next: + fast = fast.next.next + slow = slow.next + + return slow \ No newline at end of file diff --git "a/0881.\346\225\221\347\224\237\350\211\207/0881-\346\225\221\347\224\237\350\211\207.py" "b/0881.\346\225\221\347\224\237\350\211\207/0881-\346\225\221\347\224\237\350\211\207.py" new file mode 100644 index 0000000..d0b4d3a --- /dev/null +++ "b/0881.\346\225\221\347\224\237\350\211\207/0881-\346\225\221\347\224\237\350\211\207.py" @@ -0,0 +1,19 @@ +class Solution(object): + def numRescueBoats(self, people, limit): + """ + :type people: List[int] + :type limit: int + :rtype: int + """ + people.sort() + left, right = 0, len(people) - 1 + boat_count = 0 + while left <= right: + # print (people[left], people[right], boat_count) + if people[left] + people[right] <= limit: + left += 1 + right -= 1 + else: + right -= 1 + boat_count += 1 + return boat_count diff --git "a/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221.py" "b/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..c2d6aa1 --- /dev/null +++ "b/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def increasingBST(self, root: TreeNode) -> TreeNode: + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + inorder_list = inorder(root) + dummy = TreeNode(-1) + p = dummy + for val in inorder_list: + p.right = TreeNode(val) + p = p.right + + return dummy.right \ No newline at end of file diff --git "a/0912.\346\216\222\345\272\217\346\225\260\347\273\204/0912-\346\216\222\345\272\217\346\225\260\347\273\204 2.py" "b/0912.\346\216\222\345\272\217\346\225\260\347\273\204/0912-\346\216\222\345\272\217\346\225\260\347\273\204 2.py" new file mode 100644 index 0000000..3e624db --- /dev/null +++ "b/0912.\346\216\222\345\272\217\346\225\260\347\273\204/0912-\346\216\222\345\272\217\346\225\260\347\273\204 2.py" @@ -0,0 +1,8 @@ +class Solution(object): + def sortArray(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + return sorted(nums) + \ No newline at end of file diff --git "a/0938.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214/0938-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214 2.py" "b/0938.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214/0938-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214 2.py" new file mode 100644 index 0000000..c707469 --- /dev/null +++ "b/0938.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214/0938-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214 2.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def rangeSumBST(self, root, L, R): + """ + :type root: TreeNode + :type L: int + :type R: int + :rtype: int + """ + res = 0 + + if not root: + return 0 + if L <= root.val <= R: + res += root.val + if root.val < R: + res += self.rangeSumBST(root.right, L, R) + if root.val > L: + res += self.rangeSumBST(root.left, L, R) + + return res + \ No newline at end of file diff --git "a/0973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/0973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271 2.py" "b/0973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/0973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271 2.py" new file mode 100644 index 0000000..bc478a3 --- /dev/null +++ "b/0973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/0973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271 2.py" @@ -0,0 +1,9 @@ +class Solution(object): + def kClosest(self, points, K): + """ + :type points: List[List[int]] + :type K: int + :rtype: List[List[int]] + """ + return sorted(points, key = lambda x:x[0] **2 + x[1] ** 2)[:K] + \ No newline at end of file diff --git "a/0987.\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\345\272\217\351\201\215\345\216\206/0987-\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\345\272\217\351\201\215\345\216\206.py" "b/0987.\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\345\272\217\351\201\215\345\216\206/0987-\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..d3790e7 --- /dev/null +++ "b/0987.\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\345\272\217\351\201\215\345\216\206/0987-\344\272\214\345\217\211\346\240\221\347\232\204\345\236\202\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def verticalTraversal(self, root): + """ + :type root: TreeNode + :rtype: List[List[int]] + """ + from collections import defaultdict + dic = defaultdict(list) + def dfs(root, x, y): + if root: + dic[x].append((y, root.val)) + dfs(root.left, x - 1, y + 1) + dfs(root.right, x + 1, y + 1) + + dfs(root, 0, 0) + res = [] + for k in sorted(dic.keys()): + x = [pair[1] for pair in sorted(dic[k])] + res.append(x) + + return res \ No newline at end of file diff --git "a/1.\344\270\244\346\225\260\344\271\213\345\222\214/1-\344\270\244\346\225\260\344\271\213\345\222\214.py" "b/1.\344\270\244\346\225\260\344\271\213\345\222\214/1-\344\270\244\346\225\260\344\271\213\345\222\214.py" deleted file mode 100644 index 8b92177..0000000 --- "a/1.\344\270\244\346\225\260\344\271\213\345\222\214/1-\344\270\244\346\225\260\344\271\213\345\222\214.py" +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def twoSum(self, nums, target): - """ - :type nums: List[int] - :type target: int - :rtype: List[int] - """ - dic = {} - for i, num in enumerate(nums): - if target - num in dic: - return [dic[target - num], i] - dic[num] = i \ No newline at end of file diff --git "a/1008.\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/1008-\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/1008.\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/1008-\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..94069fb --- /dev/null +++ "b/1008.\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/1008-\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,27 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]: + if not preorder: + return None + + root = TreeNode(preorder[0]) + hasRight = False + for i, val in enumerate(preorder): + if val > root.val: + right_index = i + hasRight = True + break + + + if hasRight: + root.left = self.bstFromPreorder(preorder[1:right_index]) + root.right = self.bstFromPreorder(preorder[right_index:]) + else: + root.left = self.bstFromPreorder(preorder[1:]) + return root + diff --git "a/1011.\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233/1011-\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233 2.py" "b/1011.\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233/1011-\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233 2.py" new file mode 100644 index 0000000..f620fd4 --- /dev/null +++ "b/1011.\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233/1011-\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233 2.py" @@ -0,0 +1,27 @@ +class Solution(object): + def shipWithinDays(self, weights, D): + """ + :type weights: List[int] + :type D: int + :rtype: int + """ + lo, hi = max(weights), sum(weights) + while(lo <= hi): + mid = (lo + hi) // 2 # mid Ϊǰ͵capacity + + #------Ϊģ˻Ḷ́tempʾǰصdayʾõ------- + temp = 0 + day = 1 + for weight in weights: + temp += weight + if temp > mid:# ǰ˲ + day += 1 + temp = weight + #------Ϊģ˻Ĺ----------------- + + if day > D: # ǰcapacity̫СˣҪܼʱ + lo = mid + 1 + elif day <= D: + hi = mid - 1 + + return lo \ No newline at end of file diff --git "a/1016.\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262/1016-\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262 2.py" "b/1016.\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262/1016-\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262 2.py" new file mode 100644 index 0000000..f64c8c7 --- /dev/null +++ "b/1016.\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262/1016-\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262 2.py" @@ -0,0 +1,13 @@ +class Solution(object): + def queryString(self, S, N): + """ + :type S: str + :type N: int + :rtype: bool + """ + for i in range(1, N + 1): + # print str(bin(i)[2:]) + if str(bin(i)[2:]) not in S: + return False + + return True \ No newline at end of file diff --git "a/1017.\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242/1017-\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242 2.py" "b/1017.\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242/1017-\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242 2.py" new file mode 100644 index 0000000..4d5641d --- /dev/null +++ "b/1017.\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242/1017-\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242 2.py" @@ -0,0 +1,16 @@ +class Solution(object): + def baseNeg2(self, N): + """ + :type N: int + :rtype: str + """ + res = [] + # n = N + while N: + # b = N % -2 + # N = N //-2 + N, b = divmod(N, 2) + N = -N + res.append(str(b)) + return "".join(res[::-1]) or "0" + # return "0" if not n else "".join(res[::-1]) diff --git "a/1019.\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271/1019-\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271 2.py" "b/1019.\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271/1019-\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271 2.py" new file mode 100644 index 0000000..842ee77 --- /dev/null +++ "b/1019.\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271/1019-\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271 2.py" @@ -0,0 +1,35 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def nextLargerNodes(self, head): + """ + :type head: ListNode + :rtype: List[int] + """ + h = head + l = list() + while(h): #ѭΪ˰ת + l.append(h.val) + h = h.next + + stack = list() + res = [0 for i in range(len(l))] + + cnt = 0 + while(cnt < len(l)): #ǰɨ + if not stack or l[stack[-1]] >= l[cnt]: #stackΪգߵǰջӦԪرȵǰɨԪػ + stack.append(cnt)#ֱӰѵǰ±ѹջ + else:#ǰ±ӦԪرջԪش󣬾˵ҵ˱ջԪشһ + while(stack and l[stack[-1]] < l[cnt]): #һֱջֱջԪرȵǰԪС + res[stack[-1]] = l[cnt] + stack.pop() + stack.append(cnt) #ѵǰԪѹջ + + cnt += 1 + + return res + \ No newline at end of file diff --git "a/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" "b/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" deleted file mode 100644 index e28c4f8..0000000 --- "a/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" +++ /dev/null @@ -1,31 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def levelOrder(self, root): - """ - :type root: TreeNode - :rtype: List[List[int]] - """ - if not root: - return [] - queue = [root] - res = list() - while queue: - nextqueue = list() - layer = list() - for node in queue: - if node.left: - nextqueue.append(node.left) - if node.right: - nextqueue.append(node.right) - layer.append(node.val) - - queue = nextqueue[:] - res.append(layer) - return res - \ No newline at end of file diff --git "a/1021.\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267/1021-\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267 2.py" "b/1021.\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267/1021-\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267 2.py" new file mode 100644 index 0000000..096cda4 --- /dev/null +++ "b/1021.\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267/1021-\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267 2.py" @@ -0,0 +1,24 @@ +class Solution(object): + def removeOuterParentheses(self, S): + """ + :type S: str + :rtype: str + """ + s = list() + l,r = 0, 0 + res = "" + for i, x in enumerate(S): + if x == "(": + s.append(x) + l += 1 + elif x == ")": + r += 1 + if l == r: + print s[1:] + res += "".join(s[1:]) #s[0]x= ")"պù"()"ԲҪǾͺ + s = list() + else: + s.append(x) + + return res + \ No newline at end of file diff --git "a/1022.\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214/1022-\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214 2.py" "b/1022.\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214/1022-\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214 2.py" new file mode 100644 index 0000000..3a4b1a9 --- /dev/null +++ "b/1022.\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214/1022-\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214 2.py" @@ -0,0 +1,37 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def sumRootToLeaf(self, root): + """ + :type root: TreeNode + :rtype: int + """ + res = [] + + def dfs(root, tmp): + if not root: + return + tmp += str(root.val) + if not root.left and not root.right: + res.append(tmp[:]) + + dfs(root.left, tmp) + dfs(root.right, tmp) + tmp = tmp[:-1] + + + + dfs(root, "") + # print res + rres = 0 + for item in res: + # print item, int(item, 2) + rres += int(item, 2) + rres %= (10 ** 9 + 7) + + return rres \ No newline at end of file diff --git "a/1038.\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221/1038-\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221 2.py" "b/1038.\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221/1038-\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221 2.py" new file mode 100644 index 0000000..7dcbbaa --- /dev/null +++ "b/1038.\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221/1038-\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221 2.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def bstToGst(self, root): + """ + :type root: TreeNode + :rtype: TreeNode + """ + self.sum = 0 + + def inorder(node): + if not node: + return + + inorder(node.right) + self.sum += node.val + node.val = self.sum + inorder(node.left) + + inorder(root) + return root \ No newline at end of file diff --git "a/1046.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217/1046-\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217.py" "b/1046.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217/1046-\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217.py" new file mode 100644 index 0000000..f78cd85 --- /dev/null +++ "b/1046.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217/1046-\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217.py" @@ -0,0 +1,16 @@ +from heapq import * +class Solution: + def lastStoneWeight(self, stones: List[int]) -> int: + max_heap = [] + + for stone in stones: + heappush(max_heap, -stone) + + while len(max_heap) > 1: + x, y = -heappop(max_heap), -heappop(max_heap) + + if y == x: + continue + else: + heappush(max_heap, -(x - y)) + return -max_heap[0] if max_heap else 0 \ No newline at end of file diff --git "a/1054.\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201/1054-\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201 2.py" "b/1054.\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201/1054-\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201 2.py" new file mode 100644 index 0000000..f5fc68f --- /dev/null +++ "b/1054.\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201/1054-\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201 2.py" @@ -0,0 +1,34 @@ +class Solution(object): + def rearrangeBarcodes(self, barcodes): + """ + :type barcodes: List[int] + :rtype: List[int] + """ + from collections import Counter + import heapq + + record = Counter(barcodes) #ͳÿֳֵƵ + + queue = [] + for key, val in record.items(): + queue.append([-val, key]) + + heapq.heapify(queue) #ȼ + + res = [] + pre = None + while queue or pre: + if queue: + cur = heapq.heappop(queue) #ȡǰִԪأͬȽȳ + #frequency, value = cur[0], cur[1] + res.append(cur[1]) #ŵ + cur[0] += 1 #Ƶ - 1ΪPython֧СѣΪ˴ﵽѵЧȡ෴ + if cur[0] == 0: #ԪѾź + cur = None + else: + cur = None + if pre: #ǰһٽѲ + heapq.heappush(queue, pre) + pre = cur #һֵpreʱڶԱԪزظ + + return res diff --git "a/1073.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/1073-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" "b/1073.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/1073-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" new file mode 100644 index 0000000..91c5e98 --- /dev/null +++ "b/1073.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/1073-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" @@ -0,0 +1,20 @@ +class Solution: + def addNegabinary(self, arr1: List[int], arr2: List[int]) -> List[int]: + res = self.convertToDec(arr1) + self.convertToDec(arr2) + l = [] + if res == 0: + return [0] + while res: + d, m = divmod(res, -2) + res = - (res // 2) + l.append(-m) + return l[::-1] + + def convertToDec(self, arr): + res = 0 + for index, digit in enumerate(arr[::-1]): + res += digit * ((-2) ** index) + return res + + + \ No newline at end of file diff --git "a/1079.\346\264\273\345\255\227\345\215\260\345\210\267/1079-\346\264\273\345\255\227\345\215\260\345\210\267.py" "b/1079.\346\264\273\345\255\227\345\215\260\345\210\267/1079-\346\264\273\345\255\227\345\215\260\345\210\267.py" new file mode 100644 index 0000000..a61a8f3 --- /dev/null +++ "b/1079.\346\264\273\345\255\227\345\215\260\345\210\267/1079-\346\264\273\345\255\227\345\215\260\345\210\267.py" @@ -0,0 +1,14 @@ +class Solution: + def numTilePossibilities(self, tiles: str) -> int: + res = set([""]) + + for tile in tiles: + new_res = set() + for r in res: + for i in range(len(r) + 1): + t = r[:i] + tile + r[i:] + if t not in res: + new_res.add(t) + res = res.union(new_res) + + return len(res) - 1 \ No newline at end of file diff --git "a/1080.\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271/1080-\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271.py" "b/1080.\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271/1080-\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271.py" new file mode 100644 index 0000000..96e16b6 --- /dev/null +++ "b/1080.\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271/1080-\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271.py" @@ -0,0 +1,44 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sufficientSubset(self, root: Optional[TreeNode], limit: int) -> Optional[TreeNode]: + survivor_nodes = set() + + def dfs(node, path): + if not node: + return + # path.append(node) + if not node.left and not node.right: + path.append(node) + path_sum = 0 + for n in path: + path_sum += n.val + if path_sum >= limit: + for n in path: + survivor_nodes.add(n) + return + dfs(node.left, path + [node]) + dfs(node.right, path + [node]) + + dfs(root, []) + # print(survivor_nodes) + def killNodes(node): + if not node: + return + + if node.left not in survivor_nodes: + node.left = None + if node.right not in survivor_nodes: + node.right = None + + killNodes(node.left) + killNodes(node.right) + + if root not in survivor_nodes: + return None + killNodes(root) + return root \ No newline at end of file diff --git "a/1091.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1091-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204 2.py" "b/1091.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1091-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204 2.py" new file mode 100644 index 0000000..ea73840 --- /dev/null +++ "b/1091.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1091-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204 2.py" @@ -0,0 +1,35 @@ +from collections import deque +class Solution(object): + def shortestPathBinaryMatrix(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + n = len(grid) + # print n + + if grid[0][0] or grid[-1][-1] == 1: + return -1 + queue = deque([[[0, 0], 1]]) + # queue2 = deque([[[n -1 , n - 1], 1]]) + visited = set((0,0)) + dx = [1, -1, 0, 0, 1, -1, -1, 1] + dy = [0, 0, 1, -1, -1, 1, -1, 1] + cnt = 1 + record = dict() + while queue: + cur, cnt = queue.popleft() + # print cur, cnt + x0, y0 = cur[0], cur[1] + + if x0 == n - 1 and y0 == n - 1: + return cnt + for k in range(8): + x = x0 + dx[k] + y = y0 + dy[k] + + if 0 <= x height[hi]: - area = height[hi] * (hi - lo) - hi -= 1 - else: - area = height[lo] * (hi - lo) - lo += 1 - # print area - res = max(area, res) - - return res - \ No newline at end of file diff --git "a/1103.\345\210\206\347\263\226\346\236\234II/1103-\345\210\206\347\263\226\346\236\234II 2.py" "b/1103.\345\210\206\347\263\226\346\236\234II/1103-\345\210\206\347\263\226\346\236\234II 2.py" new file mode 100644 index 0000000..c988b09 --- /dev/null +++ "b/1103.\345\210\206\347\263\226\346\236\234II/1103-\345\210\206\347\263\226\346\236\234II 2.py" @@ -0,0 +1,20 @@ +class Solution(object): + def distributeCandies(self, candies, num_people): + """ + :type candies: int + :type num_people: int + :rtype: List[int] + """ + res = [0 for _ in range(num_people)] + cnt = 1 + while candies: + for i in range(num_people): + if candies >= cnt: + res[i] += cnt + candies -= cnt + cnt += 1 + else: + res[i] += candies + candies = 0 + break + return res \ No newline at end of file diff --git "a/1108.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/1108-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" "b/1108.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/1108-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" new file mode 100644 index 0000000..b9c7c37 --- /dev/null +++ "b/1108.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/1108-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" @@ -0,0 +1,4 @@ +class Solution: + def defangIPaddr(self, address: str) -> str: + + return address.replace(".", "[.]") \ No newline at end of file diff --git "a/1110.\345\210\240\347\202\271\346\210\220\346\236\227/1110-\345\210\240\347\202\271\346\210\220\346\236\227.py" "b/1110.\345\210\240\347\202\271\346\210\220\346\236\227/1110-\345\210\240\347\202\271\346\210\220\346\236\227.py" new file mode 100644 index 0000000..bd49588 --- /dev/null +++ "b/1110.\345\210\240\347\202\271\346\210\220\346\236\227/1110-\345\210\240\347\202\271\346\210\220\346\236\227.py" @@ -0,0 +1,27 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]: + to_delete = set(to_delete) + res = [] + def dfs(node, node_is_new_root): + if not node: + return + left = node.left + right = node.right + if node_is_new_root and node.val not in to_delete: + res.append(node) + if left and left.val in to_delete: + node.left = None + if right and right.val in to_delete: + node.right = None + + dfs(left, node.val in to_delete) + dfs(right, node.val in to_delete) + dfs(root, True) + return res + \ No newline at end of file diff --git "a/1119.\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263/1119-\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263 2.py" "b/1119.\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263/1119-\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263 2.py" new file mode 100644 index 0000000..cecdf5c --- /dev/null +++ "b/1119.\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263/1119-\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263 2.py" @@ -0,0 +1,8 @@ +class Solution(object): + def removeVowels(self, S): + """ + :type S: str + :rtype: str + """ + + return "".join(char for char in S if char not in "aeiou") \ No newline at end of file diff --git "a/1130.\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221/1130-\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221.py" "b/1130.\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221/1130-\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221.py" new file mode 100644 index 0000000..a72c05c --- /dev/null +++ "b/1130.\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221/1130-\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221.py" @@ -0,0 +1,12 @@ +class Solution: + def mctFromLeafValues(self, arr: List[int]) -> int: + res = 0 + stack = [16] + for num in arr: + while stack and stack[-1] < num: + res += stack.pop() * min(stack[-1], num) + stack.append(num) + + while len(stack) > 2: + res += stack.pop() * stack[-1] + return res \ No newline at end of file diff --git "a/1150.\346\243\200\346\237\245\344\270\200\344\270\252\346\225\260\346\230\257\345\220\246\345\234\250\346\225\260\347\273\204\344\270\255\345\215\240\347\273\235\345\244\247\345\244\232\346\225\260/1150-\346\243\200\346\237\245\344\270\200\344\270\252\346\225\260\346\230\257\345\220\246\345\234\250\346\225\260\347\273\204\344\270\255\345\215\240\347\273\235\345\244\247\345\244\232\346\225\260.py" "b/1150.\346\243\200\346\237\245\344\270\200\344\270\252\346\225\260\346\230\257\345\220\246\345\234\250\346\225\260\347\273\204\344\270\255\345\215\240\347\273\235\345\244\247\345\244\232\346\225\260/1150-\346\243\200\346\237\245\344\270\200\344\270\252\346\225\260\346\230\257\345\220\246\345\234\250\346\225\260\347\273\204\344\270\255\345\215\240\347\273\235\345\244\247\345\244\232\346\225\260.py" index 95f6fca..d915ca3 100644 --- "a/1150.\346\243\200\346\237\245\344\270\200\344\270\252\346\225\260\346\230\257\345\220\246\345\234\250\346\225\260\347\273\204\344\270\255\345\215\240\347\273\235\345\244\247\345\244\232\346\225\260/1150-\346\243\200\346\237\245\344\270\200\344\270\252\346\225\260\346\230\257\345\220\246\345\234\250\346\225\260\347\273\204\344\270\255\345\215\240\347\273\235\345\244\247\345\244\232\346\225\260.py" +++ "b/1150.\346\243\200\346\237\245\344\270\200\344\270\252\346\225\260\346\230\257\345\220\246\345\234\250\346\225\260\347\273\204\344\270\255\345\215\240\347\273\235\345\244\247\345\244\232\346\225\260/1150-\346\243\200\346\237\245\344\270\200\344\270\252\346\225\260\346\230\257\345\220\246\345\234\250\346\225\260\347\273\204\344\270\255\345\215\240\347\273\235\345\244\247\345\244\232\346\225\260.py" @@ -5,4 +5,4 @@ def isMajorityElement(self, nums, target): :type target: int :rtype: bool """ - return nums.count(target) > (len(nums) // 2) \ No newline at end of file + return nums.count(target) > len(nums) // 2 \ No newline at end of file diff --git "a/1152.\347\224\250\346\210\267\347\275\221\347\253\231\350\256\277\351\227\256\350\241\214\344\270\272\345\210\206\346\236\220/1152-\347\224\250\346\210\267\347\275\221\347\253\231\350\256\277\351\227\256\350\241\214\344\270\272\345\210\206\346\236\220.py" "b/1152.\347\224\250\346\210\267\347\275\221\347\253\231\350\256\277\351\227\256\350\241\214\344\270\272\345\210\206\346\236\220/1152-\347\224\250\346\210\267\347\275\221\347\253\231\350\256\277\351\227\256\350\241\214\344\270\272\345\210\206\346\236\220.py" index 4731089..5d88bcf 100644 --- "a/1152.\347\224\250\346\210\267\347\275\221\347\253\231\350\256\277\351\227\256\350\241\214\344\270\272\345\210\206\346\236\220/1152-\347\224\250\346\210\267\347\275\221\347\253\231\350\256\277\351\227\256\350\241\214\344\270\272\345\210\206\346\236\220.py" +++ "b/1152.\347\224\250\346\210\267\347\275\221\347\253\231\350\256\277\351\227\256\350\241\214\344\270\272\345\210\206\346\236\220/1152-\347\224\250\346\210\267\347\275\221\347\253\231\350\256\277\351\227\256\350\241\214\344\270\272\345\210\206\346\236\220.py" @@ -1,36 +1,42 @@ -class Solution(object): - def mostVisitedPattern(self, username, timestamp, website): - """ - :type username: List[str] - :type timestamp: List[int] - :type website: List[str] - :rtype: List[str] - """ +class Solution: + def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]: + # l = [1,2,3,4,5,6] + # for i in range(len(l)): + # for j in range(i + 1, len(l)): + # for k in range(j + 1, len(l)): + # print (l[i], l[j], l[k]) from collections import defaultdict - record = defaultdict(list) - for i, un in enumerate(username): - record[un].append([timestamp[i], website[i]]) - # print record - row = defaultdict(int) - for key in record.keys(): - record[key].sort() - # print record[key] - used = set() - for i in range(len(record[key])): - for j in range(i + 1, len(record[key])): - for k in range(j + 1, len(record[key])): - sequence = record[key][i][1] + "+" + record[key][j][1]+ "+" + record[key][k][1] - if sequence not in used: - row[sequence] += 1 - used.add(sequence) - # print row - possible_sol = [] - max_freq = max(row.values()) - for key, val in row.items(): - if val == max_freq: - possible_sol.append(key.split("+")) - possible_sol = possible_sol[::-1] - # print possible_sol - if len(possible_sol) > 1: - possible_sol.sort() - return possible_sol[0] \ No newline at end of file + max_visit_cnt = 0 + max_visit_websites = [] + name2web = defaultdict(list) + web2freq = defaultdict(int) + comb = [] + for i in range(len(username)): + comb.append((username[i], timestamp[i], website[i])) + comb.sort(key = lambda x:x[1]) + for i in range(len(username)): + name2web[comb[i][0]].append(comb[i][2]) + + for name, webs in name2web.items(): + visited = set() + for i in range(len(webs)): + for j in range(i + 1, len(webs)): + for k in range(j + 1, len(webs)): + tmp = ",".join([webs[i], webs[j], webs[k]]) + if tmp in visited: + continue + visited.add(tmp) + web2freq[tmp] += 1 + + if web2freq[tmp] > max_visit_cnt: + max_visit_cnt = web2freq[tmp] + max_visit_websites = [tmp] + elif web2freq[tmp] == max_visit_cnt: + max_visit_websites.append(tmp) + # print (max_visit_websites) + max_visit_websites.sort() + # print (max_visit_websites) + s = max_visit_websites[0] + l = s.split(",") + return l + diff --git "a/1156.\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246/1156-\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246 2.py" "b/1156.\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246/1156-\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246 2.py" new file mode 100644 index 0000000..5ce1d0d --- /dev/null +++ "b/1156.\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246/1156-\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246 2.py" @@ -0,0 +1,41 @@ +class Solution(object): + def maxRepOpt1(self, text): + """ + :type text: str + :rtype: int + """ + if len(text) == text.count(text[0]): + return len(text) + record = collections.Counter(text) + start, end = 0, 1 + cur, nxt, idx_nxt = text[0], None, 0 + res = 1 + while end < len(text): + if text[end] != cur : + if nxt is None: + nxt = text[end] #ҵ˵һַ + idx_nxt = end + else: #ǰѾ + l = end - 1 - start + 1 #һַַͬӴ + if l <= record[text[start]]: #жַͬ԰мַͬ + res = max(res, l) + else: + res = max(res, l - 1) #ֻĿǰִıַַ߽ͬ + + cur = nxt + nxt = None + start, end = idx_nxt, idx_nxt + idx_nxt = 0 + if end == len(text) - 1: #յ + # print end + l = end - start + 1 #һַַͬӴ + # print l + if l <= record[text[start]]: #жַͬ԰мַͬ + res = max(res, l) + else: + res = max(res, l - 1) #ֻĿǰִıַַ߽ͬ + + # print text[start:end + 1] + end += 1 + # print end + return res \ No newline at end of file diff --git "a/116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.py" "b/116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.py" deleted file mode 100644 index 4a71a00..0000000 --- "a/116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.py" +++ /dev/null @@ -1,36 +0,0 @@ -""" -# Definition for a Node. -class Node(object): - def __init__(self, val=0, left=None, right=None, next=None): - self.val = val - self.left = left - self.right = right - self.next = next -""" -class Solution(object): - def connect(self, root): - """ - :type root: Node - :rtype: Node - """ - if not root or not root.left: - return root - - root.left.next = root.right - if root.next: - root.right.next = root.next.left - self.connect(root.left) - self.connect(root.right) - - return root - - def findNext(self, node): - nxt = node.next - while nxt: - if nxt.left: - return nxt.left - elif nxt.right: - return nxt.right - else: - nxt = nxt.next - return None \ No newline at end of file diff --git "a/1161.\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214/1161-\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214 2.py" "b/1161.\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214/1161-\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214 2.py" new file mode 100644 index 0000000..81de25d --- /dev/null +++ "b/1161.\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214/1161-\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214 2.py" @@ -0,0 +1,33 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def maxLevelSum(self, root): + """ + :type root: TreeNode + :rtype: int + """ + from collections import deque + queue = deque([root]) + + layer = 1 + res = 1 + max_values = 0 + while queue: + values = 0 + for _ in range(len(queue)): + node = queue.popleft() + if node: + values += node.val + queue.append(node.left) + queue.append(node.right) + if values > max_values: + max_values = values + res = layer + layer += 1 + return res + \ No newline at end of file diff --git "a/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230 2.py" "b/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230 2.py" new file mode 100644 index 0000000..9da80b9 --- /dev/null +++ "b/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230 2.py" @@ -0,0 +1,16 @@ +class Solution(object): + def calculateTime(self, keyboard, word): + """ + :type keyboard: str + :type word: str + :rtype: int + """ + dic = dict() + for i, char in enumerate(keyboard): + dic[char] = i + + res, cur_pos = 0, 0 + for char in word: + res += abs(dic[char] - cur_pos) + cur_pos = dic[char] + return res diff --git "a/117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II.py" "b/117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II.py" deleted file mode 100644 index d38d977..0000000 --- "a/117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II.py" +++ /dev/null @@ -1,41 +0,0 @@ -""" -# Definition for a Node. -class Node(object): - def __init__(self, val=0, left=None, right=None, next=None): - self.val = val - self.left = left - self.right = right - self.next = next -""" -class Solution(object): - def connect(self, root): - """ - :type root: Node - :rtype: Node - """ - if not root: - return root - - if root.left and root.right: - root.left.next = root.right - root.right.next = self.findNext(root) - - elif root.left: - root.left.next = self.findNext(root) - elif root.right: - root.right.next = self.findNext(root) - - self.connect(root.right) - self.connect(root.left) - - return root - - def findNext(self, node): - nxt = node.next - while nxt: - if nxt.left: - return nxt.left - if nxt.right: - return nxt.right - nxt = nxt.next - return None \ No newline at end of file diff --git "a/1170.\346\257\224\350\276\203\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\345\255\227\346\257\215\345\207\272\347\216\260\351\242\221\346\254\241/1170-\346\257\224\350\276\203\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\345\255\227\346\257\215\345\207\272\347\216\260\351\242\221\346\254\241.py" "b/1170.\346\257\224\350\276\203\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\345\255\227\346\257\215\345\207\272\347\216\260\351\242\221\346\254\241/1170-\346\257\224\350\276\203\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\345\255\227\346\257\215\345\207\272\347\216\260\351\242\221\346\254\241.py" index 2af6b9f..1d151bc 100644 --- "a/1170.\346\257\224\350\276\203\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\345\255\227\346\257\215\345\207\272\347\216\260\351\242\221\346\254\241/1170-\346\257\224\350\276\203\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\345\255\227\346\257\215\345\207\272\347\216\260\351\242\221\346\254\241.py" +++ "b/1170.\346\257\224\350\276\203\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\345\255\227\346\257\215\345\207\272\347\216\260\351\242\221\346\254\241/1170-\346\257\224\350\276\203\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\345\255\227\346\257\215\345\207\272\347\216\260\351\242\221\346\254\241.py" @@ -5,18 +5,24 @@ def numSmallerByFrequency(self, queries, words): :type words: List[str] :rtype: List[int] """ - def f(word): - return word.count(min(word)) - f_words = sorted([f(word) for word in words]) + def func(word): + for char in "abcdefghijklmnopqrstuvwxyz": + if char in word: + return word.count(char) + return 0 - res = [] - # print f_words - for q in queries: - cnt = f(q) - # print(bisect.bisect(f_words, cnt)) - res.append(len(f_words) - bisect.bisect(f_words, cnt)) - - return res - + def func2(word): + record = collections.Counter(word) + return record[min(record.keys())] + + + words_count = sorted(map(func2, words)) + queries_count = map(func2, queries) + # print words_count, queries_count + ans = [] + for query in queries_count: + index = bisect.bisect(words_count, query) #bisectѸҳindex <= query + ans.append(len(words_count) - index)# жٸquery + return ans \ No newline at end of file diff --git "a/1180.\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262/1180-\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262 2.py" "b/1180.\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262/1180-\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262 2.py" new file mode 100644 index 0000000..d3e9c18 --- /dev/null +++ "b/1180.\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262/1180-\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262 2.py" @@ -0,0 +1,14 @@ +class Solution(object): + def countLetters(self, S): + """ + :type S: str + :rtype: int + """ + res = 0 + for i in range(len(S)): + for j in range(i + 1, len(S) + 1): + substring = S[i:j] + if substring == substring[0] * len(substring): + res += 1 + # print substring + return res \ No newline at end of file diff --git "a/1198.\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240/1198-\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240 2.py" "b/1198.\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240/1198-\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240 2.py" new file mode 100644 index 0000000..744f711 --- /dev/null +++ "b/1198.\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240/1198-\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240 2.py" @@ -0,0 +1,13 @@ +class Solution(object): + def smallestCommonElement(self, mat): + """ + :type mat: List[List[int]] + :rtype: int + """ + from collections import Counter + flatten = sum(mat,[]) + dic = Counter(flatten) + for num in mat[0]: + if dic[num] == len(mat): + return num + return -1 \ No newline at end of file diff --git "a/1209.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271II/1209-\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271II.py" "b/1209.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271II/1209-\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271II.py" deleted file mode 100644 index e816f7c..0000000 --- "a/1209.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271II/1209-\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271II.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def removeDuplicates(self, s, k): - """ - :type s: str - :type k: int - :rtype: str - """ - stack = [] - for ch in s: - if not stack: - stack.append([ch, 1]) - else: - if stack[-1][0] == ch: - stack[-1][1] += 1 - if stack[-1][1] % k == 0: - stack.pop() - else: - stack.append([ch, 1]) - - return "".join(ch * freq for ch, freq in stack) \ No newline at end of file diff --git "a/1219.\351\273\204\351\207\221\347\237\277\345\267\245/1219-\351\273\204\351\207\221\347\237\277\345\267\245.py" "b/1219.\351\273\204\351\207\221\347\237\277\345\267\245/1219-\351\273\204\351\207\221\347\237\277\345\267\245.py" deleted file mode 100644 index 5790cac..0000000 --- "a/1219.\351\273\204\351\207\221\347\237\277\345\267\245/1219-\351\273\204\351\207\221\347\237\277\345\267\245.py" +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def getMaximumGold(self, grid): - """ - :type grid: List[List[int]] - :rtype: int - """ - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - m, n = len(grid), len(grid[0]) - visited = set() - self.res = 0 - - def dfs(x0, y0, tmp): - self.res = max(self.res, tmp) - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - - if 0 <= x < m and 0 <= y < n and grid[x][y] > 0: - value = grid[x][y] - grid[x][y] = -1 - dfs(x, y, tmp + value) - grid[x][y] = value - - for i in range(m): - for j in range(n): - value = grid[i][j] - grid[i][j] = -1 - dfs(i, j, value) - grid[i][j] = value - return self.res \ No newline at end of file diff --git "a/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262/1221-\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262 2.py" "b/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262/1221-\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262 2.py" new file mode 100644 index 0000000..6dcc6a1 --- /dev/null +++ "b/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262/1221-\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262 2.py" @@ -0,0 +1,21 @@ +class Solution(object): + def balancedStringSplit(self, s): + """ + :type s: str + :rtype: int + """ + if not s: + return 0 + # print s + l, r = 0, 0 + + for i in range(len(s)): + if s[i] == "R": + r += 1 + else: + l += 1 + # print r, l + if l == r: + return 1 + self.balancedStringSplit(s[i + 1:]) + + return 0 \ No newline at end of file diff --git "a/1230.\346\212\233\346\216\267\347\241\254\345\270\201/1230-\346\212\233\346\216\267\347\241\254\345\270\201.py" "b/1230.\346\212\233\346\216\267\347\241\254\345\270\201/1230-\346\212\233\346\216\267\347\241\254\345\270\201.py" index f6c8cf3..d928eef 100644 --- "a/1230.\346\212\233\346\216\267\347\241\254\345\270\201/1230-\346\212\233\346\216\267\347\241\254\345\270\201.py" +++ "b/1230.\346\212\233\346\216\267\347\241\254\345\270\201/1230-\346\212\233\346\216\267\347\241\254\345\270\201.py" @@ -16,7 +16,7 @@ def probabilityOfHeads(self, prob, target): return dp[target] # dp = [[0 for _ in range(len(prob) + 1)] for _ in range(len(prob))] - # # dp[i][j] 表示前i个硬币里,有j个硬币正面朝上的概率 + # # dp[i][j] ʾǰiӲjӲ泯ϵĸ # dp[0][1] = prob[0] # dp[0][0] = 1 - prob[0] # for i, p in enumerate(prob): diff --git "a/1231.\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233/1231-\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233 2.py" "b/1231.\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233/1231-\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233 2.py" new file mode 100644 index 0000000..ba54ff0 --- /dev/null +++ "b/1231.\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233/1231-\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233 2.py" @@ -0,0 +1,27 @@ +class Solution(object): + def maximizeSweetness(self, sweetness, K): + """ + :type sweetness: List[int] + :type K: int + :rtype: int + """ + if not K: + return sum(sweetness) + left, right = 0, sum(sweetness) // K + res = 0 + while left <= right: + mid = (left + right) // 2 + cnt = 0 + tmp = 0 + for s in sweetness: + if tmp + s > mid: + cnt += 1 + tmp = 0 + else: + tmp += s + + if cnt < K + 1: + right = mid - 1 + else: + left = mid + 1 + return left \ No newline at end of file diff --git "a/1237.\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243/1237-\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243.py" "b/1237.\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243/1237-\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243.py" new file mode 100644 index 0000000..b6568c3 --- /dev/null +++ "b/1237.\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243/1237-\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243.py" @@ -0,0 +1,26 @@ +""" + This is the custom function interface. + You should not implement it, or speculate about its implementation + class CustomFunction: + # Returns f(x, y) for any given positive integers x and y. + # Note that f(x, y) is increasing with respect to both x and y. + # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1) + def f(self, x, y): + +""" + +class Solution: + def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]: + res = [] + for x in range(1, 1001): + left, right = 1, 1001 + while left <= right: + mid = (left + right)// 2 + if customfunction.f(x, mid) == z: + res.append([x, mid]) + break + elif customfunction.f(x, mid) < z: + left = mid + 1 + else: + right = mid - 1 + return res diff --git "a/125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.py" "b/125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.py" deleted file mode 100644 index ac5138b..0000000 --- "a/125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.py" +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def isPalindrome(self, s): - """ - :type s: str - :rtype: bool - """ - res = "" - for ch in s: - if ch.isalpha() or ch.isdigit(): - res += ch.lower() - # print res - return res == res[::-1] \ No newline at end of file diff --git "a/1252.\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256/1252-\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256 2.py" "b/1252.\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256/1252-\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256 2.py" new file mode 100644 index 0000000..be35f2f --- /dev/null +++ "b/1252.\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256/1252-\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256 2.py" @@ -0,0 +1,22 @@ +class Solution(object): + def oddCells(self, n, m, indices): + """ + :type n: int + :type m: int + :type indices: List[List[int]] + :rtype: int + """ + b = [[0 for _ in range(m)] for _ in range(n)] + for row, col in indices: + for i in range(m): + b[row][i] += 1 + + for j in range(n): + b[j][col] += 1 + + res = 0 + for i in range(n): + for j in range(m): + if b[i][j] % 2: + res += 1 + return res \ No newline at end of file diff --git "a/1253.\351\207\215\346\236\2042\350\241\214\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265/1253-\351\207\215\346\236\2042\350\241\214\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265.py" "b/1253.\351\207\215\346\236\2042\350\241\214\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265/1253-\351\207\215\346\236\2042\350\241\214\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265.py" deleted file mode 100644 index cf75510..0000000 --- "a/1253.\351\207\215\346\236\2042\350\241\214\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265/1253-\351\207\215\346\236\2042\350\241\214\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265.py" +++ /dev/null @@ -1,27 +0,0 @@ -class Solution(object): - def reconstructMatrix(self, upper, lower, colsum): - """ - :type upper: int - :type lower: int - :type colsum: List[int] - :rtype: List[List[int]] - """ - b = [[0 for _ in range(len(colsum))] for _ in range(2)] - - for i, cs in enumerate(colsum): - if cs == 2: - b[0][i] = 1 - b[1][i] = 1 - upper -= 1 - lower -= 1 - for i, cs in enumerate(colsum): - if cs == 1: - if lower > upper: - b[1][i] = 1 - lower -= 1 - else: - b[0][i] = 1 - upper -= 1 - if upper != 0 or lower != 0: - return [] - return b \ No newline at end of file diff --git "a/1255.\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\345\215\225\350\257\215\351\233\206\345\220\210/1255-\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\345\215\225\350\257\215\351\233\206\345\220\210.py" "b/1255.\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\345\215\225\350\257\215\351\233\206\345\220\210/1255-\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\345\215\225\350\257\215\351\233\206\345\220\210.py" index e9f0305..32f2f05 100644 --- "a/1255.\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\345\215\225\350\257\215\351\233\206\345\220\210/1255-\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\345\215\225\350\257\215\351\233\206\345\220\210.py" +++ "b/1255.\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\345\215\225\350\257\215\351\233\206\345\220\210/1255-\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\345\215\225\350\257\215\351\233\206\345\220\210.py" @@ -9,25 +9,25 @@ def maxScoreWords(self, words, letters, score): from collections import defaultdict, Counter dic = dict() letter_dic = defaultdict(int) - for i, val in enumerate(score):#构建一个字典 - dic[chr(ord("a") + i)] = val #key是 字母,val是字母对应的分数 + for i, val in enumerate(score):#һֵ + dic[chr(ord("a") + i)] = val #key ĸvalĸӦķ - letter_dic = Counter(letters)#构建另一个字典, key是字母, val是每次字母剩余的个数 + letter_dic = Counter(letters)#һֵ䣬 keyĸ valÿĸʣĸ s = set(letters) v_words = [] - for word in words:#删掉所有根本不可能被构成的单词 + for word in words:#ɾиܱɵĵ flag = 0 for char in word: if char not in s: flag = 1 - if flag: # 如果一个单词里存在某个在letters里找不到的字母,则无需考虑这个单词 + if flag: # һijlettersҲĸ迼 continue v_words.append(word) self.res = 0 def helper(word, letter_dic): - # return True 如果word能用letter_dic里的letter构成,否则返回False + # return True wordletter_dicletterɣ򷵻False dicc = collections.Counter(word) for key in dicc: if dicc[key] > letter_dic[key]: @@ -39,12 +39,12 @@ def dfs(start, tmp): if start >= len(v_words): return - for i in range(start, len(v_words)):#从start开始找,避免重复 - if helper(v_words[i], letter_dic):#如果当前单词可以被构成 - for char in v_words[i]: #构成它,更新字典 + for i in range(start, len(v_words)):#startʼңظ + if helper(v_words[i], letter_dic):#ǰʿԱ + for char in v_words[i]: #ֵ letter_dic[char] -= 1 - dfs(i + 1, tmp + sum([dic[char] for char in v_words[i]])) #dfs下一层 - for char in v_words[i]: #回溯,复原所有状态 + dfs(i + 1, tmp + sum([dic[char] for char in v_words[i]])) #dfsһ + for char in v_words[i]: #ݣԭ״̬ letter_dic[char] += 1 dfs(0, 0) return self.res \ No newline at end of file diff --git "a/1256.\345\212\240\345\257\206\346\225\260\345\255\227/1256-\345\212\240\345\257\206\346\225\260\345\255\227.py" "b/1256.\345\212\240\345\257\206\346\225\260\345\255\227/1256-\345\212\240\345\257\206\346\225\260\345\255\227.py" deleted file mode 100644 index 967b748..0000000 --- "a/1256.\345\212\240\345\257\206\346\225\260\345\255\227/1256-\345\212\240\345\257\206\346\225\260\345\255\227.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def encode(self, num): - """ - :type num: int - :rtype: str - """ - return bin(num + 1)[3:] \ No newline at end of file diff --git "a/1257.\346\234\200\345\260\217\345\205\254\345\205\261\345\214\272\345\237\237/1257-\346\234\200\345\260\217\345\205\254\345\205\261\345\214\272\345\237\237.py" "b/1257.\346\234\200\345\260\217\345\205\254\345\205\261\345\214\272\345\237\237/1257-\346\234\200\345\260\217\345\205\254\345\205\261\345\214\272\345\237\237.py" deleted file mode 100644 index 87162ed..0000000 --- "a/1257.\346\234\200\345\260\217\345\205\254\345\205\261\345\214\272\345\237\237/1257-\346\234\200\345\260\217\345\205\254\345\205\261\345\214\272\345\237\237.py" +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def findSmallestRegion(self, regions, region1, region2): - """ - :type regions: List[List[str]] - :type region1: str - :type region2: str - :rtype: str - """ - from collections import defaultdict - dic = dict() - for reg in regions: - parent = reg[0] - for child in reg[1:]: # 遍历所有的子区域 - dic[child] = (parent) - - ancestors_1 = set() - while region1 in dic: - ancestors_1.add(region1) - region1 = dic[region1] - - while region2 not in ancestors_1 and region2 in dic: - region2 = dic[region2] - - return region2 \ No newline at end of file diff --git "a/1258.\350\277\221\344\271\211\350\257\215\345\217\245\345\255\220/1258-\350\277\221\344\271\211\350\257\215\345\217\245\345\255\220.py" "b/1258.\350\277\221\344\271\211\350\257\215\345\217\245\345\255\220/1258-\350\277\221\344\271\211\350\257\215\345\217\245\345\255\220.py" deleted file mode 100644 index a164966..0000000 --- "a/1258.\350\277\221\344\271\211\350\257\215\345\217\245\345\255\220/1258-\350\277\221\344\271\211\350\257\215\345\217\245\345\255\220.py" +++ /dev/null @@ -1,56 +0,0 @@ -class Solution(object): - def generateSentences(self, synonyms, text): - """ - :type synonyms: List[List[str]] - :type text: str - :rtype: List[str] - """ - from collections import defaultdict - dic = defaultdict(set) - vocab = set() - text = text.split() - - for w1, w2 in synonyms: - dic[w1].add(w2) - dic[w2].add(w1) - vocab.add(w1) - vocab.add(w2) - - self.res = [] - - def generateList(word, res, visited): - res.add(word) - for w in dic[word]: - if w not in visited: - visited.add(w) - generateList(w, res, visited) - - return res - - - def dfs(start, tmp): - if start >= len(text): - self.res.append(tmp) - return - - word = text[start] - if word in vocab: - l = set() - visited = set() - generateList(word, l, visited) - for w in l: - if start > 0: - dfs(start + 1, tmp + " " + w) - else: - dfs(start + 1, w) - else: - if start > 0: - dfs(start + 1, tmp + " " + word) - else: - dfs(start + 1, word) - dfs(0, "") - self.res.sort() - return self.res - - - \ No newline at end of file diff --git "a/1259.\344\270\215\347\233\270\344\272\244\347\232\204\346\217\241\346\211\213/1259-\344\270\215\347\233\270\344\272\244\347\232\204\346\217\241\346\211\213.py" "b/1259.\344\270\215\347\233\270\344\272\244\347\232\204\346\217\241\346\211\213/1259-\344\270\215\347\233\270\344\272\244\347\232\204\346\217\241\346\211\213.py" deleted file mode 100644 index fcd69b1..0000000 --- "a/1259.\344\270\215\347\233\270\344\272\244\347\232\204\346\217\241\346\211\213/1259-\344\270\215\347\233\270\344\272\244\347\232\204\346\217\241\346\211\213.py" +++ /dev/null @@ -1,10 +0,0 @@ -from math import factorial as fac -class Solution(object): - def numberOfWays(self, num_people): - """ - :type num_people: int - :rtype: int - """ - def catalan(n): - return fac(2*n) // (fac(n+1) * fac(n)) - return catalan(num_people // 2) % ( 10 ** 9 + 7) \ No newline at end of file diff --git "a/1260.\344\272\214\347\273\264\347\275\221\346\240\274\350\277\201\347\247\273/1260-\344\272\214\347\273\264\347\275\221\346\240\274\350\277\201\347\247\273.py" "b/1260.\344\272\214\347\273\264\347\275\221\346\240\274\350\277\201\347\247\273/1260-\344\272\214\347\273\264\347\275\221\346\240\274\350\277\201\347\247\273.py" deleted file mode 100644 index 8b088ec..0000000 --- "a/1260.\344\272\214\347\273\264\347\275\221\346\240\274\350\277\201\347\247\273/1260-\344\272\214\347\273\264\347\275\221\346\240\274\350\277\201\347\247\273.py" +++ /dev/null @@ -1,19 +0,0 @@ -class Solution(object): - def shiftGrid(self, grid, k): - """ - :type grid: List[List[int]] - :type k: int - :rtype: List[List[int]] - """ - m, n = len(grid), len(grid[0]) - - size = m * n - k = k % size - l = [grid[i][j] for i in range(m) for j in range(n)] - - l = l[-k:] + l[:-k] - - for i in range(m): - for j in range(n): - grid[i][j] = l[i * n + j] - return grid \ No newline at end of file diff --git "a/1261.\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240/1261-\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240.py" "b/1261.\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240/1261-\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240.py" index c031724..2f0ce01 100644 --- "a/1261.\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240/1261-\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240.py" +++ "b/1261.\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240/1261-\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240.py" @@ -1,40 +1,33 @@ # Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class FindElements: -class FindElements(object): - - def __init__(self, root): - """ - :type root: TreeNode - """ + def __init__(self, root: Optional[TreeNode]): root.val = 0 self.values = set() def dfs(node): if not node: - return + return + self.values.add(node.val) if node.left: node.left.val = 2 * node.val + 1 - dfs(node.left) if node.right: node.right.val = 2 * node.val + 2 - dfs(node.right) - dfs(root) - + + dfs(node.left) + dfs(node.right) + dfs(root) - def find(self, target): - """ - :type target: int - :rtype: bool - """ - + def find(self, target: int) -> bool: return target in self.values + # Your FindElements object will be instantiated and called as such: # obj = FindElements(root) # param_1 = obj.find(target) \ No newline at end of file diff --git "a/1262.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214/1262-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214.py" "b/1262.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214/1262-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214.py" deleted file mode 100644 index 9050833..0000000 --- "a/1262.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214/1262-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\346\234\200\345\244\247\345\222\214.py" +++ /dev/null @@ -1,42 +0,0 @@ -class Solution(object): - def maxSumDivThree(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - from collections import defaultdict - dic = defaultdict(list) - - nums.sort() - for num in nums: - dic[num % 3].append(num) - - - s = sum(nums) - if s % 3 == 0: - return s - - if s % 3 == 2: - t1, t2 = float("inf"),float("inf") - if 2 in dic: #可以删除一个模为 2 的最小数 - t1 = dic[2][0] - - if len(dic[1]) >= 2: # 也可以删除两个模为 1 的最小数 - t2 = dic[1][0] + dic[1][1] - - if t1 > t2:# 选择两种可能中较小的值删除 - return s - t2 - - return s - t1 - - if s % 3 == 1: - t1, t2 = float("inf"), float("inf") - if 1 in dic: # 可以删除一个模为 1 的最小数 - t1 = dic[1][0] - if len(dic[2]) >= 2: # 也可以删除两个模为 2 的最小数 - t2 = dic[2][0] + dic[2][1] - - if t1 > t2: # 选择两种可能中较小的值删除 - return s - t2 - - return s - t1 \ No newline at end of file diff --git "a/1265.\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250/1265-\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250.py" "b/1265.\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250/1265-\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250.py" index bd98f36..1b04458 100644 --- "a/1265.\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250/1265-\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250.py" +++ "b/1265.\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250/1265-\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250.py" @@ -1,32 +1,15 @@ -# """ -# This is the ImmutableListNode's API interface. -# You should not implement it, or speculate about its implementation. -# """ -# class ImmutableListNode(object): -# def printValue(self): # print the value of this node. -# . """ -# :rtype None -# """ -# -# def getNext(self): # return the next node. -# . """ -# :rtype ImmutableListNode -# """ +# """ +# This is the ImmutableListNode's API interface. +# You should not implement it, or speculate about its implementation. +# """ +# class ImmutableListNode: +# def printValue(self) -> None: # print the value of this node. +# def getNext(self) -> 'ImmutableListNode': # return the next node. -class Solution(object): - def printLinkedListInReverse(self, head): - """ - :type head: ImmutableListNode - :rtype: None - """ +class Solution: + def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None: if not head: return - l = [] - p = head - while p: - l.append(p) - p = p.getNext() - - while l: - l.pop().printValue() - \ No newline at end of file + + self.printLinkedListInReverse(head.getNext()) + head.printValue() \ No newline at end of file diff --git "a/1275.\346\211\276\345\207\272\344\272\225\345\255\227\346\243\213\347\232\204\350\216\267\350\203\234\350\200\205/1275-\346\211\276\345\207\272\344\272\225\345\255\227\346\243\213\347\232\204\350\216\267\350\203\234\350\200\205.py" "b/1275.\346\211\276\345\207\272\344\272\225\345\255\227\346\243\213\347\232\204\350\216\267\350\203\234\350\200\205/1275-\346\211\276\345\207\272\344\272\225\345\255\227\346\243\213\347\232\204\350\216\267\350\203\234\350\200\205.py" index 5f5014f..26dfeca 100644 --- "a/1275.\346\211\276\345\207\272\344\272\225\345\255\227\346\243\213\347\232\204\350\216\267\350\203\234\350\200\205/1275-\346\211\276\345\207\272\344\272\225\345\255\227\346\243\213\347\232\204\350\216\267\350\203\234\350\200\205.py" +++ "b/1275.\346\211\276\345\207\272\344\272\225\345\255\227\346\243\213\347\232\204\350\216\267\350\203\234\350\200\205/1275-\346\211\276\345\207\272\344\272\225\345\255\227\346\243\213\347\232\204\350\216\267\350\203\234\350\200\205.py" @@ -43,4 +43,6 @@ def check(): tmp = check() if tmp != -1: return "A" if tmp == 0 else "B" - return "Draw" if len(moves) == 9 else "Pending" \ No newline at end of file + return "Draw" if len(moves) == 9 else "Pending" + + \ No newline at end of file diff --git "a/1281.\346\225\264\346\225\260\347\232\204\345\220\204\344\275\215\347\247\257\345\222\214\344\271\213\345\267\256/1281-\346\225\264\346\225\260\347\232\204\345\220\204\344\275\215\347\247\257\345\222\214\344\271\213\345\267\256.py" "b/1281.\346\225\264\346\225\260\347\232\204\345\220\204\344\275\215\347\247\257\345\222\214\344\271\213\345\267\256/1281-\346\225\264\346\225\260\347\232\204\345\220\204\344\275\215\347\247\257\345\222\214\344\271\213\345\267\256.py" deleted file mode 100644 index d78a8cc..0000000 --- "a/1281.\346\225\264\346\225\260\347\232\204\345\220\204\344\275\215\347\247\257\345\222\214\344\271\213\345\267\256/1281-\346\225\264\346\225\260\347\232\204\345\220\204\344\275\215\347\247\257\345\222\214\344\271\213\345\267\256.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def subtractProductAndSum(self, n): - """ - :type n: int - :rtype: int - """ - product, s = 1, 0 - - while n: - n, digit = divmod(n, 10) - product *= digit - s += digit - return product - s \ No newline at end of file diff --git "a/1282.\347\224\250\346\210\267\345\210\206\347\273\204/1282-\347\224\250\346\210\267\345\210\206\347\273\204.py" "b/1282.\347\224\250\346\210\267\345\210\206\347\273\204/1282-\347\224\250\346\210\267\345\210\206\347\273\204.py" index 7b41f8e..4e18cba 100644 --- "a/1282.\347\224\250\346\210\267\345\210\206\347\273\204/1282-\347\224\250\346\210\267\345\210\206\347\273\204.py" +++ "b/1282.\347\224\250\346\210\267\345\210\206\347\273\204/1282-\347\224\250\346\210\267\345\210\206\347\273\204.py" @@ -1,18 +1,13 @@ -class Solution(object): - def groupThePeople(self, groupSizes): - """ - :type groupSizes: List[int] - :rtype: List[List[int]] - """ +class Solution: + def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: from collections import defaultdict - dic = defaultdict(list) + + size2people = defaultdict(list) + for i, size in enumerate(groupSizes): + size2people[size].append(i) res = [] - for i, x in enumerate(groupSizes): - if x not in dic or len(dic[x]) < x: - dic[x].append(i) - if len(dic[x]) == x: - res.append(dic[x]) - dic[x] = [] - + for size, peoples in size2people.items(): + for i in range(0, len(peoples), size): + res.append(peoples[i: i + size]) return res \ No newline at end of file diff --git "a/1283.\344\275\277\347\273\223\346\236\234\344\270\215\350\266\205\350\277\207\351\230\210\345\200\274\347\232\204\346\234\200\345\260\217\351\231\244\346\225\260/1283-\344\275\277\347\273\223\346\236\234\344\270\215\350\266\205\350\277\207\351\230\210\345\200\274\347\232\204\346\234\200\345\260\217\351\231\244\346\225\260.py" "b/1283.\344\275\277\347\273\223\346\236\234\344\270\215\350\266\205\350\277\207\351\230\210\345\200\274\347\232\204\346\234\200\345\260\217\351\231\244\346\225\260/1283-\344\275\277\347\273\223\346\236\234\344\270\215\350\266\205\350\277\207\351\230\210\345\200\274\347\232\204\346\234\200\345\260\217\351\231\244\346\225\260.py" deleted file mode 100644 index f98756d..0000000 --- "a/1283.\344\275\277\347\273\223\346\236\234\344\270\215\350\266\205\350\277\207\351\230\210\345\200\274\347\232\204\346\234\200\345\260\217\351\231\244\346\225\260/1283-\344\275\277\347\273\223\346\236\234\344\270\215\350\266\205\350\277\207\351\230\210\345\200\274\347\232\204\346\234\200\345\260\217\351\231\244\346\225\260.py" +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def smallestDivisor(self, nums, threshold): - """ - :type nums: List[int] - :type threshold: int - :rtype: int - """ - left, right = 1, max(nums) - - while left <= right: - mid = (left + right) // 2 - - tmp = 0 - for num in nums: - tmp += math.ceil(num * 1.0 / mid) - if tmp > threshold: - break - - if tmp > threshold: - left = mid + 1 - else: - right = mid - 1 - - return left diff --git "a/1284.\350\275\254\345\214\226\344\270\272\345\205\250\351\233\266\347\237\251\351\230\265\347\232\204\346\234\200\345\260\221\345\217\215\350\275\254\346\254\241\346\225\260/1284-\350\275\254\345\214\226\344\270\272\345\205\250\351\233\266\347\237\251\351\230\265\347\232\204\346\234\200\345\260\221\345\217\215\350\275\254\346\254\241\346\225\260.py" "b/1284.\350\275\254\345\214\226\344\270\272\345\205\250\351\233\266\347\237\251\351\230\265\347\232\204\346\234\200\345\260\221\345\217\215\350\275\254\346\254\241\346\225\260/1284-\350\275\254\345\214\226\344\270\272\345\205\250\351\233\266\347\237\251\351\230\265\347\232\204\346\234\200\345\260\221\345\217\215\350\275\254\346\254\241\346\225\260.py" deleted file mode 100644 index f0461fb..0000000 --- "a/1284.\350\275\254\345\214\226\344\270\272\345\205\250\351\233\266\347\237\251\351\230\265\347\232\204\346\234\200\345\260\221\345\217\215\350\275\254\346\254\241\346\225\260/1284-\350\275\254\345\214\226\344\270\272\345\205\250\351\233\266\347\237\251\351\230\265\347\232\204\346\234\200\345\260\221\345\217\215\350\275\254\346\254\241\346\225\260.py" +++ /dev/null @@ -1,71 +0,0 @@ -import numpy -class Solution(object): - def minFlips(self, mat): - """ - :type mat: List[List[int]] - :rtype: int - """ - if not mat or not mat[0]: - return 0 - - m, n = len(mat), len(mat[0]) - def check(matrix): - - return sum(sum(matrix, [])) == 0 - - def encode(matrix): - s = "" - for row in matrix: - for ch in row: - s += str(ch) - return s - - def decode(s): - mat = [] - for ch in s: - mat.append(int(ch)) - - res = [[0 for _ in range(n)] for _ in range(m)] - for i in range(m): - for j in range(n): - res[i][j] = mat[i * n + j] - return res - - def convert(mat, i, j, m, n): - for k in range(5): - x, y = i + dx[k], j + dy[k] - if 0 <= x < m and 0 <= y < n: - mat[x][y] ^= 1 - return mat - - res = -1 - from collections import deque - queue = deque([encode(mat)]) - - dx = [1, 0, 0, -1, 0] - dy = [0, 1, -1, 0, 0] - visited = set() - visited.add(encode(mat)) - while queue: - res += 1 - for _ in range(len(queue)): - encoded_cur = queue.popleft() - cur = decode(encoded_cur) - - if check(cur): - return res - - for i in range(m): - for j in range(n): - mat = convert(cur, i, j, m, n) - encoded_mat = encode(mat) - if encoded_mat not in visited: - queue.append(encoded_mat) - visited.add(encoded_mat) - - mat = convert(cur, i, j, m, n) - - return -1 - - - diff --git "a/1285.\346\211\276\345\210\260\350\277\236\347\273\255\345\214\272\351\227\264\347\232\204\345\274\200\345\247\213\345\222\214\347\273\223\346\235\237\346\225\260\345\255\227/1285-\346\211\276\345\210\260\350\277\236\347\273\255\345\214\272\351\227\264\347\232\204\345\274\200\345\247\213\345\222\214\347\273\223\346\235\237\346\225\260\345\255\227.sql" "b/1285.\346\211\276\345\210\260\350\277\236\347\273\255\345\214\272\351\227\264\347\232\204\345\274\200\345\247\213\345\222\214\347\273\223\346\235\237\346\225\260\345\255\227/1285-\346\211\276\345\210\260\350\277\236\347\273\255\345\214\272\351\227\264\347\232\204\345\274\200\345\247\213\345\222\214\347\273\223\346\235\237\346\225\260\345\255\227.sql" deleted file mode 100644 index eb6c348..0000000 --- "a/1285.\346\211\276\345\210\260\350\277\236\347\273\255\345\214\272\351\227\264\347\232\204\345\274\200\345\247\213\345\222\214\347\273\223\346\235\237\346\225\260\345\255\227/1285-\346\211\276\345\210\260\350\277\236\347\273\255\345\214\272\351\227\264\347\232\204\345\274\200\345\247\213\345\222\214\347\273\223\346\235\237\346\225\260\345\255\227.sql" +++ /dev/null @@ -1,6 +0,0 @@ -# Write your MySQL query statement below -select a.log_id as START_ID, b.log_id as END_ID -from (select log_id from Logs where log_id-1 not in (select * from logs)) as a, - (select log_id from Logs where log_id+1 not in (select * from logs)) as b -where b.log_id>=a.log_id -group by a.log_id \ No newline at end of file diff --git "a/1286.\345\255\227\346\257\215\347\273\204\345\220\210\350\277\255\344\273\243\345\231\250/1286-\345\255\227\346\257\215\347\273\204\345\220\210\350\277\255\344\273\243\345\231\250.py" "b/1286.\345\255\227\346\257\215\347\273\204\345\220\210\350\277\255\344\273\243\345\231\250/1286-\345\255\227\346\257\215\347\273\204\345\220\210\350\277\255\344\273\243\345\231\250.py" deleted file mode 100644 index adbe100..0000000 --- "a/1286.\345\255\227\346\257\215\347\273\204\345\220\210\350\277\255\344\273\243\345\231\250/1286-\345\255\227\346\257\215\347\273\204\345\220\210\350\277\255\344\273\243\345\231\250.py" +++ /dev/null @@ -1,30 +0,0 @@ -class CombinationIterator(object): - - def __init__(self, characters, combinationLength): - """ - :type characters: str - :type combinationLength: int - """ - # from itertools import permututations - self.s = list(itertools.combinations(characters, combinationLength)) - self.index = 0 - - def next(self): - """ - :rtype: str - """ - self.index += 1 - return "".join(self.s[self.index - 1]) - - def hasNext(self): - """ - :rtype: bool - """ - return self.index < len(self.s) - - - -# Your CombinationIterator object will be instantiated and called as such: -# obj = CombinationIterator(characters, combinationLength) -# param_1 = obj.next() -# param_2 = obj.hasNext() \ No newline at end of file diff --git "a/1287.\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\20725%\347\232\204\345\205\203\347\264\240/1287-\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\20725%\347\232\204\345\205\203\347\264\240.py" "b/1287.\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\20725%\347\232\204\345\205\203\347\264\240/1287-\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\20725%\347\232\204\345\205\203\347\264\240.py" deleted file mode 100644 index b899810..0000000 --- "a/1287.\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\20725%\347\232\204\345\205\203\347\264\240/1287-\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\20725%\347\232\204\345\205\203\347\264\240.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def findSpecialInteger(self, arr): - """ - :type arr: List[int] - :rtype: int - """ - cnt = 1 - for i, num in enumerate(arr): - if i: - if num == arr[i - 1]: - cnt += 1 - else: - cnt = 1 - if cnt > len(arr) / 4: - return num - \ No newline at end of file diff --git "a/1288.\345\210\240\351\231\244\350\242\253\350\246\206\347\233\226\345\214\272\351\227\264/1288-\345\210\240\351\231\244\350\242\253\350\246\206\347\233\226\345\214\272\351\227\264.py" "b/1288.\345\210\240\351\231\244\350\242\253\350\246\206\347\233\226\345\214\272\351\227\264/1288-\345\210\240\351\231\244\350\242\253\350\246\206\347\233\226\345\214\272\351\227\264.py" deleted file mode 100644 index 2e14aeb..0000000 --- "a/1288.\345\210\240\351\231\244\350\242\253\350\246\206\347\233\226\345\214\272\351\227\264/1288-\345\210\240\351\231\244\350\242\253\350\246\206\347\233\226\345\214\272\351\227\264.py" +++ /dev/null @@ -1,19 +0,0 @@ -class Solution(object): - def removeCoveredIntervals(self, intervals): - """ - :type intervals: List[List[int]] - :rtype: int - """ - intervals = sorted(intervals, key = lambda x:(x[0], x[1])) - - start, end = intervals[0][0], intervals[0][1] - - n = len(intervals) - max_end = intervals[0][1] - for s, e in intervals: - if max_end >= e: - n -= 1 - else: - max_end = e - - return n + 1 \ No newline at end of file diff --git "a/1289.\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214II/1289-\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214II.py" "b/1289.\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214II/1289-\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214II.py" deleted file mode 100644 index 5e15a73..0000000 --- "a/1289.\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214II/1289-\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214II.py" +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def minFallingPathSum(self, arr): - """ - :type arr: List[List[int]] - :rtype: int - """ - from heapq import * - if not arr or not arr[0]: - return 0 - - m, n = len(arr), len(arr[0]) - - for i in range(1, m): - max_heap = [] - for j in range(n): - if len(max_heap) < 2: - heappush(max_heap, -arr[i - 1][j]) - else: - heappush(max_heap, -arr[i - 1][j]) - heappop(max_heap) - # print max_heap - for j in range(n): - arr[i][j] -= max_heap[1] if -max_heap[1] != arr[i - 1][j] else max_heap[0] - return min(arr[-1]) \ No newline at end of file diff --git "a/1290.\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260/1290-\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260.py" "b/1290.\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260/1290-\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260.py" index 89fd7bb..70a5142 100644 --- "a/1290.\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260/1290-\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260.py" +++ "b/1290.\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260/1290-\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260.py" @@ -1,17 +1,13 @@ # Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def getDecimalValue(self, head): - """ - :type head: ListNode - :rtype: int - """ +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def getDecimalValue(self, head: ListNode) -> int: res = 0 - while head: - res = res * 2 + head.val - head = head.next + p = head + while p: + res = res * 2 + p.val + p = p.next return res \ No newline at end of file diff --git "a/1291.\351\241\272\346\254\241\346\225\260/1291-\351\241\272\346\254\241\346\225\260.py" "b/1291.\351\241\272\346\254\241\346\225\260/1291-\351\241\272\346\254\241\346\225\260.py" deleted file mode 100644 index 09dceb9..0000000 --- "a/1291.\351\241\272\346\254\241\346\225\260/1291-\351\241\272\346\254\241\346\225\260.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def sequentialDigits(self, low, high): - """ - :type low: int - :type high: int - :rtype: List[int] - """ - nums = [12, 23, 34, 45, 56, 67, 78, 89, - 123, 234, 345, 456, 567, 678, 789, - 1234, 2345, 3456,4567, 5678, 6789, - 12345, 23456, 34567, 45678, 56789, - 123456, 234567, 345678, 456789, - 1234567, 2345678, 3456789, - 12345678, 23456789, - 123456789] - res = [] - - for num in nums: - if low <= num <= high: - res.append(num) - - return res \ No newline at end of file diff --git "a/1295.\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227/1295-\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227.py" "b/1295.\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227/1295-\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227.py" deleted file mode 100644 index 5e87082..0000000 --- "a/1295.\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227/1295-\347\273\237\350\256\241\344\275\215\346\225\260\344\270\272\345\201\266\346\225\260\347\232\204\346\225\260\345\255\227.py" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution(object): - def findNumbers(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - res = 0 - for num in nums: - if len(str(num)) % 2 == 0: - res += 1 - return res \ No newline at end of file diff --git "a/1296.\345\210\222\345\210\206\346\225\260\347\273\204\344\270\272\350\277\236\347\273\255\346\225\260\345\255\227\347\232\204\351\233\206\345\220\210/1296-\345\210\222\345\210\206\346\225\260\347\273\204\344\270\272\350\277\236\347\273\255\346\225\260\345\255\227\347\232\204\351\233\206\345\220\210.py" "b/1296.\345\210\222\345\210\206\346\225\260\347\273\204\344\270\272\350\277\236\347\273\255\346\225\260\345\255\227\347\232\204\351\233\206\345\220\210/1296-\345\210\222\345\210\206\346\225\260\347\273\204\344\270\272\350\277\236\347\273\255\346\225\260\345\255\227\347\232\204\351\233\206\345\220\210.py" deleted file mode 100644 index 986dd8c..0000000 --- "a/1296.\345\210\222\345\210\206\346\225\260\347\273\204\344\270\272\350\277\236\347\273\255\346\225\260\345\255\227\347\232\204\351\233\206\345\220\210/1296-\345\210\222\345\210\206\346\225\260\347\273\204\344\270\272\350\277\236\347\273\255\346\225\260\345\255\227\347\232\204\351\233\206\345\220\210.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def isPossibleDivide(self, nums, k): - """ - :type nums: List[int] - :type k: int - :rtype: bool - """ - from collections import Counter, deque - dic = Counter(nums) - nums = deque(sorted(list(set(nums)))) - - res = [] - while nums: - num = nums[0] - for i in range(k): - target = num + i - if target not in dic: - return False - dic[target] -= 1 - if dic[target] == 0: - nums.popleft() - return True \ No newline at end of file diff --git "a/1297.\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\345\207\272\347\216\260\346\254\241\346\225\260/1297-\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\345\207\272\347\216\260\346\254\241\346\225\260.py" "b/1297.\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\345\207\272\347\216\260\346\254\241\346\225\260/1297-\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\345\207\272\347\216\260\346\254\241\346\225\260.py" deleted file mode 100644 index 7c2e30e..0000000 --- "a/1297.\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\345\207\272\347\216\260\346\254\241\346\225\260/1297-\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\345\207\272\347\216\260\346\254\241\346\225\260.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def maxFreq(self, s, maxLetters, minSize, maxSize): - """ - :type s: str - :type maxLetters: int - :type minSize: int - :type maxSize: int - :rtype: int - """ - from collections import defaultdict - - record = defaultdict(int) - - res = 0 - for i in range(len(s) - minSize + 1): - substring = s[i:i+minSize] - if len(set(substring)) <= maxLetters: - record[substring] += 1 - res = max(res, record[substring]) - - return res \ No newline at end of file diff --git "a/1298.\344\275\240\350\203\275\344\273\216\347\233\222\345\255\220\351\207\214\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\263\226\346\236\234\346\225\260/1298-\344\275\240\350\203\275\344\273\216\347\233\222\345\255\220\351\207\214\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\263\226\346\236\234\346\225\260.py" "b/1298.\344\275\240\350\203\275\344\273\216\347\233\222\345\255\220\351\207\214\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\263\226\346\236\234\346\225\260/1298-\344\275\240\350\203\275\344\273\216\347\233\222\345\255\220\351\207\214\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\263\226\346\236\234\346\225\260.py" deleted file mode 100644 index b4fc042..0000000 --- "a/1298.\344\275\240\350\203\275\344\273\216\347\233\222\345\255\220\351\207\214\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\263\226\346\236\234\346\225\260/1298-\344\275\240\350\203\275\344\273\216\347\233\222\345\255\220\351\207\214\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\263\226\346\236\234\346\225\260.py" +++ /dev/null @@ -1,34 +0,0 @@ -class Solution(object): - def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes): - """ - :type status: List[int] - :type candies: List[int] - :type keys: List[List[int]] - :type containedBoxes: List[List[int]] - :type initialBoxes: List[int] - :rtype: int - """ - if not initialBoxes: - return 0 - - # boxes = set(initialBoxes) - owned_keys = set() - from collections import deque - queue = deque(initialBoxes) - res = 0 - record = dict() - while queue: - cur = queue.popleft() - # print cur - if status[cur] or cur in owned_keys: # This box could be opened - for key in keys[cur]: - owned_keys.add(key) - for box in containedBoxes[cur]: - queue.append(box) - res += candies[cur] - else: - if cur in record and record[cur] == owned_keys: - break - queue.append(cur) - record[cur] = owned_keys - return res \ No newline at end of file diff --git "a/1299.\345\260\206\346\257\217\344\270\252\345\205\203\347\264\240\346\233\277\346\215\242\344\270\272\345\217\263\344\276\247\346\234\200\345\244\247\345\205\203\347\264\240/1299-\345\260\206\346\257\217\344\270\252\345\205\203\347\264\240\346\233\277\346\215\242\344\270\272\345\217\263\344\276\247\346\234\200\345\244\247\345\205\203\347\264\240.py" "b/1299.\345\260\206\346\257\217\344\270\252\345\205\203\347\264\240\346\233\277\346\215\242\344\270\272\345\217\263\344\276\247\346\234\200\345\244\247\345\205\203\347\264\240/1299-\345\260\206\346\257\217\344\270\252\345\205\203\347\264\240\346\233\277\346\215\242\344\270\272\345\217\263\344\276\247\346\234\200\345\244\247\345\205\203\347\264\240.py" deleted file mode 100644 index af2248b..0000000 --- "a/1299.\345\260\206\346\257\217\344\270\252\345\205\203\347\264\240\346\233\277\346\215\242\344\270\272\345\217\263\344\276\247\346\234\200\345\244\247\345\205\203\347\264\240/1299-\345\260\206\346\257\217\344\270\252\345\205\203\347\264\240\346\233\277\346\215\242\344\270\272\345\217\263\344\276\247\346\234\200\345\244\247\345\205\203\347\264\240.py" +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def replaceElements(self, arr): - """ - :type arr: List[int] - :rtype: List[int] - """ - right_max = -1 - for i in range(len(arr) - 1, -1, -1): - tmp = arr[i] - arr[i] = right_max - right_max = max(right_max, tmp) - return arr \ No newline at end of file diff --git "a/1300.\350\275\254\345\217\230\346\225\260\347\273\204\345\220\216\346\234\200\346\216\245\350\277\221\347\233\256\346\240\207\345\200\274\347\232\204\346\225\260\347\273\204\345\222\214/1300-\350\275\254\345\217\230\346\225\260\347\273\204\345\220\216\346\234\200\346\216\245\350\277\221\347\233\256\346\240\207\345\200\274\347\232\204\346\225\260\347\273\204\345\222\214.py" "b/1300.\350\275\254\345\217\230\346\225\260\347\273\204\345\220\216\346\234\200\346\216\245\350\277\221\347\233\256\346\240\207\345\200\274\347\232\204\346\225\260\347\273\204\345\222\214/1300-\350\275\254\345\217\230\346\225\260\347\273\204\345\220\216\346\234\200\346\216\245\350\277\221\347\233\256\346\240\207\345\200\274\347\232\204\346\225\260\347\273\204\345\222\214.py" deleted file mode 100644 index d2eed65..0000000 --- "a/1300.\350\275\254\345\217\230\346\225\260\347\273\204\345\220\216\346\234\200\346\216\245\350\277\221\347\233\256\346\240\207\345\200\274\347\232\204\346\225\260\347\273\204\345\222\214/1300-\350\275\254\345\217\230\346\225\260\347\273\204\345\220\216\346\234\200\346\216\245\350\277\221\347\233\256\346\240\207\345\200\274\347\232\204\346\225\260\347\273\204\345\222\214.py" +++ /dev/null @@ -1,39 +0,0 @@ -class Solution(object): - def findBestValue(self, arr, target): - """ - :type arr: List[int] - :type target: int - :rtype: int - """ - left, right = 0, max(arr) - sub, ans = float("inf"), float("inf") - - while left <= right: - mid = (left + right) // 2 # mid 为本次猜测的答案 - - tmp = 0 # tmp 是本次猜测新数组之和 - for num in arr: - if num > mid: - tmp += mid - else: - tmp += num - - cur_sub = abs(tmp - target) #当前差的最小值 - - if cur_sub < sub: # 如果有更小的答案 - sub = cur_sub - ans = mid - elif cur_sub == sub: - ans = min(ans, mid) - - if tmp > target: - right = mid - 1 - elif tmp < target: - left = mid + 1 - elif tmp == target: - return mid - - return ans - - - \ No newline at end of file diff --git "a/1301.\346\234\200\345\244\247\345\276\227\345\210\206\347\232\204\350\267\257\345\276\204\346\225\260\347\233\256/1301-\346\234\200\345\244\247\345\276\227\345\210\206\347\232\204\350\267\257\345\276\204\346\225\260\347\233\256.py" "b/1301.\346\234\200\345\244\247\345\276\227\345\210\206\347\232\204\350\267\257\345\276\204\346\225\260\347\233\256/1301-\346\234\200\345\244\247\345\276\227\345\210\206\347\232\204\350\267\257\345\276\204\346\225\260\347\233\256.py" deleted file mode 100644 index 0e1be3e..0000000 --- "a/1301.\346\234\200\345\244\247\345\276\227\345\210\206\347\232\204\350\267\257\345\276\204\346\225\260\347\233\256/1301-\346\234\200\345\244\247\345\276\227\345\210\206\347\232\204\350\267\257\345\276\204\346\225\260\347\233\256.py" +++ /dev/null @@ -1,30 +0,0 @@ -class Solution(object): - def pathsWithMaxScore(self, board): - """ - :type board: List[str] - :rtype: List[int] - """ - n = len(board) - MOD = 10 ** 9 + 7 - - dp = [[[float("-inf"), 0] for _ in range(n + 1)] for _ in range(n + 1)] - dp[n - 1][n - 1] = [0, 1] - for i in range(n - 1, -1, -1): - for j in range(n - 1, -1, -1): - if board[i][j] not in "XS": - - for dx, dy in [[0, 1], [1, 0], [1, 1]]: - if dp[i][j][0] < dp[i + dx][j + dy][0]: - dp[i][j] = [dp[i + dx][j + dy][0], 0] - - if dp[i][j][0] == dp[i + dx][j + dy][0]: - dp[i][j][1] += dp[i + dx][j + dy][1] - - dp[i][j][0] += int(board[i][j]) if board[i][j] != "E" else 0 - - return [dp[0][0][0] if dp[0][0][1] else 0, dp[0][0][1] % MOD] - - - - - \ No newline at end of file diff --git "a/1302.\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214/1302-\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214.py" "b/1302.\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214/1302-\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214.py" index e98e035..fb0b769 100644 --- "a/1302.\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214/1302-\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214.py" +++ "b/1302.\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214/1302-\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214.py" @@ -1,27 +1,21 @@ # Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def deepestLeavesSum(self, root): - """ - :type root: TreeNode - :rtype: int - """ - from collections import deque - if not root: - return 0 - queue = deque([root]) +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def deepestLeavesSum(self, root: Optional[TreeNode]) -> int: + queue = [root] while queue: - res = [] - for _ in range(len(queue)): - node = queue.popleft() - res.append(node.val) + level_sum = 0 + next_queue = [] + for node in queue: if node.left: - queue += [node.left] + next_queue.append(node.left) if node.right: - queue += [node.right] - return sum(res) \ No newline at end of file + next_queue.append(node.right) + level_sum += node.val + + queue = next_queue[:] + return level_sum \ No newline at end of file diff --git "a/1304.\345\222\214\344\270\272\351\233\266\347\232\204N\344\270\252\345\224\257\344\270\200\346\225\264\346\225\260/1304-\345\222\214\344\270\272\351\233\266\347\232\204N\344\270\252\345\224\257\344\270\200\346\225\264\346\225\260.py" "b/1304.\345\222\214\344\270\272\351\233\266\347\232\204N\344\270\252\345\224\257\344\270\200\346\225\264\346\225\260/1304-\345\222\214\344\270\272\351\233\266\347\232\204N\344\270\252\345\224\257\344\270\200\346\225\264\346\225\260.py" deleted file mode 100644 index 8f816d2..0000000 --- "a/1304.\345\222\214\344\270\272\351\233\266\347\232\204N\344\270\252\345\224\257\344\270\200\346\225\264\346\225\260/1304-\345\222\214\344\270\272\351\233\266\347\232\204N\344\270\252\345\224\257\344\270\200\346\225\264\346\225\260.py" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution(object): - def sumZero(self, n): - """ - :type n: int - :rtype: List[int] - """ - res = [] - - for i in range(1, n // 2 + 1): - res.append(i) - res.append(-i) - if n % 2: - res.append(0) - return res \ No newline at end of file diff --git "a/1305.\344\270\244\346\243\265\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\211\200\346\234\211\345\205\203\347\264\240/1305-\344\270\244\346\243\265\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\211\200\346\234\211\345\205\203\347\264\240.py" "b/1305.\344\270\244\346\243\265\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\211\200\346\234\211\345\205\203\347\264\240/1305-\344\270\244\346\243\265\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\211\200\346\234\211\345\205\203\347\264\240.py" deleted file mode 100644 index ebc4858..0000000 --- "a/1305.\344\270\244\346\243\265\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\211\200\346\234\211\345\205\203\347\264\240/1305-\344\270\244\346\243\265\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\211\200\346\234\211\345\205\203\347\264\240.py" +++ /dev/null @@ -1,24 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def getAllElements(self, root1, root2): - """ - :type root1: TreeNode - :type root2: TreeNode - :rtype: List[int] - """ - - def inorder(node): - if not node: - return [] - return inorder(node.left) + [node.val] + inorder(node.right) - - l1 = inorder(root1) - l2 = inorder(root2) - - return sorted(l1 + l2) \ No newline at end of file diff --git "a/1306.\350\267\263\350\267\203\346\270\270\346\210\217III/1306-\350\267\263\350\267\203\346\270\270\346\210\217III.py" "b/1306.\350\267\263\350\267\203\346\270\270\346\210\217III/1306-\350\267\263\350\267\203\346\270\270\346\210\217III.py" deleted file mode 100644 index e33687f..0000000 --- "a/1306.\350\267\263\350\267\203\346\270\270\346\210\217III/1306-\350\267\263\350\267\203\346\270\270\346\210\217III.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def canReach(self, arr, start): - """ - :type arr: List[int] - :type start: int - :rtype: bool - """ - from collections import deque - queue = deque([start]) - visited = set([start]) - while queue: - cur = queue.popleft() - - if arr[cur] == 0: - return True - - for i in [cur + arr[cur], cur - arr[cur]]: - if 0 <= i < len(arr) and i not in visited: - visited.add(i) - queue.append(i) - return False \ No newline at end of file diff --git "a/1309.\350\247\243\347\240\201\345\255\227\346\257\215\345\210\260\346\225\264\346\225\260\346\230\240\345\260\204/1309-\350\247\243\347\240\201\345\255\227\346\257\215\345\210\260\346\225\264\346\225\260\346\230\240\345\260\204.py" "b/1309.\350\247\243\347\240\201\345\255\227\346\257\215\345\210\260\346\225\264\346\225\260\346\230\240\345\260\204/1309-\350\247\243\347\240\201\345\255\227\346\257\215\345\210\260\346\225\264\346\225\260\346\230\240\345\260\204.py" deleted file mode 100644 index 7b5faa6..0000000 --- "a/1309.\350\247\243\347\240\201\345\255\227\346\257\215\345\210\260\346\225\264\346\225\260\346\230\240\345\260\204/1309-\350\247\243\347\240\201\345\255\227\346\257\215\345\210\260\346\225\264\346\225\260\346\230\240\345\260\204.py" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution(object): - def freqAlphabets(self, s): - """ - :type s: str - :rtype: str - """ - res, i = "", 0 - while i < len(s): - if i + 2 < len(s) and s[i + 2] == "#": - res += chr(ord("a") + int(s[i:i + 2]) - 1) - i += 3 - else: - res += chr(ord("a") + int(s[i]) - 1) - i += 1 - return res \ No newline at end of file diff --git "a/131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262/131-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.py" "b/131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262/131-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.py" deleted file mode 100644 index fadcc8c..0000000 --- "a/131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262/131-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.py" +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def partition(self, s): - """ - :type s: str - :rtype: List[List[str]] - """ - - res = [] - - def dfs(start, tmp): - - if start >= len(s): - # print tmp - res.append(tmp[:]) - return - - for i in range(start, len(s)): - substring = s[start:i + 1] - if substring == substring[::-1]: - tmp.append(substring) - dfs(i + 1, tmp) - tmp.pop() - dfs(0, []) - return res \ No newline at end of file diff --git "a/1310.\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242/1310-\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242.py" "b/1310.\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242/1310-\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242.py" deleted file mode 100644 index bffcb3b..0000000 --- "a/1310.\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242/1310-\345\255\220\346\225\260\347\273\204\345\274\202\346\210\226\346\237\245\350\257\242.py" +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def xorQueries(self, arr, queries): - """ - :type arr: List[int] - :type queries: List[List[int]] - :rtype: List[int] - """ - - prefix = [0 for _ in [0] + arr] - for i in range(len(arr)): - prefix[i + 1] = prefix[i] ^ arr[i] - - # print prefix - res = [] - for l, r in queries: - res.append(prefix[l] ^ prefix[r + 1]) - - return res \ No newline at end of file diff --git "a/1311.\350\216\267\345\217\226\344\275\240\345\245\275\345\217\213\345\267\262\350\247\202\347\234\213\347\232\204\350\247\206\351\242\221/1311-\350\216\267\345\217\226\344\275\240\345\245\275\345\217\213\345\267\262\350\247\202\347\234\213\347\232\204\350\247\206\351\242\221.py" "b/1311.\350\216\267\345\217\226\344\275\240\345\245\275\345\217\213\345\267\262\350\247\202\347\234\213\347\232\204\350\247\206\351\242\221/1311-\350\216\267\345\217\226\344\275\240\345\245\275\345\217\213\345\267\262\350\247\202\347\234\213\347\232\204\350\247\206\351\242\221.py" deleted file mode 100644 index 094b33b..0000000 --- "a/1311.\350\216\267\345\217\226\344\275\240\345\245\275\345\217\213\345\267\262\350\247\202\347\234\213\347\232\204\350\247\206\351\242\221/1311-\350\216\267\345\217\226\344\275\240\345\245\275\345\217\213\345\267\262\350\247\202\347\234\213\347\232\204\350\247\206\351\242\221.py" +++ /dev/null @@ -1,34 +0,0 @@ -class Solution(object): - def watchedVideosByFriends(self, watchedVideos, friends, idd, level): - """ - :type watchedVideos: List[List[str]] - :type friends: List[List[int]] - :type id: int - :type level: int - :rtype: List[str] - """ - from collections import deque,defaultdict - # 1. find all k-level friends by BFS - queue = deque([idd]) - visited = set([idd]) - for l in range(level): - friendset = set() - for _ in range(len(queue)): - cur = queue.popleft() - - for fri in friends[cur]: - if fri not in visited: - visited.add(fri) - queue.append(fri) - - # 2. find watched videos of all k-level friends - videos = defaultdict(int) - for friend in queue: - for video in watchedVideos[friend]: - videos[video] += 1 - - # 3. count the frequency - res = [[key, val] for key, val in videos.items()] - res = sorted(res, key = lambda x:(x[1], x[0])) - - return [x[0] for x in res] \ No newline at end of file diff --git "a/1312.\350\256\251\345\255\227\347\254\246\344\270\262\346\210\220\344\270\272\345\233\236\346\226\207\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260/1312-\350\256\251\345\255\227\347\254\246\344\270\262\346\210\220\344\270\272\345\233\236\346\226\207\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260.py" "b/1312.\350\256\251\345\255\227\347\254\246\344\270\262\346\210\220\344\270\272\345\233\236\346\226\207\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260/1312-\350\256\251\345\255\227\347\254\246\344\270\262\346\210\220\344\270\272\345\233\236\346\226\207\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260.py" deleted file mode 100644 index 99d3117..0000000 --- "a/1312.\350\256\251\345\255\227\347\254\246\344\270\262\346\210\220\344\270\272\345\233\236\346\226\207\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260/1312-\350\256\251\345\255\227\347\254\246\344\270\262\346\210\220\344\270\272\345\233\236\346\226\207\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260.py" +++ /dev/null @@ -1,23 +0,0 @@ -class Solution(object): - def minInsertions(self, s): - """ - :type s: str - :rtype: int - """ - return len(s) - self.longestPalindromeSubseq(s) - def longestPalindromeSubseq(self, s): - """ - :type s: str - :rtype: int - """ - n = len(s) - dp = [[0] * n for _ in range(n)] - - for i in range(n): - dp[i][i] = 1 - for j in range(i - 1, -1, -1): - if s[i] == s[j]: - dp[i][j] = dp[i - 1][j + 1] + 2 - else: - dp[i][j] = max(dp[i][j + 1], dp[i - 1][j]) - return dp[n - 1][0] \ No newline at end of file diff --git "a/1313.\350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250/1313-\350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250.py" "b/1313.\350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250/1313-\350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250.py" deleted file mode 100644 index d380b2c..0000000 --- "a/1313.\350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250/1313-\350\247\243\345\216\213\347\274\251\347\274\226\347\240\201\345\210\227\350\241\250.py" +++ /dev/null @@ -1,10 +0,0 @@ -class Solution(object): - def decompressRLElist(self, nums): - """ - :type nums: List[int] - :rtype: List[int] - """ - res = [] - for i in range(0, len(nums), 2): - res += nums[i] * [nums[i + 1]] - return res \ No newline at end of file diff --git "a/1315.\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214/1315-\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214.py" "b/1315.\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214/1315-\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214.py" index 8a80225..564b692 100644 --- "a/1315.\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214/1315-\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214.py" +++ "b/1315.\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214/1315-\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214.py" @@ -1,24 +1,21 @@ # Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def sumEvenGrandparent(self, root): - """ - :type root: TreeNode - :rtype: int - """ - self.res = 0 - def dfs(node, parent, grand): +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumEvenGrandparent(self, root: TreeNode) -> int: + self.sum = 0 + def dfs(node, isParentEven, isGrandparentEven): if not node: return - if grand: - self.res += node.val - - dfs(node.left, node.val % 2 == 0, parent) - dfs(node.right, node.val % 2 == 0, parent) + + if isGrandparentEven: + self.sum += node.val + + dfs(node.left, node.val % 2 == 0, isParentEven) + dfs(node.right, node.val % 2 == 0, isParentEven) + dfs(root, False, False) - return self.res \ No newline at end of file + return self.sum \ No newline at end of file diff --git "a/1317.\345\260\206\346\225\264\346\225\260\350\275\254\346\215\242\344\270\272\344\270\244\344\270\252\346\227\240\351\233\266\346\225\264\346\225\260\347\232\204\345\222\214/1317-\345\260\206\346\225\264\346\225\260\350\275\254\346\215\242\344\270\272\344\270\244\344\270\252\346\227\240\351\233\266\346\225\264\346\225\260\347\232\204\345\222\214.py" "b/1317.\345\260\206\346\225\264\346\225\260\350\275\254\346\215\242\344\270\272\344\270\244\344\270\252\346\227\240\351\233\266\346\225\264\346\225\260\347\232\204\345\222\214/1317-\345\260\206\346\225\264\346\225\260\350\275\254\346\215\242\344\270\272\344\270\244\344\270\252\346\227\240\351\233\266\346\225\264\346\225\260\347\232\204\345\222\214.py" deleted file mode 100644 index bea11e1..0000000 --- "a/1317.\345\260\206\346\225\264\346\225\260\350\275\254\346\215\242\344\270\272\344\270\244\344\270\252\346\227\240\351\233\266\346\225\264\346\225\260\347\232\204\345\222\214/1317-\345\260\206\346\225\264\346\225\260\350\275\254\346\215\242\344\270\272\344\270\244\344\270\252\346\227\240\351\233\266\346\225\264\346\225\260\347\232\204\345\222\214.py" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution(object): - def getNoZeroIntegers(self, n): - """ - :type n: int - :rtype: List[int] - """ - - for i in range(1, n): - tmp = n - i - if "0" not in str(i) and "0" not in str(tmp): - return [i, tmp] \ No newline at end of file diff --git "a/132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II/132-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.py" "b/132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II/132-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.py" deleted file mode 100644 index 2dd7a1b..0000000 --- "a/132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II/132-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.py" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution(object): - def minCut(self, s): - """ - :type s: str - :rtype: int - """ - # dp[i][j] - dp = [len(s) for _ in range(len(s) + 1)] - dp[0] = -1 - for i in range(len(s)): - for j in range(i + 1): - if s[j:i + 1] == s[j:i + 1][::-1]: - dp[i + 1] = min(dp[j] + 1, dp[i + 1]) - # print dp - return dp[-1] \ No newline at end of file diff --git "a/133.\345\205\213\351\232\206\345\233\276/133-\345\205\213\351\232\206\345\233\276.py" "b/133.\345\205\213\351\232\206\345\233\276/133-\345\205\213\351\232\206\345\233\276.py" deleted file mode 100644 index 936a69b..0000000 --- "a/133.\345\205\213\351\232\206\345\233\276/133-\345\205\213\351\232\206\345\233\276.py" +++ /dev/null @@ -1,34 +0,0 @@ -""" -# Definition for a Node. -class Node(object): - def __init__(self, val, neighbors): - self.val = val - self.neighbors = neighbors -""" -class Solution(object): - def cloneGraph(self, node): - """ - :type node: Node - :rtype: Node - """ - from collections import defaultdict, deque - # neibors = defaultdict(list) # key is the original nodes, value is its neibors - mapping = dict() # key is the original node, value is its copy - - queue = deque([node]) - visited = set() - visited.add(node) - while queue: - cur = queue.popleft() - visited.add(cur) - - copy = Node(cur.val, []) - mapping[cur] = copy - for neigh in cur.neighbors: - if neigh not in visited: - queue.append(neigh) - - for cur, copy in mapping.items(): - for each in cur.neighbors: - copy.neighbors.append(mapping[each]) - return mapping[node] \ No newline at end of file diff --git "a/1337.\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214/1337-\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214.py" "b/1337.\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214/1337-\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214.py" new file mode 100644 index 0000000..89e5ff2 --- /dev/null +++ "b/1337.\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214/1337-\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214.py" @@ -0,0 +1,5 @@ +class Solution: + def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]: + pair = [(i, sum(row)) for i, row in enumerate(mat)] + pair.sort(key = lambda x: x[1]) + return [p[0] for p in pair[:k]] \ No newline at end of file diff --git "a/1338.\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212/1338-\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212 2.py" "b/1338.\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212/1338-\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212 2.py" new file mode 100644 index 0000000..201e5f8 --- /dev/null +++ "b/1338.\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212/1338-\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212 2.py" @@ -0,0 +1,23 @@ +from heapq import * +from collections import Counter +class Solution(object): + def minSetSize(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + t = len(arr) // 2 + dic = Counter(arr) + + queue = [] + for key, val in dic.items(): + heappush(queue, -val) + + cnt = 0 + res = 0 + while cnt < t: + tmp = heappop(queue) + res += 1 + cnt += -tmp + # print cnt, tmp, t + return res \ No newline at end of file diff --git "a/1351.\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260/1351-\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260 2.py" "b/1351.\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260/1351-\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260 2.py" new file mode 100644 index 0000000..7a7ef5d --- /dev/null +++ "b/1351.\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260/1351-\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260 2.py" @@ -0,0 +1,16 @@ +class Solution(object): + def countNegatives(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + if not grid or not grid[0]: + return 0 + m, n = len(grid), len(grid[0]) + + res = 0 + for i in range(m): + for j in range(n): + if grid[i][j] < 0: + res += 1 + return res \ No newline at end of file diff --git "a/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217/1356-\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.py" "b/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217/1356-\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.py" deleted file mode 100644 index 55bda80..0000000 --- "a/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217/1356-\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def sortByBits(self, arr): - """ - :type arr: List[int] - :rtype: List[int] - """ - return sorted(arr, key = lambda x:(str(bin(x)).count("1"), x)) \ No newline at end of file diff --git "a/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227/1365-\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.py" "b/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227/1365-\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.py" deleted file mode 100644 index d0f49c9..0000000 --- "a/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227/1365-\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def smallerNumbersThanCurrent(self, nums): - """ - :type nums: List[int] - :rtype: List[int] - """ - res = [0 for _ in nums] - for i in range(len(nums)): - for j in range(len(nums)): - if nums[j] < nums[i]: - res[i] += 1 - - return res \ No newline at end of file diff --git "a/1370.\344\270\212\345\215\207\344\270\213\351\231\215\345\255\227\347\254\246\344\270\262/1370-\344\270\212\345\215\207\344\270\213\351\231\215\345\255\227\347\254\246\344\270\262.py" "b/1370.\344\270\212\345\215\207\344\270\213\351\231\215\345\255\227\347\254\246\344\270\262/1370-\344\270\212\345\215\207\344\270\213\351\231\215\345\255\227\347\254\246\344\270\262.py" deleted file mode 100644 index faabd50..0000000 --- "a/1370.\344\270\212\345\215\207\344\270\213\351\231\215\345\255\227\347\254\246\344\270\262/1370-\344\270\212\345\215\207\344\270\213\351\231\215\345\255\227\347\254\246\344\270\262.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def sortString(self, s): - """ - :type s: str - :rtype: str - """ - from collections import Counter - dic = Counter(s) - res = "" - while len(res) < len(s): - for ch in "abcdefghijklmnopqrstuvwxyz": - if ch in dic and dic[ch]: - res += ch - dic[ch] -= 1 - for ch in "abcdefghijklmnopqrstuvwxyz"[::-1]: - if ch in dic and dic[ch]: - res += ch - dic[ch] -= 1 - - return res \ No newline at end of file diff --git "a/1374.\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262/1374-\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262 2.py" "b/1374.\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262/1374-\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262 2.py" new file mode 100644 index 0000000..e09d933 --- /dev/null +++ "b/1374.\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262/1374-\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262 2.py" @@ -0,0 +1,9 @@ +class Solution(object): + def generateTheString(self, n): + """ + :type n: int + :rtype: str + """ + if n % 2: + return "a" * n + return "a" * (n - 1) + "b" \ No newline at end of file diff --git "a/1379.\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271/1379-\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271.py" "b/1379.\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271/1379-\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271.py" index 32a3a01..8c9b498 100644 --- "a/1379.\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271/1379-\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271.py" +++ "b/1379.\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271/1379-\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271.py" @@ -1,28 +1,20 @@ # Definition for a binary tree node. -# class TreeNode(object): +# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None -class Solution(object): - def getTargetCopy(self, original, cloned, target): - """ - :type original: TreeNode - :type cloned: TreeNode - :type target: TreeNode - :rtype: TreeNode - """ - self.res = None - def dfs(node1, node2): - if not node1: - return - if node1 == target: - self.res = node2 - return - - dfs(node1.left, node2.left) - dfs(node1.right, node2.right) - - dfs(original, cloned) - return self.res \ No newline at end of file +class Solution: + def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode: + queue = [cloned] + while queue: + next_queue = [] + for node in queue: + if node: + if node.val == target.val: + return node + next_queue.append(node.left) + next_queue.append(node.right) + queue = next_queue[:] + \ No newline at end of file diff --git "a/138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250.py" "b/138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250.py" deleted file mode 100644 index 500145b..0000000 --- "a/138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250.py" +++ /dev/null @@ -1,26 +0,0 @@ -""" -# Definition for a Node. -class Node(object): - def __init__(self, val, next, random): - self.val = val - self.next = next - self.random = random -""" -class Solution(object): - def copyRandomList(self, head): - """ - :type head: Node - :rtype: Node - """ - cur = head - mapping = dict() - while cur: - mapping[cur] = Node(cur.val, None, None) - cur = cur.next - - for cur, copy in mapping.items(): - if cur.next: - copy.next = mapping[cur.next] - if cur.random: - copy.random = mapping[cur.random] - return mapping[head] if head else head \ No newline at end of file diff --git "a/1385.\344\270\244\344\270\252\346\225\260\347\273\204\351\227\264\347\232\204\350\267\235\347\246\273\345\200\274/1385-\344\270\244\344\270\252\346\225\260\347\273\204\351\227\264\347\232\204\350\267\235\347\246\273\345\200\274.py" "b/1385.\344\270\244\344\270\252\346\225\260\347\273\204\351\227\264\347\232\204\350\267\235\347\246\273\345\200\274/1385-\344\270\244\344\270\252\346\225\260\347\273\204\351\227\264\347\232\204\350\267\235\347\246\273\345\200\274.py" deleted file mode 100644 index 037aa40..0000000 --- "a/1385.\344\270\244\344\270\252\346\225\260\347\273\204\351\227\264\347\232\204\350\267\235\347\246\273\345\200\274/1385-\344\270\244\344\270\252\346\225\260\347\273\204\351\227\264\347\232\204\350\267\235\347\246\273\345\200\274.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def findTheDistanceValue(self, arr1, arr2, d): - """ - :type arr1: List[int] - :type arr2: List[int] - :type d: int - :rtype: int - """ - res = 0 - - for num1 in arr1: - flag = 1 - for num2 in arr2: - if abs(num1 - num2) <= d: - flag = 0 - break - if flag: - res += 1 - - return res \ No newline at end of file diff --git "a/1386.\345\256\211\346\216\222\347\224\265\345\275\261\351\231\242\345\272\247\344\275\215/1386-\345\256\211\346\216\222\347\224\265\345\275\261\351\231\242\345\272\247\344\275\215.py" "b/1386.\345\256\211\346\216\222\347\224\265\345\275\261\351\231\242\345\272\247\344\275\215/1386-\345\256\211\346\216\222\347\224\265\345\275\261\351\231\242\345\272\247\344\275\215.py" deleted file mode 100644 index f223e8b..0000000 --- "a/1386.\345\256\211\346\216\222\347\224\265\345\275\261\351\231\242\345\272\247\344\275\215/1386-\345\256\211\346\216\222\347\224\265\345\275\261\351\231\242\345\272\247\344\275\215.py" +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def maxNumberOfFamilies(self, n, reservedSeats): - """ - :type n: int - :type reservedSeats: List[List[int]] - :rtype: int - """ - from collections import defaultdict - dic = defaultdict(set) - res = 0 - usedrow = set() - for row, seat in reservedSeats: - dic[row].add(seat) - usedrow.add(row) - - for row in usedrow: - twothree = 2 not in dic[row] and 3 not in dic[row] - fourfive = 4 not in dic[row] and 5 not in dic[row] - sixseven = 6 not in dic[row] and 7 not in dic[row] - eightnine = 8 not in dic[row] and 9 not in dic[row] - - if twothree and fourfive and sixseven and eightnine: - res += 2 - elif twothree and fourfive: - res += 1 - elif fourfive and sixseven: - res += 1 - elif sixseven and eightnine: - res += 1 - return res + (n - len(usedrow)) * 2 - \ No newline at end of file diff --git "a/1387.\345\260\206\346\225\264\346\225\260\346\214\211\346\235\203\351\207\215\346\216\222\345\272\217/1387-\345\260\206\346\225\264\346\225\260\346\214\211\346\235\203\351\207\215\346\216\222\345\272\217.py" "b/1387.\345\260\206\346\225\264\346\225\260\346\214\211\346\235\203\351\207\215\346\216\222\345\272\217/1387-\345\260\206\346\225\264\346\225\260\346\214\211\346\235\203\351\207\215\346\216\222\345\272\217.py" deleted file mode 100644 index ac2b840..0000000 --- "a/1387.\345\260\206\346\225\264\346\225\260\346\214\211\346\235\203\351\207\215\346\216\222\345\272\217/1387-\345\260\206\346\225\264\346\225\260\346\214\211\346\235\203\351\207\215\346\216\222\345\272\217.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def getKth(self, lo, hi, k): - """ - :type lo: int - :type hi: int - :type k: int - :rtype: int - """ - dic = {} - dic[1] = 0 - def func(x): - if x in dic: - return dic[x] - if x % 2: - res = 1 + func(3 * x + 1) - else: - res = 1 + func(x / 2) - dic[x] = res - return res - - return sorted(range(lo, hi + 1), key = lambda x: (func(x), x))[k - 1] \ No newline at end of file diff --git "a/1389.\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204/1389-\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204 2.py" "b/1389.\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204/1389-\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204 2.py" new file mode 100644 index 0000000..0c987fe --- /dev/null +++ "b/1389.\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204/1389-\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204 2.py" @@ -0,0 +1,11 @@ +class Solution(object): + def createTargetArray(self, nums, index): + """ + :type nums: List[int] + :type index: List[int] + :rtype: List[int] + """ + res = [] + for i in range(len(nums)): + res.insert(index[i], nums[i]) + return res \ No newline at end of file diff --git "a/139.\345\215\225\350\257\215\346\213\206\345\210\206/139-\345\215\225\350\257\215\346\213\206\345\210\206.py" "b/139.\345\215\225\350\257\215\346\213\206\345\210\206/139-\345\215\225\350\257\215\346\213\206\345\210\206.py" deleted file mode 100644 index e843e45..0000000 --- "a/139.\345\215\225\350\257\215\346\213\206\345\210\206/139-\345\215\225\350\257\215\346\213\206\345\210\206.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def wordBreak(self, s, wordDict): - """ - :type s: str - :type wordDict: List[str] - :rtype: bool - """ - dp = [-1] # dp[i] 表示从0~下标为i的单词可以被拆分 - - for i, ch in enumerate(s): - for start in dp: - if s[start + 1:i + 1] in wordDict: - dp.append(i) - break - - return dp[-1] == len(s) - 1 \ No newline at end of file diff --git "a/1394.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1394-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" "b/1394.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1394-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" deleted file mode 100644 index 52fbeff..0000000 --- "a/1394.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1394-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" +++ /dev/null @@ -1,8 +0,0 @@ -class Solution(object): - def findLucky(self, arr): - """ - :type arr: List[int] - :rtype: int - """ - l = [key for key, val in collections.Counter(arr).items() if key == val] - return max(l) if l else -1 \ No newline at end of file diff --git "a/1395.\347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260/1395-\347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260.py" "b/1395.\347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260/1395-\347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260.py" deleted file mode 100644 index ebbe4d5..0000000 --- "a/1395.\347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260/1395-\347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260.py" +++ /dev/null @@ -1,26 +0,0 @@ -class Solution(object): - def numTeams(self, rating): - """ - :type rating: List[int] - :rtype: int - """ - from collections import defaultdict - - - def helper(rating): - dic = defaultdict(int) - res = 0 - - for i in range(len(rating)): - for j in range(i + 1, len(rating)): - if rating[j] > rating[i]: - dic[i] += 1 - - - for i in range(len(rating)): - for j in range(i + 1, len(rating)): - if rating[j] > rating[i]: - res += dic[j] - return res - - return helper(rating) + helper(rating[::-1]) diff --git "a/1396.\350\256\276\350\256\241\345\234\260\351\223\201\347\263\273\347\273\237/1396-\350\256\276\350\256\241\345\234\260\351\223\201\347\263\273\347\273\237.py" "b/1396.\350\256\276\350\256\241\345\234\260\351\223\201\347\263\273\347\273\237/1396-\350\256\276\350\256\241\345\234\260\351\223\201\347\263\273\347\273\237.py" index dc1328d..a54cba9 100644 --- "a/1396.\350\256\276\350\256\241\345\234\260\351\223\201\347\263\273\347\273\237/1396-\350\256\276\350\256\241\345\234\260\351\223\201\347\263\273\347\273\237.py" +++ "b/1396.\350\256\276\350\256\241\345\234\260\351\223\201\347\263\273\347\273\237/1396-\350\256\276\350\256\241\345\234\260\351\223\201\347\263\273\347\273\237.py" @@ -1,10 +1,8 @@ class UndergroundSystem(object): def __init__(self): - from collections import defaultdict - self.cnt = defaultdict(int) - self.total = defaultdict(int) - self.record = {} # key is id, val is the checkin stationName and time + self.check_in_history = dict() # key is id + stationName, val is t + self.StationTraverlTime = dict() def checkIn(self, id, stationName, t): """ :type id: int @@ -12,7 +10,8 @@ def checkIn(self, id, stationName, t): :type t: int :rtype: None """ - self.record[id] = (stationName, t) + self.check_in_history[str(id)] = [stationName, t] + def checkOut(self, id, stationName, t): """ @@ -21,11 +20,15 @@ def checkOut(self, id, stationName, t): :type t: int :rtype: None """ - checkinStation = self.record[id][0] - checkinTime = self.record[id][1] - - self.total[checkinStation + "#" + stationName] += t - checkinTime - self.cnt[checkinStation + "#" + stationName] += 1 + start_station, start_time = self.check_in_history[str(id)] + self.check_in_history.pop(str(id)) + time_spent = t - start_time + key = start_station + "#" + stationName + if key not in self.StationTraverlTime: + self.StationTraverlTime[key] = [time_spent,1] + else: + self.StationTraverlTime[key][0] += time_spent + self.StationTraverlTime[key][1] += 1 def getAverageTime(self, startStation, endStation): @@ -34,7 +37,8 @@ def getAverageTime(self, startStation, endStation): :type endStation: str :rtype: float """ - return self.total[startStation + "#" + endStation] * 1.0 / self.cnt[startStation + "#" + endStation] + key = startStation + "#" + endStation + return self.StationTraverlTime[key][0] * 1.0 / self.StationTraverlTime[key][1] # Your UndergroundSystem object will be instantiated and called as such: diff --git "a/1399.\347\273\237\350\256\241\346\234\200\345\244\247\347\273\204\347\232\204\346\225\260\347\233\256/1399-\347\273\237\350\256\241\346\234\200\345\244\247\347\273\204\347\232\204\346\225\260\347\233\256.py" "b/1399.\347\273\237\350\256\241\346\234\200\345\244\247\347\273\204\347\232\204\346\225\260\347\233\256/1399-\347\273\237\350\256\241\346\234\200\345\244\247\347\273\204\347\232\204\346\225\260\347\233\256.py" deleted file mode 100644 index e73c683..0000000 --- "a/1399.\347\273\237\350\256\241\346\234\200\345\244\247\347\273\204\347\232\204\346\225\260\347\233\256/1399-\347\273\237\350\256\241\346\234\200\345\244\247\347\273\204\347\232\204\346\225\260\347\233\256.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def countLargestGroup(self, n): - """ - :type n: int - :rtype: int - """ - from collections import defaultdict - l = defaultdict(int) - - def helper(num): - # 计算num数位之和,eg:输入34, 返回3 + 4 = 7 - s = 0 - while num: - num, tmp = divmod(num, 10) - s += tmp - return s - - for num in range(1, n + 1): - l[helper(num)] += 1 - - mmax = max(l.values()) - return sum([1 for item in l.values() if item == mmax]) \ No newline at end of file diff --git "a/1400.\346\236\204\351\200\240K\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/1400-\346\236\204\351\200\240K\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" "b/1400.\346\236\204\351\200\240K\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/1400-\346\236\204\351\200\240K\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" deleted file mode 100644 index 8bd5502..0000000 --- "a/1400.\346\236\204\351\200\240K\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/1400-\346\236\204\351\200\240K\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" +++ /dev/null @@ -1,19 +0,0 @@ -class Solution(object): - def canConstruct(self, s, k): - """ - :type s: str - :type k: int - :rtype: bool - """ - if k >= len(s): - # 长度 == k,必然可以;长度 < k,必然不可以。 - return k == len(s) - - from collections import Counter - dic = Counter(s) - # 对于每一个出现次数为奇数次的字符来说,比如 "aaa", 它至少能构成一个回文串,最多能构成三个回文串, - s_odd = 0 - for val in dic.values(): - if val % 2: - s_odd += 1 - return s_odd <= k \ No newline at end of file diff --git "a/1402.\345\201\232\350\217\234\351\241\272\345\272\217/1402-\345\201\232\350\217\234\351\241\272\345\272\217.py" "b/1402.\345\201\232\350\217\234\351\241\272\345\272\217/1402-\345\201\232\350\217\234\351\241\272\345\272\217.py" deleted file mode 100644 index ba60d1b..0000000 --- "a/1402.\345\201\232\350\217\234\351\241\272\345\272\217/1402-\345\201\232\350\217\234\351\241\272\345\272\217.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def maxSatisfaction(self, satisfaction): - """ - :type satisfaction: List[int] - :rtype: int - """ - res = 0 - - s = sorted(satisfaction) - - for i in range(len(s)): - cnt = 1 - tmp = 0 - for j in range(i, len(s)): - tmp += cnt * s[j] - cnt += 1 - # print tmp - res = max(tmp, res) - - return res diff --git "a/1403.\351\235\236\351\200\222\345\242\236\351\241\272\345\272\217\347\232\204\346\234\200\345\260\217\345\255\220\345\272\217\345\210\227/1403-\351\235\236\351\200\222\345\242\236\351\241\272\345\272\217\347\232\204\346\234\200\345\260\217\345\255\220\345\272\217\345\210\227.py" "b/1403.\351\235\236\351\200\222\345\242\236\351\241\272\345\272\217\347\232\204\346\234\200\345\260\217\345\255\220\345\272\217\345\210\227/1403-\351\235\236\351\200\222\345\242\236\351\241\272\345\272\217\347\232\204\346\234\200\345\260\217\345\255\220\345\272\217\345\210\227.py" deleted file mode 100644 index a01dd06..0000000 --- "a/1403.\351\235\236\351\200\222\345\242\236\351\241\272\345\272\217\347\232\204\346\234\200\345\260\217\345\255\220\345\272\217\345\210\227/1403-\351\235\236\351\200\222\345\242\236\351\241\272\345\272\217\347\232\204\346\234\200\345\260\217\345\255\220\345\272\217\345\210\227.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def minSubsequence(self, nums): - """ - :type nums: List[int] - :rtype: List[int] - """ - s = sum(nums) - - t = 0 - res = [] - for num in sorted(nums)[::-1]: - res.append(num) - t += num - s -= num - - if t > s: - return res \ No newline at end of file diff --git "a/1404.\345\260\206\344\272\214\350\277\233\345\210\266\350\241\250\347\244\272\345\207\217\345\210\2601\347\232\204\346\255\245\351\252\244\346\225\260/1404-\345\260\206\344\272\214\350\277\233\345\210\266\350\241\250\347\244\272\345\207\217\345\210\2601\347\232\204\346\255\245\351\252\244\346\225\260.py" "b/1404.\345\260\206\344\272\214\350\277\233\345\210\266\350\241\250\347\244\272\345\207\217\345\210\2601\347\232\204\346\255\245\351\252\244\346\225\260/1404-\345\260\206\344\272\214\350\277\233\345\210\266\350\241\250\347\244\272\345\207\217\345\210\2601\347\232\204\346\255\245\351\252\244\346\225\260.py" deleted file mode 100644 index 08c4a9c..0000000 --- "a/1404.\345\260\206\344\272\214\350\277\233\345\210\266\350\241\250\347\244\272\345\207\217\345\210\2601\347\232\204\346\255\245\351\252\244\346\225\260/1404-\345\260\206\344\272\214\350\277\233\345\210\266\350\241\250\347\244\272\345\207\217\345\210\2601\347\232\204\346\255\245\351\252\244\346\225\260.py" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution(object): - def numSteps(self, s): - """ - :type s: str - :rtype: int - """ - cnt = 0 - s = int(s, 2) - while s != 1: - cnt += 1 - if s % 2: - s += 1 - else: - s //= 2 - return cnt \ No newline at end of file diff --git "a/1405.\346\234\200\351\225\277\345\277\253\344\271\220\345\255\227\347\254\246\344\270\262/1405-\346\234\200\351\225\277\345\277\253\344\271\220\345\255\227\347\254\246\344\270\262.py" "b/1405.\346\234\200\351\225\277\345\277\253\344\271\220\345\255\227\347\254\246\344\270\262/1405-\346\234\200\351\225\277\345\277\253\344\271\220\345\255\227\347\254\246\344\270\262.py" deleted file mode 100644 index 8bffec5..0000000 --- "a/1405.\346\234\200\351\225\277\345\277\253\344\271\220\345\255\227\347\254\246\344\270\262/1405-\346\234\200\351\225\277\345\277\253\344\271\220\345\255\227\347\254\246\344\270\262.py" +++ /dev/null @@ -1,38 +0,0 @@ -class Solution(object): - def longestDiverseString(self, a, b, c): - """ - :type a: int - :type b: int - :type c: int - :rtype: str - """ - from heapq import * - heap = [] - res = "" - if a: - heappush(heap, (-a, "a")) - if b: - heappush(heap, (-b, "b")) - if c: - heappush(heap, (-c, "c")) - pre_cnt, pre_char = None, None - s = a + b + c - - while heap: - cnt, char = heappop(heap) - cnt = -cnt - # print cnt, char - - if cnt > s - cnt: - res += char * min(cnt, 2) - cnt -= min(cnt, 2) - s -= min(cnt, 2) - else: - res += char - cnt -= 1 - s -= 1 - - if pre_cnt: - heappush(heap, (-pre_cnt, pre_char)) - pre_cnt, pre_char = cnt, char - return res \ No newline at end of file diff --git "a/1408.\346\225\260\347\273\204\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\345\214\271\351\205\215/1408-\346\225\260\347\273\204\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\345\214\271\351\205\215.py" "b/1408.\346\225\260\347\273\204\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\345\214\271\351\205\215/1408-\346\225\260\347\273\204\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\345\214\271\351\205\215.py" deleted file mode 100644 index 5b41923..0000000 --- "a/1408.\346\225\260\347\273\204\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\345\214\271\351\205\215/1408-\346\225\260\347\273\204\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\345\214\271\351\205\215.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def stringMatching(self, words): - """ - :type words: List[str] - :rtype: List[str] - """ - fix = set() - res = [] - - for word1 in words: - for word2 in words: - if len(word1) < len(word2) and word1 in word2: - res.append(word1) - break - - return res \ No newline at end of file diff --git "a/1409.\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227/1409-\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227.py" "b/1409.\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227/1409-\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227.py" index f27eefc..edec649 100644 --- "a/1409.\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227/1409-\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227.py" +++ "b/1409.\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227/1409-\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227.py" @@ -1,15 +1,9 @@ -class Solution(object): - def processQueries(self, queries, m): - """ - :type queries: List[int] - :type m: int - :rtype: List[int] - """ - q = list(range(1, m + 1)) +class Solution: + def processQueries(self, queries: List[int], m: int) -> List[int]: + P = [i for i in range(1, m + 1)] res = [] - for i, x in enumerate(queries): - idx = q.index(queries[i]) - res.append(idx) - q.pop(idx) - q.insert(0, queries[i]) + for query in queries: + index = P.index(query) + res.append(index) + P = [P[index]] + P[:index] + P[index + 1:] return res \ No newline at end of file diff --git "a/1410.HTML\345\256\236\344\275\223\350\247\243\346\236\220\345\231\250/1410-HTML\345\256\236\344\275\223\350\247\243\346\236\220\345\231\250.py" "b/1410.HTML\345\256\236\344\275\223\350\247\243\346\236\220\345\231\250/1410-HTML\345\256\236\344\275\223\350\247\243\346\236\220\345\231\250.py" deleted file mode 100644 index 923436c..0000000 --- "a/1410.HTML\345\256\236\344\275\223\350\247\243\346\236\220\345\231\250/1410-HTML\345\256\236\344\275\223\350\247\243\346\236\220\345\231\250.py" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution(object): - def entityParser(self, text): - """ - :type text: str - :rtype: str - """ - t = text.replace(""", "\"") - t = t.replace("'", "\'") - - t = t.replace(">", ">") - t = t.replace("<", "<") - t = t.replace("⁄","/") - t = t.replace("&", "&") - return t \ No newline at end of file diff --git "a/1413.\351\200\220\346\255\245\346\261\202\345\222\214\345\276\227\345\210\260\346\255\243\346\225\260\347\232\204\346\234\200\345\260\217\345\200\274/1413-\351\200\220\346\255\245\346\261\202\345\222\214\345\276\227\345\210\260\346\255\243\346\225\260\347\232\204\346\234\200\345\260\217\345\200\274.py" "b/1413.\351\200\220\346\255\245\346\261\202\345\222\214\345\276\227\345\210\260\346\255\243\346\225\260\347\232\204\346\234\200\345\260\217\345\200\274/1413-\351\200\220\346\255\245\346\261\202\345\222\214\345\276\227\345\210\260\346\255\243\346\225\260\347\232\204\346\234\200\345\260\217\345\200\274.py" deleted file mode 100644 index 13ad386..0000000 --- "a/1413.\351\200\220\346\255\245\346\261\202\345\222\214\345\276\227\345\210\260\346\255\243\346\225\260\347\232\204\346\234\200\345\260\217\345\200\274/1413-\351\200\220\346\255\245\346\261\202\345\222\214\345\276\227\345\210\260\346\255\243\346\225\260\347\232\204\346\234\200\345\260\217\345\200\274.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def minStartValue(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - min_s = nums[0] - s = 0 - for num in nums: - s += num - min_s = min(min_s, s) - - return 1 if min_s >= 1 else 1 - min_s \ No newline at end of file diff --git "a/1414.\345\222\214\344\270\272K\347\232\204\346\234\200\345\260\221\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\255\227\346\225\260\347\233\256/1414-\345\222\214\344\270\272K\347\232\204\346\234\200\345\260\221\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\255\227\346\225\260\347\233\256.py" "b/1414.\345\222\214\344\270\272K\347\232\204\346\234\200\345\260\221\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\255\227\346\225\260\347\233\256/1414-\345\222\214\344\270\272K\347\232\204\346\234\200\345\260\221\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\255\227\346\225\260\347\233\256.py" deleted file mode 100644 index b3bb5d8..0000000 --- "a/1414.\345\222\214\344\270\272K\347\232\204\346\234\200\345\260\221\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\255\227\346\225\260\347\233\256/1414-\345\222\214\344\270\272K\347\232\204\346\234\200\345\260\221\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\255\227\346\225\260\347\233\256.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def findMinFibonacciNumbers(self, k): - """ - :type k: int - :rtype: int - """ - first, second = 1, 1 - fib = [1, 1] - while second < k: - nxt = first + second - fib.append(nxt) - first = second - second = nxt - res = 0 - while k > 0: - idx = bisect.bisect_left(fib, k) - if fib[idx] == k: - k -= fib[idx] - else: - k -= fib[idx - 1] - res += 1 - return res \ No newline at end of file diff --git "a/1417.\351\207\215\346\226\260\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262/1417-\351\207\215\346\226\260\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262.py" "b/1417.\351\207\215\346\226\260\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262/1417-\351\207\215\346\226\260\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262.py" deleted file mode 100644 index 7a2e1ad..0000000 --- "a/1417.\351\207\215\346\226\260\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262/1417-\351\207\215\346\226\260\346\240\274\345\274\217\345\214\226\345\255\227\347\254\246\344\270\262.py" +++ /dev/null @@ -1,32 +0,0 @@ -class Solution(object): - def reformat(self, s): - """ - :type s: str - :rtype: str - """ - char = [ch for ch in s if ch.isalpha()] - digit = [ch for ch in s if ch.isdigit()] - - if abs(len(char) - len(digit)) > 1: - return "" - - res = "" - i = 0 - if len(char) > len(digit): - while i < len(digit): - res += char[i] - res += digit[i] - i += 1 - res += char[i] - - else: - while i < len(char): - res += digit[i] - res += char[i] - i += 1 - - if len(char) < len(digit): - res += digit[i] - - return res - \ No newline at end of file diff --git "a/1418.\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250/1418-\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250.py" "b/1418.\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250/1418-\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250.py" index b407f23..d0936a2 100644 --- "a/1418.\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250/1418-\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250.py" +++ "b/1418.\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250/1418-\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250.py" @@ -1,30 +1,23 @@ -class Solution(object): - def displayTable(self, orders): - """ - :type orders: List[List[str]] - :rtype: List[List[str]] - """ +class Solution: + def displayTable(self, orders: List[List[str]]) -> List[List[str]]: from collections import defaultdict - dishes = set() - tables = set() - dic = dict() - - for c, t, f in orders: - dishes.add(f) - tables.add(int(t)) - - if t not in dic: - dic[t] = defaultdict(int) - dic[t][f] += 1 + table_set = set() + tablefood2cnt = defaultdict(int) + food_set = set() - dishes = sorted(list(dishes)) - res = [["Table"] + dishes] + for order in orders: + table, food = order[1], order[2] + food_set.add(food) + table_set.add(table) + tablefood2cnt[table + "-" + food] += 1 - for t in sorted(list(tables)): - tmp = [str(t)] - for d in dishes: - tmp.append(str(dic[str(t)][d])) - res.append(tmp) - return res - - \ No newline at end of file + sorted_table = sorted(list(table_set), key = lambda x: int(x)) + sorted_food = sorted(list(food_set)) + res = [["Table"] + sorted_food] + + for table in sorted_table: + temp = [table] + for f in sorted_food: + temp.append(str(tablefood2cnt[table + "-" + f])) + res.append(temp) + return res \ No newline at end of file diff --git "a/1419.\346\225\260\351\235\222\350\233\231/1419-\346\225\260\351\235\222\350\233\231.py" "b/1419.\346\225\260\351\235\222\350\233\231/1419-\346\225\260\351\235\222\350\233\231.py" deleted file mode 100644 index 7ab83fd..0000000 --- "a/1419.\346\225\260\351\235\222\350\233\231/1419-\346\225\260\351\235\222\350\233\231.py" +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def minNumberOfFrogs(self, croakOfFrogs): - """ - :type croakOfFrogs: str - :rtype: int - """ - from collections import defaultdict - if len(croakOfFrogs) % 5 != 0: - return -1 - - dic = defaultdict(int) - res = 0 - pre = {"r":"c", "o":"r", "a":"o"} - for ch in croakOfFrogs: - if ch == "c": - dic[ch] += 1 - - elif ch in "roa": - if dic[pre[ch]] == 0: - return -1 - dic[pre[ch]] -= 1 - dic[ch] += 1 - - elif ch == "k": - if dic["a"] == 0: - return -1 - dic["k"] -= 1 - - res = max(res, sum(dic.values())) - - return res \ No newline at end of file diff --git "a/1422.\345\210\206\345\211\262\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1422-\345\210\206\345\211\262\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" "b/1422.\345\210\206\345\211\262\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1422-\345\210\206\345\211\262\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" deleted file mode 100644 index cf48db3..0000000 --- "a/1422.\345\210\206\345\211\262\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1422-\345\210\206\345\211\262\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def maxScore(self, s): - """ - :type s: str - :rtype: int - """ - zero = s.count("0") - one = len(s) - zero - zero_cnt = 0 - res = 0 - for i, x in enumerate(s[:-1]): - if x == "0": - zero_cnt += 1 - one_cnt = one - (i + 1 - zero_cnt) - res = max(res, zero_cnt + one_cnt) - - return res \ No newline at end of file diff --git "a/1423.\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260/1423-\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260.py" "b/1423.\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260/1423-\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260.py" deleted file mode 100644 index fb17c81..0000000 --- "a/1423.\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260/1423-\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def maxScore(self, cardPoints, k): - """ - :type cardPoints: List[int] - :type k: int - :rtype: int - """ - # 连续摸 n - k 张牌,求最小点数和 - n = len(cardPoints) - s = sum(cardPoints) - - left, right = 0, n - k - window_sum = sum(cardPoints[left:right]) - min_s = window_sum - for right in range(n - k, n): - # print cardPoints[left:right + 1] - window_sum -= cardPoints[left] - window_sum += cardPoints[right] - min_s = min(min_s, window_sum) - left += 1 - return s - min_s diff --git "a/1426.\346\225\260\345\205\203\347\264\240/1426-\346\225\260\345\205\203\347\264\240.py" "b/1426.\346\225\260\345\205\203\347\264\240/1426-\346\225\260\345\205\203\347\264\240.py" deleted file mode 100644 index 2878cfe..0000000 --- "a/1426.\346\225\260\345\205\203\347\264\240/1426-\346\225\260\345\205\203\347\264\240.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def countElements(self, arr): - """ - :type arr: List[int] - :rtype: int - """ - s = set(arr) - - res = 0 - for num in arr: - if num + 1 in s: - res += 1 - return res \ No newline at end of file diff --git "a/1436.\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231/1436-\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231 2.py" "b/1436.\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231/1436-\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231 2.py" new file mode 100644 index 0000000..e065712 --- /dev/null +++ "b/1436.\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231/1436-\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231 2.py" @@ -0,0 +1,7 @@ +class Solution(object): + def destCity(self, paths): + """ + :type paths: List[List[str]] + :rtype: str + """ + return (set(pair[1] for pair in paths) - set(pair[0] for pair in paths)).pop() \ No newline at end of file diff --git "a/1439.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214/1439-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214.py" "b/1439.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214/1439-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214.py" new file mode 100644 index 0000000..1659fd7 --- /dev/null +++ "b/1439.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214/1439-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214.py" @@ -0,0 +1,24 @@ +from heapq import * +class Solution: + def kthSmallest(self, mat: List[List[int]], k: int) -> int: + m, n = len(mat), len(mat[0]) + min_sum = sum([mat[i][0] for i in range(m)]) + min_heap = [(min_sum, [0 for i in range(m)])] + cur = 0 + visited = set() + while cur < k: + s, indices = heappop(min_heap) + cur += 1 + if cur == k: + return s + + for i in range(m): + if indices[i] + 1 < n: + nxt_s = s - mat[i][indices[i]] + mat[i][indices[i] + 1] + nxt_indices = indices[:] + nxt_indices[i] += 1 + str_indices = "".join([str(i) for i in nxt_indices]) + if str_indices not in visited: + visited.add(str_indices) + heappush(min_heap, (nxt_s, nxt_indices)) + \ No newline at end of file diff --git "a/1441.\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204/1441-\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204 2.py" "b/1441.\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204/1441-\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204 2.py" new file mode 100644 index 0000000..020d531 --- /dev/null +++ "b/1441.\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204/1441-\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204 2.py" @@ -0,0 +1,29 @@ +class Solution(object): + def buildArray(self, target, n): + """ + :type target: List[int] + :type n: int + :rtype: List[str] + """ + pre = 1 + res = [] + for i, num in enumerate(target): + if i == 0: + res += (num - pre) * ["Push", "Pop"] + ["Push"] + else: + res += (num - pre - 1) * ["Push", "Pop"] + ["Push"] + pre = num + return res + +# i = 0 #index +# j = 1 #num for num in range(1, n) +# res = [] +# while i < len(target): +# if target[i] == j: +# res += ["Push"] +# i += 1 +# else: +# res += ["Push", "Pop"] +# j += 1 + +# return res \ No newline at end of file diff --git "a/1447.\346\234\200\347\256\200\345\210\206\346\225\260/1447-\346\234\200\347\256\200\345\210\206\346\225\260.py" "b/1447.\346\234\200\347\256\200\345\210\206\346\225\260/1447-\346\234\200\347\256\200\345\210\206\346\225\260.py" index a4d3f35..1c1ba4f 100644 --- "a/1447.\346\234\200\347\256\200\345\210\206\346\225\260/1447-\346\234\200\347\256\200\345\210\206\346\225\260.py" +++ "b/1447.\346\234\200\347\256\200\345\210\206\346\225\260/1447-\346\234\200\347\256\200\345\210\206\346\225\260.py" @@ -1,11 +1,11 @@ class Solution: def simplifiedFractions(self, n: int) -> List[str]: import math - res = set() + res = [] for down in range(1, n + 1): for up in range(1, down): if math.gcd(up, down) == 1: - res.add(str(up) + "/" + str(down)) + res.append(str(up) + "/" + str(down)) - return list(res) \ No newline at end of file + return res \ No newline at end of file diff --git "a/146.LRU\347\274\223\345\255\230\346\234\272\345\210\266/146-LRU\347\274\223\345\255\230\346\234\272\345\210\266.py" "b/146.LRU\347\274\223\345\255\230\346\234\272\345\210\266/146-LRU\347\274\223\345\255\230\346\234\272\345\210\266.py" deleted file mode 100644 index 8942475..0000000 --- "a/146.LRU\347\274\223\345\255\230\346\234\272\345\210\266/146-LRU\347\274\223\345\255\230\346\234\272\345\210\266.py" +++ /dev/null @@ -1,74 +0,0 @@ -class Node(object): - def __init__(self, key, value, nxt, prev): - self.key = key - self.value = value - self.next = nxt - self.prev = prev -class LRUCache(object): - - def __init__(self, capacity): - """ - :type capacity: int - """ - self.capacity = capacity - self.record = dict() - self.head = Node(-1, -1, None, None) - self.tail = Node(-1, -1, self.head, self.head) - self.head.next = self.tail - self.head.prev = self.tail - - def move_to_end(self, key): - node = self.record[key] - - node.prev.next = node.next - node.next.prev = node.prev - - prev_to_tail = self.tail.prev - node.next = self.tail - node.prev = prev_to_tail - prev_to_tail.next = node - self.tail.prev = node - - - def get(self, key): - """ - :type key: int - :rtype: int - """ - - if key in self.record: - self.move_to_end(key) - return self.record[key].value - else: - return -1 - - - def put(self, key, value): - """ - :type key: int - :type value: int - :rtype: None - """ - if key in self.record: - self.move_to_end(key) - self.record[key].value = value - else: - if self.capacity == 0: - self.record.pop(self.head.next.key) - new_first_node = self.head.next.next - self.head.next = new_first_node - new_first_node.prev = self.head - else: - self.capacity -= 1 - - prev_to_tail = self.tail.prev - new_node = Node(key, value, self.tail, prev_to_tail) - self.record[key] = new_node - prev_to_tail.next = new_node - self.tail.prev = new_node - - -# Your LRUCache object will be instantiated and called as such: -# obj = LRUCache(capacity) -# param_1 = obj.get(key) -# obj.put(key,value) \ No newline at end of file diff --git "a/1464.\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1464-\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257 2.py" "b/1464.\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1464-\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257 2.py" new file mode 100644 index 0000000..4a1af45 --- /dev/null +++ "b/1464.\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1464-\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257 2.py" @@ -0,0 +1,8 @@ +class Solution(object): + def maxProduct(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + l = sorted(nums) + return (l[-1] - 1) * (l[-2] - 1) \ No newline at end of file diff --git "a/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271 2.py" "b/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271 2.py" new file mode 100644 index 0000000..9dba7a0 --- /dev/null +++ "b/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271 2.py" @@ -0,0 +1,30 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution(object): + def getLonelyNodes(self, root): + """ + :type root: TreeNode + :rtype: List[int] + """ + self.res = [] + def dfs(node, siblings_cnt): + if not node: + return + + if siblings_cnt == 1: + self.res.append(node.val) + + siblings_cnt = 0 + if node.left: + siblings_cnt += 1 + if node.right: + siblings_cnt += 1 + dfs(node.left, siblings_cnt) + dfs(node.right, siblings_cnt) + + dfs(root, 0) + return self.res \ No newline at end of file diff --git "a/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271.py" "b/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271.py" new file mode 100644 index 0000000..9dba7a0 --- /dev/null +++ "b/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271.py" @@ -0,0 +1,30 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution(object): + def getLonelyNodes(self, root): + """ + :type root: TreeNode + :rtype: List[int] + """ + self.res = [] + def dfs(node, siblings_cnt): + if not node: + return + + if siblings_cnt == 1: + self.res.append(node.val) + + siblings_cnt = 0 + if node.left: + siblings_cnt += 1 + if node.right: + siblings_cnt += 1 + dfs(node.left, siblings_cnt) + dfs(node.right, siblings_cnt) + + dfs(root, 0) + return self.res \ No newline at end of file diff --git "a/1470.\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204/1470-\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204 2.py" "b/1470.\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204/1470-\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204 2.py" new file mode 100644 index 0000000..274e58a --- /dev/null +++ "b/1470.\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204/1470-\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204 2.py" @@ -0,0 +1,12 @@ +class Solution(object): + def shuffle(self, nums, n): + """ + :type nums: List[int] + :type n: int + :rtype: List[int] + """ + res = [] + for i in range(n): + res.append(nums[i]) + res.append(nums[i + n]) + return res \ No newline at end of file diff --git "a/1474.\345\210\240\351\231\244\351\223\276\350\241\250M\344\270\252\350\212\202\347\202\271\344\271\213\345\220\216\347\232\204N\344\270\252\350\212\202\347\202\271/1474-\345\210\240\351\231\244\351\223\276\350\241\250M\344\270\252\350\212\202\347\202\271\344\271\213\345\220\216\347\232\204N\344\270\252\350\212\202\347\202\271.py" "b/1474.\345\210\240\351\231\244\351\223\276\350\241\250M\344\270\252\350\212\202\347\202\271\344\271\213\345\220\216\347\232\204N\344\270\252\350\212\202\347\202\271/1474-\345\210\240\351\231\244\351\223\276\350\241\250M\344\270\252\350\212\202\347\202\271\344\271\213\345\220\216\347\232\204N\344\270\252\350\212\202\347\202\271.py" new file mode 100644 index 0000000..320d3d3 --- /dev/null +++ "b/1474.\345\210\240\351\231\244\351\223\276\350\241\250M\344\270\252\350\212\202\347\202\271\344\271\213\345\220\216\347\232\204N\344\270\252\350\212\202\347\202\271/1474-\345\210\240\351\231\244\351\223\276\350\241\250M\344\270\252\350\212\202\347\202\271\344\271\213\345\220\216\347\232\204N\344\270\252\350\212\202\347\202\271.py" @@ -0,0 +1,30 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution(object): + def deleteNodes(self, head, m, n): + """ + :type head: ListNode + :type m: int + :type n: int + :rtype: ListNode + """ + if not head: + return head + + p = head + tm = m - 1 + while tm and p: + tm -= 1 + p = p.next + if p: + pp = p.next + tn = n + while tn and pp: + tn -= 1 + pp = pp.next + + p.next = self.deleteNodes(pp, m, n) + return head \ No newline at end of file diff --git "a/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274 2.py" "b/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274 2.py" new file mode 100644 index 0000000..41ad968 --- /dev/null +++ "b/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274 2.py" @@ -0,0 +1,20 @@ +class Solution(object): + def finalPrices(self, prices): + """ + :type prices: List[int] + :rtype: List[int] + """ + stack = [] + res = [] + for i in range(len(prices) - 1, -1, -1): + if not stack: + res.append(prices[i]) + else: + while stack and stack[-1] > prices[i]: + stack.pop() + if stack: + res.append(prices[i] - stack[-1]) + else: + res.append(prices[i]) + stack.append(prices[i]) + return res[::-1] \ No newline at end of file diff --git "a/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274.py" "b/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274.py" new file mode 100644 index 0000000..41ad968 --- /dev/null +++ "b/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274.py" @@ -0,0 +1,20 @@ +class Solution(object): + def finalPrices(self, prices): + """ + :type prices: List[int] + :rtype: List[int] + """ + stack = [] + res = [] + for i in range(len(prices) - 1, -1, -1): + if not stack: + res.append(prices[i]) + else: + while stack and stack[-1] > prices[i]: + stack.pop() + if stack: + res.append(prices[i] - stack[-1]) + else: + res.append(prices[i]) + stack.append(prices[i]) + return res[::-1] \ No newline at end of file diff --git "a/1476.\345\255\220\347\237\251\345\275\242\346\237\245\350\257\242/1476-\345\255\220\347\237\251\345\275\242\346\237\245\350\257\242.py" "b/1476.\345\255\220\347\237\251\345\275\242\346\237\245\350\257\242/1476-\345\255\220\347\237\251\345\275\242\346\237\245\350\257\242.py" new file mode 100644 index 0000000..a607529 --- /dev/null +++ "b/1476.\345\255\220\347\237\251\345\275\242\346\237\245\350\257\242/1476-\345\255\220\347\237\251\345\275\242\346\237\245\350\257\242.py" @@ -0,0 +1,35 @@ +class SubrectangleQueries(object): + + def __init__(self, rectangle): + """ + :type rectangle: List[List[int]] + """ + self.l = rectangle + + def updateSubrectangle(self, row1, col1, row2, col2, newValue): + """ + :type row1: int + :type col1: int + :type row2: int + :type col2: int + :type newValue: int + :rtype: None + """ + for i in range(row1, row2 + 1): + for j in range(col1, col2 + 1): + self.l[i][j] = newValue + + + def getValue(self, row, col): + """ + :type row: int + :type col: int + :rtype: int + """ + return self.l[row][col] + + +# Your SubrectangleQueries object will be instantiated and called as such: +# obj = SubrectangleQueries(rectangle) +# obj.updateSubrectangle(row1,col1,row2,col2,newValue) +# param_2 = obj.getValue(row,col) \ No newline at end of file diff --git "a/1480.\344\270\200\347\273\264\346\225\260\347\273\204\347\232\204\345\212\250\346\200\201\345\222\214/1480-\344\270\200\347\273\264\346\225\260\347\273\204\347\232\204\345\212\250\346\200\201\345\222\214.py" "b/1480.\344\270\200\347\273\264\346\225\260\347\273\204\347\232\204\345\212\250\346\200\201\345\222\214/1480-\344\270\200\347\273\264\346\225\260\347\273\204\347\232\204\345\212\250\346\200\201\345\222\214.py" new file mode 100644 index 0000000..8b7bdf1 --- /dev/null +++ "b/1480.\344\270\200\347\273\264\346\225\260\347\273\204\347\232\204\345\212\250\346\200\201\345\222\214/1480-\344\270\200\347\273\264\346\225\260\347\273\204\347\232\204\345\212\250\346\200\201\345\222\214.py" @@ -0,0 +1,12 @@ +class Solution(object): + def runningSum(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + s = 0 + res = [] + for num in nums: + s += num + res.append(s) + return res \ No newline at end of file diff --git "a/1481.\344\270\215\345\220\214\346\225\264\346\225\260\347\232\204\346\234\200\345\260\221\346\225\260\347\233\256/1481-\344\270\215\345\220\214\346\225\264\346\225\260\347\232\204\346\234\200\345\260\221\346\225\260\347\233\256.py" "b/1481.\344\270\215\345\220\214\346\225\264\346\225\260\347\232\204\346\234\200\345\260\221\346\225\260\347\233\256/1481-\344\270\215\345\220\214\346\225\264\346\225\260\347\232\204\346\234\200\345\260\221\346\225\260\347\233\256.py" new file mode 100644 index 0000000..fbba5f2 --- /dev/null +++ "b/1481.\344\270\215\345\220\214\346\225\264\346\225\260\347\232\204\346\234\200\345\260\221\346\225\260\347\233\256/1481-\344\270\215\345\220\214\346\225\264\346\225\260\347\232\204\346\234\200\345\260\221\346\225\260\347\233\256.py" @@ -0,0 +1,21 @@ +class Solution(object): + def findLeastNumOfUniqueInts(self, arr, k): + """ + :type arr: List[int] + :type k: int + :rtype: int + """ + if not arr or len(arr) == k: + return 0 + from collections import Counter + dic = Counter(arr) + l = dic.values() + l = sorted(l)[::-1] + + target = len(arr) - k + s = 0 + for i, num in enumerate(l): + s += num + if s >= target: + return i + 1 + \ No newline at end of file diff --git "a/1482.\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260/1482-\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260.py" "b/1482.\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260/1482-\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260.py" new file mode 100644 index 0000000..16affa2 --- /dev/null +++ "b/1482.\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260/1482-\345\210\266\344\275\234m\346\235\237\350\212\261\346\211\200\351\234\200\347\232\204\346\234\200\345\260\221\345\244\251\346\225\260.py" @@ -0,0 +1,32 @@ +class Solution(object): + def minDays(self, bloomDay, m, k): + """ + :type bloomDay: List[int] + :type m: int + :type k: int + :rtype: int + """ + if m * k > len(bloomDay): + return -1 + + left, right = 1, max(bloomDay) + while left <= right: + mid = (left + right) // 2 + # print mid + flowerCnt = 0 + dayCnt = 0 + for day in bloomDay: + if day <= mid: + dayCnt += 1 + if dayCnt >= k: + flowerCnt += 1 + dayCnt = 0 + else: + dayCnt = 0 + # print flowerCnt + if flowerCnt >= m: + right = mid - 1 + else: + left = mid + 1 + return left + \ No newline at end of file diff --git "a/1485.\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221/1485-\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221.py" "b/1485.\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221/1485-\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..df3e01c --- /dev/null +++ "b/1485.\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221/1485-\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,41 @@ +# Definition for Node. +# class Node: +# def __init__(self, val=0, left=None, right=None, random=None): +# self.val = val +# self.left = left +# self.right = right +# self.random = random + +from collections import deque +class Solution: + def copyRandomBinaryTree(self, root: 'Optional[Node]') -> 'Optional[NodeCopy]': + if not root: + return root + + old2new = dict() + queue = deque([root]) + while queue: + cur_node = queue.popleft() + new_node = NodeCopy(cur_node.val) + old2new[cur_node] = new_node + + if cur_node.left: + queue.append(cur_node.left) + if cur_node.right: + queue.append(cur_node.right) + + queue = deque([root]) + while queue: + cur_node = queue.popleft() + new_node = old2new[cur_node] + + if cur_node.left: + new_node.left = old2new[cur_node.left] + queue.append(cur_node.left) + if cur_node.right: + new_node.right = old2new[cur_node.right] + queue.append(cur_node.right) + if cur_node.random: + new_node.random = old2new[cur_node.random] + + return old2new[root] \ No newline at end of file diff --git "a/1486.\346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234/1486-\346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234.py" "b/1486.\346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234/1486-\346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234.py" new file mode 100644 index 0000000..3cbe8fd --- /dev/null +++ "b/1486.\346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234/1486-\346\225\260\347\273\204\345\274\202\346\210\226\346\223\215\344\275\234.py" @@ -0,0 +1,11 @@ +class Solution(object): + def xorOperation(self, n, start): + """ + :type n: int + :type start: int + :rtype: int + """ + res = start + for i in range(1, n): + res ^= start + 2 * i + return res \ No newline at end of file diff --git "a/1507.\350\275\254\345\217\230\346\227\245\346\234\237\346\240\274\345\274\217/1507-\350\275\254\345\217\230\346\227\245\346\234\237\346\240\274\345\274\217.py" "b/1507.\350\275\254\345\217\230\346\227\245\346\234\237\346\240\274\345\274\217/1507-\350\275\254\345\217\230\346\227\245\346\234\237\346\240\274\345\274\217.py" new file mode 100644 index 0000000..3da2a3f --- /dev/null +++ "b/1507.\350\275\254\345\217\230\346\227\245\346\234\237\346\240\274\345\274\217/1507-\350\275\254\345\217\230\346\227\245\346\234\237\346\240\274\345\274\217.py" @@ -0,0 +1,13 @@ +class Solution(object): + def reformatDate(self, date): + """ + :type date: str + :rtype: str + """ + date = date.split(" ") + + day = "0" + date[0][:1] if len(date[0]) == 3 else date[0][:2] + mon = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", + "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}[date[1]] + year = date[2] + return "-".join([year, mon, day]) \ No newline at end of file diff --git "a/1508.\345\255\220\346\225\260\347\273\204\345\222\214\346\216\222\345\272\217\345\220\216\347\232\204\345\214\272\351\227\264\345\222\214/1508-\345\255\220\346\225\260\347\273\204\345\222\214\346\216\222\345\272\217\345\220\216\347\232\204\345\214\272\351\227\264\345\222\214.py" "b/1508.\345\255\220\346\225\260\347\273\204\345\222\214\346\216\222\345\272\217\345\220\216\347\232\204\345\214\272\351\227\264\345\222\214/1508-\345\255\220\346\225\260\347\273\204\345\222\214\346\216\222\345\272\217\345\220\216\347\232\204\345\214\272\351\227\264\345\222\214.py" new file mode 100644 index 0000000..518444a --- /dev/null +++ "b/1508.\345\255\220\346\225\260\347\273\204\345\222\214\346\216\222\345\272\217\345\220\216\347\232\204\345\214\272\351\227\264\345\222\214/1508-\345\255\220\346\225\260\347\273\204\345\222\214\346\216\222\345\272\217\345\220\216\347\232\204\345\214\272\351\227\264\345\222\214.py" @@ -0,0 +1,18 @@ +class Solution(object): + def rangeSum(self, nums, n, left, right): + """ + :type nums: List[int] + :type n: int + :type left: int + :type right: int + :rtype: int + """ + res = [] + for i in range(n): + for j in range(i + 1, n + 1): + res.append(sum(nums[i:j])) + res.sort() + # print res + return sum(res[left - 1:right]) % (10 ** 9 + 7) + + \ No newline at end of file diff --git "a/1509.\344\270\211\346\254\241\346\223\215\344\275\234\345\220\216\346\234\200\345\244\247\345\200\274\344\270\216\346\234\200\345\260\217\345\200\274\347\232\204\346\234\200\345\260\217\345\267\256/1509-\344\270\211\346\254\241\346\223\215\344\275\234\345\220\216\346\234\200\345\244\247\345\200\274\344\270\216\346\234\200\345\260\217\345\200\274\347\232\204\346\234\200\345\260\217\345\267\256.py" "b/1509.\344\270\211\346\254\241\346\223\215\344\275\234\345\220\216\346\234\200\345\244\247\345\200\274\344\270\216\346\234\200\345\260\217\345\200\274\347\232\204\346\234\200\345\260\217\345\267\256/1509-\344\270\211\346\254\241\346\223\215\344\275\234\345\220\216\346\234\200\345\244\247\345\200\274\344\270\216\346\234\200\345\260\217\345\200\274\347\232\204\346\234\200\345\260\217\345\267\256.py" new file mode 100644 index 0000000..07aaea9 --- /dev/null +++ "b/1509.\344\270\211\346\254\241\346\223\215\344\275\234\345\220\216\346\234\200\345\244\247\345\200\274\344\270\216\346\234\200\345\260\217\345\200\274\347\232\204\346\234\200\345\260\217\345\267\256/1509-\344\270\211\346\254\241\346\223\215\344\275\234\345\220\216\346\234\200\345\244\247\345\200\274\344\270\216\346\234\200\345\260\217\345\200\274\347\232\204\346\234\200\345\260\217\345\267\256.py" @@ -0,0 +1,17 @@ +class Solution(object): + def minDifference(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if len(nums) <= 4: + return 0 + nums.sort() + # print nums + # 1. 去前三个 + res = nums[-1] - nums[3] + # 2. 去前两个和最后一个 + res = min(res, nums[-2] - nums[2]) + res = min(res, nums[-3] - nums[1]) + res = min(res, nums[-4] - nums[0]) + return res \ No newline at end of file diff --git "a/151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215/151-\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.py" "b/151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215/151-\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.py" deleted file mode 100644 index e1ef72a..0000000 --- "a/151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215/151-\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def reverseWords(self, s): - """ - :type s: str - :rtype: str - """ - return " ".join([item for item in s.strip().rstrip().split(" ") if item][::-1]) \ No newline at end of file diff --git "a/1510.\347\237\263\345\255\220\346\270\270\346\210\217IV/1510-\347\237\263\345\255\220\346\270\270\346\210\217IV.py" "b/1510.\347\237\263\345\255\220\346\270\270\346\210\217IV/1510-\347\237\263\345\255\220\346\270\270\346\210\217IV.py" new file mode 100644 index 0000000..408a00a --- /dev/null +++ "b/1510.\347\237\263\345\255\220\346\270\270\346\210\217IV/1510-\347\237\263\345\255\220\346\270\270\346\210\217IV.py" @@ -0,0 +1,15 @@ +class Solution(object): + def winnerSquareGame(self, n): + """ + :type n: int + :rtype: bool + """ + dp = [0 for _ in range(n + 1)] + dp[1] = 1 + for i in range(2, n + 1): + for j in range(1, int(i ** 0.5 + 1)): + if not dp[i - j * j]: + dp[i] = 1 + break + return dp[n] == 1 + \ No newline at end of file diff --git "a/1513.\344\273\205\345\220\2531\347\232\204\345\255\220\344\270\262\346\225\260/1513-\344\273\205\345\220\2531\347\232\204\345\255\220\344\270\262\346\225\260.py" "b/1513.\344\273\205\345\220\2531\347\232\204\345\255\220\344\270\262\346\225\260/1513-\344\273\205\345\220\2531\347\232\204\345\255\220\344\270\262\346\225\260.py" new file mode 100644 index 0000000..c5b0684 --- /dev/null +++ "b/1513.\344\273\205\345\220\2531\347\232\204\345\255\220\344\270\262\346\225\260/1513-\344\273\205\345\220\2531\347\232\204\345\255\220\344\270\262\346\225\260.py" @@ -0,0 +1,16 @@ +class Solution(object): + def numSub(self, s): + """ + :type s: str + :rtype: int + """ + cons_length = 0 + res = 0 + MOD = 10 ** 9 + 7 + for ch in s: + if ch == '1': + cons_length += 1 + else: + res = (res + (1 + cons_length) * cons_length // 2) % MOD + cons_length = 0 + return (res + (1 + cons_length) * cons_length // 2) % MOD \ No newline at end of file diff --git "a/1518.\346\215\242\351\205\222\351\227\256\351\242\230/1518-\346\215\242\351\205\222\351\227\256\351\242\230.py" "b/1518.\346\215\242\351\205\222\351\227\256\351\242\230/1518-\346\215\242\351\205\222\351\227\256\351\242\230.py" new file mode 100644 index 0000000..0fd23e3 --- /dev/null +++ "b/1518.\346\215\242\351\205\222\351\227\256\351\242\230/1518-\346\215\242\351\205\222\351\227\256\351\242\230.py" @@ -0,0 +1,13 @@ +class Solution(object): + def numWaterBottles(self, numBottles, numExchange): + """ + :type numBottles: int + :type numExchange: int + :rtype: int + """ + empty = res = numBottles + while empty >= numExchange: + res += empty / numExchange + empty = empty / numExchange + empty % numExchange + return res + \ No newline at end of file diff --git "a/1519.\345\255\220\346\240\221\344\270\255\346\240\207\347\255\276\347\233\270\345\220\214\347\232\204\350\212\202\347\202\271\346\225\260/1519-\345\255\220\346\240\221\344\270\255\346\240\207\347\255\276\347\233\270\345\220\214\347\232\204\350\212\202\347\202\271\346\225\260.py" "b/1519.\345\255\220\346\240\221\344\270\255\346\240\207\347\255\276\347\233\270\345\220\214\347\232\204\350\212\202\347\202\271\346\225\260/1519-\345\255\220\346\240\221\344\270\255\346\240\207\347\255\276\347\233\270\345\220\214\347\232\204\350\212\202\347\202\271\346\225\260.py" new file mode 100644 index 0000000..b2e6604 --- /dev/null +++ "b/1519.\345\255\220\346\240\221\344\270\255\346\240\207\347\255\276\347\233\270\345\220\214\347\232\204\350\212\202\347\202\271\346\225\260/1519-\345\255\220\346\240\221\344\270\255\346\240\207\347\255\276\347\233\270\345\220\214\347\232\204\350\212\202\347\202\271\346\225\260.py" @@ -0,0 +1,30 @@ +class Solution(object): + def countSubTrees(self, n, edges, labels): + """ + :type n: int + :type edges: List[List[int]] + :type labels: str + :rtype: List[int] + """ + from collections import defaultdict + par2child = defaultdict(set) + for s, d in edges: + par2child[d].add(s) + par2child[s].add(d) + + self.res = [0 for _ in labels] + def dfs(node, visited): + dic = defaultdict(int) + for child in par2child[node]: + if child not in visited: + visited.add(child) + child_dic = dfs(child, visited) + for key, val in child_dic.items(): + dic[key] = dic[key] + val + # print(node, dic) + self.res[node] = 1 + dic[labels[node]] + dic[labels[node]] += 1 + return dic + dfs(0, set([0])) + return self.res + diff --git "a/1523.\345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256/1523-\345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256.py" "b/1523.\345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256/1523-\345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256.py" new file mode 100644 index 0000000..ccd4a6d --- /dev/null +++ "b/1523.\345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256/1523-\345\234\250\345\214\272\351\227\264\350\214\203\345\233\264\345\206\205\347\273\237\350\256\241\345\245\207\346\225\260\346\225\260\347\233\256.py" @@ -0,0 +1,10 @@ +class Solution(object): + def countOdds(self, low, high): + """ + :type low: int + :type high: int + :rtype: int + """ + if low % 2 + high % 2 == 0: + return (high - low) // 2 + return 1 + (high - low) // 2 \ No newline at end of file diff --git "a/1524.\345\222\214\344\270\272\345\245\207\346\225\260\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1524-\345\222\214\344\270\272\345\245\207\346\225\260\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256.py" "b/1524.\345\222\214\344\270\272\345\245\207\346\225\260\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1524-\345\222\214\344\270\272\345\245\207\346\225\260\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..0e154e8 --- /dev/null +++ "b/1524.\345\222\214\344\270\272\345\245\207\346\225\260\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1524-\345\222\214\344\270\272\345\245\207\346\225\260\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256.py" @@ -0,0 +1,25 @@ +class Solution(object): + def numOfSubarrays(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + prefix_sum = [0 for _ in arr] + prefix_sum[0] = arr[0] + + for i in range(1, len(arr)): + prefix_sum[i] = prefix_sum[i - 1] + arr[i] + + MOD = 10 ** 9 + 7 + even, odd = 1, 0 + res = 0 + for i in range(len(arr)): + if prefix_sum[i] % 2 == 0: + res += odd + even += 1 + else: + res += even + odd += 1 + res = res % MOD + return res + \ No newline at end of file diff --git "a/1525.\345\255\227\347\254\246\344\270\262\347\232\204\345\245\275\345\210\206\345\211\262\346\225\260\347\233\256/1525-\345\255\227\347\254\246\344\270\262\347\232\204\345\245\275\345\210\206\345\211\262\346\225\260\347\233\256.py" "b/1525.\345\255\227\347\254\246\344\270\262\347\232\204\345\245\275\345\210\206\345\211\262\346\225\260\347\233\256/1525-\345\255\227\347\254\246\344\270\262\347\232\204\345\245\275\345\210\206\345\211\262\346\225\260\347\233\256.py" new file mode 100644 index 0000000..9ac7e73 --- /dev/null +++ "b/1525.\345\255\227\347\254\246\344\270\262\347\232\204\345\245\275\345\210\206\345\211\262\346\225\260\347\233\256/1525-\345\255\227\347\254\246\344\270\262\347\232\204\345\245\275\345\210\206\345\211\262\346\225\260\347\233\256.py" @@ -0,0 +1,25 @@ +class Solution(object): + def numSplits(self, s): + """ + :type s: str + :rtype: int + """ + from collections import defaultdict + import copy + rs = defaultdict(set) + + for i in range(len(s) - 1, -1, -1): + if i == len(s) - 1: + rs[i] = set(s[i]) + else: + rs[i] = copy.deepcopy(rs[i + 1]) + rs[i].add(s[i]) + + ls = set(s[0]) + res = 0 + for i in range(len(s) - 1): + ls.add(s[i]) + if len(ls) == len(rs[i + 1]): + res += 1 + + return res \ No newline at end of file diff --git "a/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262 2.py" "b/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262 2.py" new file mode 100644 index 0000000..6ddd80a --- /dev/null +++ "b/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262 2.py" @@ -0,0 +1,11 @@ +class Solution(object): + def restoreString(self, s, indices): + """ + :type s: str + :type indices: List[int] + :rtype: str + """ + res = ["" for _ in indices] + for i in range(len(s)): + res[indices[i]] = s[i] + return "".join(ch for ch in res) \ No newline at end of file diff --git "a/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.py" "b/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..6ddd80a --- /dev/null +++ "b/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,11 @@ +class Solution(object): + def restoreString(self, s, indices): + """ + :type s: str + :type indices: List[int] + :rtype: str + """ + res = ["" for _ in indices] + for i in range(len(s)): + res[indices[i]] = s[i] + return "".join(ch for ch in res) \ No newline at end of file diff --git "a/1529.\347\201\257\346\263\241\345\274\200\345\205\263IV/1529-\347\201\257\346\263\241\345\274\200\345\205\263IV.py" "b/1529.\347\201\257\346\263\241\345\274\200\345\205\263IV/1529-\347\201\257\346\263\241\345\274\200\345\205\263IV.py" new file mode 100644 index 0000000..eed9616 --- /dev/null +++ "b/1529.\347\201\257\346\263\241\345\274\200\345\205\263IV/1529-\347\201\257\346\263\241\345\274\200\345\205\263IV.py" @@ -0,0 +1,19 @@ +class Solution(object): + def minFlips(self, target): + """ + :type target: str + :rtype: int + """ + i = 0 + flag = 0 + pre = None + res = 0 + for i, ch in enumerate(target): + if not flag and ch == "0": + continue + else: + if ch != pre: + res += 1 + flag = 1 + pre = ch + return res \ No newline at end of file diff --git "a/1534.\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/1534-\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204.py" "b/1534.\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/1534-\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204.py" new file mode 100644 index 0000000..769e5ab --- /dev/null +++ "b/1534.\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204/1534-\347\273\237\350\256\241\345\245\275\344\270\211\345\205\203\347\273\204.py" @@ -0,0 +1,18 @@ +class Solution(object): + def countGoodTriplets(self, arr, a, b, c): + """ + :type arr: List[int] + :type a: int + :type b: int + :type c: int + :rtype: int + """ + res = 0 + for i in range(len(arr)): + for j in range(i + 1, len(arr)): + for k in range(j + 1, len(arr)): + if abs(arr[i] - arr[j]) <= a and \ + abs(arr[j] - arr[k]) <= b and \ + abs(arr[i] - arr[k]) <= c: + res += 1 + return res \ No newline at end of file diff --git "a/1535.\346\211\276\345\207\272\346\225\260\347\273\204\346\270\270\346\210\217\347\232\204\350\265\242\345\256\266/1535-\346\211\276\345\207\272\346\225\260\347\273\204\346\270\270\346\210\217\347\232\204\350\265\242\345\256\266.py" "b/1535.\346\211\276\345\207\272\346\225\260\347\273\204\346\270\270\346\210\217\347\232\204\350\265\242\345\256\266/1535-\346\211\276\345\207\272\346\225\260\347\273\204\346\270\270\346\210\217\347\232\204\350\265\242\345\256\266.py" new file mode 100644 index 0000000..b81bf48 --- /dev/null +++ "b/1535.\346\211\276\345\207\272\346\225\260\347\273\204\346\270\270\346\210\217\347\232\204\350\265\242\345\256\266/1535-\346\211\276\345\207\272\346\225\260\347\273\204\346\270\270\346\210\217\347\232\204\350\265\242\345\256\266.py" @@ -0,0 +1,19 @@ +class Solution(object): + def getWinner(self, arr, k): + """ + :type arr: List[int] + :type k: int + :rtype: int + """ + winner = max(arr[0], arr[1]) + cnt = 1 + for num in arr[2:]: + if cnt == k: + return winner + if winner > num: + cnt += 1 + else: + #刷新winner + winner = num + cnt = 1 + return winner \ No newline at end of file diff --git "a/1536.\346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260/1536-\346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260.py" "b/1536.\346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260/1536-\346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260.py" new file mode 100644 index 0000000..bd629d5 --- /dev/null +++ "b/1536.\346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260/1536-\346\216\222\345\270\203\344\272\214\350\277\233\345\210\266\347\275\221\346\240\274\347\232\204\346\234\200\345\260\221\344\272\244\346\215\242\346\254\241\346\225\260.py" @@ -0,0 +1,31 @@ +class Solution(object): + def minSwaps(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + dic = dict() # key 是每行的idx, val 是这一行末尾0的个数 + n = len(grid) + for i, row in enumerate(grid): # 统计每一行末尾有几个0 + cnt = 0 + for j in range(n - 1, -1, -1): + if not row[j]: + cnt += 1 + else: + break + dic[i] = cnt + + res = 0 + for i in range(n): + if dic[i] < n - i - 1: # 这一行0太少,需要放到下面去 + for j in range(i + 1, n): + if dic[j] >= n - i - 1: # 找到0足够多的行 + break + if dic[j] < n - i - 1: # 没找到说明无解 + return -1 + + for k in range(j, i, -1): #把第i行换到第j行的位置上去 + dic[k] = dic[k - 1] + + res += j - i + return res \ No newline at end of file diff --git "a/1537.\346\234\200\345\244\247\345\276\227\345\210\206/1537-\346\234\200\345\244\247\345\276\227\345\210\206.py" "b/1537.\346\234\200\345\244\247\345\276\227\345\210\206/1537-\346\234\200\345\244\247\345\276\227\345\210\206.py" new file mode 100644 index 0000000..a596c4c --- /dev/null +++ "b/1537.\346\234\200\345\244\247\345\276\227\345\210\206/1537-\346\234\200\345\244\247\345\276\227\345\210\206.py" @@ -0,0 +1,27 @@ +class Solution(object): + def maxSum(self, nums1, nums2): + """ + :type nums1: List[int] + :type nums2: List[int] + :rtype: int + """ + dups = set(nums1) & set(nums2) + + s1 = self.getFragmentedSum(nums1, dups) + s2 = self.getFragmentedSum(nums2, dups) + + res = 0 + for sum1, sum2 in zip(s1, s2): + res += max(sum1, sum2) + return res % (10 ** 9 + 7) + + def getFragmentedSum(self, nums, dups): + l = [] + s = 0 + for num in nums: + s += num + if num in dups: + l.append(s) + s = 0 + l.append(s) + return l \ No newline at end of file diff --git "a/1539.\347\254\254k\344\270\252\347\274\272\345\244\261\347\232\204\346\255\243\346\225\264\346\225\260/1539-\347\254\254k\344\270\252\347\274\272\345\244\261\347\232\204\346\255\243\346\225\264\346\225\260.py" "b/1539.\347\254\254k\344\270\252\347\274\272\345\244\261\347\232\204\346\255\243\346\225\264\346\225\260/1539-\347\254\254k\344\270\252\347\274\272\345\244\261\347\232\204\346\255\243\346\225\264\346\225\260.py" new file mode 100644 index 0000000..96aaa68 --- /dev/null +++ "b/1539.\347\254\254k\344\270\252\347\274\272\345\244\261\347\232\204\346\255\243\346\225\264\346\225\260/1539-\347\254\254k\344\270\252\347\274\272\345\244\261\347\232\204\346\255\243\346\225\264\346\225\260.py" @@ -0,0 +1,15 @@ +class Solution(object): + def findKthPositive(self, arr, k): + """ + :type arr: List[int] + :type k: int + :rtype: int + """ + arr = set(arr) + num = 1 + while k: + if num not in arr: + k -= 1 + + num += 1 + return num - 1 \ No newline at end of file diff --git "a/1540.K\346\254\241\346\223\215\344\275\234\350\275\254\345\217\230\345\255\227\347\254\246\344\270\262/1540-K\346\254\241\346\223\215\344\275\234\350\275\254\345\217\230\345\255\227\347\254\246\344\270\262.py" "b/1540.K\346\254\241\346\223\215\344\275\234\350\275\254\345\217\230\345\255\227\347\254\246\344\270\262/1540-K\346\254\241\346\223\215\344\275\234\350\275\254\345\217\230\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..b474363 --- /dev/null +++ "b/1540.K\346\254\241\346\223\215\344\275\234\350\275\254\345\217\230\345\255\227\347\254\246\344\270\262/1540-K\346\254\241\346\223\215\344\275\234\350\275\254\345\217\230\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,36 @@ +class Solution(object): + def canConvertString(self, s, t, k): + """ + :type s: str + :type t: str + :type k: int + :rtype: bool + """ + from collections import defaultdict + + if not s or not t or len(s) != len(t): + return False + + d = list() + + for i in range(len(s)): + if s[i] < t[i]: + d.append(ord(t[i]) - ord(s[i])) + elif s[i] > t[i]: + d.append(26 - ord(s[i]) + ord(t[i])) + + d.sort() + res = 0 + pre = None + for distance in d: + if not pre or pre != distance: + res = max(res, distance) + pre = distance + pre_cnt = 1 + else: + res = max(res, 26 * pre_cnt + distance) + pre_cnt += 1 + if res > k: + return False + return True + diff --git "a/1541.\345\271\263\350\241\241\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260/1541-\345\271\263\350\241\241\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260.py" "b/1541.\345\271\263\350\241\241\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260/1541-\345\271\263\350\241\241\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260.py" new file mode 100644 index 0000000..b818001 --- /dev/null +++ "b/1541.\345\271\263\350\241\241\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260/1541-\345\271\263\350\241\241\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\260\221\346\217\222\345\205\245\346\254\241\346\225\260.py" @@ -0,0 +1,39 @@ +class Solution(object): + def minInsertions(self, s): + """ + :type s: str + :rtype: int + """ + left_cnt, cons_right_cnt = 0, 0 + res = 0 + for ch in s: + if ch == "(": + if cons_right_cnt: + if cons_right_cnt % 2: # add 1 to make it even + res += 1 + cons_right_cnt += 1 + pair = min(left_cnt, cons_right_cnt // 2) # build all possible pairs + left_cnt -= pair + cons_right_cnt -= pair * 2 + + if cons_right_cnt: # if two or more ) left + res += cons_right_cnt // 2 # add "(" every 2 ) + cons_right_cnt = 0 + left_cnt += 1 + else: + cons_right_cnt += 1 + + if cons_right_cnt: + if cons_right_cnt % 2: # add 1 to make it even + res += 1 + cons_right_cnt += 1 + pair = min(left_cnt, cons_right_cnt // 2) # build all possible pairs + left_cnt -= pair + cons_right_cnt -= pair * 2 + + if cons_right_cnt: # if two or more ) left + res += cons_right_cnt // 2 # add "(" every 2 ) + cons_right_cnt = 0 + + res += left_cnt * 2 # add 2 ) every ( + return res \ No newline at end of file diff --git "a/1544.\346\225\264\347\220\206\345\255\227\347\254\246\344\270\262/1544-\346\225\264\347\220\206\345\255\227\347\254\246\344\270\262.py" "b/1544.\346\225\264\347\220\206\345\255\227\347\254\246\344\270\262/1544-\346\225\264\347\220\206\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..792fb4a --- /dev/null +++ "b/1544.\346\225\264\347\220\206\345\255\227\347\254\246\344\270\262/1544-\346\225\264\347\220\206\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,14 @@ +class Solution(object): + def makeGood(self, s): + """ + :type s: str + :rtype: str + """ + stack = [] + for ch in s: + if not stack or abs(ord(ch) - ord(stack[-1])) != 32: + stack.append(ch) + else: + stack.pop() + + return "".join(stack) \ No newline at end of file diff --git "a/1545.\346\211\276\345\207\272\347\254\254N\344\270\252\344\272\214\350\277\233\345\210\266\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254K\344\275\215/1545-\346\211\276\345\207\272\347\254\254N\344\270\252\344\272\214\350\277\233\345\210\266\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254K\344\275\215.py" "b/1545.\346\211\276\345\207\272\347\254\254N\344\270\252\344\272\214\350\277\233\345\210\266\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254K\344\275\215/1545-\346\211\276\345\207\272\347\254\254N\344\270\252\344\272\214\350\277\233\345\210\266\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254K\344\275\215.py" new file mode 100644 index 0000000..296cb74 --- /dev/null +++ "b/1545.\346\211\276\345\207\272\347\254\254N\344\270\252\344\272\214\350\277\233\345\210\266\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254K\344\275\215/1545-\346\211\276\345\207\272\347\254\254N\344\270\252\344\272\214\350\277\233\345\210\266\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254K\344\275\215.py" @@ -0,0 +1,14 @@ +class Solution(object): + def findKthBit(self, n, k): + """ + :type n: int + :type k: int + :rtype: str + """ + # len 1, 3, 7, 15 ... L(n) = 2 * (L(n - 1)) + 1 + # if k == 2 * (n - 2) + 2: return 1 + s = "0" + while len(s) <= k: + s = s + "1" + "".join([str(1 - int(ch)) for ch in s])[::-1] + # print (s) + return s[k - 1] \ No newline at end of file diff --git "a/1546.\345\222\214\344\270\272\347\233\256\346\240\207\345\200\274\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256\344\270\215\351\207\215\345\217\240\351\235\236\347\251\272\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1546-\345\222\214\344\270\272\347\233\256\346\240\207\345\200\274\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256\344\270\215\351\207\215\345\217\240\351\235\236\347\251\272\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256.py" "b/1546.\345\222\214\344\270\272\347\233\256\346\240\207\345\200\274\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256\344\270\215\351\207\215\345\217\240\351\235\236\347\251\272\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1546-\345\222\214\344\270\272\347\233\256\346\240\207\345\200\274\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256\344\270\215\351\207\215\345\217\240\351\235\236\347\251\272\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..78f8610 --- /dev/null +++ "b/1546.\345\222\214\344\270\272\347\233\256\346\240\207\345\200\274\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256\344\270\215\351\207\215\345\217\240\351\235\236\347\251\272\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1546-\345\222\214\344\270\272\347\233\256\346\240\207\345\200\274\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256\344\270\215\351\207\215\345\217\240\351\235\236\347\251\272\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256.py" @@ -0,0 +1,19 @@ +class Solution(object): + def maxNonOverlapping(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: int + """ + pre_sum = {0} + res = 0 + s = 0 + for num in nums: + s += num + if s - target in pre_sum: + res += 1 + s = 0 + pre_sum = {0} + else: + pre_sum.add(s) + return res diff --git "a/1550.\345\255\230\345\234\250\350\277\236\347\273\255\344\270\211\344\270\252\345\245\207\346\225\260\347\232\204\346\225\260\347\273\204/1550-\345\255\230\345\234\250\350\277\236\347\273\255\344\270\211\344\270\252\345\245\207\346\225\260\347\232\204\346\225\260\347\273\204.py" "b/1550.\345\255\230\345\234\250\350\277\236\347\273\255\344\270\211\344\270\252\345\245\207\346\225\260\347\232\204\346\225\260\347\273\204/1550-\345\255\230\345\234\250\350\277\236\347\273\255\344\270\211\344\270\252\345\245\207\346\225\260\347\232\204\346\225\260\347\273\204.py" new file mode 100644 index 0000000..bb96e4f --- /dev/null +++ "b/1550.\345\255\230\345\234\250\350\277\236\347\273\255\344\270\211\344\270\252\345\245\207\346\225\260\347\232\204\346\225\260\347\273\204/1550-\345\255\230\345\234\250\350\277\236\347\273\255\344\270\211\344\270\252\345\245\207\346\225\260\347\232\204\346\225\260\347\273\204.py" @@ -0,0 +1,15 @@ +class Solution(object): + def threeConsecutiveOdds(self, arr): + """ + :type arr: List[int] + :rtype: bool + """ + l = 0 + for num in arr: + if num % 2: + l += 1 + if l == 3: + return True + else: + l = 0 + return False \ No newline at end of file diff --git "a/1551.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1551-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" "b/1551.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1551-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" new file mode 100644 index 0000000..c1cb604 --- /dev/null +++ "b/1551.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1551-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" @@ -0,0 +1,12 @@ +class Solution(object): + def minOperations(self, n): + """ + :type n: int + :rtype: int + """ + k = n // 2 + if n % 2: + return 2 * k + k * (k - 1) // 2 * 2 + else: + return 1 * k + k * (k - 1) // 2 * 2 + \ No newline at end of file diff --git "a/1552.\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233/1552-\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233.py" "b/1552.\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233/1552-\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233.py" new file mode 100644 index 0000000..8cb54d6 --- /dev/null +++ "b/1552.\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233/1552-\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233.py" @@ -0,0 +1,18 @@ +class Solution: + def maxDistance(self, position: List[int], m: int) -> int: + position.sort() + left, right = 1, position[-1] - position[0] + while left <= right: + mid = (left + right) // 2 # target answer + + cnt, prev = 1, position[0] + for p in position: + if prev + mid <= p: # find another possible position + cnt += 1 + prev = p + + if cnt >= m: + left = mid + 1 + elif cnt < m: + right = mid - 1 + return right \ No newline at end of file diff --git "a/1556.\345\215\203\344\275\215\345\210\206\351\232\224\346\225\260/1556-\345\215\203\344\275\215\345\210\206\351\232\224\346\225\260.py" "b/1556.\345\215\203\344\275\215\345\210\206\351\232\224\346\225\260/1556-\345\215\203\344\275\215\345\210\206\351\232\224\346\225\260.py" new file mode 100644 index 0000000..c84dd2b --- /dev/null +++ "b/1556.\345\215\203\344\275\215\345\210\206\351\232\224\346\225\260/1556-\345\215\203\344\275\215\345\210\206\351\232\224\346\225\260.py" @@ -0,0 +1,16 @@ +class Solution(object): + def thousandSeparator(self, n): + """ + :type n: int + :rtype: str + """ + res = "" + cnt = 0 + for digit in str(n)[::-1]: + res += digit + cnt += 1 + if cnt == 3: + cnt = 0 + res += "." + + return res[::-1].strip(".") \ No newline at end of file diff --git "a/1558.\345\276\227\345\210\260\347\233\256\346\240\207\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\207\275\346\225\260\350\260\203\347\224\250\346\254\241\346\225\260/1558-\345\276\227\345\210\260\347\233\256\346\240\207\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\207\275\346\225\260\350\260\203\347\224\250\346\254\241\346\225\260.py" "b/1558.\345\276\227\345\210\260\347\233\256\346\240\207\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\207\275\346\225\260\350\260\203\347\224\250\346\254\241\346\225\260/1558-\345\276\227\345\210\260\347\233\256\346\240\207\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\207\275\346\225\260\350\260\203\347\224\250\346\254\241\346\225\260.py" new file mode 100644 index 0000000..9a8effc --- /dev/null +++ "b/1558.\345\276\227\345\210\260\347\233\256\346\240\207\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\207\275\346\225\260\350\260\203\347\224\250\346\254\241\346\225\260/1558-\345\276\227\345\210\260\347\233\256\346\240\207\346\225\260\347\273\204\347\232\204\346\234\200\345\260\221\345\207\275\346\225\260\350\260\203\347\224\250\346\254\241\346\225\260.py" @@ -0,0 +1,12 @@ +class Solution(object): + def minOperations(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + res = 0 + for num in nums: + res += bin(num).count("1") + + res += len(bin(max(nums))[2:]) - 1 + return res \ No newline at end of file diff --git "a/1559.\344\272\214\347\273\264\347\275\221\346\240\274\345\233\276\344\270\255\346\216\242\346\265\213\347\216\257/1559-\344\272\214\347\273\264\347\275\221\346\240\274\345\233\276\344\270\255\346\216\242\346\265\213\347\216\257.py" "b/1559.\344\272\214\347\273\264\347\275\221\346\240\274\345\233\276\344\270\255\346\216\242\346\265\213\347\216\257/1559-\344\272\214\347\273\264\347\275\221\346\240\274\345\233\276\344\270\255\346\216\242\346\265\213\347\216\257.py" new file mode 100644 index 0000000..b6e8536 --- /dev/null +++ "b/1559.\344\272\214\347\273\264\347\275\221\346\240\274\345\233\276\344\270\255\346\216\242\346\265\213\347\216\257/1559-\344\272\214\347\273\264\347\275\221\346\240\274\345\233\276\344\270\255\346\216\242\346\265\213\347\216\257.py" @@ -0,0 +1,54 @@ +class Solution(object): + def containsCycle(self, grid): + """ + :type grid: List[List[str]] + :rtype: bool + """ + dx = [1, -1, 0, 0] + dy = [0, 0, 1, -1] + ufs = UnionFindSet(grid) + m, n = len(grid), len(grid[0]) + for i in range(m): + for j in range(n): + for next_i, next_j in [(i - 1, j), (i, j - 1)]: + if 0 <= next_i < m and 0 <= next_j < n and grid[next_i][next_j] == grid[i][j]: + if ufs.find(i * n + j) == ufs.find(next_i * n + next_j): + return True + else: + ufs.union(i * n + j, next_i * n + next_j) + return False + +class UnionFindSet(object): + def __init__(self, grid): + m, n = len(grid), len(grid[0]) + self.roots = [-1 for i in range(m*n)] + self.rank = [0 for i in range(m*n)] + self.count = 0 + + for i in range(m): + for j in range(n): + self.roots[i * n + j] = i * n + j + self.count += 1 + + def find(self, member): + tmp = [] + while member != self.roots[member]: + tmp.append(member) + member = self.roots[member] + for root in tmp: + self.roots[root] = member + return member + + def union(self, p, q): + parentP = self.find(p) + parentQ = self.find(q) + if parentP != parentQ: + if self.rank[parentP] > self.rank[parentQ]: + self.roots[parentQ] = parentP + elif self.rank[parentP] < self.rank[parentQ]: + self.roots[parentP] = parentQ + else: + self.roots[parentQ] = parentP + self.rank[parentP] -= 1 + self.count -= 1 + \ No newline at end of file diff --git "a/1576.\346\233\277\346\215\242\346\211\200\346\234\211\347\232\204\351\227\256\345\217\267/1576-\346\233\277\346\215\242\346\211\200\346\234\211\347\232\204\351\227\256\345\217\267.py" "b/1576.\346\233\277\346\215\242\346\211\200\346\234\211\347\232\204\351\227\256\345\217\267/1576-\346\233\277\346\215\242\346\211\200\346\234\211\347\232\204\351\227\256\345\217\267.py" new file mode 100644 index 0000000..6ab4307 --- /dev/null +++ "b/1576.\346\233\277\346\215\242\346\211\200\346\234\211\347\232\204\351\227\256\345\217\267/1576-\346\233\277\346\215\242\346\211\200\346\234\211\347\232\204\351\227\256\345\217\267.py" @@ -0,0 +1,17 @@ +class Solution(object): + def modifyString(self, s): + """ + :type s: str + :rtype: str + """ + res = [] + for i, ch in enumerate(s): + if ch == "?": + for new in "abc": + if ((i and res[-1] != new) or not i) and ((i < len(s) - 1 and s[i + 1] != new) or i == len(s) - 1): + res.append(new) + break + else: + res.append(ch) + + return "".join(res) \ No newline at end of file diff --git "a/1582.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256/1582-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256.py" "b/1582.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256/1582-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256.py" new file mode 100644 index 0000000..12c3357 --- /dev/null +++ "b/1582.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256/1582-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\347\211\271\346\256\212\344\275\215\347\275\256.py" @@ -0,0 +1,12 @@ +class Solution(object): + def numSpecial(self, mat): + """ + :type mat: List[List[int]] + :rtype: int + """ + # 找出所有满足条件的行和列 + possible_rows = [i for i, row in enumerate(mat) if sum(row) == 1] + possible_cols = [i for i, col in enumerate(zip(*mat)) if sum(col) == 1] + + # 在满足条件的行和列里统计值为1的点 + return sum([mat[i][j] for i in possible_rows for j in possible_cols]) \ No newline at end of file diff --git "a/1583.\347\273\237\350\256\241\344\270\215\345\274\200\345\277\203\347\232\204\346\234\213\345\217\213/1583-\347\273\237\350\256\241\344\270\215\345\274\200\345\277\203\347\232\204\346\234\213\345\217\213.py" "b/1583.\347\273\237\350\256\241\344\270\215\345\274\200\345\277\203\347\232\204\346\234\213\345\217\213/1583-\347\273\237\350\256\241\344\270\215\345\274\200\345\277\203\347\232\204\346\234\213\345\217\213.py" new file mode 100644 index 0000000..b6db322 --- /dev/null +++ "b/1583.\347\273\237\350\256\241\344\270\215\345\274\200\345\277\203\347\232\204\346\234\213\345\217\213/1583-\347\273\237\350\256\241\344\270\215\345\274\200\345\277\203\347\232\204\346\234\213\345\217\213.py" @@ -0,0 +1,32 @@ +class Solution(object): + def unhappyFriends(self, n, preferences, pairs): + """ + :type n: int + :type preferences: List[List[int]] + :type pairs: List[List[int]] + :rtype: int + """ + # 建立亲密度矩阵,prefer_degrees[i][j]即为 i 对 j 的亲密度 + prefer_degrees = [[-n-1 for _ in range(n)] for _ in range(n)] + for i, preference in enumerate(preferences): + for degree, j in enumerate(preference): + prefer_degrees[i][j] = -degree + + # 建立配对字典,给定x, ppl2friends[x]即为 x 分配的朋友 + ppl2friends = dict() + for x, y in pairs: + ppl2friends[x] = y + ppl2friends[y] = x + + def isUnhappy(x): + # 判定 x 是否快乐 + y = ppl2friends[x] + + for u in range(n): + v = ppl2friends[u] + if x != u and prefer_degrees[x][u] > prefer_degrees[x][y] and \ + prefer_degrees[u][x] > prefer_degrees[u][v]: + return 1 + return 0 + + return sum([isUnhappy(i) for i in range(n)]) \ No newline at end of file diff --git "a/1584.\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250/1584-\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.py" "b/1584.\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250/1584-\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.py" new file mode 100644 index 0000000..c140670 --- /dev/null +++ "b/1584.\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250/1584-\350\277\236\346\216\245\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\350\264\271\347\224\250.py" @@ -0,0 +1,54 @@ + +class UnionFindSet(object): + def __init__(self, n): + # m, n = len(grid), len(grid[0]) + self.roots = [i for i in range(n + 1)] + self.rank = [0 for i in range(n + 1)] + self.count = n + + def find(self, member): + tmp = [] + while member != self.roots[member]: + tmp.append(member) + member = self.roots[member] + for root in tmp: + self.roots[root] = member + return member + + def union(self, p, q): + parentP = self.find(p) + parentQ = self.find(q) + if parentP != parentQ: + if self.rank[parentP] > self.rank[parentQ]: + self.roots[parentQ] = parentP + elif self.rank[parentP] < self.rank[parentQ]: + self.roots[parentP] = parentQ + else: + self.roots[parentQ] = parentP + self.rank[parentP] -= 1 + self.count -= 1 + +class Solution(object): + def minCostConnectPoints(self, points): + """ + :type points: List[List[int]] + :rtype: int + """ + from heapq import * + queue = [] + res = 0 + n = len(points) + for i in range(n): + for j in range(i + 1, n): + d = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]) + heappush(queue, (d, i, j)) + + ufs = UnionFindSet(n) + while ufs.count > 1: + d, i, j = heappop(queue) + + if ufs.find(i) != ufs.find(j): + res += d + ufs.union(i, j) + + return res \ No newline at end of file diff --git "a/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250 2.py" "b/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250 2.py" new file mode 100644 index 0000000..ecf5e8f --- /dev/null +++ "b/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250 2.py" @@ -0,0 +1,13 @@ +class Solution(object): + def minOperations(self, logs): + """ + :type logs: List[str] + :rtype: int + """ + steps = 0 + for log in logs: + if log == "../" and steps: + steps -= 1 + elif log not in ["../", "./"]: + steps += 1 + return steps \ No newline at end of file diff --git "a/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250.py" "b/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250.py" new file mode 100644 index 0000000..ecf5e8f --- /dev/null +++ "b/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250.py" @@ -0,0 +1,13 @@ +class Solution(object): + def minOperations(self, logs): + """ + :type logs: List[str] + :rtype: int + """ + steps = 0 + for log in logs: + if log == "../" and steps: + steps -= 1 + elif log not in ["../", "./"]: + steps += 1 + return steps \ No newline at end of file diff --git "a/1600.\347\232\207\344\275\215\347\273\247\346\211\277\351\241\272\345\272\217/1600-\347\232\207\344\275\215\347\273\247\346\211\277\351\241\272\345\272\217.py" "b/1600.\347\232\207\344\275\215\347\273\247\346\211\277\351\241\272\345\272\217/1600-\347\232\207\344\275\215\347\273\247\346\211\277\351\241\272\345\272\217.py" new file mode 100644 index 0000000..a60f5a1 --- /dev/null +++ "b/1600.\347\232\207\344\275\215\347\273\247\346\211\277\351\241\272\345\272\217/1600-\347\232\207\344\275\215\347\273\247\346\211\277\351\241\272\345\272\217.py" @@ -0,0 +1,50 @@ +class ThroneInheritance(object): + def __init__(self, kingName): + """ + :type kingName: str + """ + from collections import defaultdict + self.par2child = defaultdict(list) + self.kingName = kingName + self.deaths = set() + + def birth(self, parentName, childName): + """ + :type parentName: str + :type childName: str + :rtype: None + """ + self.par2child[parentName].append(childName) + + + def death(self, name): + """ + :type name: str + :rtype: None + """ + self.deaths.add(name) + + def getInheritanceOrder(self): + """ + :rtype: List[str] + """ + return self.preorder(self.kingName) + + def preorder(self, cur): + res = [] + if cur not in self.deaths: + res.append(cur) + + for child in self.par2child[cur]: + res += self.preorder(child) + return res + + + + + +# Your ThroneInheritance object will be instantiated and called as such: +# obj = ThroneInheritance(kingName) +# obj.birth(parentName,childName) +# obj.death(name) +# param_3 = obj.getInheritanceOrder() \ No newline at end of file diff --git "a/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237 2.py" "b/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237 2.py" new file mode 100644 index 0000000..3259d52 --- /dev/null +++ "b/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237 2.py" @@ -0,0 +1,24 @@ +class ParkingSystem(object): + + def __init__(self, big, medium, small): + """ + :type big: int + :type medium: int + :type small: int + """ + self.space = [0, big, medium, small] + + def addCar(self, carType): + """ + :type carType: int + :rtype: bool + """ + if self.space[carType]: + self.space[carType] -= 1 + return True + return False + + +# Your ParkingSystem object will be instantiated and called as such: +# obj = ParkingSystem(big, medium, small) +# param_1 = obj.addCar(carType) \ No newline at end of file diff --git "a/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237.py" "b/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237.py" new file mode 100644 index 0000000..3259d52 --- /dev/null +++ "b/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237.py" @@ -0,0 +1,24 @@ +class ParkingSystem(object): + + def __init__(self, big, medium, small): + """ + :type big: int + :type medium: int + :type small: int + """ + self.space = [0, big, medium, small] + + def addCar(self, carType): + """ + :type carType: int + :rtype: bool + """ + if self.space[carType]: + self.space[carType] -= 1 + return True + return False + + +# Your ParkingSystem object will be instantiated and called as such: +# obj = ParkingSystem(big, medium, small) +# param_1 = obj.addCar(carType) \ No newline at end of file diff --git "a/1604.\350\255\246\345\221\212\344\270\200\345\260\217\346\227\266\345\206\205\344\275\277\347\224\250\347\233\270\345\220\214\345\221\230\345\267\245\345\215\241\345\244\247\344\272\216\347\255\211\344\272\216\344\270\211\346\254\241\347\232\204\344\272\272/1604-\350\255\246\345\221\212\344\270\200\345\260\217\346\227\266\345\206\205\344\275\277\347\224\250\347\233\270\345\220\214\345\221\230\345\267\245\345\215\241\345\244\247\344\272\216\347\255\211\344\272\216\344\270\211\346\254\241\347\232\204\344\272\272.py" "b/1604.\350\255\246\345\221\212\344\270\200\345\260\217\346\227\266\345\206\205\344\275\277\347\224\250\347\233\270\345\220\214\345\221\230\345\267\245\345\215\241\345\244\247\344\272\216\347\255\211\344\272\216\344\270\211\346\254\241\347\232\204\344\272\272/1604-\350\255\246\345\221\212\344\270\200\345\260\217\346\227\266\345\206\205\344\275\277\347\224\250\347\233\270\345\220\214\345\221\230\345\267\245\345\215\241\345\244\247\344\272\216\347\255\211\344\272\216\344\270\211\346\254\241\347\232\204\344\272\272.py" new file mode 100644 index 0000000..75da20e --- /dev/null +++ "b/1604.\350\255\246\345\221\212\344\270\200\345\260\217\346\227\266\345\206\205\344\275\277\347\224\250\347\233\270\345\220\214\345\221\230\345\267\245\345\215\241\345\244\247\344\272\216\347\255\211\344\272\216\344\270\211\346\254\241\347\232\204\344\272\272/1604-\350\255\246\345\221\212\344\270\200\345\260\217\346\227\266\345\206\205\344\275\277\347\224\250\347\233\270\345\220\214\345\221\230\345\267\245\345\215\241\345\244\247\344\272\216\347\255\211\344\272\216\344\270\211\346\254\241\347\232\204\344\272\272.py" @@ -0,0 +1,28 @@ +class Solution(object): + def alertNames(self, keyName, keyTime): + """ + :type keyName: List[str] + :type keyTime: List[str] + :rtype: List[str] + """ + from collections import defaultdict + name2time = defaultdict(list) + + res = set() + + def timeToMinutes(time): + # convert 10:05 to 605 + splitted_time = time.split(":") + return 60 * int(splitted_time[0]) + int(splitted_time[1]) + + pairs = sorted(zip(keyName, keyTime), key = lambda x: (x[0], x[1])) + + for name, time in pairs: + if name not in res: + name2time[name].append(time) + if len(name2time[name]) >= 3 and 0 <= timeToMinutes(time) - timeToMinutes(name2time[name][-3]) <= 60: + res.add(name) + + return sorted(list(res)) + + diff --git "a/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246 2.py" "b/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246 2.py" new file mode 100644 index 0000000..6944a1f --- /dev/null +++ "b/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246 2.py" @@ -0,0 +1,16 @@ +class Solution(object): + def maxDepth(self, s): + """ + :type s: str + :rtype: int + """ + depth = 0 + res = 0 + for ch in s: + if ch == "(": + depth += 1 + res = max(res, depth) + elif ch == ")": + depth -= 1 + + return res \ No newline at end of file diff --git "a/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246.py" "b/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246.py" new file mode 100644 index 0000000..6944a1f --- /dev/null +++ "b/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246.py" @@ -0,0 +1,16 @@ +class Solution(object): + def maxDepth(self, s): + """ + :type s: str + :rtype: int + """ + depth = 0 + res = 0 + for ch in s: + if ch == "(": + depth += 1 + res = max(res, depth) + elif ch == ")": + depth -= 1 + + return res \ No newline at end of file diff --git "a/1636.\346\214\211\347\205\247\351\242\221\347\216\207\345\260\206\346\225\260\347\273\204\345\215\207\345\272\217\346\216\222\345\272\217/1636-\346\214\211\347\205\247\351\242\221\347\216\207\345\260\206\346\225\260\347\273\204\345\215\207\345\272\217\346\216\222\345\272\217.py" "b/1636.\346\214\211\347\205\247\351\242\221\347\216\207\345\260\206\346\225\260\347\273\204\345\215\207\345\272\217\346\216\222\345\272\217/1636-\346\214\211\347\205\247\351\242\221\347\216\207\345\260\206\346\225\260\347\273\204\345\215\207\345\272\217\346\216\222\345\272\217.py" new file mode 100644 index 0000000..3f193e2 --- /dev/null +++ "b/1636.\346\214\211\347\205\247\351\242\221\347\216\207\345\260\206\346\225\260\347\273\204\345\215\207\345\272\217\346\216\222\345\272\217/1636-\346\214\211\347\205\247\351\242\221\347\216\207\345\260\206\346\225\260\347\273\204\345\215\207\345\272\217\346\216\222\345\272\217.py" @@ -0,0 +1,10 @@ +class Solution(object): + def frequencySort(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + from collections import Counter + dic = Counter(nums) + + return sorted(nums, key = lambda x:(dic[x], -x)) \ No newline at end of file diff --git "a/1646.\350\216\267\345\217\226\347\224\237\346\210\220\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\244\247\345\200\274/1646-\350\216\267\345\217\226\347\224\237\346\210\220\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/1646.\350\216\267\345\217\226\347\224\237\346\210\220\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\244\247\345\200\274/1646-\350\216\267\345\217\226\347\224\237\346\210\220\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..2d64c3c --- /dev/null +++ "b/1646.\350\216\267\345\217\226\347\224\237\346\210\220\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\244\247\345\200\274/1646-\350\216\267\345\217\226\347\224\237\346\210\220\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,14 @@ +class Solution(object): + def getMaximumGenerated(self, n): + """ + :type n: int + :rtype: int + """ + nums = [0, 1] + for i in range(2, n + 1): + if i % 2 == 0: + nums.append(nums[i // 2]) + else: + nums.append(nums[i // 2] + nums[i // 2 + 1]) + # print nums + return max(nums) if n else 0 \ No newline at end of file diff --git "a/1669.\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250/1669-\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250.py" "b/1669.\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250/1669-\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250.py" new file mode 100644 index 0000000..4b6cd69 --- /dev/null +++ "b/1669.\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250/1669-\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode: + dummy = ListNode(-1) + dummy.next = list1 + prev, cur = dummy, list1 + left, right = None, None + count = 0 + while cur: + if count == a: + left = prev + + if count == b: + right = cur.next + + count += 1 + prev, cur = cur, cur.next + + left.next = list2 + p = list2 + while p and p.next: + p = p.next + p.next = right + return dummy.next \ No newline at end of file diff --git "a/167.\344\270\244\346\225\260\344\271\213\345\222\214II-\350\276\223\345\205\245\346\234\211\345\272\217\346\225\260\347\273\204/167-\344\270\244\346\225\260\344\271\213\345\222\214II-\350\276\223\345\205\245\346\234\211\345\272\217\346\225\260\347\273\204.py" "b/167.\344\270\244\346\225\260\344\271\213\345\222\214II-\350\276\223\345\205\245\346\234\211\345\272\217\346\225\260\347\273\204/167-\344\270\244\346\225\260\344\271\213\345\222\214II-\350\276\223\345\205\245\346\234\211\345\272\217\346\225\260\347\273\204.py" deleted file mode 100644 index 720d9e2..0000000 --- "a/167.\344\270\244\346\225\260\344\271\213\345\222\214II-\350\276\223\345\205\245\346\234\211\345\272\217\346\225\260\347\273\204/167-\344\270\244\346\225\260\344\271\213\345\222\214II-\350\276\223\345\205\245\346\234\211\345\272\217\346\225\260\347\273\204.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def twoSum(self, numbers, target): - """ - :type numbers: List[int] - :type target: int - :rtype: List[int] - """ - left, right = 0, len(numbers) - 1 - while 1: - tmp = numbers[left] + numbers[right] - if tmp == target: - return [left + 1, right + 1] - elif tmp < target: - left += 1 - elif tmp > target: - right -= 1 diff --git "a/1678.\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250/1678-\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250.py" "b/1678.\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250/1678-\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250.py" new file mode 100644 index 0000000..c6d4300 --- /dev/null +++ "b/1678.\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250/1678-\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250.py" @@ -0,0 +1,21 @@ +class Solution: + def interpret(self, command: str) -> str: + res = "" + stack = [] + for char in command: + if char == "G": + res += char + elif char == "(": + stack.append(char) + elif char == ")": + if stack and stack[-1] == "(": + res += "o" + stack.pop() + else: + res += "al" + stack.pop() + stack.pop() + stack.pop() + else: + stack.append(char) + return res diff --git "a/1688.\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260/1688-\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260.py" "b/1688.\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260/1688-\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260.py" new file mode 100644 index 0000000..50a8100 --- /dev/null +++ "b/1688.\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260/1688-\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260.py" @@ -0,0 +1,11 @@ +class Solution: + def numberOfMatches(self, n: int) -> int: + res = 0 + while n != 1: + if n % 2: + res += (n - 1) // 2 + n = (n - 1) // 2 + 1 + else: + res += n // 2 + n = n // 2 + return res \ No newline at end of file diff --git "a/1700.\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217/1700-\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217.py" "b/1700.\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217/1700-\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217.py" new file mode 100644 index 0000000..1f1db8c --- /dev/null +++ "b/1700.\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217/1700-\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217.py" @@ -0,0 +1,16 @@ +class Solution: + def countStudents(self, students: List[int], sandwiches: List[int]) -> int: + from collections import deque + students = deque(students) + while sandwiches: + cur, l = 0, len(students) + while cur < l: + student = students.popleft() + if student == sandwiches[0]: + break + students.append(student) + cur += 1 + if cur == l: + break + sandwiches = sandwiches[1:] + return len(students) \ No newline at end of file diff --git "a/1704.\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274/1704-\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274.py" "b/1704.\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274/1704-\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274.py" new file mode 100644 index 0000000..b9a2ae4 --- /dev/null +++ "b/1704.\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274/1704-\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274.py" @@ -0,0 +1,8 @@ +class Solution: + def halvesAreAlike(self, s: str) -> bool: + s1, s2 = s[:len(s) // 2], s[len(s) // 2:] + return self.countVowels(s1) == self.countVowels(s2) + + def countVowels(self, s): + vowels = "aeiouAEIOU" + return sum(char in vowels for char in s) \ No newline at end of file diff --git "a/1716.\350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261/1716-\350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261.py" "b/1716.\350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261/1716-\350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261.py" new file mode 100644 index 0000000..e1090ee --- /dev/null +++ "b/1716.\350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261/1716-\350\256\241\347\256\227\345\212\233\346\211\243\351\223\266\350\241\214\347\232\204\351\222\261.py" @@ -0,0 +1,15 @@ +class Solution(object): + def totalMoney(self, n): + """ + :type n: int + :rtype: int + """ + day_cnt = 1 + summ = 0 + for i in range(1, n + 1): + if i % 7 == 1: + day_cnt = i // 7 + 1 + summ += day_cnt + day_cnt += 1 + + return summ diff --git "a/1720.\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204/1720-\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204.py" "b/1720.\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204/1720-\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204.py" new file mode 100644 index 0000000..efc2c51 --- /dev/null +++ "b/1720.\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204/1720-\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204.py" @@ -0,0 +1,6 @@ +class Solution: + def decode(self, encoded: List[int], first: int) -> List[int]: + res = [first] + for num in encoded: + res.append(res[-1] ^ num) + return res \ No newline at end of file diff --git "a/1721.\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/1721-\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/1721.\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/1721-\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..61bb471 --- /dev/null +++ "b/1721.\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/1721-\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,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: + p = head + l = 0 + while p: + l += 1 + p = p.next + + reversed_k = l - k + 1 + + count = 0 + p = head + while p: + count += 1 + if count == k: + left_node = p + if count == reversed_k: + right_nnode = p + p = p.next + + left_node.val, right_nnode.val = right_nnode.val, left_node.val + + return head \ No newline at end of file diff --git "a/1738.\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274/1738-\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.py" "b/1738.\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274/1738-\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.py" new file mode 100644 index 0000000..8dcc9f4 --- /dev/null +++ "b/1738.\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274/1738-\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.py" @@ -0,0 +1,24 @@ +from heapq import * +class Solution: + def kthLargestValue(self, matrix: List[List[int]], k: int) -> int: + m, n = len(matrix), len(matrix[0]) + xor_matrix = [[matrix[i][j] for j in range(n)] for i in range(m) ] + min_heap = [] + for i in range(m): + for j in range(n): + if i or j: + if not i: + # the first row + xor_matrix[i][j] ^= xor_matrix[i][j - 1] + elif not j: + xor_matrix[i][j] ^= xor_matrix[i - 1][j] + else: + xor_matrix[i][j] ^= xor_matrix[i][j - 1] ^ xor_matrix[i - 1][j] ^ xor_matrix[i - 1][j - 1] + if len(min_heap) < k: + heappush(min_heap, xor_matrix[i][j]) + else: + heappushpop(min_heap, xor_matrix[i][j]) + + return min_heap[0] + + \ No newline at end of file diff --git "a/1753.\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1753-\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" "b/1753.\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1753-\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" new file mode 100644 index 0000000..dcb4af2 --- /dev/null +++ "b/1753.\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1753-\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" @@ -0,0 +1,15 @@ +from heapq import * +class Solution: + def maximumScore(self, a: int, b: int, c: int) -> int: + max_heap = [-a, -b, -c] + heapify(max_heap) + res = 0 + while len(max_heap) > 1: + first, second = -heappop(max_heap), -heappop(max_heap) + res += 1 + + if first > 1: + heappush(max_heap, -(first - 1)) + if second > 1: + heappush(max_heap, -(second - 1)) + return res diff --git "a/1756.\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227/1756-\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227.py" "b/1756.\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227/1756-\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227.py" new file mode 100644 index 0000000..36e7a7a --- /dev/null +++ "b/1756.\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227/1756-\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227.py" @@ -0,0 +1,15 @@ +class MRUQueue: + + def __init__(self, n: int): + self.queue = [i for i in range(1, n + 1)] + + def fetch(self, k: int) -> int: + node = self.queue[k - 1] + self.queue = self.queue[:k - 1] + self.queue[k:] + [node] + return node + + + +# Your MRUQueue object will be instantiated and called as such: +# obj = MRUQueue(n) +# param_1 = obj.fetch(k) \ No newline at end of file diff --git "a/1762.\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251/1762-\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251.py" "b/1762.\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251/1762-\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251.py" new file mode 100644 index 0000000..ee30caa --- /dev/null +++ "b/1762.\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251/1762-\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251.py" @@ -0,0 +1,9 @@ +class Solution: + def findBuildings(self, heights: List[int]) -> List[int]: + stack = [] + for i, height in enumerate(heights): + while stack and heights[stack[-1]] <= height: + last_index = stack[-1] + stack.pop() + stack.append(i) + return sorted(stack) \ No newline at end of file diff --git "a/1768.\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262/1768-\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262.py" "b/1768.\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262/1768-\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..42dd0b1 --- /dev/null +++ "b/1768.\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262/1768-\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,12 @@ +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + res = "" + + for index, char1 in enumerate(word1): + res += char1 + if index < len(word2): + res += word2[index] + + if index < len(word2): + res += word2[index + 1:] + return res \ No newline at end of file diff --git "a/1769.\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1769-\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" "b/1769.\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1769-\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" new file mode 100644 index 0000000..f0ad0d4 --- /dev/null +++ "b/1769.\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1769-\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" @@ -0,0 +1,15 @@ +class Solution: + def minOperations(self, boxes: str) -> List[int]: + ones = [] + for index, box in enumerate(boxes): + if box == "1": + ones.append(index) + + res = [] + for index, box in enumerate(boxes): + cur_sum = 0 + for one_index in ones: + cur_sum += abs(one_index - index) + res.append(cur_sum) + + return res diff --git "a/1773.\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217/1773-\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217.py" "b/1773.\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217/1773-\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217.py" new file mode 100644 index 0000000..793e12a --- /dev/null +++ "b/1773.\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217/1773-\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217.py" @@ -0,0 +1,8 @@ +class Solution: + def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: + res = 0 + key2index = {"type":0, "color":1, "name":2} + for item in items: + if item[key2index[ruleKey]] == ruleValue: + res += 1 + return res diff --git "a/1812.\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262/1812-\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262.py" "b/1812.\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262/1812-\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262.py" new file mode 100644 index 0000000..33ae741 --- /dev/null +++ "b/1812.\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262/1812-\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262.py" @@ -0,0 +1,13 @@ +class Solution: + def squareIsWhite(self, coordinates: str) -> bool: + row, col = int(coordinates[1]), ord(coordinates[0]) - ord("a") + # 0 for balck, 1 for white + if col % 2 == 0: + color = 0 + else: + color = 1 + + if row % 2 == 0: + color = 1 - color + return color == 1 + \ No newline at end of file diff --git "a/1827.\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236/1827-\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236.cpp" "b/1827.\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236/1827-\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236.cpp" new file mode 100644 index 0000000..318b613 --- /dev/null +++ "b/1827.\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236/1827-\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236.cpp" @@ -0,0 +1,14 @@ +class Solution { +public: + int minOperations(vector& nums) { + int res = 0; + for (int i = 1; i < nums.size(); i++) + { + if (nums[i] <= nums[i - 1]) { + res += nums[i - 1] - nums[i] + 1; + nums[i] = nums[i - 1] + 1; + } + } + return res; + } +}; diff --git "a/1827.\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236/1827-\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236.py" "b/1827.\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236/1827-\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236.py" new file mode 100644 index 0000000..da4b37b --- /dev/null +++ "b/1827.\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236/1827-\346\234\200\345\260\221\346\223\215\344\275\234\344\275\277\346\225\260\347\273\204\351\200\222\345\242\236.py" @@ -0,0 +1,17 @@ +class Solution(object): + def minOperations(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if len(nums) == 1: + return 0 + res = 0 + for i, num in enumerate(nums): + if i: + if num <= nums[i - 1]: + res += (nums[i - 1] + 1) - num + nums[i] = nums[i - 1] + 1 + # print(nums) + return res + \ No newline at end of file diff --git "a/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245 2.py" "b/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245 2.py" new file mode 100644 index 0000000..502c70e --- /dev/null +++ "b/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245 2.py" @@ -0,0 +1,7 @@ +class Solution(object): + def checkIfPangram(self, sentence): + """ + :type sentence: str + :rtype: bool + """ + return len(set(sentence)) == 26 \ No newline at end of file diff --git "a/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245.py" "b/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245.py" new file mode 100644 index 0000000..502c70e --- /dev/null +++ "b/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245.py" @@ -0,0 +1,7 @@ +class Solution(object): + def checkIfPangram(self, sentence): + """ + :type sentence: str + :rtype: bool + """ + return len(set(sentence)) == 26 \ No newline at end of file diff --git "a/1833.\351\233\252\347\263\225\347\232\204\346\234\200\345\244\247\346\225\260\351\207\217/1833-\351\233\252\347\263\225\347\232\204\346\234\200\345\244\247\346\225\260\351\207\217.py" "b/1833.\351\233\252\347\263\225\347\232\204\346\234\200\345\244\247\346\225\260\351\207\217/1833-\351\233\252\347\263\225\347\232\204\346\234\200\345\244\247\346\225\260\351\207\217.py" new file mode 100644 index 0000000..b719006 --- /dev/null +++ "b/1833.\351\233\252\347\263\225\347\232\204\346\234\200\345\244\247\346\225\260\351\207\217/1833-\351\233\252\347\263\225\347\232\204\346\234\200\345\244\247\346\225\260\351\207\217.py" @@ -0,0 +1,13 @@ +class Solution(object): + def maxIceCream(self, costs, coins): + """ + :type costs: List[int] + :type coins: int + :rtype: int + """ + costs.sort() + for i in range(len(costs)): + if coins < costs[i]: + return i + coins -= costs[i] + return len(costs) \ No newline at end of file diff --git "a/1834.\345\215\225\347\272\277\347\250\213CPU/1834-\345\215\225\347\272\277\347\250\213CPU.py" "b/1834.\345\215\225\347\272\277\347\250\213CPU/1834-\345\215\225\347\272\277\347\250\213CPU.py" new file mode 100644 index 0000000..f62a4a2 --- /dev/null +++ "b/1834.\345\215\225\347\272\277\347\250\213CPU/1834-\345\215\225\347\272\277\347\250\213CPU.py" @@ -0,0 +1,34 @@ +class Solution(object): + def getOrder(self, tasks): + """ + :type tasks: List[List[int]] + :rtype: List[int] + """ + from heapq import * + if not tasks: + return [] + + tasks = [(pair[1], index, pair[0]) for index, pair in enumerate(tasks)] # 打包原始下标 + tasks.sort(key = lambda x: x[2]) # 按入队时间排序 + + next_task_id = 0 # 下一项要干的工作 + cur_time = tasks[0][2] + min_heap = [] + res = [] + while next_task_id < len(tasks) or min_heap: + while next_task_id < len(tasks) and tasks[next_task_id][2] <= cur_time: + # 入队所有已经可以开始干的工作 + heappush(min_heap, tasks[next_task_id]) + next_task_id += 1 + + # 开始工作 + if not min_heap: + # 直接跳到下一个有效时间 + cur_time = tasks[next_task_id][2] + else: + # 工作 + working_task = heappop(min_heap) + cur_time += working_task[0] + res.append(working_task[1]) + + return res \ No newline at end of file diff --git "a/1836.\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240/1836-\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.py" "b/1836.\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240/1836-\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.py" new file mode 100644 index 0000000..ce980fd --- /dev/null +++ "b/1836.\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240/1836-\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def deleteDuplicatesUnsorted(self, head: ListNode) -> ListNode: + visited = set() + duplicates = set() + p = head + while p: + if p.val not in visited: + visited.add(p.val) + else: + duplicates.add(p.val) + p = p.next + + dummy = ListNode(-1) + dummy.next = head + prev, cur = dummy, head + while cur: + if cur.val in duplicates: + prev.next = cur.next + cur = cur.next + else: + prev, cur = cur, cur.next + + return dummy.next diff --git "a/1837.K\350\277\233\345\210\266\350\241\250\347\244\272\344\270\213\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\346\200\273\345\222\214/1837-K\350\277\233\345\210\266\350\241\250\347\244\272\344\270\213\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\346\200\273\345\222\214.py" "b/1837.K\350\277\233\345\210\266\350\241\250\347\244\272\344\270\213\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\346\200\273\345\222\214/1837-K\350\277\233\345\210\266\350\241\250\347\244\272\344\270\213\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\346\200\273\345\222\214.py" new file mode 100644 index 0000000..6c8119e --- /dev/null +++ "b/1837.K\350\277\233\345\210\266\350\241\250\347\244\272\344\270\213\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\346\200\273\345\222\214/1837-K\350\277\233\345\210\266\350\241\250\347\244\272\344\270\213\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\346\200\273\345\222\214.py" @@ -0,0 +1,12 @@ +class Solution(object): + def sumBase(self, n, k): + """ + :type n: int + :type k: int + :rtype: int + """ + res = 0 + while n: + res += n % k + n //= k + return res \ No newline at end of file diff --git "a/1839.\346\211\200\346\234\211\345\205\203\351\237\263\346\214\211\351\241\272\345\272\217\346\216\222\345\270\203\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/1839-\346\211\200\346\234\211\345\205\203\351\237\263\346\214\211\351\241\272\345\272\217\346\216\222\345\270\203\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262.py" "b/1839.\346\211\200\346\234\211\345\205\203\351\237\263\346\214\211\351\241\272\345\272\217\346\216\222\345\270\203\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/1839-\346\211\200\346\234\211\345\205\203\351\237\263\346\214\211\351\241\272\345\272\217\346\216\222\345\270\203\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..bcbe22d --- /dev/null +++ "b/1839.\346\211\200\346\234\211\345\205\203\351\237\263\346\214\211\351\241\272\345\272\217\346\216\222\345\270\203\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262/1839-\346\211\200\346\234\211\345\205\203\351\237\263\346\214\211\351\241\272\345\272\217\346\216\222\345\270\203\347\232\204\346\234\200\351\225\277\345\255\220\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,23 @@ +class Solution(object): + def longestBeautifulSubstring(self, word): + """ + :type word: str + :rtype: int + """ + sequence = {"a":1, "e":2, "i":3, "o":4, "u":5} + + last_vowel = None + res = 0 + start = 0 + cur_set = set() + for i, ch in enumerate(word): + if not i or sequence[last_vowel] > sequence[ch]: + start = i + cur_set = set(ch) + else: + cur_set.add(ch) + # print(cur_set) + if ch == "u" and len(cur_set) == 5: + res = max(res, i - start + 1) + last_vowel = ch + return res diff --git "a/1844.\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242/1844-\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.py" "b/1844.\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242/1844-\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.py" new file mode 100644 index 0000000..3629862 --- /dev/null +++ "b/1844.\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242/1844-\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.py" @@ -0,0 +1,12 @@ +class Solution: + def replaceDigits(self, s: str) -> str: + res = "" + for i in range(0, len(s), 2): + char = s[i] + + if i + 1 < len(s): + shift = int(s[i + 1]) + res += char + chr(ord(char) + shift) + else: + res += char + return res \ No newline at end of file diff --git "a/1860.\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262/1860-\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262.py" "b/1860.\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262/1860-\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262.py" new file mode 100644 index 0000000..c18d4ca --- /dev/null +++ "b/1860.\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262/1860-\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262.py" @@ -0,0 +1,18 @@ +class Solution: + def memLeak(self, memory1: int, memory2: int) -> List[int]: + res = [] + memory = 1 + while memory1 or memory2: + if memory1 >= memory2: + if memory1 < memory: + break + else: + memory1 -= memory + else: + if memory2 < memory: + break + else: + memory2 -= memory + + memory += 1 + return [memory, memory1, memory2] \ No newline at end of file diff --git "a/1863.\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214/1863-\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214.py" "b/1863.\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214/1863-\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214.py" new file mode 100644 index 0000000..fb765fa --- /dev/null +++ "b/1863.\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214/1863-\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214.py" @@ -0,0 +1,12 @@ +class Solution: + def subsetXORSum(self, nums: List[int]) -> int: + subsets = [[]] + res = 0 + for num in nums: + new_subsets = [] + for subset in subsets: + new_subset = subset + [num] + res += reduce(lambda x, y: x^y, new_subset) + new_subsets.append(new_subset) + subsets += new_subsets + return res \ No newline at end of file diff --git "a/1874.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214/1874-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214.py" "b/1874.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214/1874-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214.py" new file mode 100644 index 0000000..62d9a28 --- /dev/null +++ "b/1874.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214/1874-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214.py" @@ -0,0 +1,10 @@ +class Solution: + def minProductSum(self, nums1: List[int], nums2: List[int]) -> int: + nums1.sort() + nums2.sort() + # print(nums1, nums2[::-1]) + res = 0 + for i in range(len(nums1)): + res += nums1[i] * nums2[-(i + 1)] + + return res \ No newline at end of file diff --git "a/1880.\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214/1880-\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214.py" "b/1880.\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214/1880-\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214.py" new file mode 100644 index 0000000..fad2cee --- /dev/null +++ "b/1880.\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214/1880-\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214.py" @@ -0,0 +1,9 @@ +class Solution: + def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool: + return self.sumOfDigit(firstWord) + self.sumOfDigit(secondWord) == self.sumOfDigit(targetWord) + + def sumOfDigit(self, word): + res = "" + for char in word: + res += str(ord(char) - ord("a")) + return int(res) \ No newline at end of file diff --git "a/1920.\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204/1920-\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204.py" "b/1920.\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204/1920-\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204.py" new file mode 100644 index 0000000..bbff3e1 --- /dev/null +++ "b/1920.\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204/1920-\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204.py" @@ -0,0 +1,3 @@ +class Solution: + def buildArray(self, nums: List[int]) -> List[int]: + return [nums[num] for num in nums] \ No newline at end of file diff --git "a/1929.\346\225\260\347\273\204\344\270\262\350\201\224/1929-\346\225\260\347\273\204\344\270\262\350\201\224.py" "b/1929.\346\225\260\347\273\204\344\270\262\350\201\224/1929-\346\225\260\347\273\204\344\270\262\350\201\224.py" new file mode 100644 index 0000000..5e822e9 --- /dev/null +++ "b/1929.\346\225\260\347\273\204\344\270\262\350\201\224/1929-\346\225\260\347\273\204\344\270\262\350\201\224.py" @@ -0,0 +1,3 @@ +class Solution: + def getConcatenation(self, nums: List[int]) -> List[int]: + return nums + nums \ No newline at end of file diff --git "a/1941.\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214/1941-\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214.py" "b/1941.\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214/1941-\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214.py" new file mode 100644 index 0000000..43abcbc --- /dev/null +++ "b/1941.\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214/1941-\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214.py" @@ -0,0 +1,5 @@ +class Solution: + def areOccurrencesEqual(self, s: str) -> bool: + from collections import Counter + c = Counter(s) + return 1 == len(set(c.values())) \ No newline at end of file diff --git "a/1945.\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214/1945-\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214.py" "b/1945.\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214/1945-\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214.py" new file mode 100644 index 0000000..66328fc --- /dev/null +++ "b/1945.\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214/1945-\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -0,0 +1,16 @@ +class Solution: + def getLucky(self, s: str, k: int) -> int: + + num = "" + for char in s: + num += str(ord(char) - ord("a") + 1) + + num = int(num) + while k: + new_num = 0 + while num: + num, m = divmod(num, 10) + new_num += m + num = new_num + k -= 1 + return num diff --git "a/1967.\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/1967-\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" "b/1967.\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/1967-\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" new file mode 100644 index 0000000..72ad316 --- /dev/null +++ "b/1967.\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/1967-\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" @@ -0,0 +1,9 @@ +class Solution: + def numOfStrings(self, patterns: List[str], word: str) -> int: + + res = 0 + for pattern in patterns: + if pattern in word: + res += 1 + + return res \ No newline at end of file diff --git "a/199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276.py" "b/199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276.py" deleted file mode 100644 index 7533bb6..0000000 --- "a/199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276.py" +++ /dev/null @@ -1,31 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def rightSideView(self, root): - """ - :type root: TreeNode - :rtype: List[int] - """ - from collections import deque - if not root: - return [] - - queue = deque([root]) - res = [] - last_element = None - while queue: - for _ in range(len(queue)): - cur = queue.popleft() - last_element = cur.val - if cur.left: - queue.append(cur.left) - if cur.right: - queue.append(cur.right) - res.append(last_element) - - return res \ No newline at end of file diff --git "a/200.\345\262\233\345\261\277\346\225\260\351\207\217/200-\345\262\233\345\261\277\346\225\260\351\207\217.py" "b/200.\345\262\233\345\261\277\346\225\260\351\207\217/200-\345\262\233\345\261\277\346\225\260\351\207\217.py" deleted file mode 100644 index 5e1ec45..0000000 --- "a/200.\345\262\233\345\261\277\346\225\260\351\207\217/200-\345\262\233\345\261\277\346\225\260\351\207\217.py" +++ /dev/null @@ -1,33 +0,0 @@ -class Solution(object): - def numIslands(self, M): - """ - :type grid: List[List[str]] - :rtype: int - """ - if not M or not M[0]: - return 0 - m, n = len(M), len(M[0]) - visited = [[0 for j in range(n)] for i in range(m)] - # print visited - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - res = 0 - - def dfs(x0, y0): - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - # print x, y - if 0<= x < m and 0 <= y < n and M[x][y] == '1' and visited[x][y] ==0: - visited[x][y] = 1 - dfs(x, y) - - for i in range(m): - for j in range(n): - if M[i][j] == '1' and visited[i][j] == 0: - res += 1 - visited[i][j] = 1 - dfs(i, j) - # print visited - - return res \ No newline at end of file diff --git "a/2000.\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200/2000-\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200.py" "b/2000.\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200/2000-\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200.py" new file mode 100644 index 0000000..7477494 --- /dev/null +++ "b/2000.\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200/2000-\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200.py" @@ -0,0 +1,7 @@ +class Solution: + def reversePrefix(self, word: str, ch: str) -> str: + if word.count(ch): + index = word.index(ch) + return word[:index + 1][::-1] + word[index + 1:] + else: + return word \ No newline at end of file diff --git "a/2011.\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/2011-\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274.py" "b/2011.\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/2011-\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274.py" new file mode 100644 index 0000000..e1aed68 --- /dev/null +++ "b/2011.\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/2011-\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274.py" @@ -0,0 +1,10 @@ +class Solution: + def finalValueAfterOperations(self, operations: List[str]) -> int: + res = 0 + for operation in operations: + if operation[0] == "-" or operation[-1] == "-": + res-= 1 + else: + res += 1 + + return res \ No newline at end of file diff --git "a/2089.\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207/2089-\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207.py" "b/2089.\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207/2089-\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207.py" new file mode 100644 index 0000000..e33ddba --- /dev/null +++ "b/2089.\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207/2089-\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207.py" @@ -0,0 +1,8 @@ +class Solution: + def targetIndices(self, nums: List[int], target: int) -> List[int]: + nums.sort() + res = [] + for i, num in enumerate(nums): + if num == target: + res.append(i) + return res \ No newline at end of file diff --git "a/2103.\347\216\257\345\222\214\346\235\206/2103-\347\216\257\345\222\214\346\235\206.py" "b/2103.\347\216\257\345\222\214\346\235\206/2103-\347\216\257\345\222\214\346\235\206.py" new file mode 100644 index 0000000..a1afb38 --- /dev/null +++ "b/2103.\347\216\257\345\222\214\346\235\206/2103-\347\216\257\345\222\214\346\235\206.py" @@ -0,0 +1,14 @@ +class Solution: + def countPoints(self, rings: str) -> int: + from collections import defaultdict + + ring2color = defaultdict(set) + for index in range(0, len(rings), 2): + color, ring = rings[index], rings[index + 1] + + ring2color[ring].add(color) + res = 0 + for ring, color in ring2color.items(): + if len(color) == 3: + res += 1 + return res \ No newline at end of file diff --git "a/2108.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/2108-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" "b/2108.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/2108-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..b2fb005 --- /dev/null +++ "b/2108.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/2108-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,6 @@ +class Solution: + def firstPalindrome(self, words: List[str]) -> str: + for word in words: + if word == word[::-1]: + return word + return "" \ No newline at end of file diff --git "a/2114.\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260/2114-\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260.py" "b/2114.\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260/2114-\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260.py" new file mode 100644 index 0000000..02760a0 --- /dev/null +++ "b/2114.\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260/2114-\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260.py" @@ -0,0 +1,8 @@ +class Solution: + def mostWordsFound(self, sentences: List[str]) -> int: + res = 0 + + for sentence in sentences: + word_count = len(sentence.split()) + res = max(res, word_count) + return res \ No newline at end of file diff --git "a/2120.\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244/2120-\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244.py" "b/2120.\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244/2120-\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244.py" new file mode 100644 index 0000000..611fcfd --- /dev/null +++ "b/2120.\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244/2120-\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244.py" @@ -0,0 +1,27 @@ +class Solution: + def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]: + res = [] + for i, _ in enumerate(s): + j = i + cur_row, cur_col = startPos[0], startPos[1] + while 1: + if j == len(s): + break + ins = s[j] + if ins == "R": + cur_col += 1 + elif ins == "L": + cur_col -= 1 + elif ins == "U": + cur_row -= 1 + elif ins == "D": + cur_row += 1 + j += 1 + if self.moveOutside(cur_row, cur_col, n): + j -= 1 + break + res.append(j - i) + return res + + def moveOutside(self, cur_row, cur_col, n): + return not 0 <= cur_row < n or not 0 <= cur_col < n diff --git "a/2125.\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/2125-\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217.py" "b/2125.\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/2125-\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217.py" new file mode 100644 index 0000000..3ad4623 --- /dev/null +++ "b/2125.\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/2125-\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217.py" @@ -0,0 +1,10 @@ +class Solution: + def numberOfBeams(self, bank: List[str]) -> int: + res = 0 + last_device_count = 0 + for row_index, row in enumerate(bank): + cur_device_count = row.count("1") + res += cur_device_count * last_device_count + if cur_device_count: + last_device_count = cur_device_count + return res \ No newline at end of file diff --git "a/2130.\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214/2130-\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214.py" "b/2130.\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214/2130-\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214.py" new file mode 100644 index 0000000..05973ff --- /dev/null +++ "b/2130.\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214/2130-\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214.py" @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def pairSum(self, head: Optional[ListNode]) -> int: + p, l = head, 0 + while p: + l += 1 + p = p.next + + stack = [] + cur = 0 + p = head + while cur < l // 2: + cur += 1 + stack.append(p.val) + p = p.next + + res = 0 + while cur < l: + cur += 1 + res = max(res, p.val + stack.pop()) + p = p.next + return res \ No newline at end of file diff --git "a/2149.\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204/2149-\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204.py" "b/2149.\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204/2149-\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204.py" new file mode 100644 index 0000000..b2f3320 --- /dev/null +++ "b/2149.\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204/2149-\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204.py" @@ -0,0 +1,17 @@ +class Solution: + def rearrangeArray(self, nums: List[int]) -> List[int]: + pos, neg = [], [] + + for num in nums: + if num > 0: + pos.append(num) + else: + neg.append(num) + + t = [(pos[i], neg[i]) for i in range(len(pos))] + + res = [] + for pair in t: + res.append(pair[0]) + res.append(pair[1]) + return res \ No newline at end of file diff --git "a/2154.\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452/2154-\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452.py" "b/2154.\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452/2154-\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452.py" new file mode 100644 index 0000000..6c09e8b --- /dev/null +++ "b/2154.\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452/2154-\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452.py" @@ -0,0 +1,6 @@ +class Solution: + def findFinalValue(self, nums: List[int], original: int) -> int: + s = set(nums) + while original in s: + original *= 2 + return original \ No newline at end of file diff --git "a/2161.\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204/2161-\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204.py" "b/2161.\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204/2161-\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204.py" new file mode 100644 index 0000000..1f17945 --- /dev/null +++ "b/2161.\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204/2161-\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204.py" @@ -0,0 +1,13 @@ +class Solution: + def pivotArray(self, nums: List[int], pivot: int) -> List[int]: + small, equal, larger = [], 0, [] + + for num in nums: + if num < pivot: + small.append(num) + elif num == pivot: + equal += 1 + else: + larger.append(num) + + return small + equal * [pivot] + larger \ No newline at end of file diff --git "a/2169.\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260/2169-\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260.py" "b/2169.\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260/2169-\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260.py" new file mode 100644 index 0000000..5c7e93e --- /dev/null +++ "b/2169.\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260/2169-\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260.py" @@ -0,0 +1,11 @@ +class Solution: + def countOperations(self, num1: int, num2: int) -> int: + res = 0 + while num1 and num2: + res += 1 + if num1 >= num2: + num1 -= num2 + else: + num2 -= num1 + + return res \ No newline at end of file diff --git "a/2181.\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271/2181-\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271.py" "b/2181.\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271/2181-\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271.py" new file mode 100644 index 0000000..fbb8dcd --- /dev/null +++ "b/2181.\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271/2181-\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271.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 mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(-1) + p_new = dummy + + p = head + cur_sum = 0 + while p: + if p.val == 0: + if cur_sum: + node = ListNode(cur_sum) + p_new.next = node + p_new = p_new.next + cur_sum = 0 + else: + cur_sum += p.val + p = p.next + + return dummy.next \ No newline at end of file diff --git "a/2185.\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262/2185-\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262.py" "b/2185.\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262/2185-\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..b618879 --- /dev/null +++ "b/2185.\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262/2185-\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,8 @@ +class Solution: + def prefixCount(self, words: List[str], pref: str) -> int: + res = 0 + + for word in words: + if word.startswith(pref): + res += 1 + return res \ No newline at end of file diff --git "a/2194.Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274/2194-Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274.py" "b/2194.Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274/2194-Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274.py" new file mode 100644 index 0000000..f5008cd --- /dev/null +++ "b/2194.Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274/2194-Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274.py" @@ -0,0 +1,10 @@ +class Solution: + def cellsInRange(self, s: str) -> List[str]: + res = [] + start_row, end_row = int(s[1]), int(s[-1]) + start_col, end_col = s[0], s[3] + + for cur in range(ord(start_col), ord(end_col) + 1): + for row in range(start_row, end_row + 1): + res.append(chr(cur) + str(row)) + return res \ No newline at end of file diff --git "a/22.\346\213\254\345\217\267\347\224\237\346\210\220/22-\346\213\254\345\217\267\347\224\237\346\210\220.py" "b/22.\346\213\254\345\217\267\347\224\237\346\210\220/22-\346\213\254\345\217\267\347\224\237\346\210\220.py" deleted file mode 100644 index d92c65c..0000000 --- "a/22.\346\213\254\345\217\267\347\224\237\346\210\220/22-\346\213\254\345\217\267\347\224\237\346\210\220.py" +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def generateParenthesis(self, n): - """ - :type n: int - :rtype: List[str] - """ - - res = [] - - def dfs(l, r, tmp): - if not l and not r: - res.append(tmp[:]) - if l: - dfs(l - 1, r, tmp + "(") - if l < r: - dfs(l, r - 1, tmp + ")") - dfs(n, n, "") - return res \ No newline at end of file diff --git "a/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221-\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.py" "b/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221-\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.py" deleted file mode 100644 index 66eb540..0000000 --- "a/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/221-\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.py" +++ /dev/null @@ -1,29 +0,0 @@ -class Solution(object): - def maximalSquare(self, matrix): - """ - :type matrix: List[List[str]] - :rtype: int - """ - if not matrix or not matrix[0]: - return 0 - m, n = len(matrix), len(matrix[0]) - - dp = [[0 for _ in range(n)] for _ in range(m)] - res = 0 - for j in range(n): - if matrix[0][j] == "1": - dp[0][j] = 1 - res = 1 - - for i in range(m): - if matrix[i][0] == "1": - dp[i][0] = 1 - res = 1 - - for i in range(1, m): - for j in range(1, n): - if matrix[i][j] == "1": - dp[i][j] = min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1 - res = max(res, dp[i][j] ** 2) - # print dp - return res \ No newline at end of file diff --git "a/2221.\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214/2221-\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214.py" "b/2221.\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214/2221-\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214.py" new file mode 100644 index 0000000..d755d2b --- /dev/null +++ "b/2221.\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214/2221-\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214.py" @@ -0,0 +1,6 @@ +class Solution: + def triangularSum(self, nums: List[int]) -> int: + if len(nums) == 1: + return nums[0] + + return self.triangularSum([nums[i] + nums[i - 1] for i in range(1, len(nums))]) % 10 \ No newline at end of file diff --git "a/2231.\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/2231-\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" "b/2231.\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/2231-\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" new file mode 100644 index 0000000..07efd32 --- /dev/null +++ "b/2231.\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/2231-\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" @@ -0,0 +1,12 @@ +class Solution: + def largestInteger(self, num: int) -> int: + odd = sorted([int(digit) for digit in str(num) if digit in "13579"]) + even = sorted([int(digit) for digit in str(num) if digit not in "13579"]) + res = 0 + for digit in str(num): + if int(digit) % 2: + res = res * 10 + odd.pop() + else: + res = res * 10 + even.pop() + + return res diff --git "a/2235.\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240/2235-\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240.py" "b/2235.\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240/2235-\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240.py" new file mode 100644 index 0000000..a0a7c1f --- /dev/null +++ "b/2235.\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240/2235-\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240.py" @@ -0,0 +1,3 @@ +class Solution: + def sum(self, num1: int, num2: int) -> int: + return num1 + num2 \ No newline at end of file diff --git "a/2236.\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214/2236-\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214.py" "b/2236.\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214/2236-\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214.py" new file mode 100644 index 0000000..157a3c8 --- /dev/null +++ "b/2236.\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214/2236-\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214.py" @@ -0,0 +1,9 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def checkTree(self, root: Optional[TreeNode]) -> bool: + return root.val == root.left.val + root.right.val \ No newline at end of file diff --git "a/225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/225-\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.py" "b/225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/225-\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.py" deleted file mode 100644 index 21c0098..0000000 --- "a/225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210/225-\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.py" +++ /dev/null @@ -1,49 +0,0 @@ -from collections import deque -class MyStack(object): - def __init__(self): - """ - Initialize your data structure here. - """ - self.q1 = deque() - self.q2 = deque() - - def push(self, x): - """ - Push element x onto stack. - :type x: int - :rtype: None - """ - self.q1.append(x) - while self.q2: - self.q1.append(self.q2.popleft()) - self.q2 = self.q1 - self.q1 = deque() - - def pop(self): - """ - Removes the element on top of the stack and returns that element. - :rtype: int - """ - return self.q2.popleft() - - def top(self): - """ - Get the top element. - :rtype: int - """ - return self.q2[0] - - def empty(self): - """ - Returns whether the stack is empty. - :rtype: bool - """ - return not self.q2 - - -# Your MyStack object will be instantiated and called as such: -# obj = MyStack() -# obj.push(x) -# param_2 = obj.pop() -# param_3 = obj.top() -# param_4 = obj.empty() \ No newline at end of file diff --git "a/2255.\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/2255-\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" "b/2255.\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/2255-\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" new file mode 100644 index 0000000..0966cdd --- /dev/null +++ "b/2255.\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/2255-\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" @@ -0,0 +1,7 @@ +class Solution: + def countPrefixes(self, words: List[str], s: str) -> int: + res = 0 + for word in words: + if s.startswith(word): + res += 1 + return res \ No newline at end of file diff --git "a/2265.\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260/2265-\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260.py" "b/2265.\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260/2265-\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260.py" new file mode 100644 index 0000000..a326504 --- /dev/null +++ "b/2265.\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260/2265-\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def averageOfSubtree(self, root: Optional[TreeNode]) -> int: + + self.res = 0 + def getAverageOfSubtree(node): + # post-order traversal + # return sum count + if not node: + return 0, 0 + + left_subtree_sum, left_count = getAverageOfSubtree(node.left) + right_subtree_sum, right_count = getAverageOfSubtree(node.right) + + subtree_sum = node.val + left_subtree_sum + right_subtree_sum + subtree_count = left_count + right_count + 1 + if node.val == subtree_sum // subtree_count: + self.res += 1 + + return subtree_sum, subtree_count + + getAverageOfSubtree(root) + return self.res \ No newline at end of file diff --git "a/2278.\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224/2278-\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224.py" "b/2278.\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224/2278-\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224.py" new file mode 100644 index 0000000..2974f8e --- /dev/null +++ "b/2278.\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224/2278-\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224.py" @@ -0,0 +1,3 @@ +class Solution: + def percentageLetter(self, s: str, letter: str) -> int: + return 100 * s.count(letter) // len(s) \ No newline at end of file diff --git "a/2283.\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274/2283-\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274.py" "b/2283.\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274/2283-\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274.py" new file mode 100644 index 0000000..8f5aabe --- /dev/null +++ "b/2283.\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274/2283-\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274.py" @@ -0,0 +1,10 @@ +class Solution: + def digitCount(self, num: str) -> bool: + from collections import Counter + c = Counter(num) + + for index, n in enumerate(num): + if c[str(index)] != int(n): + # print(c[index], index, int(n)) + return False + return True \ No newline at end of file diff --git "a/2309.\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215/2309-\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215.py" "b/2309.\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215/2309-\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215.py" new file mode 100644 index 0000000..00682ae --- /dev/null +++ "b/2309.\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215/2309-\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215.py" @@ -0,0 +1,9 @@ +class Solution: + def greatestLetter(self, s: str) -> str: + res = "" + s = set(s) + for char in s: + if char.lower() in s and char.upper() in s: + if not res or char.upper() > res: + res = char.upper() + return res \ No newline at end of file diff --git "a/2315.\347\273\237\350\256\241\346\230\237\345\217\267/2315-\347\273\237\350\256\241\346\230\237\345\217\267.py" "b/2315.\347\273\237\350\256\241\346\230\237\345\217\267/2315-\347\273\237\350\256\241\346\230\237\345\217\267.py" new file mode 100644 index 0000000..cfed43d --- /dev/null +++ "b/2315.\347\273\237\350\256\241\346\230\237\345\217\267/2315-\347\273\237\350\256\241\346\230\237\345\217\267.py" @@ -0,0 +1,10 @@ +class Solution: + def countAsterisks(self, s: str) -> int: + bar = False + res = 0 + for char in s: + if char == "|": + bar = not bar + if not bar and char == "*": + res += 1 + return res \ No newline at end of file diff --git "a/2319.\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265/2319-\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265.py" "b/2319.\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265/2319-\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265.py" new file mode 100644 index 0000000..c8d94e1 --- /dev/null +++ "b/2319.\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265/2319-\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265.py" @@ -0,0 +1,15 @@ +class Solution: + def checkXMatrix(self, grid: List[List[int]]) -> bool: + if not grid or not grid[0]: + return False + m, n = len(grid), len(grid[0]) + + for i in range(m): + for j in range(n): + if i == j or i + j == n - 1: + if grid[i][j] == 0: + return False + else: + if grid[i][j] != 0: + return False + return True \ No newline at end of file diff --git "a/2330.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV/2330-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV.py" "b/2330.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV/2330-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV.py" new file mode 100644 index 0000000..2ce85bf --- /dev/null +++ "b/2330.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV/2330-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV.py" @@ -0,0 +1,12 @@ +class Solution: + def makePalindrome(self, s: str) -> bool: + left, right = 0, len(s) - 1 + + step = 0 + while left < right: + if s[left] != s[right]: + step += 1 + left += 1 + right -= 1 + + return step <= 2 \ No newline at end of file diff --git "a/2331.\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274/2331-\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274.py" "b/2331.\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274/2331-\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274.py" new file mode 100644 index 0000000..29c46c5 --- /dev/null +++ "b/2331.\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274/2331-\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274.py" @@ -0,0 +1,16 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def evaluateTree(self, root: Optional[TreeNode]) -> bool: + if root.val == 0: + return False + if root.val == 1: + return True + if root.val == 2: + return self.evaluateTree(root.left) or self.evaluateTree(root.right) + if root.val == 3: + return self.evaluateTree(root.left) and self.evaluateTree(root.right) \ No newline at end of file diff --git "a/2335.\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277/2335-\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277.py" "b/2335.\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277/2335-\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277.py" new file mode 100644 index 0000000..7775e85 --- /dev/null +++ "b/2335.\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277/2335-\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277.py" @@ -0,0 +1,16 @@ +from heapq import * +class Solution: + def fillCups(self, amount: List[int]) -> int: + max_heap = [-a for a in amount if a] + heapify(max_heap) + res = 0 + while len(max_heap) > 1: + first, second = -heappop(max_heap), -heappop(max_heap) + if first > 1: + heappush(max_heap, -(first - 1)) + if second > 1: + heappush(max_heap, -(second - 1)) + res += 1 + + return res + -max_heap[0] if max_heap else res + \ No newline at end of file diff --git "a/2336.\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/2336-\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" "b/2336.\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/2336-\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" new file mode 100644 index 0000000..65801ca --- /dev/null +++ "b/2336.\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/2336-\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" @@ -0,0 +1,23 @@ +from heapq import * +class SmallestInfiniteSet: + + def __init__(self): + self.min_heap = [i for i in range(1, 1001)] + heapify(self.min_heap) + self.set = set(self.min_heap) + + def popSmallest(self) -> int: + val = heappop(self.min_heap) + self.set.remove(val) + return val + + def addBack(self, num: int) -> None: + if num not in self.set: + heappush(self.min_heap, num) + self.set.add(num) + + +# Your SmallestInfiniteSet object will be instantiated and called as such: +# obj = SmallestInfiniteSet() +# param_1 = obj.popSmallest() +# obj.addBack(num) \ No newline at end of file diff --git "a/2352.\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271/2352-\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271.py" "b/2352.\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271/2352-\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271.py" new file mode 100644 index 0000000..6b08e20 --- /dev/null +++ "b/2352.\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271/2352-\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271.py" @@ -0,0 +1,8 @@ +class Solution: + def equalPairs(self, grid: List[List[int]]) -> int: + col_grid = [list(col) for col in zip(*grid)] + res = 0 + for row in grid: + for col in col_grid: + res += row == col + return res \ No newline at end of file diff --git "a/2357.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266/2357-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266.py" "b/2357.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266/2357-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266.py" new file mode 100644 index 0000000..23592d7 --- /dev/null +++ "b/2357.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266/2357-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266.py" @@ -0,0 +1,13 @@ +class Solution: + def minimumOperations(self, nums: List[int]) -> int: + res = 0 + while sum(nums): + res += 1 + m = 101 + for num in nums: + if num: + m = min(m, num) + for i, num in enumerate(nums): + if num: + nums[i] -= m + return res \ No newline at end of file diff --git "a/2391.\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264/2391-\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264.py" "b/2391.\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264/2391-\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264.py" new file mode 100644 index 0000000..95d587a --- /dev/null +++ "b/2391.\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264/2391-\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264.py" @@ -0,0 +1,36 @@ +class Solution: + def garbageCollection(self, garbage: List[str], travel: List[int]) -> int: + t_m, t_p, t_g = 0, 0, 0 + + driving_time_m = 0 + driving_time_p = 0 + driving_time_g = 0 + for index, g in enumerate(garbage): + # M + count_m = g.count("M") + if index: + driving_time_m += travel[index - 1] + if count_m: + t_m += count_m + t_m += driving_time_m + driving_time_m = 0 + + + count_p = g.count("P") + if index: + driving_time_p += travel[index - 1] + if count_p: + t_p += count_p + t_p += driving_time_p + driving_time_p = 0 + + + count_g = g.count("G") + if index: + driving_time_g += travel[index - 1] + if count_g: + t_g += count_g + t_g += driving_time_g + driving_time_g = 0 + + return t_m + t_p + t_g \ No newline at end of file diff --git "a/2396.\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227/2396-\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227.py" "b/2396.\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227/2396-\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..a6f7c89 --- /dev/null +++ "b/2396.\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227/2396-\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,11 @@ +class Solution: + def isStrictlyPalindromic(self, n: int) -> bool: + for k in range(2, n - 1): + bk = "" + temp = n + while temp: + temp, m = divmod(temp, k) + bk += str(m) + if bk != bk[::-1]: + return False + return True \ No newline at end of file diff --git "a/2399.\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273/2399-\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273.py" "b/2399.\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273/2399-\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273.py" new file mode 100644 index 0000000..f9b5b32 --- /dev/null +++ "b/2399.\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273/2399-\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273.py" @@ -0,0 +1,9 @@ +class Solution: + def checkDistances(self, s: str, distance: List[int]) -> bool: + res = True + for i, d in enumerate(distance): + char = chr(ord("a") + i) + if s.count(char): + if s.rfind(char) - s.index(char) != d + 1: + res = False + return res \ No newline at end of file diff --git "a/2413.\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260/2413-\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260.py" "b/2413.\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260/2413-\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260.py" new file mode 100644 index 0000000..c5d3e13 --- /dev/null +++ "b/2413.\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260/2413-\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260.py" @@ -0,0 +1,3 @@ +class Solution: + def smallestEvenMultiple(self, n: int) -> int: + return 2 * n if n % 2 else n \ No newline at end of file diff --git "a/2418.\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217/2418-\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217.py" "b/2418.\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217/2418-\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217.py" new file mode 100644 index 0000000..ae9c914 --- /dev/null +++ "b/2418.\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217/2418-\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217.py" @@ -0,0 +1,6 @@ +class Solution: + def sortPeople(self, names: List[str], heights: List[int]) -> List[str]: + combine = [(name, heights[index]) for index, name in enumerate(names)] + + + return [pair[0] for pair in sorted(combine, key = lambda x: -x[1])] \ No newline at end of file diff --git "a/2441.\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260/2441-\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260.py" "b/2441.\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260/2441-\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260.py" new file mode 100644 index 0000000..fc19c6b --- /dev/null +++ "b/2441.\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260/2441-\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260.py" @@ -0,0 +1,8 @@ +class Solution: + def findMaxK(self, nums: List[int]) -> int: + s = set(nums) + res = -1 + for num in nums: + if -num in s: + res = max(num, -num, res) + return res \ No newline at end of file diff --git "a/2446.\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201/2446-\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201.py" "b/2446.\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201/2446-\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201.py" new file mode 100644 index 0000000..15bcc06 --- /dev/null +++ "b/2446.\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201/2446-\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201.py" @@ -0,0 +1,8 @@ +class Solution: + def haveConflict(self, event1: List[str], event2: List[str]) -> bool: + return not (self.time1EarlierThanTime2(event1[1], event2[0]) or self.time1EarlierThanTime2(event2[1], event1[0])) + + def time1EarlierThanTime2(self, time1, time2): + h1, h2 = int(time1[:2]), int(time2[:2]) + m1, m2 = int(time1[3:]), int(time2[3:]) + return h1 < h2 or (h1 == h2 and m1 < m2) \ No newline at end of file diff --git "a/2451.\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262/2451-\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262.py" "b/2451.\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262/2451-\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..b713bed --- /dev/null +++ "b/2451.\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262/2451-\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,17 @@ +class Solution: + def oddString(self, words: List[str]) -> str: + l = [] + for word in words: + diff = [] + for i, char in enumerate(word): + if i: + diff.append(ord(char) - ord(word[i - 1])) + l.append(diff) + # print(l) + + if l[0] != l[1] and l[0] != l[2]: + return words[0] + + for i, diff in enumerate(l): + if diff != l[0]: + return words[i] \ No newline at end of file diff --git "a/2455.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274/2455-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274.py" "b/2455.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274/2455-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274.py" new file mode 100644 index 0000000..94144da --- /dev/null +++ "b/2455.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274/2455-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274.py" @@ -0,0 +1,8 @@ +class Solution: + def averageValue(self, nums: List[int]) -> int: + s, cnt = 0, 0 + for num in nums: + if num % 6 == 0: + cnt += 1 + s += num + return int(s / cnt) if cnt else 0 \ No newline at end of file diff --git "a/2460.\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234/2460-\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234.py" "b/2460.\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234/2460-\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234.py" new file mode 100644 index 0000000..6a40092 --- /dev/null +++ "b/2460.\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234/2460-\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234.py" @@ -0,0 +1,17 @@ +class Solution: + def applyOperations(self, nums: List[int]) -> List[int]: + n = len(nums) + for i in range(n - 1): + if nums[i] == nums[i + 1]: + nums[i] *= 2 + nums[i + 1] = 0 + + index = 0 + for i in range(n): + if nums[i]: + nums[index] = nums[i] + index += 1 + + for i in range(index, n): + nums[i] = 0 + return nums \ No newline at end of file diff --git "a/2465.\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256/2465-\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256.py" "b/2465.\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256/2465-\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256.py" new file mode 100644 index 0000000..02dfad9 --- /dev/null +++ "b/2465.\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256/2465-\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256.py" @@ -0,0 +1,9 @@ +class Solution: + def distinctAverages(self, nums: List[int]) -> int: + nums.sort() + res = set() + while nums: + res.add((nums[0] + nums[-1]) / 2.0) + nums = nums[1:-1] + + return len(res) \ No newline at end of file diff --git "a/2466.\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260/2466-\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260.py" "b/2466.\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260/2466-\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260.py" new file mode 100644 index 0000000..812b4f1 --- /dev/null +++ "b/2466.\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260/2466-\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260.py" @@ -0,0 +1,36 @@ +class Solution: + def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int: + # f(i) = f(i - zero) + f(i - one) + # 00, 10, 11, 10, 0, 1 + # self.res = 0 + # MOD = int(1e9 + 7) + # memo = dict() + # def dfs(i): + # if i == 0: + # return 1 + # count = 0 + # if i in memo: + # return memo[i] + # if i >= zero: + # count += dfs(i - zero) + # if i >= one: + # count += dfs(i - one) + # # l represents count of distinct good string of length i + # if i >= low: + # self.res += count % MOD + # memo[i] = count + # return count + # dfs(high) + # return self.res % MOD + MOD = int(1e9 + 7) + dp = [0] * (high + max(zero, one)) + dp[0] = 1 + res = 0 + for i in range(1, high + 1): + if i >= zero: + dp[i] += dp[i - zero] + if i >= one: + dp[i] += dp[i - one] + if i >= low: + res += dp[i] % MOD + return res % MOD \ No newline at end of file diff --git "a/2469.\346\270\251\345\272\246\350\275\254\346\215\242/2469-\346\270\251\345\272\246\350\275\254\346\215\242.py" "b/2469.\346\270\251\345\272\246\350\275\254\346\215\242/2469-\346\270\251\345\272\246\350\275\254\346\215\242.py" new file mode 100644 index 0000000..091b21a --- /dev/null +++ "b/2469.\346\270\251\345\272\246\350\275\254\346\215\242/2469-\346\270\251\345\272\246\350\275\254\346\215\242.py" @@ -0,0 +1,3 @@ +class Solution: + def convertTemperature(self, celsius: float) -> List[float]: + return [celsius + 273.15, celsius * 1.8 + 32] \ No newline at end of file diff --git "a/2482.\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274/2482-\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274.py" "b/2482.\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274/2482-\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274.py" new file mode 100644 index 0000000..270f3e4 --- /dev/null +++ "b/2482.\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274/2482-\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274.py" @@ -0,0 +1,15 @@ +class Solution: + def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]: + m, n = len(grid), len(grid[0]) + row2one = {} + col2one = {} + for i, row in enumerate(grid): + row2one[i] = sum(row) + for j, col in enumerate(zip(*grid)): + col2one[j] = sum(col) + + diff = [[0 for i in range(n)] for j in range(m)] + for i in range(m): + for j in range(n): + diff[i][j] = row2one[i] + col2one[j] - (n - row2one[i]) - (m - col2one[j]) + return diff \ No newline at end of file diff --git "a/2487.\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271/2487-\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271.py" "b/2487.\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271/2487-\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271.py" new file mode 100644 index 0000000..0848d99 --- /dev/null +++ "b/2487.\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271/2487-\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(-1) + dummy.next = head + + stack = [] # [node] + nodes_to_be_deleted = set() + p = head + while p: + while stack and stack[-1].val < p.val: + nodes_to_be_deleted.add(stack[-1]) + stack.pop() + stack.append(p) + p = p.next + + p = dummy + while p.next: + if p.next in nodes_to_be_deleted: + p.next = p.next.next + else: + p = p.next + return dummy.next + diff --git "a/2496.\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274/2496-\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/2496.\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274/2496-\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..5a09c51 --- /dev/null +++ "b/2496.\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274/2496-\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,14 @@ +class Solution: + def maximumValue(self, strs: List[str]) -> int: + res = float("-inf") + for s in strs: + isNumber = True + for char in s: + if not char.isdigit(): + isNumber = False + break + if isNumber: + res = max(res, int(s)) + else: + res = max(res, len(s)) + return res \ No newline at end of file diff --git "a/25.K\344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250/25-K\344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250.py" "b/25.K\344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250/25-K\344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250.py" deleted file mode 100644 index ca17ef9..0000000 --- "a/25.K\344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250/25-K\344\270\252\344\270\200\347\273\204\347\277\273\350\275\254\351\223\276\350\241\250.py" +++ /dev/null @@ -1,45 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def reverseKGroup(self, head, k): - """ - :type head: ListNode - :type k: int - :rtype: ListNode - """ - if not head or not head.next: - return head - - cnt = 0 - p = head - while p: - cnt += 1 - if cnt == k: - break - p = p.next - - if cnt < k: - return head - - tail = p.next - p.next = None - - tmp = self.reverseKGroup(tail, k) - newhead = self.reverseLL(head) - head.next = tmp - - return newhead - - - - def reverseLL(self, head): - if not head or not head.next: - return head - p = self.reverseLL(head.next) - head.next.next = head - head.next = None - return p \ No newline at end of file diff --git "a/2506.\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256/2506-\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256.py" "b/2506.\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256/2506-\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..80070f6 --- /dev/null +++ "b/2506.\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256/2506-\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256.py" @@ -0,0 +1,11 @@ +class Solution: + def similarPairs(self, words: List[str]) -> int: + from collections import defaultdict + pattern2count = defaultdict(int) + for word in words: + pattern2count["".join(sorted(list(set(word))))] += 1 + + res = 0 + for pattern, count in pattern2count.items(): + res += count * (count - 1) // 2 + return res \ No newline at end of file diff --git "a/2517.\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246/2517-\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246.py" "b/2517.\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246/2517-\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246.py" new file mode 100644 index 0000000..a38b8cd --- /dev/null +++ "b/2517.\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246/2517-\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246.py" @@ -0,0 +1,25 @@ +class Solution: + def maximumTastiness(self, price: List[int], k: int) -> int: + # 13, 1, 21 - 12, 20, 8 + price = set(price) + if len(price) < k: + return 0 + + # 1, 2, 5, 8, 13, 21 + price = sorted(list(price)) + left, right = 0, price[-1] - price[0] + while left <= right: + mid = (left + right) // 2 # target tastyness + + cnt, prev = 1, price[0] + for p in price: + if prev + mid <= p: # 又找到了一个 + cnt += 1 + prev = p + + if cnt >= k: + left = mid + 1 + elif cnt < k: + right = mid - 1 + + return right \ No newline at end of file diff --git "a/252.\344\274\232\350\256\256\345\256\244/252-\344\274\232\350\256\256\345\256\244.py" "b/252.\344\274\232\350\256\256\345\256\244/252-\344\274\232\350\256\256\345\256\244.py" deleted file mode 100644 index b0d5838..0000000 --- "a/252.\344\274\232\350\256\256\345\256\244/252-\344\274\232\350\256\256\345\256\244.py" +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def canAttendMeetings(self, intervals): - """ - :type intervals: List[List[int]] - :rtype: bool - """ - if not intervals or not intervals[0]: - return True - - intervals.sort(key = lambda x:x[0]) - end = intervals[0][1] - for i in range(1, len(intervals)): - s, e = intervals[i][0], intervals[i][1] - - if s < end: - return False - end = e - return True \ No newline at end of file diff --git "a/2529.\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260/2529-\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260.py" "b/2529.\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260/2529-\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260.py" new file mode 100644 index 0000000..e7d86a4 --- /dev/null +++ "b/2529.\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260/2529-\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260.py" @@ -0,0 +1,32 @@ +class Solution: + def maximumCount(self, nums: List[int]) -> int: + # 1. find the right-most negative number + negative_index = -1 + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] < 0: + negative_index = mid + left = mid + 1 + elif nums[mid] >= 0: + right = mid - 1 + + # print(negative_index) + # 2. find the left-most positive number + positive_index = -1 + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] > 0: + positive_index = mid + right = mid - 1 + else: + left = mid + 1 + + if negative_index > -1 and positive_index > -1: + return max(negative_index + 1, len(nums) - positive_index) + elif negative_index > -1: + return negative_index + 1 + elif positive_index > -1: + return len(nums) - positive_index + return 0 \ No newline at end of file diff --git "a/253.\344\274\232\350\256\256\345\256\244II/253-\344\274\232\350\256\256\345\256\244II.py" "b/253.\344\274\232\350\256\256\345\256\244II/253-\344\274\232\350\256\256\345\256\244II.py" deleted file mode 100644 index 13c7f4e..0000000 --- "a/253.\344\274\232\350\256\256\345\256\244II/253-\344\274\232\350\256\256\345\256\244II.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def minMeetingRooms(self, intervals): - """ - :type intervals: List[List[int]] - :rtype: int - """ - if not intervals or not intervals[0]: - return 0 - intervals.sort() - from heapq import * - queue = [] - heappush(queue, intervals[0][1]) - for i in range(1, len(intervals)): - start, end = intervals[i][0], intervals[i][1] - - if start >= queue[0]: - heappop(queue) - heappush(queue, end) - - return len(queue) \ No newline at end of file diff --git "a/2545.\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217/2545-\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217.py" "b/2545.\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217/2545-\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217.py" new file mode 100644 index 0000000..70cbf0e --- /dev/null +++ "b/2545.\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217/2545-\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217.py" @@ -0,0 +1,3 @@ +class Solution: + def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]: + return sorted(score, key = lambda x:-x[k]) \ No newline at end of file diff --git "a/2553.\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215/2553-\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215.py" "b/2553.\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215/2553-\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215.py" new file mode 100644 index 0000000..54d4646 --- /dev/null +++ "b/2553.\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215/2553-\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215.py" @@ -0,0 +1,8 @@ +class Solution: + def separateDigits(self, nums: List[int]) -> List[int]: + res = [] + + for num in nums: + for digit in str(num): + res.append(int(digit)) + return res \ No newline at end of file diff --git "a/2558.\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251/2558-\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251.py" "b/2558.\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251/2558-\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251.py" new file mode 100644 index 0000000..ddfc2cf --- /dev/null +++ "b/2558.\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251/2558-\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251.py" @@ -0,0 +1,12 @@ +from heapq import * +class Solution: + def pickGifts(self, gifts: List[int], k: int) -> int: + max_heap = [] + for gift in gifts: + heappush(max_heap, -gift) + + while k: + gift = -heappop(max_heap) + heappush(max_heap, -int(gift ** 0.5)) + k -= 1 + return -sum(max_heap) diff --git "a/2562.\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274/2562-\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274.py" "b/2562.\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274/2562-\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274.py" new file mode 100644 index 0000000..188fa3b --- /dev/null +++ "b/2562.\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274/2562-\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274.py" @@ -0,0 +1,11 @@ +class Solution: + def findTheArrayConcVal(self, nums: List[int]) -> int: + res = 0 + while nums: + if len(nums) > 1: + serial = int(str(nums[0]) + str(nums[-1])) + else: + serial = nums[0] + res += serial + nums = nums[1:-1] + return res \ No newline at end of file diff --git "a/2574.\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274/2574-\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274.py" "b/2574.\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274/2574-\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274.py" new file mode 100644 index 0000000..844959c --- /dev/null +++ "b/2574.\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274/2574-\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274.py" @@ -0,0 +1,12 @@ +class Solution: + def leftRightDifference(self, nums: List[int]) -> List[int]: + left_sum = [0] + right_sum = [0] + + for num in nums[:-1]: + left_sum.append(num + left_sum[-1]) + + for num in nums[::-1][:-1]: + right_sum.append(num + right_sum[-1]) + + return [abs(l - r) for l, r in zip(left_sum, right_sum[::-1])] \ No newline at end of file diff --git "a/2586.\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/2586-\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260.py" "b/2586.\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/2586-\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260.py" new file mode 100644 index 0000000..ee2f5d3 --- /dev/null +++ "b/2586.\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/2586-\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260.py" @@ -0,0 +1,17 @@ +class Solution: + def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]: + vowelStringCount = [0 for _ in words] + VOWELS = set("aeiou") + for i in range(len(words)): + if words[i][0] in VOWELS and words[i][-1] in VOWELS: + vowelStringCount[i] = vowelStringCount[i - 1] + 1 + else: + vowelStringCount[i] = vowelStringCount[i - 1] + + res = [] + for l, r in queries: + if l: + res.append(vowelStringCount[r] - vowelStringCount[l - 1]) + else: + res.append(vowelStringCount[r]) + return res diff --git "a/2610.\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204/2610-\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204.py" "b/2610.\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204/2610-\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204.py" new file mode 100644 index 0000000..203e13d --- /dev/null +++ "b/2610.\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204/2610-\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204.py" @@ -0,0 +1,12 @@ +class Solution: + def findMatrix(self, nums: List[int]) -> List[List[int]]: + from collections import Counter + c = Counter(nums) + res = [] + for num, freq in c.items(): + while len(res) < freq: + res.append([]) + + for i in range(freq): + res[i].append(num) + return res diff --git "a/2656.K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214/2656-K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214.py" "b/2656.K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214/2656-K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214.py" new file mode 100644 index 0000000..46fe631 --- /dev/null +++ "b/2656.K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214/2656-K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214.py" @@ -0,0 +1,3 @@ +class Solution: + def maximizeSum(self, nums: List[int], k: int) -> int: + return max(nums) * k + k * (k - 1) // 2 \ No newline at end of file diff --git "a/2670.\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204/2670-\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204.py" "b/2670.\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204/2670-\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204.py" new file mode 100644 index 0000000..d3bda30 --- /dev/null +++ "b/2670.\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204/2670-\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204.py" @@ -0,0 +1,10 @@ +class Solution: + def distinctDifferenceArray(self, nums: List[int]) -> List[int]: + res = [] + + for i, num in enumerate(nums): + pre_count = len(set(nums[:i + 1])) + post_count = len(set(nums[i + 1:])) + + res.append(pre_count - post_count) + return res \ No newline at end of file diff --git "a/2671.\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250/2671-\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250.py" "b/2671.\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250/2671-\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250.py" new file mode 100644 index 0000000..cd5339d --- /dev/null +++ "b/2671.\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250/2671-\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250.py" @@ -0,0 +1,34 @@ +from collections import defaultdict +class FrequencyTracker: + + def __init__(self): + self.freq2num = defaultdict(set) + self.num2freq = defaultdict(int) + + def add(self, number: int) -> None: + # print(self.num2freq, self.freq2num) + freq = self.num2freq[number] + self.num2freq[number] += 1 + if number in self.freq2num[freq]: + self.freq2num[freq].remove(number) + self.freq2num[freq + 1].add(number) + + def deleteOne(self, number: int) -> None: + # print(self.num2freq, self.freq2num) + if number not in self.num2freq or self.num2freq[number] == 0: + return + freq = self.num2freq[number] + self.num2freq[number] -= 1 + if number in self.freq2num[freq]: + self.freq2num[freq].remove(number) + self.freq2num[freq - 1].add(number) + + def hasFrequency(self, frequency: int) -> bool: + return len(self.freq2num[frequency]) > 0 + + +# Your FrequencyTracker object will be instantiated and called as such: +# obj = FrequencyTracker() +# obj.add(number) +# obj.deleteOne(number) +# param_3 = obj.hasFrequency(frequency) \ No newline at end of file diff --git "a/2674.\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250/2674-\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250.py" "b/2674.\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250/2674-\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250.py" new file mode 100644 index 0000000..aa8f15e --- /dev/null +++ "b/2674.\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250/2674-\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250.py" @@ -0,0 +1,29 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def splitCircularLinkedList(self, head: Optional[ListNode]) -> List[Optional[ListNode]]: + + l, p = 0, head + last = None + flag = False + while not flag or p != head: + flag = True + l += 1 + if p.next == head: + last = p + p = p.next + # print(l, last) + mid = math.ceil(l / 2) + + cnt, p = 1, head + while cnt < mid: + p = p.next + cnt += 1 + + next_head = p.next + p.next = head + last.next = next_head + return [head, next_head] diff --git "a/2678.\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256/2678-\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256.py" "b/2678.\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256/2678-\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..6173751 --- /dev/null +++ "b/2678.\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256/2678-\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256.py" @@ -0,0 +1,8 @@ +class Solution: + def countSeniors(self, details: List[str]) -> int: + # res = 0 + # for detail in details: + # if int(detail[-4:-2]) > 60: + # res += 1 + # return res + return sum([int(detail[-4:-2]) > 60 for detail in details]) \ No newline at end of file diff --git "a/2679.\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214/2679-\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214.py" "b/2679.\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214/2679-\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214.py" new file mode 100644 index 0000000..96145c7 --- /dev/null +++ "b/2679.\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214/2679-\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214.py" @@ -0,0 +1,23 @@ +from heapq import * +class Solution: + def matrixSum(self, nums: List[List[int]]) -> int: + # if not nums or not nums[0]: + # return 0 + # m, n = len(nums), len(nums[0]) + # all_max_heap = [] + # for num in nums: + # max_heap = [-n for n in num] + # heapify(max_heap) + # all_max_heap.append(max_heap) + + # res = 0 + # for _ in range(n): + # score = 0 + # for i in range(m): + # score = max(score, -heappop(all_max_heap[i])) + # res += score + # return res + for num in nums: + num.sort() + + return sum(max(col) for col in zip(*nums)) \ No newline at end of file diff --git "a/2682.\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266/2682-\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266.py" "b/2682.\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266/2682-\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266.py" new file mode 100644 index 0000000..7947da9 --- /dev/null +++ "b/2682.\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266/2682-\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266.py" @@ -0,0 +1,12 @@ +class Solution: + def circularGameLosers(self, n: int, k: int) -> List[int]: + visited = set() + cur, cnt = 1, 1 + while cur not in visited: + visited.add(cur) + cur = cur + k * cnt + while cur > n: + cur = cur - n + cnt += 1 + + return [i for i in range(1, n + 1) if i not in visited] \ No newline at end of file diff --git "a/2683.\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226/2683-\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226.py" "b/2683.\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226/2683-\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226.py" new file mode 100644 index 0000000..d7e390a --- /dev/null +++ "b/2683.\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226/2683-\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226.py" @@ -0,0 +1,9 @@ +class Solution: + def doesValidArrayExist(self, derived: List[int]) -> bool: + cur = 0 + for i, n in enumerate(derived): + if i != len(derived) - 1: + if n == 1: + cur = 1 - cur + else: + return 0 ^ cur == n \ No newline at end of file diff --git "a/2684.\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260/2684-\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260.py" "b/2684.\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260/2684-\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260.py" new file mode 100644 index 0000000..8fc6a8a --- /dev/null +++ "b/2684.\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260/2684-\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260.py" @@ -0,0 +1,43 @@ +class Solution: + def maxMoves(self, grid: List[List[int]]) -> int: + # if not grid or not grid[0]: + # return 0 + # m, n = len(grid), len(grid[0]) + # dp = [[0 for _ in range(n)] for k in range(m)] + # dij = [[-1, 1], [0, 1], [1, 1]] + + # for col in range(n): + # for row in range(m): + # if not col or dp[row][col] != 0: + # for d in dij: + # i, j = row + d[0], col + d[1] + # if self.nodeInMatrix(i, j, m, n) and grid[i][j] > grid[row][col]: + # dp[i][j] = max(dp[i][j], dp[row][col] + 1) + + # return max([max(row) for row in dp]) + from collections import deque + queue = deque([]) + if not grid or not grid[0]: + return 0 + m, n = len(grid), len(grid[0]) + dxy = [[-1, 1], [0, 1], [1, 1]] + res = 0 + visited = set() + for i in range(m): + queue.append((0, i, 0)) # step, x, y + + while queue: + step, x, y = queue.popleft() + res = max(res, step) + + for dx, dy in dxy: + xx, yy = x + dx, y + dy + + if self.nodeInMatrix(xx, yy, m, n) and grid[xx][yy] > grid[x][y] and (xx, yy) not in visited: + visited.add((xx, yy)) + queue.append((step + 1, xx, yy)) + return res + + def nodeInMatrix(self, row, col, m, n): + return 0 <= row < m and 0 <= col < n + \ No newline at end of file diff --git "a/2685.\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217/2685-\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217.py" "b/2685.\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217/2685-\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217.py" new file mode 100644 index 0000000..69d881a --- /dev/null +++ "b/2685.\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217/2685-\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217.py" @@ -0,0 +1,27 @@ +class Solution: + def countCompleteComponents(self, n: int, edges: List[List[int]]) -> int: + from collections import defaultdict + src2des = defaultdict(set) + + res = 0 + visited = set() + for edge in edges: + src, des = edge[0], edge[1] + src2des[src].add(des) + src2des[src].add(src) + src2des[des].add(src) + src2des[des].add(des) + + for node in range(n): + if node not in visited: + connected = True + visited.add(node) + for connected_node in src2des[node]: + visited.add(connected_node) + if src2des[connected_node] != src2des[node]: + connected = False + break + + if connected: + res += 1 + return res \ No newline at end of file diff --git "a/2696.\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246/2696-\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246.py" "b/2696.\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246/2696-\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246.py" new file mode 100644 index 0000000..793fbb8 --- /dev/null +++ "b/2696.\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246/2696-\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246.py" @@ -0,0 +1,12 @@ +class Solution: + def minLength(self, s: str) -> int: + stack = [] + + for char in s: + if char == "D" and stack and stack[-1] == "C": + stack.pop() + elif char == "B" and stack and stack[-1] == "A": + stack.pop() + else: + stack.append(char) + return len(stack) \ No newline at end of file diff --git "a/2697.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262/2697-\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262.py" "b/2697.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262/2697-\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262.py" new file mode 100644 index 0000000..aae6620 --- /dev/null +++ "b/2697.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262/2697-\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262.py" @@ -0,0 +1,16 @@ +class Solution: + def makeSmallestPalindrome(self, s: str) -> str: + left, right = 0, len(s) - 1 + res_left = "" + res_right = "" + while left < right: + res_left += min(s[left], s[right]) + res_right = min(s[left], s[right]) + res_right + left += 1 + right -= 1 + + if left == right: + res = res_left + s[left] + res_right + else: + res = res_left + res_right + return res \ No newline at end of file diff --git "a/2698.\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260/2698-\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260.py" "b/2698.\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260/2698-\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260.py" new file mode 100644 index 0000000..b152802 --- /dev/null +++ "b/2698.\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260/2698-\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260.py" @@ -0,0 +1,27 @@ +class Solution: + def punishmentNumber(self, n: int) -> int: + res = 0 + for i in range(1, n + 1): + i2 = i * i + self.possible(i) + if self.res: + res += i2 + return res + + def possible(self, num): + # 判断 1296 是否可以分成 1 + 29 + 6 == 36 + self.res = False + def helper(n, s): + # 首次递归 n = 36, s = 1296 + if not s or n > int(s): + return + if int(s) == n: + self.res = True + return + for i in range(1, len(str(n)) + 1): + # 下次递归 n = 30, s = 129 + helper(n - int(s[-i:]), s[:-i]) + helper(num, str(num * num)) + + + \ No newline at end of file diff --git "a/2706.\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233/2706-\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233.py" "b/2706.\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233/2706-\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233.py" new file mode 100644 index 0000000..b368281 --- /dev/null +++ "b/2706.\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233/2706-\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233.py" @@ -0,0 +1,5 @@ +class Solution: + def buyChoco(self, prices: List[int], money: int) -> int: + prices.sort() + s = prices[0] + prices[1] + return money if s > money else money - s \ No newline at end of file diff --git "a/2707.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246/2707-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246.py" "b/2707.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246/2707-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246.py" new file mode 100644 index 0000000..eb2f6c5 --- /dev/null +++ "b/2707.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246/2707-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246.py" @@ -0,0 +1,15 @@ +class Solution: + def minExtraChar(self, s: str, dictionary: List[str]) -> int: + # dp[i] represents res for s[:i] + dp = [i for i in range(len(s) + 1)] + + for i in range(len(s) + 1): + dp[i] = min(dp[i], dp[i - 1] + 1) + for d in dictionary: + if i >= len(d) and s[i - len(d):i] == d: + dp[i] = min(dp[i], dp[i - len(d)]) + + return dp[-1] + + + \ No newline at end of file diff --git "a/2708.\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274/2708-\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274.py" "b/2708.\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274/2708-\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274.py" new file mode 100644 index 0000000..d91c8b4 --- /dev/null +++ "b/2708.\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274/2708-\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274.py" @@ -0,0 +1,21 @@ +class Solution: + def maxStrength(self, nums: List[int]) -> int: + if all([num == 0 for num in nums]): + return 0 + if len(nums) == 1: + return nums[0] + pos = [num for num in nums if num > 0] + neg = [num for num in nums if num < 0] + + res = 0 + neg_length = len(neg) + if neg_length >= 2: + if neg_length % 2 == 0: + res = reduce((lambda x, y: x * y), neg) + else: + neg.sort() + res = reduce((lambda x, y: x * y), neg[:-1]) + + if pos: + res = reduce((lambda x, y: x * y), pos) * max(res, 1) + return res diff --git "a/2710.\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266/2710-\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266.py" "b/2710.\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266/2710-\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266.py" new file mode 100644 index 0000000..2d286a5 --- /dev/null +++ "b/2710.\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266/2710-\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266.py" @@ -0,0 +1,5 @@ +class Solution: + def removeTrailingZeros(self, num: str) -> str: + for i in range(len(num) - 1, -1, -1): + if num[i] != "0": + return num[:i + 1] \ No newline at end of file diff --git "a/2712.\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254/2712-\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254.py" "b/2712.\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254/2712-\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254.py" new file mode 100644 index 0000000..c6ad528 --- /dev/null +++ "b/2712.\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254/2712-\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254.py" @@ -0,0 +1,12 @@ +class Solution: + def minimumCost(self, s: str) -> int: + # 110101 + # 111010 min(i, n - i) + prev = s[0] + res = 0 + for i, char in enumerate(s): + if i and char != prev: + # flip is required: + res += min(i, len(s) - i) + prev = char + return res \ No newline at end of file diff --git "a/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/287-\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260.py" "b/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/287-\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260.py" deleted file mode 100644 index 0b5774b..0000000 --- "a/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/287-\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def findDuplicate(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - - slow, fast = 0, 0 - while 1: - fast = nums[nums[fast]] - slow = nums[slow] - if fast == slow: - fast = 0 - while nums[fast] != nums[slow]: - fast = nums[fast] - slow = nums[slow] - return nums[slow] diff --git "a/289.\347\224\237\345\221\275\346\270\270\346\210\217/289-\347\224\237\345\221\275\346\270\270\346\210\217.py" "b/289.\347\224\237\345\221\275\346\270\270\346\210\217/289-\347\224\237\345\221\275\346\270\270\346\210\217.py" deleted file mode 100644 index 23ec6db..0000000 --- "a/289.\347\224\237\345\221\275\346\270\270\346\210\217/289-\347\224\237\345\221\275\346\270\270\346\210\217.py" +++ /dev/null @@ -1,50 +0,0 @@ -class Solution(object): - def gameOfLife(self, board): - """ - :type board: List[List[int]] - :rtype: void Do not return anything, modify board in-place instead. - """ - m = len(board) - if m == 0: - return board - n = len(board[0]) - if n == 0: - return board - - alivex = list() - alivey = list() - - def neibor(x, y): #统计八个邻居里有几个是活细胞(1) - dx = [1, -1, 0, 0, 1, -1, -1, 1] - dy = [0, 0, 1, -1, 1, -1, 1, -1] - - cnt = 0 - for k in range(8): - xx = x + dx[k] - yy = y + dy[k] - - if 0 <= xx < m and 0 <= yy < n and board[xx][yy] == 1: - cnt += 1 - - return cnt - - - for i in range(m): - for j in range(n): - cnt = neibor(i, j) - # print i, j, cnt - if (board[i][j] == 1 and 2 <= cnt <= 3) or (board[i][j] == 0 and cnt == 3): - alivex.append(i) - alivey.append(j) - - alivecnt = 0 - for i in range(m): - for j in range(n): - board[i][j] = 0 - - if alivecnt < len(alivex): - if alivex[alivecnt] == i and alivey[alivecnt] == j: - board[i][j] = 1 - alivecnt += 1 - - return board \ No newline at end of file diff --git "a/296.\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271/296-\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271.py" "b/296.\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271/296-\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271.py" deleted file mode 100644 index e602a1f..0000000 --- "a/296.\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271/296-\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271.py" +++ /dev/null @@ -1,29 +0,0 @@ -class Solution(object): - def minTotalDistance(self, grid): - """ - :type grid: List[List[int]] - :rtype: int - """ - if not grid or not grid[0]: - return -1 - - m, n = len(grid), len(grid[0]) - row, col = [], [] - for i in range(m): - for j in range(n): - if grid[i][j]: - row.append(i) - col.append(j) - meet_point = [self.findMedian(row), self.findMedian(col)] - - res = 0 - for i in range(m): - for j in range(n): - if grid[i][j]: - res += abs(i - meet_point[0]) + abs(j - meet_point[1]) - return res - - - def findMedian(self, nums): - nums.sort() - return nums[len(nums) // 2] diff --git "a/299.\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217/299-\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217.py" "b/299.\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217/299-\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217.py" deleted file mode 100644 index 13447ba..0000000 --- "a/299.\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217/299-\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def getHint(self, secret, guess): - """ - :type secret: str - :type guess: str - :rtype: str - """ - from collections import Counter - dic_s = Counter(secret) - dic_g = Counter(guess) - - a, b = 0, 0 - for i in range(len(secret)): - if secret[i] == guess[i]: - a += 1 - dic_s[secret[i]] -= 1 - dic_g[secret[i]] -= 1 - - for i in dic_s & dic_g: - b += min(dic_s[i], dic_g[i]) - - return "{}A{}B".format(a, b) \ No newline at end of file diff --git "a/300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227/300-\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.py" "b/300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227/300-\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.py" deleted file mode 100644 index f5fcf2e..0000000 --- "a/300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227/300-\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.py" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution(object): - def lengthOfLIS(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - dp = [1 for _ in nums] - - for i in range(len(nums)): - for j in range(i): - if nums[i] > nums[j]: - dp[i] = max(dp[i], dp[j] + 1) - - return max(dp) if dp else 0 \ No newline at end of file diff --git "a/315.\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260/315-\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260.py" "b/315.\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260/315-\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260.py" deleted file mode 100644 index 4abc475..0000000 --- "a/315.\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260/315-\350\256\241\347\256\227\345\217\263\344\276\247\345\260\217\344\272\216\345\275\223\345\211\215\345\205\203\347\264\240\347\232\204\344\270\252\346\225\260.py" +++ /dev/null @@ -1,35 +0,0 @@ -class TreeNode(object): - def __init__(self, val): - self.left = None - self.right = None - self.val = val - self.left_subtree_cnt = 0 - -class Solution(object): - def countSmaller(self, nums): - """ - :type nums: List[int] - :rtype: List[int] - """ - - # 从左往右处理,右边是未知的,所以不好弄 - # 从右往左处理,则对于每个数,其右边的数都已知 - res = [0 for _ in nums] - root = None - for i, num in enumerate(nums[::-1]): - root = self.insert(root, num, i, res) - return res[::-1] - - def insert(self, root, val, i, res): - if not root: - root = TreeNode(val) - elif root.val >= val: - root.left_subtree_cnt += 1 - root.left = self.insert(root.left, val, i, res) - elif root.val < val: - res[i] += root.left_subtree_cnt + 1 - root.right = self.insert(root.right, val, i, res) - - return root - - \ No newline at end of file diff --git "a/322.\351\233\266\351\222\261\345\205\221\346\215\242/322-\351\233\266\351\222\261\345\205\221\346\215\242.py" "b/322.\351\233\266\351\222\261\345\205\221\346\215\242/322-\351\233\266\351\222\261\345\205\221\346\215\242.py" deleted file mode 100644 index 13e35ba..0000000 --- "a/322.\351\233\266\351\222\261\345\205\221\346\215\242/322-\351\233\266\351\222\261\345\205\221\346\215\242.py" +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def coinChange(self, coins, amount): - """ - :type coins: List[int] - :type amount: int - :rtype: int - """ - from collections import deque - - queue = deque([(0, 0)]) - visited = set([0]) - while queue: - cur, step = queue.popleft() - if cur == amount: - return step - if cur > amount: - continue - - for coin in coins: - value = cur + coin - if value not in visited: - visited.add((value)) - queue.append((value, step + 1)) - - return -1 \ No newline at end of file diff --git "a/326.3\347\232\204\345\271\202/326-3\347\232\204\345\271\202.py" "b/326.3\347\232\204\345\271\202/326-3\347\232\204\345\271\202.py" deleted file mode 100644 index 513bb5a..0000000 --- "a/326.3\347\232\204\345\271\202/326-3\347\232\204\345\271\202.py" +++ /dev/null @@ -1,10 +0,0 @@ -class Solution(object): - def isPowerOfThree(self, n): - """ - :type n: int - :rtype: bool - """ - t = 1 - while t < n: - t *= 3 - return n == t \ No newline at end of file diff --git "a/347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/347-\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.py" "b/347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/347-\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.py" deleted file mode 100644 index 8dc6838..0000000 --- "a/347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/347-\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def topKFrequent(self, nums, k): - """ - :type nums: List[int] - :type k: int - :rtype: List[int] - """ - from collections import Counter - from heapq import * - dic = Counter(nums) - queue = [] - for digit, fre in dic.items(): - if len(queue) < k: - heappush(queue, (fre, digit)) - else: - heappushpop(queue, (fre, digit)) - # print queue - res = [] - while queue: - res.append(heappop(queue)[1]) - return res \ No newline at end of file diff --git "a/355.\350\256\276\350\256\241\346\216\250\347\211\271/355-\350\256\276\350\256\241\346\216\250\347\211\271.py" "b/355.\350\256\276\350\256\241\346\216\250\347\211\271/355-\350\256\276\350\256\241\346\216\250\347\211\271.py" deleted file mode 100644 index 55d2c19..0000000 --- "a/355.\350\256\276\350\256\241\346\216\250\347\211\271/355-\350\256\276\350\256\241\346\216\250\347\211\271.py" +++ /dev/null @@ -1,51 +0,0 @@ -class Twitter: - - def __init__(self): - """ - Initialize your data structure here. - """ - self.stack = [] #发推记录 - self.f = {} #记录每个人关注了谁 - - - def postTweet(self, userId: int, tweetId: int) -> None: - """ - Compose a new tweet. - """ - self.stack.append((userId, tweetId)) - - - def getNewsFeed(self, userId: int) -> List[int]: - """ - Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. - """ - out = [] - following = [userId] - if userId in self.f: - following += self.f[userId] - for i in range(len(self.stack)-1, -1, -1): - if self.stack[i][0] in following: - out.append(self.stack[i][1]) - if len(out) == 10: - return out - return out - - - def follow(self, followerId: int, followeeId: int) -> None: - """ - Follower follows a followee. If the operation is invalid, it should be a no-op. - """ - if followerId not in self.f: - self.f[followerId] = [followeeId] - else: - if followeeId not in self.f[followerId]: - self.f[followerId].append(followeeId) - - - def unfollow(self, followerId: int, followeeId: int) -> None: - """ - Follower unfollows a followee. If the operation is invalid, it should be a no-op. - """ - if followerId in self.f: - if followeeId in self.f[followerId]: - self.f[followerId].remove(followeeId) \ No newline at end of file diff --git "a/365.\346\260\264\345\243\266\351\227\256\351\242\230/365-\346\260\264\345\243\266\351\227\256\351\242\230.py" "b/365.\346\260\264\345\243\266\351\227\256\351\242\230/365-\346\260\264\345\243\266\351\227\256\351\242\230.py" deleted file mode 100644 index 29817d8..0000000 --- "a/365.\346\260\264\345\243\266\351\227\256\351\242\230/365-\346\260\264\345\243\266\351\227\256\351\242\230.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def canMeasureWater(self, x, y, z): - """ - :type x: int - :type y: int - :type z: int - :rtype: bool - """ - if not z: - return True - if not x: - return y == z - if not y: - return x == z - if x + y < z: - return False - def gcd(a, b): - while a % b: - a, b = b, a % b - return b - return not z % gcd(x, y) \ No newline at end of file diff --git "a/366.\345\257\273\346\211\276\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/366-\345\257\273\346\211\276\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271.py" "b/366.\345\257\273\346\211\276\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/366-\345\257\273\346\211\276\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271.py" deleted file mode 100644 index 38ea4d5..0000000 --- "a/366.\345\257\273\346\211\276\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/366-\345\257\273\346\211\276\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271.py" +++ /dev/null @@ -1,35 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def findLeaves(self, root): - """ - :type root: TreeNode - :rtype: List[List[int]] - """ - from collections import defaultdict - self.dic = defaultdict(list) - res = [] - def get_Height(node): - if not node: - return -1 - lh = get_Height(node.left) - rh = get_Height(node.right) - h = max(lh, rh) + 1 - self.dic[h].append(node.val) - return h - - get_Height(root) - # print self.dic - h = 0 - while 1: - if h not in self.dic: - break - res.append(self.dic[h]) - h += 1 - return res - \ No newline at end of file diff --git "a/378.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/378-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.py" "b/378.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/378-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.py" deleted file mode 100644 index d07d9f3..0000000 --- "a/378.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/378-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.py" +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def kthSmallest(self, matrix, k): - """ - :type matrix: List[List[int]] - :type k: int - :rtype: int - """ - if not matrix or not matrix[0]: - return matrix - - from heapq import * - queue = [] - for i in range(len(matrix)): - heappush(queue, (matrix[i][0], i, 0)) - - cnt = 0 - while cnt < k: - cnt += 1 - - val, row, col = heappop(queue) - if col + 1 < len(matrix): - heappush(queue, (matrix[row][col + 1], row, col + 1)) - - return val - \ No newline at end of file diff --git "a/386.\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260/386-\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260.py" "b/386.\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260/386-\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260.py" deleted file mode 100644 index 140f172..0000000 --- "a/386.\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260/386-\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def lexicalOrder(self, n): - """ - :type n: int - :rtype: List[int] - """ - return sorted(range(1, n + 1), key = str) -# res = [] - -# def dfs(k): -# if k > n: -# return -# res.append(k) - -# for i in range(10): -# dfs(10 * k + i) - -# for i in range(1, 10): -# dfs(i) -# return res \ No newline at end of file diff --git "a/387.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246/387-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.py" "b/387.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246/387-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.py" deleted file mode 100644 index a640d0d..0000000 --- "a/387.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246/387-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\224\257\344\270\200\345\255\227\347\254\246.py" +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def firstUniqChar(self, s): - """ - :type s: str - :rtype: int - """ - dic = collections.Counter(s) - - for i, ch in enumerate(s): - if dic[ch] == 1: - return i - return -1 \ No newline at end of file diff --git "a/398.\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225/398-\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225.py" "b/398.\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225/398-\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225.py" deleted file mode 100644 index f69bb27..0000000 --- "a/398.\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225/398-\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - - def __init__(self, nums): - """ - :type nums: List[int] - """ - from collections import defaultdict - self.dic = defaultdict(list) - for i, num in enumerate(nums): - self.dic[num].append(i) - - def pick(self, target): - """ - :type target: int - :rtype: int - """ - return random.choice(self.dic[target]) - - -# Your Solution object will be instantiated and called as such: -# obj = Solution(nums) -# param_1 = obj.pick(target) \ No newline at end of file diff --git "a/409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/409-\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262.py" "b/409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/409-\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262.py" deleted file mode 100644 index 1acf581..0000000 --- "a/409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/409-\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def longestPalindrome(self, s): - """ - :type s: str - :rtype: int - """ - return len(s) -max(0,sum([s.count(i)%2 for i in set(s)])-1) \ No newline at end of file diff --git "a/42.\346\216\245\351\233\250\346\260\264/42-\346\216\245\351\233\250\346\260\264.py" "b/42.\346\216\245\351\233\250\346\260\264/42-\346\216\245\351\233\250\346\260\264.py" deleted file mode 100644 index a5926c1..0000000 --- "a/42.\346\216\245\351\233\250\346\260\264/42-\346\216\245\351\233\250\346\260\264.py" +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def trap(self, height): - """ - :type height: List[int] - :rtype: int - """ - left_max = [0 for _ in height] - right_max = [0 for _ in height] - water = [0 for _ in height] - - for i in range(len(height)): - if i - 1 >= 0: - left_max[i] = max(left_max[i - 1], height[i]) - else: - left_max[i] = height[i] - - for i in range(len(height) - 1, -1, -1): - if i < len(height) - 1: - right_max[i] = max(right_max[i + 1], height[i]) - else: - right_max[i] = height[i] - - for i in range(len(height)): - tmp = min(left_max[i], right_max[i]) - height[i] - if tmp > 0: - water[i] = tmp - # print height - # print water - # print left_max - # print right_max - return sum(water) \ No newline at end of file diff --git "a/426.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\345\214\226\344\270\272\346\216\222\345\272\217\347\232\204\345\217\214\345\220\221\351\223\276\350\241\250/426-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\345\214\226\344\270\272\346\216\222\345\272\217\347\232\204\345\217\214\345\220\221\351\223\276\350\241\250.py" "b/426.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\345\214\226\344\270\272\346\216\222\345\272\217\347\232\204\345\217\214\345\220\221\351\223\276\350\241\250/426-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\345\214\226\344\270\272\346\216\222\345\272\217\347\232\204\345\217\214\345\220\221\351\223\276\350\241\250.py" deleted file mode 100644 index 669fb96..0000000 --- "a/426.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\345\214\226\344\270\272\346\216\222\345\272\217\347\232\204\345\217\214\345\220\221\351\223\276\350\241\250/426-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\345\214\226\344\270\272\346\216\222\345\272\217\347\232\204\345\217\214\345\220\221\351\223\276\350\241\250.py" +++ /dev/null @@ -1,47 +0,0 @@ -""" -# Definition for a Node. -class Node(object): - def __init__(self, val, left=None, right=None): - self.val = val - self.left = left - self.right = right -""" -class Solution(object): - def treeToDoublyList(self, root): - """ - :type root: Node - :rtype: Node - """ - if not root: - return root - if not root.left and not root.right: - root.left = root - root.right = root - return root - - left = self.treeToDoublyList(root.left) - right = self.treeToDoublyList(root.right) - if root.left and root.right: - left_tail = left.left - right_tail = right.left - - left_tail.right = root - root.left = left_tail - root.right = right - right.left = root - - left.left = right_tail - right_tail.right = left - elif root.left: - left_tail = left.left - left_tail.right = root - root.left = left_tail - left.left = root - root.right = left - elif root.right: - right_tail = right.left - root.right = right - root.left = right_tail - right_tail.right = root - right.left = root - return left if left else root \ No newline at end of file diff --git "a/430.\346\211\201\345\271\263\345\214\226\345\244\232\347\272\247\345\217\214\345\220\221\351\223\276\350\241\250/430-\346\211\201\345\271\263\345\214\226\345\244\232\347\272\247\345\217\214\345\220\221\351\223\276\350\241\250.py" "b/430.\346\211\201\345\271\263\345\214\226\345\244\232\347\272\247\345\217\214\345\220\221\351\223\276\350\241\250/430-\346\211\201\345\271\263\345\214\226\345\244\232\347\272\247\345\217\214\345\220\221\351\223\276\350\241\250.py" deleted file mode 100644 index 08d25c6..0000000 --- "a/430.\346\211\201\345\271\263\345\214\226\345\244\232\347\272\247\345\217\214\345\220\221\351\223\276\350\241\250/430-\346\211\201\345\271\263\345\214\226\345\244\232\347\272\247\345\217\214\345\220\221\351\223\276\350\241\250.py" +++ /dev/null @@ -1,38 +0,0 @@ -""" -# Definition for a Node. -class Node(object): - def __init__(self, val, prev, next, child): - self.val = val - self.prev = prev - self.next = next - self.child = child -""" -class Solution(object): - def flatten(self, head): - """ - :type head: Node - :rtype: Node - """ - if not head: - return head - - def helper(node): - # 返回的是最后一个节点 - if not node: - return - while node: - nxt = node.next # 备份 next - if not nxt: - tail = node # 记录 tail,用于返回 - if node.child: - node.next = node.child # 把child 变成next - node.next.prev = node - t = helper(node.child) # 递归处理,t 是处理之后的 原来的child 的最后一个节点 - node.child = None # 把child 置空 - if nxt: # 如果有next 部分,就让next的prev指向 原来的child 处理之后的最后一个节点 - nxt.prev = t - t.next = nxt # 让 t.next 指向原来的 next - node = node.next - return tail - helper(head) - return head \ No newline at end of file diff --git "a/436.\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264/436-\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264.py" "b/436.\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264/436-\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264.py" deleted file mode 100644 index cffe9ed..0000000 --- "a/436.\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264/436-\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264.py" +++ /dev/null @@ -1,23 +0,0 @@ -class Solution(object): - def findRightInterval(self, intervals): - """ - :type intervals: List[List[int]] - :rtype: List[int] - """ - dic = {} - for i, (start, end) in enumerate(intervals): - dic[start] = i - - res = [-1 for _ in range(len(intervals))] - - l = [interval[0] for interval in intervals] - l = sorted(l, key = lambda x:x) - - for i, (start, end) in enumerate(intervals): - idx = bisect.bisect_left(l, end) - if idx < len(l): - res[i] = dic[l[idx]] - - return res - - \ No newline at end of file diff --git "a/445.\344\270\244\346\225\260\347\233\270\345\212\240II/445-\344\270\244\346\225\260\347\233\270\345\212\240II.py" "b/445.\344\270\244\346\225\260\347\233\270\345\212\240II/445-\344\270\244\346\225\260\347\233\270\345\212\240II.py" deleted file mode 100644 index ae75d3c..0000000 --- "a/445.\344\270\244\346\225\260\347\233\270\345\212\240II/445-\344\270\244\346\225\260\347\233\270\345\212\240II.py" +++ /dev/null @@ -1,44 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def addTwoNumbers(self, l1, l2): - """ - :type l1: ListNode - :type l2: ListNode - :rtype: ListNode - """ - s1, s2 = [], [] - while l1: - s1.append(l1.val) - l1 = l1.next - - while l2: - s2.append(l2.val) - l2 = l2.next - - carry = 0 - cur = ListNode(-1) - while s1 or s2: - value = carry - if s1: - value += s1.pop() - if s2: - value += s2.pop() - - carry = value > 9 - value %= 10 - - cur.val = value - pre = ListNode(-1) - pre.next = cur - cur = pre - - if carry: #处理可能的进位 - pre.val = 1 - return pre - - return pre.next \ No newline at end of file diff --git "a/452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203/452-\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.py" "b/452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203/452-\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.py" deleted file mode 100644 index 64b8736..0000000 --- "a/452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203/452-\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def findMinArrowShots(self, points): - """ - :type points: List[List[int]] - :rtype: int - """ - if not points or not points[0]: - return 0 - - points.sort(key = lambda x:x[1]) - - arrow = points[0][1] - - res = 1 - for point in points: - if arrow < point[0]: - res += 1 - arrow = point[1] - - return res \ No newline at end of file diff --git "a/460.LFU\347\274\223\345\255\230/460-LFU\347\274\223\345\255\230.py" "b/460.LFU\347\274\223\345\255\230/460-LFU\347\274\223\345\255\230.py" deleted file mode 100644 index e8fc752..0000000 --- "a/460.LFU\347\274\223\345\255\230/460-LFU\347\274\223\345\255\230.py" +++ /dev/null @@ -1,60 +0,0 @@ -class LFUCache(object): - - def __init__(self, capacity): - """ - :type capacity: int - """ - self.use = {} # 使用的频率 - self.cache = {} # 值 - self.size = capacity - self.arr = [] # 确保频率一样时,删除最近没有使用 - - def get(self, key): - """ - :type key: int - :rtype: int - """ - if key in self.cache: - self.use[key] += 1 - self.arr.remove(key) - self.arr.append(key) - return self.cache[key] - else: - return -1 - - def put(self, key, value): - """ - :type key: int - :type value: int - :rtype: None - """ - if key in self.cache: - self.cache[key] = value - self.use[key] += 1 - self.arr.remove(key) - self.arr.append(key) - else: - if len(self.cache) < self.size: - self.cache[key] = value - self.use[key] = 1 - self.arr.append(key) - else: - if self.use: - v = min(self.use.values()) - lost = -1 - for x in self.arr: - if self.use[x] == v: - lost = x - break - self.cache.pop(lost) - self.use.pop(lost) - self.arr.remove(lost) - self.cache[key] = value - self.use[key] = 1 - self.arr.append(key) - - -# Your LFUCache object will be instantiated and called as such: -# obj = LFUCache(capacity) -# param_1 = obj.get(key) -# obj.put(key,value) \ No newline at end of file diff --git "a/466.\347\273\237\350\256\241\351\207\215\345\244\215\344\270\252\346\225\260/466-\347\273\237\350\256\241\351\207\215\345\244\215\344\270\252\346\225\260.py" "b/466.\347\273\237\350\256\241\351\207\215\345\244\215\344\270\252\346\225\260/466-\347\273\237\350\256\241\351\207\215\345\244\215\344\270\252\346\225\260.py" deleted file mode 100644 index 08cf39f..0000000 --- "a/466.\347\273\237\350\256\241\351\207\215\345\244\215\344\270\252\346\225\260/466-\347\273\237\350\256\241\351\207\215\345\244\215\344\270\252\346\225\260.py" +++ /dev/null @@ -1,57 +0,0 @@ -class Solution(object): - def getMaxRepetitions(self, s1, n1, s2, n2): - """ - :type s1: str - :type n1: int - :type s2: str - :type n2: int - :rtype: int - """ - if n1 == 0: - return 0 - s1cnt, index, s2cnt = 0, 0, 0 - # recall 是我们用来找循环节的变量,它是一个哈希映射 - # 我们如何找循环节?假设我们遍历了 s1cnt 个 s1,此时匹配到了第 s2cnt 个 s2 中的第 index 个字符 - # 如果我们之前遍历了 s1cnt' 个 s1 时,匹配到的是第 s2cnt' 个 s2 中同样的第 index 个字符,那么就有循环节了 - # 我们用 (s1cnt', s2cnt', index) 和 (s1cnt, s2cnt, index) 表示两次包含相同 index 的匹配结果 - # 那么哈希映射中的键就是 index,值就是 (s1cnt', s2cnt') 这个二元组 - # 循环节就是; - # - 前 s1cnt' 个 s1 包含了 s2cnt' 个 s2 - # - 以后的每 (s1cnt - s1cnt') 个 s1 包含了 (s2cnt - s2cnt') 个 s2 - # 那么还会剩下 (n1 - s1cnt') % (s1cnt - s1cnt') 个 s1, 我们对这些与 s2 进行暴力匹配 - # 注意 s2 要从第 index 个字符开始匹配 - recall = dict() - while True: - # 我们多遍历一个 s1,看看能不能找到循环节 - s1cnt += 1 - for ch in s1: - if ch == s2[index]: - index += 1 - if index == len(s2): - s2cnt, index = s2cnt + 1, 0 - # 还没有找到循环节,所有的 s1 就用完了 - if s1cnt == n1: - return s2cnt // n2 - # 出现了之前的 index,表示找到了循环节 - if index in recall: - s1cnt_prime, s2cnt_prime = recall[index] - # 前 s1cnt' 个 s1 包含了 s2cnt' 个 s2 - pre_loop = (s1cnt_prime, s2cnt_prime) - # 以后的每 (s1cnt - s1cnt') 个 s1 包含了 (s2cnt - s2cnt') 个 s2 - in_loop = (s1cnt - s1cnt_prime, s2cnt - s2cnt_prime) - break - else: - recall[index] = (s1cnt, s2cnt) - - # ans 存储的是 S1 包含的 s2 的数量,考虑的之前的 pre_loop 和 in_loop - ans = pre_loop[1] + (n1 - pre_loop[0]) // in_loop[0] * in_loop[1] - # S1 的末尾还剩下一些 s1,我们暴力进行匹配 - rest = (n1 - pre_loop[0]) % in_loop[0] - for i in range(rest): - for ch in s1: - if ch == s2[index]: - index += 1 - if index == len(s2): - ans, index = ans + 1, 0 - # S1 包含 ans 个 s2,那么就包含 ans / n2 个 S2 - return ans // n2 diff --git "a/476.\346\225\260\345\255\227\347\232\204\350\241\245\346\225\260/476-\346\225\260\345\255\227\347\232\204\350\241\245\346\225\260.py" "b/476.\346\225\260\345\255\227\347\232\204\350\241\245\346\225\260/476-\346\225\260\345\255\227\347\232\204\350\241\245\346\225\260.py" deleted file mode 100644 index 2091668..0000000 --- "a/476.\346\225\260\345\255\227\347\232\204\350\241\245\346\225\260/476-\346\225\260\345\255\227\347\232\204\350\241\245\346\225\260.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def findComplement(self, num): - """ - :type num: int - :rtype: int - """ - s = bin(num)[2:] - b = "" - for ch in s: - - if ch == "0": - b += "1" - else: - b += "0" - # print b - return int(b,2) \ No newline at end of file diff --git "a/477.\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214/477-\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214.py" "b/477.\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214/477-\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214.py" deleted file mode 100644 index 9ce810a..0000000 --- "a/477.\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214/477-\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def totalHammingDistance(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - if not nums: - return 0 - res = 0 - mask = 1 - for i in range(32): - cnt_one = 0 - for num in nums: - cnt_one += 1 if num & mask else 0 - - res += cnt_one * (len(nums) - cnt_one) - mask = mask << 1 - return res - - \ No newline at end of file diff --git "a/482.\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226/482-\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226.py" "b/482.\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226/482-\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226.py" deleted file mode 100644 index a58af44..0000000 --- "a/482.\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226/482-\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226.py" +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def licenseKeyFormatting(self, S, K): - """ - :type S: str - :type K: int - :rtype: str - """ - s = "".join(S.split("-")).upper() - length_of_first_part = len(s) % K - if not length_of_first_part: - length_of_first_part = K - - res = s[:length_of_first_part] - for i in range(length_of_first_part, len(s), K): - res += "-" - res += s[i:i+K] - return res - \ No newline at end of file diff --git "a/49.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/49-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.py" "b/49.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/49-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.py" deleted file mode 100644 index b11b5a3..0000000 --- "a/49.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/49-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def groupAnagrams(self, strs): - """ - :type strs: List[str] - :rtype: List[List[str]] - """ - from collections import defaultdict - dic = defaultdict(list) - for word in strs: - sortedword = "".join(sorted(word)) - dic[sortedword].append(word) - - return dic.values() \ No newline at end of file diff --git a/50.Pow(x,n)/50-Pow(x,n).py b/50.Pow(x,n)/50-Pow(x,n).py deleted file mode 100644 index 47442e6..0000000 --- a/50.Pow(x,n)/50-Pow(x,n).py +++ /dev/null @@ -1,8 +0,0 @@ -class Solution(object): - def myPow(self, x, n): - """ - :type x: float - :type n: int - :rtype: float - """ - return x ** n \ No newline at end of file diff --git "a/516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/516-\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.py" "b/516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/516-\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.py" deleted file mode 100644 index 05f6119..0000000 --- "a/516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/516-\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def longestPalindromeSubseq(self, s): - """ - :type s: str - :rtype: int - """ - n = len(s) - dp = [[0] * n for _ in range(n)] - - for i in range(n): - dp[i][i] = 1 - for j in range(i - 1, -1, -1): - if s[i] == s[j]: - dp[i][j] = dp[i - 1][j + 1] + 2 - else: - dp[i][j] = max(dp[i][j + 1], dp[i - 1][j]) - return dp[n - 1][0] \ No newline at end of file diff --git "a/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/53-\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.py" "b/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/53-\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.py" deleted file mode 100644 index 4358bfe..0000000 --- "a/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/53-\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.py" +++ /dev/null @@ -1,19 +0,0 @@ -class Solution(object): - def maxSubArray(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - if not nums: - return 0 - dp = [0 for _ in nums] - dp[0] = nums[0] - - for i, x in enumerate(nums): - if i: - if dp[i - 1] > 0: - dp[i] = max(dp[i - 1] + x, dp[i]) - else: - dp[i] = x - - return max(dp) \ No newline at end of file diff --git "a/542.01\347\237\251\351\230\265/542-01\347\237\251\351\230\265.py" "b/542.01\347\237\251\351\230\265/542-01\347\237\251\351\230\265.py" deleted file mode 100644 index 8eafb66..0000000 --- "a/542.01\347\237\251\351\230\265/542-01\347\237\251\351\230\265.py" +++ /dev/null @@ -1,38 +0,0 @@ -from collections import deque -class Solution(object): - def updateMatrix(self, matrix): - """ - :type matrix: List[List[int]] - :rtype: List[List[int]] - """ - if not matrix or not matrix[0]: - return matrix - m, n = len(matrix), len(matrix[0]) - res = matrix[:] - - q = deque() - - for i in range(m): - for j in range(n): - if matrix[i][j] == 0: - q.append([[i, j], 0]) - - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - visited = [[0 for _ in range(n + 1)] for _ in range(m + 1)] - - while q: - tmp, distance = q.popleft() - x0, y0 = tmp[0], tmp[1] - - if matrix[x0][y0] == 1: - res[x0][y0] = distance - - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - - if 0 <= x < m and 0 <= y < n and visited[x][y] != 1: - q.append([[x, y], distance + 1]) - visited[x][y] = 1 - return res \ No newline at end of file diff --git "a/543.\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204/543-\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204.py" "b/543.\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204/543-\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204.py" deleted file mode 100644 index 8814891..0000000 --- "a/543.\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204/543-\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204.py" +++ /dev/null @@ -1,20 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def diameterOfBinaryTree(self, root): - """ - :type root: TreeNode - :rtype: int - """ - if not root: - return 0 - def Height(node): - if not node: - return 0 - return 1 + max(Height(node.left), Height(node.right)) - return max(self.diameterOfBinaryTree(root.left), Height(root.left) + Height(root.right), self.diameterOfBinaryTree(root.right)) \ No newline at end of file diff --git "a/55.\350\267\263\350\267\203\346\270\270\346\210\217/55-\350\267\263\350\267\203\346\270\270\346\210\217.py" "b/55.\350\267\263\350\267\203\346\270\270\346\210\217/55-\350\267\263\350\267\203\346\270\270\346\210\217.py" deleted file mode 100644 index 8198fc2..0000000 --- "a/55.\350\267\263\350\267\203\346\270\270\346\210\217/55-\350\267\263\350\267\203\346\270\270\346\210\217.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def canJump(self, nums): - """ - :type nums: List[int] - :rtype: bool - """ - - max_jump = 0 - for index, num in enumerate(nums): - if index > max_jump: - return False - max_jump = max(max_jump, index + num) - return True \ No newline at end of file diff --git "a/551.\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I/551-\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I.py" "b/551.\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I/551-\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I.py" deleted file mode 100644 index 0283b38..0000000 --- "a/551.\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I/551-\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def checkRecord(self, s): - """ - :type s: str - :rtype: bool - """ - return s.count("A") <= 1 and "LLL" not in s \ No newline at end of file diff --git "a/56.\345\220\210\345\271\266\345\214\272\351\227\264/56-\345\220\210\345\271\266\345\214\272\351\227\264.py" "b/56.\345\220\210\345\271\266\345\214\272\351\227\264/56-\345\220\210\345\271\266\345\214\272\351\227\264.py" deleted file mode 100644 index c005fbe..0000000 --- "a/56.\345\220\210\345\271\266\345\214\272\351\227\264/56-\345\220\210\345\271\266\345\214\272\351\227\264.py" +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def merge(self, intervals): - """ - :type intervals: List[List[int]] - :rtype: List[List[int]] - """ - if not intervals or not intervals[0]: - return intervals - - intervals = sorted(intervals, key = lambda x:x[0]) - - res = [] - start, end = intervals[0][0], intervals[0][1] - for interval in intervals: - s, e = interval[0], interval[1] - - if s <= end: # overlap - end = max(end, e) - else: - res.append([start, end]) - start, end = s, e - - res.append([start, end]) - return res \ No newline at end of file diff --git "a/560.\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204/560-\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204.py" "b/560.\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204/560-\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204.py" deleted file mode 100644 index 0b63b1d..0000000 --- "a/560.\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204/560-\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def subarraySum(self, nums, k): - """ - :type nums: List[int] - :type k: int - :rtype: int - """ - prefix = [] - - for i, num in enumerate(nums): - if i: - prefix.append(num + prefix[-1]) - else: - prefix.append(num) - - res = 0 - dic = collections.defaultdict(int) - dic[0] = 1 - for s in prefix: - res += dic[s - k] - dic[s] += 1 - return res \ No newline at end of file diff --git "a/561.\346\225\260\347\273\204\346\213\206\345\210\206I/561-\346\225\260\347\273\204\346\213\206\345\210\206I.py" "b/561.\346\225\260\347\273\204\346\213\206\345\210\206I/561-\346\225\260\347\273\204\346\213\206\345\210\206I.py" deleted file mode 100644 index de11b13..0000000 --- "a/561.\346\225\260\347\273\204\346\213\206\345\210\206I/561-\346\225\260\347\273\204\346\213\206\345\210\206I.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def arrayPairSum(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - return sum(sorted(nums)[::2]) \ No newline at end of file diff --git "a/566.\351\207\215\345\241\221\347\237\251\351\230\265/566-\351\207\215\345\241\221\347\237\251\351\230\265.py" "b/566.\351\207\215\345\241\221\347\237\251\351\230\265/566-\351\207\215\345\241\221\347\237\251\351\230\265.py" deleted file mode 100644 index 0f2bddc..0000000 --- "a/566.\351\207\215\345\241\221\347\237\251\351\230\265/566-\351\207\215\345\241\221\347\237\251\351\230\265.py" +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def matrixReshape(self, nums, r, c): - """ - :type nums: List[List[int]] - :type r: int - :type c: int - :rtype: List[List[int]] - """ - m, n = len(nums), len(nums[0]) - if r * c != m * n: - return nums - nums = sum(nums, []) - - res = [[0 for _ in range(c)] for _ in range(r)] - - for i in range(r): - for j in range(c): - res[i][j] = nums[i * c + j] - - return res \ No newline at end of file diff --git "a/57.\346\217\222\345\205\245\345\214\272\351\227\264/57-\346\217\222\345\205\245\345\214\272\351\227\264.py" "b/57.\346\217\222\345\205\245\345\214\272\351\227\264/57-\346\217\222\345\205\245\345\214\272\351\227\264.py" deleted file mode 100644 index bc5a818..0000000 --- "a/57.\346\217\222\345\205\245\345\214\272\351\227\264/57-\346\217\222\345\205\245\345\214\272\351\227\264.py" +++ /dev/null @@ -1,35 +0,0 @@ -class Solution(object): - def insert(self, intervals, newInterval): - """ - :type intervals: List[List[int]] - :type newInterval: List[int] - :rtype: List[List[int]] - """ - - return self.merge(intervals + [newInterval]) - - - - def merge(self, intervals): - """ - :type intervals: List[List[int]] - :rtype: List[List[int]] - """ - if not intervals or not intervals[0]: - return intervals - - intervals = sorted(intervals, key = lambda x:x[0]) - - res = [] - start, end = intervals[0][0], intervals[0][1] - for interval in intervals: - s, e = interval[0], interval[1] - - if s <= end: # overlap - end = max(end, e) - else: - res.append([start, end]) - start, end = s, e - - res.append([start, end]) - return res \ No newline at end of file diff --git "a/572.\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221/572-\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221.py" "b/572.\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221/572-\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221.py" deleted file mode 100644 index 7456e24..0000000 --- "a/572.\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221/572-\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221.py" +++ /dev/null @@ -1,32 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def isSubtree(self, s, t): - """ - :type s: TreeNode - :type t: TreeNode - :rtype: bool - """ - - if not s and not t: - return True - if s and not t: - return False - if t and not s: - return False - - return self.isSameTree(t, s) or self.isSubtree(s.left, t) or self.isSubtree(s.right, t) - - def isSameTree(self, t1, t2): - if not t1 and not t2: - return True - if t1 and not t2: - return False - if t2 and not t1: - return False - return t1.val == t2.val and self.isSameTree(t1.left, t2.left) and self.isSameTree(t1.right, t2.right) \ No newline at end of file diff --git "a/58.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/58-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" "b/58.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/58-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" deleted file mode 100644 index 77fb484..0000000 --- "a/58.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/58-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" +++ /dev/null @@ -1,9 +0,0 @@ -class Solution(object): - def lengthOfLastWord(self, s): - """ - :type s: str - :rtype: int - """ - if not s.split(): - return 0 - return len(s.split()[-1]) \ No newline at end of file diff --git "a/581.\346\234\200\347\237\255\346\227\240\345\272\217\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204/581-\346\234\200\347\237\255\346\227\240\345\272\217\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204.py" "b/581.\346\234\200\347\237\255\346\227\240\345\272\217\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204/581-\346\234\200\347\237\255\346\227\240\345\272\217\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204.py" deleted file mode 100644 index 33bc985..0000000 --- "a/581.\346\234\200\347\237\255\346\227\240\345\272\217\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204/581-\346\234\200\347\237\255\346\227\240\345\272\217\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204.py" +++ /dev/null @@ -1,19 +0,0 @@ -class Solution(object): - def findUnsortedSubarray(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - s = sorted(nums) - if s == nums: - return 0 - for i in range(len(s)): - if s[i] != nums[i]: - break - for j in range(len(s) - 1, -1, -1): - if s[j] != nums[j]: - break - # print i, j - # print s, nums - return len(s) - i - (len(s) - 1 -j) - \ No newline at end of file diff --git "a/633.\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214/633-\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214.py" "b/633.\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214/633-\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214.py" deleted file mode 100644 index 9347d22..0000000 --- "a/633.\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214/633-\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214.py" +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def judgeSquareSum(self, c): - """ - :type c: int - :rtype: bool - """ - for i in range(int(c ** 0.5) + 1): - t = c - i ** 2 - s = int (t ** 0.5) - if t == s ** 2: - return True - return False if c else True \ No newline at end of file diff --git "a/634.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\351\224\231\344\275\215\346\216\222\345\210\227/634-\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\351\224\231\344\275\215\346\216\222\345\210\227.py" "b/634.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\351\224\231\344\275\215\346\216\222\345\210\227/634-\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\351\224\231\344\275\215\346\216\222\345\210\227.py" deleted file mode 100644 index 99bf7db..0000000 --- "a/634.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\351\224\231\344\275\215\346\216\222\345\210\227/634-\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\351\224\231\344\275\215\346\216\222\345\210\227.py" +++ /dev/null @@ -1,10 +0,0 @@ -class Solution(object): - def findDerangement(self, n): - """ - :type n: int - :rtype: int - """ - res = 0 - for i in range(n + 1): - res = (i * res + (-1) ** i) % (10 ** 9 + 7) - return res \ No newline at end of file diff --git "a/6424.\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227/6424-\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227.py" "b/6424.\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227/6424-\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227.py" new file mode 100644 index 0000000..21bb5b9 --- /dev/null +++ "b/6424.\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227/6424-\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227.py" @@ -0,0 +1,8 @@ +class Solution: + def semiOrderedPermutation(self, nums: List[int]) -> int: + index_1 = nums.index(1) + index_n = nums.index(len(nums)) + if index_1 < index_n: + return index_1 + (len(nums) - 1 - index_n) + else: + return index_1 + (len(nums) - 1 - index_n - 1) \ No newline at end of file diff --git "a/6462.\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246/6462-\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246.py" "b/6462.\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246/6462-\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246.py" new file mode 100644 index 0000000..cae0b56 --- /dev/null +++ "b/6462.\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246/6462-\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246.py" @@ -0,0 +1,3 @@ +class Solution: + def minimizedStringLength(self, s: str) -> int: + return len(set(s)) \ No newline at end of file diff --git "a/6472.\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214/6472-\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214.py" "b/6472.\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214/6472-\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214.py" new file mode 100644 index 0000000..1f05ad5 --- /dev/null +++ "b/6472.\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214/6472-\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214.py" @@ -0,0 +1,16 @@ +class Solution: + def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int: + visited_rows, visited_cols = set(), set() + res = 0 + for t, index, val in queries[::-1]: + if t == 0: + if index not in visited_rows: + res += val * (n - len(visited_cols)) + visited_rows.add(index) + elif t == 1: + if index not in visited_cols: + res += val * (n - len(visited_rows)) + visited_cols.add(index) + + return res + diff --git "a/695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/695-\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.py" "b/695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/695-\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.py" deleted file mode 100644 index 905712b..0000000 --- "a/695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/695-\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.py" +++ /dev/null @@ -1,35 +0,0 @@ -class Solution(object): - def maxAreaOfIsland(self, grid): - """ - :type grid: List[List[int]] - :rtype: int - """ - if not grid or not grid[0]: - return 0 - - m, n = len(grid), len(grid[0]) - self.res = 0 - self.tmp = 0 - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - - def dfs(x0, y0): - self.tmp += 1 - self.res = max(self.res, self.tmp) - - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - - if 0 <= x< m and 0 <= y < n and grid[x][y] == 1: - grid[x][y] = 0 - dfs(x, y) - - - for i in range(m): - for j in range(n): - if grid[i][j]: - grid[i][j] = 0 - self.tmp = 0 - dfs(i, j) - return self.res \ No newline at end of file diff --git "a/698.\345\210\222\345\210\206\344\270\272k\344\270\252\347\233\270\347\255\211\347\232\204\345\255\220\351\233\206/698-\345\210\222\345\210\206\344\270\272k\344\270\252\347\233\270\347\255\211\347\232\204\345\255\220\351\233\206.py" "b/698.\345\210\222\345\210\206\344\270\272k\344\270\252\347\233\270\347\255\211\347\232\204\345\255\220\351\233\206/698-\345\210\222\345\210\206\344\270\272k\344\270\252\347\233\270\347\255\211\347\232\204\345\255\220\351\233\206.py" deleted file mode 100644 index bffc0f3..0000000 --- "a/698.\345\210\222\345\210\206\344\270\272k\344\270\252\347\233\270\347\255\211\347\232\204\345\255\220\351\233\206/698-\345\210\222\345\210\206\344\270\272k\344\270\252\347\233\270\347\255\211\347\232\204\345\255\220\351\233\206.py" +++ /dev/null @@ -1,39 +0,0 @@ -class Solution(object): - def canPartitionKSubsets(self, nums, k): - """ - :type nums: List[int] - :type k: int - :rtype: bool - """ - s = sum(nums) - if s % k != 0 or len(nums) < k: - return False - - target = s / k - - nums.sort(reverse = True) - visited = set() - - self.res = False - def dfs(cnt, tmp, start): - if tmp == target: - dfs(cnt - 1, 0, 0) - - if cnt == 0: - self.res = True - return - if not self.res: - for i in range(start, len(nums)): - if i not in visited and tmp + nums[i] <= target: - visited.add(i) - dfs(cnt, tmp + nums[i], i + 1) - visited.remove(i) - dfs(k, 0, 0) - return self.res - - - - - - - \ No newline at end of file diff --git "a/72.\347\274\226\350\276\221\350\267\235\347\246\273/72-\347\274\226\350\276\221\350\267\235\347\246\273.py" "b/72.\347\274\226\350\276\221\350\267\235\347\246\273/72-\347\274\226\350\276\221\350\267\235\347\246\273.py" deleted file mode 100644 index 81e9fd3..0000000 --- "a/72.\347\274\226\350\276\221\350\267\235\347\246\273/72-\347\274\226\350\276\221\350\267\235\347\246\273.py" +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def minDistance(self, word1, word2): - """ - :type word1: str - :type word2: str - :rtype: int - """ - #用dp[i][j]表示word1[:i + 1], word2[:j + 1]这个问题的解 - m, n = len(word1), len(word2) - dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)] - - for i in range(m + 1): - dp[i][0] = i - - for i in range(n + 1): - dp[0][i] = i - - for i in range(1, m + 1): - for j in range(1, n + 1): - if word1[i - 1] == word2[j - 1]: - dp[i][j] = dp[i - 1][j - 1] - else: - dp[i][j] = 1 + min(dp[i - 1][j], dp[i - 1][j - 1], dp[i][j - 1]) #分别对应插入,替换,删除 - - return dp[m][n] \ No newline at end of file diff --git "a/760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" "b/760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" deleted file mode 100644 index 58c9a94..0000000 --- "a/760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def anagramMappings(self, A, B): - """ - :type A: List[int] - :type B: List[int] - :rtype: List[int] - """ - - dic = dict() - for i, x in enumerate(B): - dic[x] = i - - return [dic[x] for x in A] \ No newline at end of file diff --git "a/763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" "b/763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" deleted file mode 100644 index 8216656..0000000 --- "a/763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" +++ /dev/null @@ -1,39 +0,0 @@ -class Solution(object): - def partitionLabels(self, S): - """ - :type S: str - :rtype: List[int] - """ - from collections import defaultdict - dic = defaultdict(list) - - for ch in "abcdefghijklmnopqrstuvwxyz": - for i, char in enumerate(S): - if char == ch: - dic[ch].append(i) - break - - for i in range(len(S) - 1, -1, -1): - if S[i] == ch: - dic[ch].append(i) - break - - - intervals = [] - for val in dic.values(): - intervals.append(val) - - intervals.sort() - #print intervals - - res = [] - start, end = 0, 0 - for s, e in intervals: - if s > end: - res.append(end - start + 1) - start, end = s, e - else: - end = max(e, end) - res.append(end - start + 1) - - return res \ No newline at end of file diff --git "a/771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" "b/771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" deleted file mode 100644 index 4b0e5c9..0000000 --- "a/771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def numJewelsInStones(self, J, S): - """ - :type J: str - :type S: str - :rtype: int - """ - J = set(J) - res = 0 - for s in S: - if s in J: - res += 1 - return res \ No newline at end of file diff --git "a/792.\345\214\271\351\205\215\345\255\220\345\272\217\345\210\227\347\232\204\345\215\225\350\257\215\346\225\260/792-\345\214\271\351\205\215\345\255\220\345\272\217\345\210\227\347\232\204\345\215\225\350\257\215\346\225\260.py" "b/792.\345\214\271\351\205\215\345\255\220\345\272\217\345\210\227\347\232\204\345\215\225\350\257\215\346\225\260/792-\345\214\271\351\205\215\345\255\220\345\272\217\345\210\227\347\232\204\345\215\225\350\257\215\346\225\260.py" deleted file mode 100644 index ad43f92..0000000 --- "a/792.\345\214\271\351\205\215\345\255\220\345\272\217\345\210\227\347\232\204\345\215\225\350\257\215\346\225\260/792-\345\214\271\351\205\215\345\255\220\345\272\217\345\210\227\347\232\204\345\215\225\350\257\215\346\225\260.py" +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def numMatchingSubseq(self, S, words): - """ - :type S: str - :type words: List[str] - :rtype: int - """ - from collections import defaultdict - - dic = defaultdict(list) - for i, ch in enumerate(S): - dic[ch].append(i) - - res = 0 - for word in words: - pre = -1 - flag = True - for i, ch in enumerate(word): - l = dic[ch] - # 在l找第一个比pre大的元素 - idx = bisect.bisect(l, pre) - - if idx == len(l):# 没找到 - flag = False - break - pre = l[idx] - - if flag: - res += 1 - - return res \ No newline at end of file diff --git "a/8.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi)/8-\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi).py" "b/8.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi)/8-\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi).py" deleted file mode 100644 index 7b8a7be..0000000 --- "a/8.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi)/8-\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi).py" +++ /dev/null @@ -1,36 +0,0 @@ -class Solution(object): - def myAtoi(self, s): - """ - :type str: str - :rtype: int - """ - s = s.strip() - - if not s: - return 0 - - sign = 1 - INT_MAX, INT_MIN = 2 ** 31 - 1, -(2 ** 31) - - if s[0] == "+": - s = s[1:] - elif s[0] == "-": - s = s[1:] - sign = -1 - elif not s[0].isdigit(): - return 0 - - num = "0" - for ch in s: - if ch.isdigit(): - num += ch - else: - break - - num = sign * int(num) - - if num > INT_MAX: - return INT_MAX - elif num < INT_MIN: - return INT_MIN - return num \ No newline at end of file diff --git "a/809.\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227/809-\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227.py" "b/809.\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227/809-\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227.py" deleted file mode 100644 index 59c336c..0000000 --- "a/809.\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227/809-\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227.py" +++ /dev/null @@ -1,38 +0,0 @@ -class Solution(object): - def expressiveWords(self, S, words): - """ - :type S: str - :type words: List[str] - :rtype: int - """ - - s = set(S) - res = 0 - for word in words: - if len(S) < len(word): - continue - - i, j = 0, 0 - flag = 0 - while i < len(S) and j < len(word): - if S[i] != word[j]: - flag = 1 - break - pre = S[i] - cnt_i = 0 - while i < len(S) and S[i] == pre: - i += 1 - cnt_i += 1 - - cnt_j = 0 - while j < len(word) and word[j] == pre: - j += 1 - cnt_j += 1 - - # print cnt_i, cnt_j - if (cnt_i < 3 and cnt_i != cnt_j) or cnt_i < cnt_j: - flag = 1 - - if not flag and i == len(S): - res += 1 - return res \ No newline at end of file diff --git "a/820.\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201/820-\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201.py" "b/820.\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201/820-\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201.py" deleted file mode 100644 index 618a6c9..0000000 --- "a/820.\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201/820-\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201.py" +++ /dev/null @@ -1,36 +0,0 @@ -class Trie(object): - def __init__(self): - """ - Initialize your data structure here. - """ - self.root = {} - self.char_cnt = 0 # 统计 a - z 字符个数 - self.word_cnt = 0 # 统计结尾符 # 个数 - def insert(self, word): - """ - Inserts a word into the trie. - :type word: str - :rtype: None - """ - node = self.root - for char in word: # word 入树 - node = node.setdefault(char, {}) - - if not node: # not node 就代表当前 word 不是之前某一 word 的后缀 - self.word_cnt += 1 - self.char_cnt += len(word) - node["end"] = True - -class Solution(object): - def minimumLengthEncoding(self, words): - """ - :type words: List[str] - :rtype: int - """ - ttree = Trie() - - for word in sorted(words, key = lambda x:len(x), reverse = True): - # 按长度由大到小排序,再将每个 word 反向插入树 - ttree.insert(word[::-1]) - # print ttree.char_cnt, ttree.word_cnt - return ttree.char_cnt + ttree.word_cnt \ No newline at end of file diff --git "a/836.\347\237\251\345\275\242\351\207\215\345\217\240/836-\347\237\251\345\275\242\351\207\215\345\217\240.py" "b/836.\347\237\251\345\275\242\351\207\215\345\217\240/836-\347\237\251\345\275\242\351\207\215\345\217\240.py" deleted file mode 100644 index a13282d..0000000 --- "a/836.\347\237\251\345\275\242\351\207\215\345\217\240/836-\347\237\251\345\275\242\351\207\215\345\217\240.py" +++ /dev/null @@ -1,10 +0,0 @@ -class Solution(object): - def isRectangleOverlap(self, rec1, rec2): - """ - :type rec1: List[int] - :type rec2: List[int] - :rtype: bool - """ - x1, y1, x2, y2 = rec1 - x3, y3, x4, y4 = rec2 - return (x3 - x2) * (x4 - x1) < 0 and (y3 - y2) * (y4 - y1) < 0 \ No newline at end of file diff --git "a/845.\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211/845-\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211.py" "b/845.\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211/845-\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211.py" deleted file mode 100644 index 970933d..0000000 --- "a/845.\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211/845-\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211.py" +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def longestMountain(self, A): - """ - :type A: List[int] - :rtype: int - """ - # a = sorted(A) - # if a == A or a[::-1] == A: - # return 0 - l, r = [0 for _ in A], [0 for _ in A] - - for i in range(1, len(A)): - if A[i] > A[i - 1]: - l[i] = l[i - 1] + 1 - - for i in range(len(A) - 2, -1, -1): - if A[i] > A[i + 1]: - r[i] = r[i + 1] + 1 - - res = 0 - for i in range(len(A)): - if l[i] and r[i] and l[i] + r[i] > 1: - res = max(l[i] + r[i] + 1, res) - return res \ No newline at end of file diff --git "a/876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" "b/876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" deleted file mode 100644 index 4a96cec..0000000 --- "a/876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" +++ /dev/null @@ -1,17 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def middleNode(self, head): - """ - :type head: ListNode - :rtype: ListNode - """ - slow, fast = head, head - while fast and fast.next: - slow = slow.next - fast = fast.next.next - return slow \ No newline at end of file diff --git "a/887.\351\270\241\350\233\213\346\216\211\350\220\275/887-\351\270\241\350\233\213\346\216\211\350\220\275.py" "b/887.\351\270\241\350\233\213\346\216\211\350\220\275/887-\351\270\241\350\233\213\346\216\211\350\220\275.py" deleted file mode 100644 index 53390d9..0000000 --- "a/887.\351\270\241\350\233\213\346\216\211\350\220\275/887-\351\270\241\350\233\213\346\216\211\350\220\275.py" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution(object): - def superEggDrop(self, K, N): - """ - :type K: int - :type N: int - :rtype: int - """ - dp = [0] * (K + 1) - m = 0 - while dp[K] < N: - m += 1 - for k in range(K, 0, -1): - # print(m, k) - dp[k] = dp[k - 1] + dp[k] + 1 - return m \ No newline at end of file diff --git "a/892.\344\270\211\347\273\264\345\275\242\344\275\223\347\232\204\350\241\250\351\235\242\347\247\257/892-\344\270\211\347\273\264\345\275\242\344\275\223\347\232\204\350\241\250\351\235\242\347\247\257.py" "b/892.\344\270\211\347\273\264\345\275\242\344\275\223\347\232\204\350\241\250\351\235\242\347\247\257/892-\344\270\211\347\273\264\345\275\242\344\275\223\347\232\204\350\241\250\351\235\242\347\247\257.py" deleted file mode 100644 index a182a0b..0000000 --- "a/892.\344\270\211\347\273\264\345\275\242\344\275\223\347\232\204\350\241\250\351\235\242\347\247\257/892-\344\270\211\347\273\264\345\275\242\344\275\223\347\232\204\350\241\250\351\235\242\347\247\257.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def surfaceArea(self, grid): - """ - :type grid: List[List[int]] - :rtype: int - """ - res = 0 - - for i in range(len(grid)): - for j in range(len(grid[0])): - if grid[i][j] > 0: res += 2 # 上和下 - for r in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]: - if 0 <= r[0] < len(grid) and 0 <= r[1] < len(grid[0]): res += max(0, grid[i][j] - grid[r[0]][r[1]]) # 中间高出相邻方块的部分 - else: res += grid[i][j] # 前后左右最外侧 - - return res \ No newline at end of file diff --git "a/905.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204/905-\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.py" "b/905.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204/905-\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.py" deleted file mode 100644 index 43e1110..0000000 --- "a/905.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204/905-\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def sortArrayByParity(self, A): - """ - :type A: List[int] - :rtype: List[int] - """ - return sorted(A, key = lambda x:x % 2) \ No newline at end of file diff --git "a/912.\346\216\222\345\272\217\346\225\260\347\273\204/912-\346\216\222\345\272\217\346\225\260\347\273\204.py" "b/912.\346\216\222\345\272\217\346\225\260\347\273\204/912-\346\216\222\345\272\217\346\225\260\347\273\204.py" deleted file mode 100644 index ea57e4d..0000000 --- "a/912.\346\216\222\345\272\217\346\225\260\347\273\204/912-\346\216\222\345\272\217\346\225\260\347\273\204.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def sortArray(self, nums): - """ - :type nums: List[int] - :rtype: List[int] - """ - return sorted(nums) \ No newline at end of file diff --git "a/914.\345\215\241\347\211\214\345\210\206\347\273\204/914-\345\215\241\347\211\214\345\210\206\347\273\204.py" "b/914.\345\215\241\347\211\214\345\210\206\347\273\204/914-\345\215\241\347\211\214\345\210\206\347\273\204.py" deleted file mode 100644 index 609c27e..0000000 --- "a/914.\345\215\241\347\211\214\345\210\206\347\273\204/914-\345\215\241\347\211\214\345\210\206\347\273\204.py" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution(object): - def hasGroupsSizeX(self, deck): - """ - :type deck: List[int] - :rtype: bool - """ - def gcd(a, b): - while b: - a, b = b, a % b - return a - return functools.reduce(gcd, collections.Counter(deck).values()) >= 2 \ No newline at end of file diff --git "a/921.\344\275\277\346\213\254\345\217\267\346\234\211\346\225\210\347\232\204\346\234\200\345\260\221\346\267\273\345\212\240/921-\344\275\277\346\213\254\345\217\267\346\234\211\346\225\210\347\232\204\346\234\200\345\260\221\346\267\273\345\212\240.py" "b/921.\344\275\277\346\213\254\345\217\267\346\234\211\346\225\210\347\232\204\346\234\200\345\260\221\346\267\273\345\212\240/921-\344\275\277\346\213\254\345\217\267\346\234\211\346\225\210\347\232\204\346\234\200\345\260\221\346\267\273\345\212\240.py" deleted file mode 100644 index 94e321e..0000000 --- "a/921.\344\275\277\346\213\254\345\217\267\346\234\211\346\225\210\347\232\204\346\234\200\345\260\221\346\267\273\345\212\240/921-\344\275\277\346\213\254\345\217\267\346\234\211\346\225\210\347\232\204\346\234\200\345\260\221\346\267\273\345\212\240.py" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution(object): - def minAddToMakeValid(self, S): - """ - :type S: str - :rtype: int - """ - stack = [] - dic = {"(":")", "{":"}", "[":"]"} - for ch in S: - if stack and stack[-1] in dic and dic[stack[-1]] == ch: - stack.pop() - else: - stack.append(ch) - return len(stack) \ No newline at end of file diff --git "a/945.\344\275\277\346\225\260\347\273\204\345\224\257\344\270\200\347\232\204\346\234\200\345\260\217\345\242\236\351\207\217/945-\344\275\277\346\225\260\347\273\204\345\224\257\344\270\200\347\232\204\346\234\200\345\260\217\345\242\236\351\207\217.py" "b/945.\344\275\277\346\225\260\347\273\204\345\224\257\344\270\200\347\232\204\346\234\200\345\260\217\345\242\236\351\207\217/945-\344\275\277\346\225\260\347\273\204\345\224\257\344\270\200\347\232\204\346\234\200\345\260\217\345\242\236\351\207\217.py" deleted file mode 100644 index ee36ba2..0000000 --- "a/945.\344\275\277\346\225\260\347\273\204\345\224\257\344\270\200\347\232\204\346\234\200\345\260\217\345\242\236\351\207\217/945-\344\275\277\346\225\260\347\273\204\345\224\257\344\270\200\347\232\204\346\234\200\345\260\217\345\242\236\351\207\217.py" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution(object): - def minIncrementForUnique(self, A): - """ - :type A: List[int] - :rtype: int - """ - i, ans = 0, 0 - for a in sorted(A): - ans += max(i - a, 0) - i = max(a, i) + 1 - return ans \ No newline at end of file diff --git "a/946.\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227/946-\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227.py" "b/946.\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227/946-\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227.py" deleted file mode 100644 index ca0caa0..0000000 --- "a/946.\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227/946-\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def validateStackSequences(self, pushed, popped): - """ - :type pushed: List[int] - :type popped: List[int] - :rtype: bool - """ - s = [] - popped = popped[::-1] - for num in pushed: - s.append(num) - while s and popped and s[-1] == popped[-1]: - s.pop() - popped.pop() - - return not s and not popped \ No newline at end of file diff --git "a/973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.py" "b/973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.py" deleted file mode 100644 index f474ff6..0000000 --- "a/973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.py" +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def kClosest(self, points, K): - left, right, target = 0, len(points) - 1, K - 1 - while True: - pos = self.partition(points, left, right) - if pos == target: - return points[:pos + 1] - elif pos > K: #要往左找 - right = pos - 1 - elif pos < K: #要往右找 - left = pos + 1 - - def partition(self, nums, left, right): - import random - k = random.randint(left, right) - pivot = nums[k][0] ** 2 + nums[k][1] ** 2 - nums[left], nums[k] = nums[k], nums[left] - index = left - - for i in range(left + 1, right + 1): - if nums[i][0] ** 2 + nums[i][1] ** 2 < pivot: - index += 1 - nums[i], nums[index] = nums[index], nums[i] - nums[left], nums[index] = nums[index], nums[left] - return index #此时所有index左侧的值都比nums[index]大, 所有右侧的值都比nums[index]小 diff --git "a/98.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/98-\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/98.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/98-\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" deleted file mode 100644 index 8da976b..0000000 --- "a/98.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/98-\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" +++ /dev/null @@ -1,20 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def isValidBST(self, root): - """ - :type root: TreeNode - :rtype: bool - """ - def inorder(node): - if not node: - return [] - return inorder(node.left) + [node.val] + inorder(node.right) - - l = inorder(root) - return l == sorted(l) and len(l) == len(set(l)) \ No newline at end of file diff --git "a/994.\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220/994-\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220.py" "b/994.\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220/994-\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220.py" deleted file mode 100644 index 5d9ce8b..0000000 --- "a/994.\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220/994-\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220.py" +++ /dev/null @@ -1,43 +0,0 @@ -class Solution(object): - def orangesRotting(self, grid): - """ - :type grid: List[List[int]] - :rtype: int - """ - from collections import deque - if not grid or not grid[0]: - return 0 - - m, n = len(grid), len(grid[0]) - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - - queue = deque() - for i in range(m): - for j in range(n): - if grid[i][j] == 2: - queue.append((i, j)) - - res = 0 - while queue: - for i in range(len(queue)): - pair = queue.popleft() - x0, y0 = pair[0], pair[1] - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - - if 0 <= x < m and 0 <= y < n and grid[x][y] == 1: - grid[x][y] = 2 - queue.append((x, y)) - if not queue: - break - res += 1 - for i in range(m): - for j in range(n): - if grid[i][j] == 1: - return -1 - return res - - - \ No newline at end of file diff --git "a/999.\345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260/999-\345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260.py" "b/999.\345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260/999-\345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260.py" deleted file mode 100644 index 2254eb0..0000000 --- "a/999.\345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260/999-\345\217\257\344\273\245\350\242\253\344\270\200\346\255\245\346\215\225\350\216\267\347\232\204\346\243\213\345\255\220\346\225\260.py" +++ /dev/null @@ -1,28 +0,0 @@ -class Solution(object): - def numRookCaptures(self, board): - """ - :type board: List[List[str]] - :rtype: int - """ - self.res,m,n = 0,len(board),len(board[0]) - - def check(n): - if n == 'B' or n == 'p': - if n == 'p':self.res += 1 - return False - return True - - for i in range(m): - for j in range(n): - if board[i][j] == 'R': - for u in range(i-1,-1,-1): - if not check(board[u][j]):break - for d in range(i+1,m): - if not check(board[d][j]):break - for l in range(j-1,-1,-1): - if not check(board[i][l]):break - for r in range(j+1,n): - if not check(board[i][r]):break - break - return self.res - diff --git "a/LCP 01.\347\214\234\346\225\260\345\255\227/LCP 01-\347\214\234\346\225\260\345\255\227.py" "b/LCP 01.\347\214\234\346\225\260\345\255\227/LCP 01-\347\214\234\346\225\260\345\255\227.py" new file mode 100644 index 0000000..71601c2 --- /dev/null +++ "b/LCP 01.\347\214\234\346\225\260\345\255\227/LCP 01-\347\214\234\346\225\260\345\255\227.py" @@ -0,0 +1,3 @@ +class Solution: + def game(self, guess: List[int], answer: List[int]) -> int: + return sum(guess[i] == answer[i] for i in range(3)) \ No newline at end of file diff --git "a/LCP 17.\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/LCP 17-\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272.py" "b/LCP 17.\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/LCP 17-\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272.py" new file mode 100644 index 0000000..303f35f --- /dev/null +++ "b/LCP 17.\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/LCP 17-\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272.py" @@ -0,0 +1,10 @@ +class Solution: + def calculate(self, s: str) -> int: + x, y = 1, 0 + + for char in s: + if char == "A": + x = 2 * x + y + else: + y = 2 * y + x + return x + y \ No newline at end of file diff --git "a/LCP 44.\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253/LCP 44-\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253.py" "b/LCP 44.\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253/LCP 44-\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253.py" new file mode 100644 index 0000000..4205610 --- /dev/null +++ "b/LCP 44.\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253/LCP 44-\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253.py" @@ -0,0 +1,14 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def numColor(self, root: TreeNode) -> int: + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + return len(set(inorder(root))) \ No newline at end of file diff --git "a/LCP 66.\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/LCP 66-\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217.py" "b/LCP 66.\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/LCP 66-\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217.py" new file mode 100644 index 0000000..3e9157b --- /dev/null +++ "b/LCP 66.\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/LCP 66-\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217.py" @@ -0,0 +1,11 @@ +class Solution: + def minNumBooths(self, demands: List[str]) -> int: + from collections import defaultdict, Counter + char2count = defaultdict(int) + for demand in demands: + c = Counter(demand) + + for char, freq in c.items(): + char2count[char] = max(char2count[char], freq) + + return sum(freq for freq in char2count.values()) \ No newline at end of file diff --git "a/LCP 67.\350\243\205\351\245\260\346\240\221/LCP 67-\350\243\205\351\245\260\346\240\221.py" "b/LCP 67.\350\243\205\351\245\260\346\240\221/LCP 67-\350\243\205\351\245\260\346\240\221.py" new file mode 100644 index 0000000..0e7a995 --- /dev/null +++ "b/LCP 67.\350\243\205\351\245\260\346\240\221/LCP 67-\350\243\205\351\245\260\346\240\221.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def expandBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + + def dfs(node): + if not node: + return node + + left, right = node.left, node.right + if left: + node.left = TreeNode(-1, left) + + if right: + node.right = TreeNode(-1, None, right) + + dfs(left) + dfs(right) + return node + + return dfs(root) + \ No newline at end of file diff --git "a/LeetCode-Python/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.py" "b/LeetCode-Python/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.py" new file mode 100644 index 0000000..fda31fd --- /dev/null +++ "b/LeetCode-Python/0001.\344\270\244\346\225\260\344\271\213\345\222\214/0001-\344\270\244\346\225\260\344\271\213\345\222\214.py" @@ -0,0 +1,9 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + value2index = {} + + for index, num in enumerate(nums): + if target - num in value2index: + return [value2index[target - num], index] + value2index[num] = index + \ No newline at end of file diff --git "a/LeetCode-Python/0002.\344\270\244\346\225\260\347\233\270\345\212\240/0002-\344\270\244\346\225\260\347\233\270\345\212\240.py" "b/LeetCode-Python/0002.\344\270\244\346\225\260\347\233\270\345\212\240/0002-\344\270\244\346\225\260\347\233\270\345\212\240.py" new file mode 100644 index 0000000..babd7fd --- /dev/null +++ "b/LeetCode-Python/0002.\344\270\244\346\225\260\347\233\270\345\212\240/0002-\344\270\244\346\225\260\347\233\270\345\212\240.py" @@ -0,0 +1,56 @@ +# 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: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: + if not l1 and not l2: + return l1 + if not l1: + return l2 + if not l2: + return l1 + + p1, p2 = l1, l2 + carry = 0 + dummy = ListNode(-1) + p = dummy + while l1 and l2: + s = l1.val + l2.val + carry + l1 = l1.next + l2 = l2.next + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + + p.next = ListNode(s) + p = p.next + + while l1: + s = l1.val + carry + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + l1 = l1.next + p.next = ListNode(s) + p = p.next + + while l2: + s = l2.val + carry + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + l2 = l2.next + p.next = ListNode(s) + p = p.next + + if carry: + p.next = ListNode(1) + return dummy.next \ No newline at end of file diff --git "a/LeetCode-Python/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260.py" "b/LeetCode-Python/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260.py" new file mode 100644 index 0000000..fce763a --- /dev/null +++ "b/LeetCode-Python/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260.py" @@ -0,0 +1,3 @@ +class Solution: + def isPalindrome(self, x: int) -> bool: + return str(x) == str(x)[::-1] \ No newline at end of file diff --git "a/LeetCode-Python/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271/0019-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271.py" "b/LeetCode-Python/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271/0019-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271.py" new file mode 100644 index 0000000..30dac97 --- /dev/null +++ "b/LeetCode-Python/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271/0019-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\347\273\223\347\202\271.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + l = 0 + p = head + while p: + l += 1 + p = p.next + + count = l - n + 1 + + dummy = ListNode(-1) + dummy.next = head + cur = -1 + p = dummy + while p: + cur += 1 + if cur == count - 1: + node_to_be_deleted = p.next + p.next = node_to_be_deleted.next + break + p = p.next + + return dummy.next \ No newline at end of file diff --git "a/LeetCode-Python/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" "b/LeetCode-Python/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" new file mode 100644 index 0000000..4635692 --- /dev/null +++ "b/LeetCode-Python/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.py" @@ -0,0 +1,11 @@ +class Solution: + def isValid(self, s: str) -> bool: + right2left = {")":"(", "]":"[", "}":"{"} + stack = [] + for char in s: + if char in right2left: + if not stack or stack.pop() != right2left[char]: + return False + else: + stack.append(char) + return stack == [] diff --git "a/LeetCode-Python/0021.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/0021-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" "b/LeetCode-Python/0021.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/0021-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" new file mode 100644 index 0000000..0c462b0 --- /dev/null +++ "b/LeetCode-Python/0021.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250/0021-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.py" @@ -0,0 +1,25 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(-1) + p = dummy + while list1 and list2: + if list1.val <= list2.val: + node = ListNode(list1.val) + list1 = list1.next + else: + node = ListNode(list2.val) + list2 = list2.next + p.next = node + p = p.next + + if list1: + p.next = list1 + elif list2: + p.next = list2 + + return dummy.next \ No newline at end of file diff --git "a/LeetCode-Python/0022.\346\213\254\345\217\267\347\224\237\346\210\220/0022-\346\213\254\345\217\267\347\224\237\346\210\220.py" "b/LeetCode-Python/0022.\346\213\254\345\217\267\347\224\237\346\210\220/0022-\346\213\254\345\217\267\347\224\237\346\210\220.py" new file mode 100644 index 0000000..1b94ac9 --- /dev/null +++ "b/LeetCode-Python/0022.\346\213\254\345\217\267\347\224\237\346\210\220/0022-\346\213\254\345\217\267\347\224\237\346\210\220.py" @@ -0,0 +1,14 @@ +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + res = [] + def dfs(left, right, tmp): + if left == 0 and right == 0: + res.append(tmp) + return + if left > 0: + dfs(left - 1, right, tmp + "(") + if right > left: + dfs(left, right - 1, tmp + ")") + + dfs(n, n, "") + return res \ No newline at end of file diff --git "a/LeetCode-Python/0024.\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/0024-\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/LeetCode-Python/0024.\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/0024-\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..a828a2c --- /dev/null +++ "b/LeetCode-Python/0024.\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/0024-\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 + first, second = head, head.next + + tail = second.next + second.next, first.next = first, self.swapPairs(tail) + return second \ No newline at end of file diff --git "a/LeetCode-Python/0026.\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/0026-\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/LeetCode-Python/0026.\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/0026-\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..5f77e5e --- /dev/null +++ "b/LeetCode-Python/0026.\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/0026-\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,10 @@ +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + visited = set() + index = 0 + for num in nums: + if num not in visited: + visited.add(num) + nums[index] = num + index += 1 + return index \ No newline at end of file diff --git "a/LeetCode-Python/0027.\347\247\273\351\231\244\345\205\203\347\264\240/0027-\347\247\273\351\231\244\345\205\203\347\264\240.py" "b/LeetCode-Python/0027.\347\247\273\351\231\244\345\205\203\347\264\240/0027-\347\247\273\351\231\244\345\205\203\347\264\240.py" new file mode 100644 index 0000000..92fd921 --- /dev/null +++ "b/LeetCode-Python/0027.\347\247\273\351\231\244\345\205\203\347\264\240/0027-\347\247\273\351\231\244\345\205\203\347\264\240.py" @@ -0,0 +1,11 @@ +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + i, right = 0, len(nums) - 1 + while i <= right: + if nums[i] == val: + while i < right and nums[right] == val: + right -= 1 + nums[i], nums[right] = nums[right], nums[i] + right -= 1 + i += 1 + return right + 1 \ No newline at end of file diff --git "a/LeetCode-Python/0028.\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/0028-\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/LeetCode-Python/0028.\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/0028-\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..781ae1a --- /dev/null +++ "b/LeetCode-Python/0028.\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/0028-\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,5 @@ +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + if needle not in haystack: + return -1 + return haystack.index(needle) \ No newline at end of file diff --git "a/LeetCode-Python/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/0034-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.py" "b/LeetCode-Python/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/0034-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.py" new file mode 100644 index 0000000..685aab9 --- /dev/null +++ "b/LeetCode-Python/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256/0034-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.py" @@ -0,0 +1,25 @@ +class Solution: + def searchRange(self, nums: List[int], target: int) -> List[int]: + # 1. find the left-most index + left, right = 0, len(nums) - 1 + left_index = -1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] >= target: + left_index = mid + right = mid - 1 + else: + left = mid + 1 + print(left, right, left_index) + # 2. find the right-most index + left, right = 0, len(nums) - 1 + right_index = -1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] <= target: + right_index = mid + left = mid + 1 + else: + right = mid - 1 + + return [left_index, right_index] if left_index > -1 and nums[left_index] == target else [-1, -1] \ No newline at end of file diff --git "a/LeetCode-Python/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.py" "b/LeetCode-Python/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.py" new file mode 100644 index 0000000..80bf81c --- /dev/null +++ "b/LeetCode-Python/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256/0035-\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.py" @@ -0,0 +1,13 @@ +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + + if nums[mid] == target: + return mid + elif nums[mid] > target: + right = mid - 1 + elif nums[mid] < target: + left = mid + 1 + return left \ No newline at end of file diff --git "a/LeetCode-Python/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.py" "b/LeetCode-Python/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.py" new file mode 100644 index 0000000..ec1d64f --- /dev/null +++ "b/LeetCode-Python/0036.\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254/0036-\346\234\211\346\225\210\347\232\204\346\225\260\347\213\254.py" @@ -0,0 +1,28 @@ +class Solution: + def isValidSudoku(self, board: List[List[str]]) -> bool: + from collections import defaultdict + # validate row + for i in range(9): + row = [digit for digit in board[i] if digit.isdigit()] + s = set(row) + if len(s) < len(row): + return False + + # validate col + boardT = [list(col) for col in zip(*board)] + for j in range(9): + col = [digit for digit in boardT[j] if digit.isdigit()] + s = set(col) + if len(s) < len(col): + return False + + # validate smaller 3 * 3 square + rowcol2digitset = defaultdict(set) + for i in range(9): + for j in range(9): + if board[i][j].isdigit(): + if board[i][j] in rowcol2digitset[str(i // 3) + str(j // 3)]: + return False + rowcol2digitset[str(i // 3) + str(j // 3)].add(board[i][j]) + + return True \ No newline at end of file diff --git "a/LeetCode-Python/0045.\350\267\263\350\267\203\346\270\270\346\210\217II/0045-\350\267\263\350\267\203\346\270\270\346\210\217II.py" "b/LeetCode-Python/0045.\350\267\263\350\267\203\346\270\270\346\210\217II/0045-\350\267\263\350\267\203\346\270\270\346\210\217II.py" new file mode 100644 index 0000000..5ad82f4 --- /dev/null +++ "b/LeetCode-Python/0045.\350\267\263\350\267\203\346\270\270\346\210\217II/0045-\350\267\263\350\267\203\346\270\270\346\210\217II.py" @@ -0,0 +1,9 @@ +class Solution: + def jump(self, nums: List[int]) -> int: + dp = [float("inf") for _ in nums] + dp[0] = 0 + for i, num in enumerate(nums): + for j in range(1, 1 + num): + if i + j < len(nums): + dp[i + j] = min(dp[i + j], dp[i] + 1) + return dp[-1] \ No newline at end of file diff --git "a/LeetCode-Python/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227.py" "b/LeetCode-Python/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227.py" new file mode 100644 index 0000000..a9d570a --- /dev/null +++ "b/LeetCode-Python/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227.py" @@ -0,0 +1,16 @@ +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + res = [] + def dfs(tmp, nums): + if not nums: + res.append(tmp) + + for i, x in enumerate(nums): + dfs(tmp + [x], nums[:i] + nums[i + 1:]) + + dfs([], nums) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0048.\346\227\213\350\275\254\345\233\276\345\203\217/0048-\346\227\213\350\275\254\345\233\276\345\203\217.py" "b/LeetCode-Python/0048.\346\227\213\350\275\254\345\233\276\345\203\217/0048-\346\227\213\350\275\254\345\233\276\345\203\217.py" new file mode 100644 index 0000000..49a4cfd --- /dev/null +++ "b/LeetCode-Python/0048.\346\227\213\350\275\254\345\233\276\345\203\217/0048-\346\227\213\350\275\254\345\233\276\345\203\217.py" @@ -0,0 +1,15 @@ +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + n = len(matrix) + for i in range(n): + for j in range(i + 1, n): + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + + for i in range(n): + row = matrix[i] + for j in range(n // 2): + row[j], row[-(j + 1)] = row[-(j + 1)], row[j] + diff --git "a/LeetCode-Python/0049.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/0049-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.py" "b/LeetCode-Python/0049.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/0049-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.py" new file mode 100644 index 0000000..50822de --- /dev/null +++ "b/LeetCode-Python/0049.\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204/0049-\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\345\210\206\347\273\204.py" @@ -0,0 +1,9 @@ +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + from collections import defaultdict + + d = defaultdict(list) + for word in strs: + d["".join(sorted(word))].append(word) + + return [val for key, val in d.items() ] \ No newline at end of file diff --git "a/LeetCode-Python/0054.\350\236\272\346\227\213\347\237\251\351\230\265/0054-\350\236\272\346\227\213\347\237\251\351\230\265.py" "b/LeetCode-Python/0054.\350\236\272\346\227\213\347\237\251\351\230\265/0054-\350\236\272\346\227\213\347\237\251\351\230\265.py" new file mode 100644 index 0000000..8827369 --- /dev/null +++ "b/LeetCode-Python/0054.\350\236\272\346\227\213\347\237\251\351\230\265/0054-\350\236\272\346\227\213\347\237\251\351\230\265.py" @@ -0,0 +1,40 @@ +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + state = "right" + if not matrix or not matrix[0]: + return [] + + m, n = len(matrix), len(matrix[0]) + count = m * n + x, y = 0, 0 + res = [] + visited = set() + while count: + res.append(matrix[x][y]) + visited.add((x, y)) + if state == "right": + if y + 1 < n and (x, y + 1) not in visited: + y += 1 + else: + state = "down" + x += 1 + elif state == "left": + if y - 1 >= 0 and (x, y - 1) not in visited: + y -= 1 + else: + state = "up" + x -= 1 + elif state == "up": + if x - 1 >= 0 and (x - 1, y) not in visited: + x -= 1 + else: + state = "right" + y += 1 + elif state == "down": + if x + 1 < m and (x + 1, y) not in visited: + x += 1 + else: + state = "left" + y -= 1 + count -= 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/0055.\350\267\263\350\267\203\346\270\270\346\210\217/0055-\350\267\263\350\267\203\346\270\270\346\210\217.py" "b/LeetCode-Python/0055.\350\267\263\350\267\203\346\270\270\346\210\217/0055-\350\267\263\350\267\203\346\270\270\346\210\217.py" new file mode 100644 index 0000000..86728ce --- /dev/null +++ "b/LeetCode-Python/0055.\350\267\263\350\267\203\346\270\270\346\210\217/0055-\350\267\263\350\267\203\346\270\270\346\210\217.py" @@ -0,0 +1,11 @@ +class Solution: + def canJump(self, nums: List[int]) -> bool: + cur, reach = 0, nums[0] + while 1: + reach = max(reach, cur + nums[cur]) + if reach >= len(nums) - 1: + return True + if reach == cur: + return False + cur += 1 + \ No newline at end of file diff --git "a/LeetCode-Python/0058.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/0058-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" "b/LeetCode-Python/0058.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/0058-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" new file mode 100644 index 0000000..40ad18e --- /dev/null +++ "b/LeetCode-Python/0058.\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246/0058-\346\234\200\345\220\216\344\270\200\344\270\252\345\215\225\350\257\215\347\232\204\351\225\277\345\272\246.py" @@ -0,0 +1,3 @@ +class Solution: + def lengthOfLastWord(self, s: str) -> int: + return len(s.split()[-1]) \ No newline at end of file diff --git "a/LeetCode-Python/0069.x\347\232\204\345\271\263\346\226\271\346\240\271/0069-x\347\232\204\345\271\263\346\226\271\346\240\271.py" "b/LeetCode-Python/0069.x\347\232\204\345\271\263\346\226\271\346\240\271/0069-x\347\232\204\345\271\263\346\226\271\346\240\271.py" new file mode 100644 index 0000000..3ebe3a8 --- /dev/null +++ "b/LeetCode-Python/0069.x\347\232\204\345\271\263\346\226\271\346\240\271/0069-x\347\232\204\345\271\263\346\226\271\346\240\271.py" @@ -0,0 +1,12 @@ +class Solution: + def mySqrt(self, x: int) -> int: + left, right = 0, x + res = 0 + while left <= right: + mid = (left + right) // 2 + if mid * mid > x: + right = mid - 1 + elif mid * mid <= x: + left = mid + 1 + res = mid + return res diff --git "a/LeetCode-Python/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257.py" "b/LeetCode-Python/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257.py" new file mode 100644 index 0000000..52850f2 --- /dev/null +++ "b/LeetCode-Python/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257.py" @@ -0,0 +1,22 @@ +class Solution: + def climbStairs(self, n: int) -> int: + # f(n) = f(n - 1) + f(n - 2) + # dp[n] = dp[n -1] + dp[n - 2] + # memo = dict() + # def dfs(n): + # if n < 0: + # return 0 + # if n <= 1: + # return 1 + # if n in memo: + # return memo[n] + # res = dfs(n - 1) + dfs(n - 2) + # memo[n] = res + # return res + # return dfs(n) + f0, f1 = 1, 1 + for i in range(1, n): + newf = f0 + f1 + f0 = f1 + f1 = newf + return f1 \ No newline at end of file diff --git "a/LeetCode-Python/0071.\347\256\200\345\214\226\350\267\257\345\276\204/0071-\347\256\200\345\214\226\350\267\257\345\276\204.py" "b/LeetCode-Python/0071.\347\256\200\345\214\226\350\267\257\345\276\204/0071-\347\256\200\345\214\226\350\267\257\345\276\204.py" new file mode 100644 index 0000000..48136d0 --- /dev/null +++ "b/LeetCode-Python/0071.\347\256\200\345\214\226\350\267\257\345\276\204/0071-\347\256\200\345\214\226\350\267\257\345\276\204.py" @@ -0,0 +1,13 @@ +class Solution: + def simplifyPath(self, path: str) -> str: + path_names = path.split("/") + stack = [] + for path_name in path_names: + if path_name: + if path_name == "..": + if stack: + stack.pop() + elif path_name != ".": + stack.append(path_name) + + return "/" + "/".join(stack) \ No newline at end of file diff --git "a/LeetCode-Python/0073.\347\237\251\351\230\265\347\275\256\351\233\266/0073-\347\237\251\351\230\265\347\275\256\351\233\266.py" "b/LeetCode-Python/0073.\347\237\251\351\230\265\347\275\256\351\233\266/0073-\347\237\251\351\230\265\347\275\256\351\233\266.py" new file mode 100644 index 0000000..cffc270 --- /dev/null +++ "b/LeetCode-Python/0073.\347\237\251\351\230\265\347\275\256\351\233\266/0073-\347\237\251\351\230\265\347\275\256\351\233\266.py" @@ -0,0 +1,26 @@ +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + if not matrix or not matrix[0]: + return matrix + + m, n = len(matrix), len(matrix[0]) + + for i in range(m): + for j in range(n): + if matrix[i][j] == 0: + # set row zero + for zj in range(n): + if matrix[i][zj] != 0: + matrix[i][zj] = "ZERO" + for zi in range(m): + if matrix[zi][j] != 0: + matrix[zi][j] = "ZERO" + + for i in range(m): + for j in range(n): + if matrix[i][j] == "ZERO": + matrix[i][j] = 0 + \ No newline at end of file diff --git "a/LeetCode-Python/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206.py" "b/LeetCode-Python/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206.py" new file mode 100644 index 0000000..52471ac --- /dev/null +++ "b/LeetCode-Python/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206.py" @@ -0,0 +1,9 @@ +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + res = [[]] + for num in nums: + new_res = [] + for subset in res: + new_res.append(subset + [num]) + res += new_res + return res \ No newline at end of file diff --git "a/LeetCode-Python/0080.\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\271II/0080-\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\271II.py" "b/LeetCode-Python/0080.\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\271II/0080-\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\271II.py" new file mode 100644 index 0000000..b9415ef --- /dev/null +++ "b/LeetCode-Python/0080.\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\271II/0080-\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\271II.py" @@ -0,0 +1,17 @@ +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + prev, prev_count = nums[0], 1 + res = 1 + for num in nums[1:]: + if num == prev: + if prev_count == 1: + nums[res] = num + res += 1 + prev_count += 1 + else: + nums[res] = num + res += 1 + prev = num + prev_count = 1 + return res + \ No newline at end of file diff --git "a/LeetCode-Python/0082.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II/0082-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II.py" "b/LeetCode-Python/0082.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II/0082-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II.py" new file mode 100644 index 0000000..598ced5 --- /dev/null +++ "b/LeetCode-Python/0082.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II/0082-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240II.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 deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(-1) + dummy.next = head + prev, cur = dummy, head + + while cur: + tail = cur.next + if cur and tail and cur.val == tail.val: + while cur.next and cur.val == cur.next.val: + tail = cur.next.next + cur = cur.next + + prev.next = tail + cur = tail + else: + prev, cur = cur, tail + + return dummy.next \ No newline at end of file diff --git "a/LeetCode-Python/0083.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/0083-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.py" "b/LeetCode-Python/0083.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/0083-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.py" new file mode 100644 index 0000000..fe7b80f --- /dev/null +++ "b/LeetCode-Python/0083.\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240/0083-\345\210\240\351\231\244\346\216\222\345\272\217\351\223\276\350\241\250\344\270\255\347\232\204\351\207\215\345\244\215\345\205\203\347\264\240.py" @@ -0,0 +1,18 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + if not head: + return head + left, right = head, head + while right: + if left.val != right.val: + left = left.next + left.val = right.val + right = right.next + + left.next = None + return head \ No newline at end of file diff --git "a/LeetCode-Python/0088.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/0088-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.py" "b/LeetCode-Python/0088.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/0088-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.py" new file mode 100644 index 0000000..60f3603 --- /dev/null +++ "b/LeetCode-Python/0088.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204/0088-\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.py" @@ -0,0 +1,21 @@ +class Solution: + def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: + """ + Do not return anything, modify nums1 in-place instead. + """ + p1, p2 = m - 1, n - 1 + index = m + n - 1 + while p1 >= 0 and p2 >= 0: + if nums1[p1] < nums2[p2]: + nums1[index] = nums2[p2] + p2 -= 1 + else: + nums1[index] = nums1[p1] + p1 -= 1 + index -= 1 + + while p2 >= 0: + nums1[index] = nums2[p2] + p2 -= 1 + index -= 1 + diff --git "a/LeetCode-Python/0092.\345\217\215\350\275\254\351\223\276\350\241\250II/0092-\345\217\215\350\275\254\351\223\276\350\241\250II.py" "b/LeetCode-Python/0092.\345\217\215\350\275\254\351\223\276\350\241\250II/0092-\345\217\215\350\275\254\351\223\276\350\241\250II.py" new file mode 100644 index 0000000..f16cdaa --- /dev/null +++ "b/LeetCode-Python/0092.\345\217\215\350\275\254\351\223\276\350\241\250II/0092-\345\217\215\350\275\254\351\223\276\350\241\250II.py" @@ -0,0 +1,40 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]: + dummy = ListNode(-501) + dummy.next = head + p = dummy.next + before_left = None + tail = None + prev, cur, count = dummy, p, 0 + left_node, right_node = None, None + while prev and cur: + count += 1 + if count == left: + before_left = prev + left_node = cur + if count == right: + tail = cur.next + right_node = cur + right_node.next = None + break + prev = cur + cur = cur.next + + self.reverse(left_node) + before_left.next = right_node + left_node.next = tail + return dummy.next + + def reverse(self, node): + if not node or not node.next: + return node + + tmp = self.reverse(node.next) + node.next.next = node + node.next = None + return tmp \ No newline at end of file diff --git "a/LeetCode-Python/0094.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/0094-\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206.py" "b/LeetCode-Python/0094.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/0094-\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..0c5549f --- /dev/null +++ "b/LeetCode-Python/0094.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/0094-\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,25 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# class Solution: +# def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: +# if not root: +# return [] +# return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) + +class Solution: + def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + stack = [root] + res = [] + while stack: + node = stack.pop() + if isinstance(node, TreeNode): + stack.append(node.right) + stack.append(node.val) + stack.append(node.left) + elif isinstance(node, int): + res.append(node) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221.py" "b/LeetCode-Python/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221.py" new file mode 100644 index 0000000..ac20078 --- /dev/null +++ "b/LeetCode-Python/0100.\347\233\270\345\220\214\347\232\204\346\240\221/0100-\347\233\270\345\220\214\347\232\204\346\240\221.py" @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: + if not p and not q: + return True + if not p and q: + return False + + if p and not q: + return False + + return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) \ No newline at end of file diff --git "a/LeetCode-Python/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221/0101-\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.py" "b/LeetCode-Python/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221/0101-\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..53d983a --- /dev/null +++ "b/LeetCode-Python/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221/0101-\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,35 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# class Solution: +# def isSymmetric(self, root: Optional[TreeNode]) -> bool: +# queue = [root] +# while queue: +# next_queue = [] +# level_val = [] +# for node in queue: +# if node: +# next_queue.append(node.left) +# next_queue.append(node.right) +# level_val.append(node.val) +# else: +# level_val.append("n") + +# if level_val != level_val[::-1]: +# return False +# queue = next_queue[:] +# return True + +class Solution: + def isSymmetric(self, root: Optional[TreeNode]) -> bool: + def helper(left:Optional[TreeNode], right: Optional[TreeNode]): + if not left: + return not right + if not right: + return not left + return left.val == right.val and helper(left.left, right.right) and helper(left.right, right.left) + + return helper(root.left, root.right) diff --git "a/LeetCode-Python/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" "b/LeetCode-Python/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..204ca41 --- /dev/null +++ "b/LeetCode-Python/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + res.append(cur_level) + queue = next_queue + return res \ No newline at end of file diff --git "a/LeetCode-Python/0103.\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206/0103-\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206.py" "b/LeetCode-Python/0103.\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206/0103-\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..6e4ed82 --- /dev/null +++ "b/LeetCode-Python/0103.\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206/0103-\344\272\214\345\217\211\346\240\221\347\232\204\351\224\257\351\275\277\345\275\242\345\261\202\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + queue = [root] + res = [] + flag = 1 + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + if flag: + res.append(cur_level) + flag = 0 + else: + flag = 1 + res.append(cur_level[::-1]) + queue = next_queue + return res \ No newline at end of file diff --git "a/LeetCode-Python/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246/0104-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.py" "b/LeetCode-Python/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246/0104-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.py" new file mode 100644 index 0000000..d544997 --- /dev/null +++ "b/LeetCode-Python/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246/0104-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# class Solution: +# def maxDepth(self, root: Optional[TreeNode]) -> int: +# if not root: +# return 0 +# return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + queue = [root] + depth = 0 + while queue: + next_queue = [] + depth += 1 + for node in queue: + if node.left: + next_queue.append(node.left) + if node.right: + next_queue.append(node.right) + queue = next_queue[:] + return depth \ No newline at end of file diff --git "a/LeetCode-Python/0105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0105-\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" "b/LeetCode-Python/0105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0105-\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..42c8031 --- /dev/null +++ "b/LeetCode-Python/0105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0105-\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + if not preorder: + return None + + root_val = preorder[0] + root_index_inorder = inorder.index(root_val) + + left_inorder = inorder[:root_index_inorder] + right_inorder = inorder[root_index_inorder + 1:] + + left_preorder = preorder[1:1 + len(left_inorder)] + right_preorder = preorder[1 + len(left_inorder):] + + return TreeNode(root_val, self.buildTree(left_preorder, left_inorder), self.buildTree(right_preorder, right_inorder)) \ No newline at end of file diff --git "a/LeetCode-Python/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0106-\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" "b/LeetCode-Python/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0106-\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..d5f5415 --- /dev/null +++ "b/LeetCode-Python/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/0106-\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,22 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: + if not inorder: + return None + + root_val = postorder[-1] + root_index_inorder = inorder.index(root_val) + + left_inorder = inorder[:root_index_inorder] + right_inorder = inorder[root_index_inorder + 1:] + + left_postorder = postorder[:len(left_inorder)] + right_postorder = postorder[len(left_inorder): -1] + + root = TreeNode(root_val, self.buildTree(left_inorder, left_postorder), self.buildTree(right_inorder, right_postorder)) + return root \ No newline at end of file diff --git "a/LeetCode-Python/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II.py" "b/LeetCode-Python/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II.py" new file mode 100644 index 0000000..e2a4c93 --- /dev/null +++ "b/LeetCode-Python/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206II.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + res.append(cur_level) + queue = next_queue + return res[::-1] \ No newline at end of file diff --git "a/LeetCode-Python/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0108-\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/LeetCode-Python/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0108-\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..9da1a22 --- /dev/null +++ "b/LeetCode-Python/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0108-\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: + if not nums: + return None + + # if len(nums) == 1: + # return TreeNode(nums[0]) + + index = len(nums) // 2 + root = TreeNode(nums[index], self.sortedArrayToBST(nums[:index]), self.sortedArrayToBST(nums[index + 1:])) + return root \ No newline at end of file diff --git "a/LeetCode-Python/0109.\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0109-\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/LeetCode-Python/0109.\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0109-\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..633975f --- /dev/null +++ "b/LeetCode-Python/0109.\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0109-\346\234\211\345\272\217\351\223\276\350\241\250\350\275\254\346\215\242\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,29 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]: + p = head + array = [] + while p: + array.append(p.val) + p = p.next + + return self.sortedArrayToBST(array) + + + def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: + if not nums: + return None + + index = len(nums) // 2 + root = TreeNode(nums[index], self.sortedArrayToBST(nums[:index]), self.sortedArrayToBST(nums[index + 1:])) + return root \ No newline at end of file diff --git "a/LeetCode-Python/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246/0111-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.py" "b/LeetCode-Python/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246/0111-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.py" new file mode 100644 index 0000000..d35dfa3 --- /dev/null +++ "b/LeetCode-Python/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246/0111-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + self.res = float("inf") + + def dfs(node, cur_depth): + if not node: + return + cur_depth += 1 + if not node.left and not node.right: + self.res = min(cur_depth, self.res) + dfs(node.left, cur_depth) + dfs(node.right, cur_depth) + + dfs(root, 0) + return self.res + + + \ No newline at end of file diff --git "a/LeetCode-Python/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214.py" "b/LeetCode-Python/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214.py" new file mode 100644 index 0000000..ae64136 --- /dev/null +++ "b/LeetCode-Python/0112.\350\267\257\345\276\204\346\200\273\345\222\214/0112-\350\267\257\345\276\204\346\200\273\345\222\214.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: + self.res = False + def dfs(node, path_sum): + if not node: + return + + path_sum += node.val + if targetSum == path_sum and (not node.left and not node.right): + self.res = True + + if not self.res: + dfs(node.left, path_sum) + dfs(node.right, path_sum) + + dfs(root, 0) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II.py" "b/LeetCode-Python/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II.py" new file mode 100644 index 0000000..3bfb413 --- /dev/null +++ "b/LeetCode-Python/0113.\350\267\257\345\276\204\346\200\273\345\222\214II/0113-\350\267\257\345\276\204\346\200\273\345\222\214II.py" @@ -0,0 +1,47 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# class Solution: +# def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: +# self.res = [] +# def dfs(node, path_sum, path): +# if not node: +# return + +# path_sum += node.val +# path.append(node.val) +# if targetSum == path_sum and (not node.left and not node.right): +# self.res.append(path[:]) + +# dfs(node.left, path_sum, path[:]) +# dfs(node.right, path_sum, path[:]) + +# dfs(root, 0, []) +# return self.res + +from collections import deque +class Solution: + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: + if not root: + return [] + + res = [] + queue = deque([(root, root.val, [root.val])]) # node, cur_path_sum, cur_path + + while queue: + node, cur_path_sum, cur_path = queue.popleft() + # print(cur_path_sum, cur_path, not node.left, not node.right) + if not node.left and not node.right and cur_path_sum == targetSum: + res.append(cur_path[:]) + continue + + if node.left: + queue.append((node.left, cur_path_sum + node.left.val, cur_path + [node.left.val])) + + if node.right: + queue.append((node.right, cur_path_sum + node.right.val, cur_path + [node.right.val])) + + return res \ No newline at end of file diff --git "a/LeetCode-Python/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250.py" "b/LeetCode-Python/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250.py" new file mode 100644 index 0000000..a5c4870 --- /dev/null +++ "b/LeetCode-Python/0114.\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250/0114-\344\272\214\345\217\211\346\240\221\345\261\225\345\274\200\344\270\272\351\223\276\350\241\250.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def flatten(self, root: Optional[TreeNode]) -> None: + """ + Do not return anything, modify root in-place instead. + """ + if not root: + return None + + left = root.left + right = root.right + + self.flatten(left) + root.right = left + root.left = None + + p = root + while p.right: + p = p.right + + self.flatten(right) + p.right = right + diff --git "a/LeetCode-Python/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/0116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.py" "b/LeetCode-Python/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/0116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.py" new file mode 100644 index 0000000..28a993b --- /dev/null +++ "b/LeetCode-Python/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210/0116-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.py" @@ -0,0 +1,31 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): + self.val = val + self.left = left + self.right = right + self.next = next +""" + +class Solution: + def connect(self, root: 'Optional[Node]') -> 'Optional[Node]': + if not root: + return root + + def dfs(node): + if not node: + return + + if node.left: + node.left.next = node.right + if node.next: + node.right.next = node.next.left + else: + node.right.next = None + + dfs(node.left) + dfs(node.right) + + dfs(root) + return root \ No newline at end of file diff --git "a/LeetCode-Python/0117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/0117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II.py" "b/LeetCode-Python/0117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/0117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II.py" new file mode 100644 index 0000000..ab10600 --- /dev/null +++ "b/LeetCode-Python/0117.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II/0117-\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210II.py" @@ -0,0 +1,46 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): + self.val = val + self.left = left + self.right = right + self.next = next +""" + +class Solution: + def connect(self, root: 'Node') -> 'Node': + if not root: + return root + + def findCousin(node, parent): + parent_nxt = parent.next + while parent_nxt: + if parent_nxt.left: + node.next = parent_nxt.left + return + + if parent_nxt.right: + node.next = parent_nxt.right + return + + parent_nxt = parent_nxt.next + + def dfs(node): + if not node: + return node + + if node.right and node.left: + node.left.next = node.right + findCousin(node.right, node) + elif node.left: + findCousin(node.left, node) + elif node.right: + findCousin(node.right, node) + + dfs(node.right) + dfs(node.left) + + + dfs(root) + return root \ No newline at end of file diff --git "a/LeetCode-Python/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/0121-\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.py" "b/LeetCode-Python/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/0121-\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.py" new file mode 100644 index 0000000..0443ae0 --- /dev/null +++ "b/LeetCode-Python/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/0121-\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.py" @@ -0,0 +1,9 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + prev_min = None + res = 0 + for price in prices: + if prev_min is None or prev_min > price: + prev_min = price + res = max(res, price - prev_min) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/0125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.py" "b/LeetCode-Python/0125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/0125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.py" new file mode 100644 index 0000000..feca307 --- /dev/null +++ "b/LeetCode-Python/0125.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262/0125-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262.py" @@ -0,0 +1,14 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + left, right = 0, len(s) - 1 + while left < right: + while left < right and (not s[left].isalpha() and not s[left].isdigit()): + left += 1 + + while left < right and (not s[right].isalpha() and not s[right].isdigit()): + right -= 1 + if s[left].upper() != s[right].upper(): + return False + left += 1 + right -= 1 + return True \ No newline at end of file diff --git "a/LeetCode-Python/0129.\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214/0129-\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.py" "b/LeetCode-Python/0129.\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214/0129-\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.py" new file mode 100644 index 0000000..8f580e1 --- /dev/null +++ "b/LeetCode-Python/0129.\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214/0129-\346\261\202\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -0,0 +1,22 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: Optional[TreeNode]) -> int: + self.res = 0 + def dfs(node, cur_sum): + if not node: + return + + cur_sum = cur_sum * 10 + node.val + if not node.left and not node.right: + self.res += cur_sum + else: + dfs(node.left, cur_sum) + dfs(node.right, cur_sum) + + dfs(root, 0) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276.py" "b/LeetCode-Python/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276.py" new file mode 100644 index 0000000..1802092 --- /dev/null +++ "b/LeetCode-Python/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276.py" @@ -0,0 +1,43 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, val = 0, neighbors = None): + self.val = val + self.neighbors = neighbors if neighbors is not None else [] +""" + +from collections import deque +class Solution: + def cloneGraph(self, node: 'Node') -> 'Node': + if not node: + return node + + old2new = dict() + queue = deque([node]) + visited = set([node]) + while queue: + cur_node = queue.popleft() + new_node = Node(cur_node.val) + old2new[cur_node] = new_node + + for neighbor in cur_node.neighbors: + if neighbor not in visited: + visited.add(neighbor) + queue.append(neighbor) + + queue = deque([node]) + visited = set([node]) + while queue: + cur_node = queue.popleft() + new_node = old2new[cur_node] + + for neighbor in cur_node.neighbors: + new_neighbor = old2new[neighbor] + new_node.neighbors.append(new_neighbor) + + if neighbor not in visited: + visited.add(neighbor) + queue.append(neighbor) + return old2new[node] + + \ No newline at end of file diff --git "a/LeetCode-Python/0138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/0138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250.py" "b/LeetCode-Python/0138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/0138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250.py" new file mode 100644 index 0000000..79dbbff --- /dev/null +++ "b/LeetCode-Python/0138.\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250/0138-\345\244\215\345\210\266\345\270\246\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\351\223\276\350\241\250.py" @@ -0,0 +1,33 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): + self.val = int(x) + self.next = next + self.random = random +""" + +class Solution: + def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]': + if not head: + return head + + old2new = dict() + p = head + while p: + cur_node = p + new_node = Node(cur_node.val) + old2new[cur_node] = new_node + p = p.next + + p = head + while p: + cur_node = p + new_node = old2new[cur_node] + + if cur_node.next: + new_node.next = old2new[cur_node.next] + if cur_node.random: + new_node.random = old2new[cur_node.random] + p = p.next + return old2new[head] \ No newline at end of file diff --git "a/LeetCode-Python/0141.\347\216\257\345\275\242\351\223\276\350\241\250/0141-\347\216\257\345\275\242\351\223\276\350\241\250.py" "b/LeetCode-Python/0141.\347\216\257\345\275\242\351\223\276\350\241\250/0141-\347\216\257\345\275\242\351\223\276\350\241\250.py" new file mode 100644 index 0000000..f817d92 --- /dev/null +++ "b/LeetCode-Python/0141.\347\216\257\345\275\242\351\223\276\350\241\250/0141-\347\216\257\345\275\242\351\223\276\350\241\250.py" @@ -0,0 +1,18 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + fast, slow = head, head + + while fast and fast.next: + fast = fast.next.next + slow = slow.next + + if fast == slow: + return True + + return False \ No newline at end of file diff --git "a/LeetCode-Python/0142.\347\216\257\345\275\242\351\223\276\350\241\250II/0142-\347\216\257\345\275\242\351\223\276\350\241\250II.py" "b/LeetCode-Python/0142.\347\216\257\345\275\242\351\223\276\350\241\250II/0142-\347\216\257\345\275\242\351\223\276\350\241\250II.py" new file mode 100644 index 0000000..2e0c9fd --- /dev/null +++ "b/LeetCode-Python/0142.\347\216\257\345\275\242\351\223\276\350\241\250II/0142-\347\216\257\345\275\242\351\223\276\350\241\250II.py" @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + fast, slow = head, head + has_loop = False + while fast and fast.next: + fast = fast.next.next + slow = slow.next + + if fast == slow: + has_loop = True + break + if not has_loop: + return None + + fast = head + while 1: + if fast == slow: + return fast + fast = fast.next + slow = slow.next diff --git "a/LeetCode-Python/0144.\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0144-\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.py" "b/LeetCode-Python/0144.\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0144-\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..fd8165e --- /dev/null +++ "b/LeetCode-Python/0144.\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0144-\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# class Solution: +# def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: +# if not root: +# return [] +# return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) + +class Solution: + def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + stack = [root] + res = [] + while stack: + node = stack.pop() + if isinstance(node, TreeNode): + stack.extend([node.right, node.left, node.val]) + elif isinstance(node, int): + res.append(node) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0145.\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0145-\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.py" "b/LeetCode-Python/0145.\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0145-\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..977b47b --- /dev/null +++ "b/LeetCode-Python/0145.\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0145-\344\272\214\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# class Solution: +# def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: +# if not root: +# return [] +# return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] + +class Solution: + def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + stack = [root] + res = [] + while stack: + node = stack.pop() + if isinstance(node, TreeNode): + stack.extend([node.val, node.right, node.left]) + elif isinstance(node, int): + res.append(node) + + return res \ No newline at end of file diff --git "a/LeetCode-Python/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/0150-\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.py" "b/LeetCode-Python/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/0150-\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.py" new file mode 100644 index 0000000..c2ab564 --- /dev/null +++ "b/LeetCode-Python/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/0150-\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.py" @@ -0,0 +1,19 @@ +class Solution: + def evalRPN(self, tokens: List[str]) -> int: + stack = [] + for token in tokens: + if token in "+-*/": + second = stack.pop() + first = stack.pop() + if token == "+": + stack.append(second + first) + elif token == "-": + stack.append(first - second) + elif token == "*": + stack.append(first * second) + else: + stack.append(int(first / second)) + else: + stack.append(int(token)) + return stack[0] + \ No newline at end of file diff --git "a/LeetCode-Python/0151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215/0151-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215.py" "b/LeetCode-Python/0151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215/0151-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215.py" new file mode 100644 index 0000000..6b07361 --- /dev/null +++ "b/LeetCode-Python/0151.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215/0151-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215.py" @@ -0,0 +1,3 @@ +class Solution: + def reverseWords(self, s: str) -> str: + return " ".join(s.split()[::-1]) \ No newline at end of file diff --git "a/LeetCode-Python/0155.\346\234\200\345\260\217\346\240\210/0155-\346\234\200\345\260\217\346\240\210.py" "b/LeetCode-Python/0155.\346\234\200\345\260\217\346\240\210/0155-\346\234\200\345\260\217\346\240\210.py" new file mode 100644 index 0000000..023718b --- /dev/null +++ "b/LeetCode-Python/0155.\346\234\200\345\260\217\346\240\210/0155-\346\234\200\345\260\217\346\240\210.py" @@ -0,0 +1,30 @@ +class MinStack: + + def __init__(self): + self.min_stack = [] + self.stack = [] + + def push(self, val: int) -> None: + self.stack.append(val) + if self.min_stack and self.min_stack[-1] < val: + self.min_stack.append(self.min_stack[-1]) + else: + self.min_stack.append(val) + + def pop(self) -> None: + self.stack.pop() + self.min_stack.pop() + + def top(self) -> int: + return self.stack[-1] + + def getMin(self) -> int: + return self.min_stack[-1] + + +# Your MinStack object will be instantiated and called as such: +# obj = MinStack() +# obj.push(val) +# obj.pop() +# param_3 = obj.top() +# param_4 = obj.getMin() \ No newline at end of file diff --git "a/LeetCode-Python/0160.\347\233\270\344\272\244\351\223\276\350\241\250/0160-\347\233\270\344\272\244\351\223\276\350\241\250.py" "b/LeetCode-Python/0160.\347\233\270\344\272\244\351\223\276\350\241\250/0160-\347\233\270\344\272\244\351\223\276\350\241\250.py" new file mode 100644 index 0000000..1e17d1e --- /dev/null +++ "b/LeetCode-Python/0160.\347\233\270\344\272\244\351\223\276\350\241\250/0160-\347\233\270\344\272\244\351\223\276\350\241\250.py" @@ -0,0 +1,37 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + la, lb = 0, 0 + p = headA + while p: + la += 1 + p = p.next + + p = headB + while p: + lb += 1 + p = p.next + + if la > lb: + headA, headB = headB, headA + la, lb = lb, la + + diff = lb - la + pa, pb = headA, headB + while diff: + pb = pb.next + diff -= 1 + + while pa and pb: + if pa == pb: + return pa + else: + pa = pa.next + pb = pb.next + + return None diff --git "a/LeetCode-Python/0169.\345\244\232\346\225\260\345\205\203\347\264\240/0169-\345\244\232\346\225\260\345\205\203\347\264\240.py" "b/LeetCode-Python/0169.\345\244\232\346\225\260\345\205\203\347\264\240/0169-\345\244\232\346\225\260\345\205\203\347\264\240.py" new file mode 100644 index 0000000..46b0d98 --- /dev/null +++ "b/LeetCode-Python/0169.\345\244\232\346\225\260\345\205\203\347\264\240/0169-\345\244\232\346\225\260\345\205\203\347\264\240.py" @@ -0,0 +1,16 @@ +class Solution: + def majorityElement(self, nums: List[int]) -> int: + mode, count = None, 0 + for num in nums: + if not mode: + mode = num + count = 1 + else: + if num != mode: + count -= 1 + if count == 0: + mode = num + count = 1 + else: + count += 1 + return mode \ No newline at end of file diff --git "a/LeetCode-Python/0189.\350\275\256\350\275\254\346\225\260\347\273\204/0189-\350\275\256\350\275\254\346\225\260\347\273\204.py" "b/LeetCode-Python/0189.\350\275\256\350\275\254\346\225\260\347\273\204/0189-\350\275\256\350\275\254\346\225\260\347\273\204.py" new file mode 100644 index 0000000..0ea399b --- /dev/null +++ "b/LeetCode-Python/0189.\350\275\256\350\275\254\346\225\260\347\273\204/0189-\350\275\256\350\275\254\346\225\260\347\273\204.py" @@ -0,0 +1,15 @@ +class Solution: + def rotate(self, nums: List[int], k: int) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def reverse(left, right): + while left < right: + nums[left], nums[right] = nums[right], nums[left] + left += 1 + right -= 1 + k = k % len(nums) + reverse(0, len(nums) - 1) + reverse(0, k - 1) + reverse(k, len(nums) - 1) + \ No newline at end of file diff --git "a/LeetCode-Python/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215.py" "b/LeetCode-Python/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215.py" new file mode 100644 index 0000000..9e6de15 --- /dev/null +++ "b/LeetCode-Python/0198.\346\211\223\345\256\266\345\212\253\350\210\215/0198-\346\211\223\345\256\266\345\212\253\350\210\215.py" @@ -0,0 +1,26 @@ +class Solution: + def rob(self, nums: List[int]) -> int: + # res = 0 + # n = len(nums) + # memo = dict() + # def dfs(n): + # if n < 0: + # return 0 + # if n in memo: + # return memo[n] + # # f(n) = max(f(n - 1), f(n - 2) + nums[n]) + # res = max(dfs(n - 1), dfs(n - 2) + nums[n]) + # memo[n] = res + # return res + # return dfs(n - 1) + res = 0 + n = len(nums) + # dp[n] = max(dp[n - 1], dp[n - 2] + nums[n]) + # dp[n + 2] = max(dp[n + 1], dp[n] + nums[n]) + # f(2) = max(f1, f0 + nums[n]) + f0, f1 = 0, 0 + for i, num in enumerate(nums): + new_f = max(f1, f0 + num) + f0 = f1 + f1 = new_f + return f1 \ No newline at end of file diff --git "a/LeetCode-Python/0199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/0199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276.py" "b/LeetCode-Python/0199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/0199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276.py" new file mode 100644 index 0000000..017550d --- /dev/null +++ "b/LeetCode-Python/0199.\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276/0199-\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def rightSideView(self, root: Optional[TreeNode]) -> List[int]: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + cur_level.append(node.val) + + if node.left: + next_queue.append(node.left) + if node.right: + next_queue.append(node.right) + + if cur_level: + res.append(cur_level[-1]) + queue = next_queue + + return res \ No newline at end of file diff --git "a/LeetCode-Python/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260.py" "b/LeetCode-Python/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260.py" new file mode 100644 index 0000000..bd5d960 --- /dev/null +++ "b/LeetCode-Python/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260.py" @@ -0,0 +1,10 @@ +class Solution: + def isHappy(self, n: int) -> bool: + visited = set() + while 1: + if n == 1: + return True + visited.add(n) + n = sum([int(digit) ** 2 for digit in str(n)]) + if n in visited: + return False diff --git "a/LeetCode-Python/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262/0205-\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262/0205-\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..17a7584 --- /dev/null +++ "b/LeetCode-Python/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262/0205-\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,20 @@ +class Solution: + def isIsomorphic(self, s: str, t: str) -> bool: + char2word = dict() + word2char = dict() + + for i in range(len(s)): + char, word = s[i], t[i] + if char not in char2word: + char2word[char] = word + else: + if char2word[char] != word: + return False + + if word not in word2char: + word2char[word] = char + else: + if word2char[word] != char: + return False + + return True \ No newline at end of file diff --git "a/LeetCode-Python/0213.\346\211\223\345\256\266\345\212\253\350\210\215II/0213-\346\211\223\345\256\266\345\212\253\350\210\215II.py" "b/LeetCode-Python/0213.\346\211\223\345\256\266\345\212\253\350\210\215II/0213-\346\211\223\345\256\266\345\212\253\350\210\215II.py" new file mode 100644 index 0000000..e025995 --- /dev/null +++ "b/LeetCode-Python/0213.\346\211\223\345\256\266\345\212\253\350\210\215II/0213-\346\211\223\345\256\266\345\212\253\350\210\215II.py" @@ -0,0 +1,19 @@ +class Solution: + def rob(self, nums: List[int]) -> int: + # dp[i][0/1] 0 represents room1 was safe + n = len(nums) + if len(nums) <= 2: + return max(nums) + dp = [[0, 0]for _ in range(n)] + dp[0][1] = nums[0] + dp[1][1] = nums[0] + dp[1][0] = nums[1] + for i in range(2, n): + num = nums[i] + if i != n - 1: + dp[i][0] = max(dp[i - 2][0] + num, dp[i - 1][0]) + dp[i][1] = max(dp[i - 2][1] + num, dp[i - 1][1]) + else: + dp[i][0] = max(dp[i - 2][0] + num, dp[i - 1][0]) + dp[i][1] = max(dp[i - 2][1], dp[i - 1][1]) + return max(dp[n - 1][0], dp[n - 1][1]) \ No newline at end of file diff --git "a/LeetCode-Python/0215.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240/0215-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.py" "b/LeetCode-Python/0215.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240/0215-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.py" new file mode 100644 index 0000000..026133a --- /dev/null +++ "b/LeetCode-Python/0215.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240/0215-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.py" @@ -0,0 +1,15 @@ +class Solution: + def findKthLargest(self, nums: List[int], k: int) -> int: + bucket = [0 for _ in range(2 * (10 ** 4) + 1)] + + for num in nums: + bucket[num + 10 ** 4] += 1 + + cur = 0 + for index, count in enumerate(bucket): + if count: + # print(index, count) + cur += count + if cur >= (len(nums) - k + 1): + return index - 10 ** 4 + \ No newline at end of file diff --git "a/LeetCode-Python/0219.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II/0219-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II.py" "b/LeetCode-Python/0219.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II/0219-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II.py" new file mode 100644 index 0000000..da1425f --- /dev/null +++ "b/LeetCode-Python/0219.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II/0219-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240II.py" @@ -0,0 +1,24 @@ +class Solution: + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: + # sliding window + from collections import defaultdict + k = min(k, len(nums)) + left, right = 0, k + 1 + num2freq = defaultdict(int) + + for index in range(left, right): + if index < len(nums): + num = nums[index] + num2freq[num] += 1 + if num2freq[num] > 1: + return True + + while right < len(nums): + # [left, right) + num2freq[nums[left]] -= 1 + num2freq[nums[right]] += 1 + if num2freq[nums[right]] > 1: + return True + right += 1 + left += 1 + return False \ No newline at end of file diff --git "a/LeetCode-Python/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.py" "b/LeetCode-Python/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.py" new file mode 100644 index 0000000..b723ccc --- /dev/null +++ "b/LeetCode-Python/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260/0222-\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.py" @@ -0,0 +1,14 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def countNodes(self, root: Optional[TreeNode]) -> int: + def inorder(node): + if not node: + return 0 + + return 1 + inorder(node.left) + inorder(node.right) + return inorder(root) \ No newline at end of file diff --git "a/LeetCode-Python/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0226-\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" "b/LeetCode-Python/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0226-\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..4fe6533 --- /dev/null +++ "b/LeetCode-Python/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0226-\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,14 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if not root: + return root + root.left, root.right = root.right, root.left + self.invertTree(root.left) + self.invertTree(root.right) + return root \ No newline at end of file diff --git "a/LeetCode-Python/0228.\346\261\207\346\200\273\345\214\272\351\227\264/0228-\346\261\207\346\200\273\345\214\272\351\227\264.py" "b/LeetCode-Python/0228.\346\261\207\346\200\273\345\214\272\351\227\264/0228-\346\261\207\346\200\273\345\214\272\351\227\264.py" new file mode 100644 index 0000000..675f568 --- /dev/null +++ "b/LeetCode-Python/0228.\346\261\207\346\200\273\345\214\272\351\227\264/0228-\346\261\207\346\200\273\345\214\272\351\227\264.py" @@ -0,0 +1,24 @@ +class Solution: + def summaryRanges(self, nums: List[int]) -> List[str]: + if not nums: + return [] + res = [] + left, right = None, None + for i, num in enumerate(nums): + if i == 0: + left = num + else: + if nums[i - 1] != num - 1: + right = nums[i - 1] + if left != right: + res.append(str(left) + "->" + str(right)) + else: + res.append(str(left)) + left = num + + if left != num: + res.append(str(left) + "->" + str(num)) + else: + res.append(str(num)) + return res + diff --git "a/LeetCode-Python/0230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/0230-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.py" "b/LeetCode-Python/0230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/0230-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.py" new file mode 100644 index 0000000..39a305e --- /dev/null +++ "b/LeetCode-Python/0230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/0230-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.py" @@ -0,0 +1,22 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: + self.count = 0 + self.res = 0 + def inorder(node): + if not node: + return + + inorder(node.left) + self.count += 1 + if self.count == k: + self.res = node.val + inorder(node.right) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/0231.2\347\232\204\345\271\202/0231-2\347\232\204\345\271\202.py" "b/LeetCode-Python/0231.2\347\232\204\345\271\202/0231-2\347\232\204\345\271\202.py" new file mode 100644 index 0000000..829fec0 --- /dev/null +++ "b/LeetCode-Python/0231.2\347\232\204\345\271\202/0231-2\347\232\204\345\271\202.py" @@ -0,0 +1,3 @@ +class Solution: + def isPowerOfTwo(self, n: int) -> bool: + return bin(n)[2:].count("1") == 1 if n > 0 else False \ No newline at end of file diff --git "a/LeetCode-Python/0237.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/0237-\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.py" "b/LeetCode-Python/0237.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/0237-\345\210\240\351\231\244\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..f2cc537 --- /dev/null +++ "b/LeetCode-Python/0237.\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/0237-\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.py" @@ -0,0 +1,20 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def deleteNode(self, node): + """ + :type node: ListNode + :rtype: void Do not return anything, modify node in-place instead. + """ + p = node + while p and p.next: + p.val = p.next.val + if not p.next.next: + p.next = None + break + p = p.next + \ No newline at end of file diff --git "a/LeetCode-Python/0238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/0238-\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257.py" "b/LeetCode-Python/0238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/0238-\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257.py" new file mode 100644 index 0000000..fd5f8a4 --- /dev/null +++ "b/LeetCode-Python/0238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/0238-\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257.py" @@ -0,0 +1,15 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + prefix_multip = [1] + [1 for _ in nums] # [1, 1, 2, 6, 24] + postfix_multip = [1 for _ in nums] + [1] # [24,12,4, 1, 1] + + for i in range(len(nums)): + prefix_multip[i + 1] = prefix_multip[i] * nums[i] + + for i in range(len(nums) - 2, -1, -1): + postfix_multip[i] = nums[i + 1] * postfix_multip[i + 1] + + res = [] + for i in range(len(nums)): + res.append(prefix_multip[i] * postfix_multip[i]) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/0242-\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.py" "b/LeetCode-Python/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/0242-\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.py" new file mode 100644 index 0000000..77d90b7 --- /dev/null +++ "b/LeetCode-Python/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215/0242-\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.py" @@ -0,0 +1,4 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + from collections import Counter + return Counter(s) == Counter(t) \ No newline at end of file diff --git "a/LeetCode-Python/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204/0257-\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.py" "b/LeetCode-Python/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204/0257-\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.py" new file mode 100644 index 0000000..00dd9b1 --- /dev/null +++ "b/LeetCode-Python/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204/0257-\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.py" @@ -0,0 +1,25 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]: + if not root: + return [] + + self.res = [] + def dfs(node, path): + if not node: + return + + path.append(str(node.val)) + if not node.left and not node.right: + self.res.append("->".join(path)) + + dfs(node.left, path[:]) + dfs(node.right, path[:]) + + dfs(root, []) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/0258.\345\220\204\344\275\215\347\233\270\345\212\240/0258-\345\220\204\344\275\215\347\233\270\345\212\240.py" "b/LeetCode-Python/0258.\345\220\204\344\275\215\347\233\270\345\212\240/0258-\345\220\204\344\275\215\347\233\270\345\212\240.py" new file mode 100644 index 0000000..60b196a --- /dev/null +++ "b/LeetCode-Python/0258.\345\220\204\344\275\215\347\233\270\345\212\240/0258-\345\220\204\344\275\215\347\233\270\345\212\240.py" @@ -0,0 +1,9 @@ +class Solution: + def addDigits(self, num: int) -> int: + while num > 9: + new_nums = 0 + while num: + num, n = divmod(num, 10) + new_nums += n + num = new_nums + return num \ No newline at end of file diff --git "a/LeetCode-Python/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.py" "b/LeetCode-Python/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.py" new file mode 100644 index 0000000..f84b4d3 --- /dev/null +++ "b/LeetCode-Python/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.py" @@ -0,0 +1,18 @@ +class Solution: + def nthUglyNumber(self, n: int) -> int: + import heapq + + min_heap = [1] # find the smallest ugly number + visited = set() + cnt = n + while cnt: + cur = heappop(min_heap) + cnt -= 1 + if not cnt: + return cur + for multiple in [2, 3, 5]: + nxt = cur * multiple + if nxt not in visited: + visited.add(nxt) + heappush(min_heap, nxt) + \ No newline at end of file diff --git "a/LeetCode-Python/0268.\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227/0268-\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227.py" "b/LeetCode-Python/0268.\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227/0268-\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..92349c3 --- /dev/null +++ "b/LeetCode-Python/0268.\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227/0268-\344\270\242\345\244\261\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,4 @@ +class Solution: + def missingNumber(self, nums: List[int]) -> int: + n = len(nums) + return (1 + n) * n // 2 - sum(nums) \ No newline at end of file diff --git "a/LeetCode-Python/0272.\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II/0272-\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II.py" "b/LeetCode-Python/0272.\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II/0272-\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II.py" new file mode 100644 index 0000000..ae57eaf --- /dev/null +++ "b/LeetCode-Python/0272.\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II/0272-\346\234\200\346\216\245\350\277\221\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\200\274II.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +from heapq import * +class Solution: + def closestKValues(self, root: Optional[TreeNode], target: float, k: int) -> List[int]: + max_heap = [] + + def inorder(node): + if not node: + return + + inorder(node.left) + if len(max_heap) < k: + heappush(max_heap, (-abs(node.val - target), node.val)) + else: + heappushpop(max_heap, (-abs(node.val - target), node.val)) + inorder(node.right) + + inorder(root) + return [val for _, val in max_heap] + + \ No newline at end of file diff --git "a/LeetCode-Python/0289.\347\224\237\345\221\275\346\270\270\346\210\217/0289-\347\224\237\345\221\275\346\270\270\346\210\217.py" "b/LeetCode-Python/0289.\347\224\237\345\221\275\346\270\270\346\210\217/0289-\347\224\237\345\221\275\346\270\270\346\210\217.py" new file mode 100644 index 0000000..d107fe9 --- /dev/null +++ "b/LeetCode-Python/0289.\347\224\237\345\221\275\346\270\270\346\210\217/0289-\347\224\237\345\221\275\346\270\270\346\210\217.py" @@ -0,0 +1,34 @@ +class Solution: + def gameOfLife(self, board: List[List[int]]) -> None: + """ + Do not return anything, modify board in-place instead. + """ + di = [1, 1, 1, -1, -1, -1, 0, 0] + dj = [1, 0, -1, 1, 0, -1, 1, -1] + + if not board or not board[0]: + return board + + m, n = len(board), len(board[0]) + dead_set = set() + live_set = set() + for i in range(m): + for j in range(n): + live_cell_count = 0 + for k in range(8): + ii, jj = i + di[k], j + dj[k] + if 0 <= ii < m and 0 <= jj < n and board[ii][jj] == 1: + live_cell_count += 1 + if board[i][j] == 1 and (live_cell_count < 2 or live_cell_count > 3): + dead_set.add((i, j)) + elif board[i][j] == 0 and live_cell_count == 3: + live_set.add((i, j)) + + for i in range(m): + for j in range(n): + if (i, j) in dead_set: + board[i][j] = 0 + elif (i, j) in live_set: + board[i][j] = 1 + return board + \ No newline at end of file diff --git "a/LeetCode-Python/0290.\345\215\225\350\257\215\350\247\204\345\276\213/0290-\345\215\225\350\257\215\350\247\204\345\276\213.py" "b/LeetCode-Python/0290.\345\215\225\350\257\215\350\247\204\345\276\213/0290-\345\215\225\350\257\215\350\247\204\345\276\213.py" new file mode 100644 index 0000000..74a9324 --- /dev/null +++ "b/LeetCode-Python/0290.\345\215\225\350\257\215\350\247\204\345\276\213/0290-\345\215\225\350\257\215\350\247\204\345\276\213.py" @@ -0,0 +1,24 @@ +class Solution: + def wordPattern(self, pattern: str, s: str) -> bool: + char2word = dict() + word2char = dict() + + words = [word for word in s.split()] + if len(words) != len(pattern): + return False + + for i in range(len(words)): + char, word = words[i], pattern[i] + if char not in char2word: + char2word[char] = word + else: + if char2word[char] != word: + return False + + if word not in word2char: + word2char[word] = char + else: + if word2char[word] != char: + return False + + return True \ No newline at end of file diff --git "a/LeetCode-Python/0292.Nim\346\270\270\346\210\217/0292-Nim\346\270\270\346\210\217.py" "b/LeetCode-Python/0292.Nim\346\270\270\346\210\217/0292-Nim\346\270\270\346\210\217.py" new file mode 100644 index 0000000..81e7126 --- /dev/null +++ "b/LeetCode-Python/0292.Nim\346\270\270\346\210\217/0292-Nim\346\270\270\346\210\217.py" @@ -0,0 +1,3 @@ +class Solution: + def canWinNim(self, n: int) -> bool: + return n % 4 != 0 \ No newline at end of file diff --git "a/LeetCode-Python/0313.\350\266\205\347\272\247\344\270\221\346\225\260/0313-\350\266\205\347\272\247\344\270\221\346\225\260.py" "b/LeetCode-Python/0313.\350\266\205\347\272\247\344\270\221\346\225\260/0313-\350\266\205\347\272\247\344\270\221\346\225\260.py" new file mode 100644 index 0000000..6fb1177 --- /dev/null +++ "b/LeetCode-Python/0313.\350\266\205\347\272\247\344\270\221\346\225\260/0313-\350\266\205\347\272\247\344\270\221\346\225\260.py" @@ -0,0 +1,16 @@ +class Solution: + def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int: + import heapq + min_heap = [1] # find the smallest ugly number + visited = set() + cnt = n + while cnt: + cur = heappop(min_heap) + cnt -= 1 + if not cnt: + return cur + for multiple in primes: + nxt = cur * multiple + if nxt not in visited: + visited.add(nxt) + heappush(min_heap, nxt) \ No newline at end of file diff --git "a/LeetCode-Python/0316.\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215/0316-\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215.py" "b/LeetCode-Python/0316.\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215/0316-\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215.py" new file mode 100644 index 0000000..0b190ed --- /dev/null +++ "b/LeetCode-Python/0316.\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215/0316-\345\216\273\351\231\244\351\207\215\345\244\215\345\255\227\346\257\215.py" @@ -0,0 +1,13 @@ +class Solution: + def removeDuplicateLetters(self, s: str) -> str: + from collections import Counter + c = Counter(s) + stack = [] # incresing stack + for char in s: + c[char] -= 1 + if char in stack: + continue + while stack and stack[-1] > char and c[stack[-1]] >= 1: + stack.pop() + stack.append(char) + return "".join(stack) \ No newline at end of file diff --git "a/LeetCode-Python/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/0344-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/0344-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..02d4896 --- /dev/null +++ "b/LeetCode-Python/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262/0344-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,11 @@ +class Solution: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + left, right = 0, len(s) - 1 + while left < right: + s[left], s[right] = s[right], s[left] + left += 1 + right -= 1 + \ No newline at end of file diff --git "a/LeetCode-Python/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/0349-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.py" "b/LeetCode-Python/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/0349-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.py" new file mode 100644 index 0000000..5b754c9 --- /dev/null +++ "b/LeetCode-Python/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/0349-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.py" @@ -0,0 +1,21 @@ +class Solution: + def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: + if len(nums1) > len(nums2): + nums1, nums2 = nums2, nums1 + + nums2.sort() + res = set() + for num in nums1: + if num not in res: + # search num in nums2 + left, right = 0, len(nums2) - 1 + while left <= right: + mid = (left + right) // 2 + if nums2[mid] == num: + res.add(num) + break + elif nums2[mid] < num: + left = mid + 1 + else: + right = mid - 1 + return list(res) \ No newline at end of file diff --git "a/LeetCode-Python/0382.\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271/0382-\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271.py" "b/LeetCode-Python/0382.\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271/0382-\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271.py" new file mode 100644 index 0000000..3b76846 --- /dev/null +++ "b/LeetCode-Python/0382.\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271/0382-\351\223\276\350\241\250\351\232\217\346\234\272\350\212\202\347\202\271.py" @@ -0,0 +1,22 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +import random +class Solution: + + def __init__(self, head: Optional[ListNode]): + self.list = [] + p = head + while p: + self.list.append(p.val) + p = p.next + + def getRandom(self) -> int: + return random.choice(self.list) + + +# Your Solution object will be instantiated and called as such: +# obj = Solution(head) +# param_1 = obj.getRandom() \ No newline at end of file diff --git "a/LeetCode-Python/0383.\350\265\216\351\207\221\344\277\241/0383-\350\265\216\351\207\221\344\277\241.py" "b/LeetCode-Python/0383.\350\265\216\351\207\221\344\277\241/0383-\350\265\216\351\207\221\344\277\241.py" new file mode 100644 index 0000000..d55d495 --- /dev/null +++ "b/LeetCode-Python/0383.\350\265\216\351\207\221\344\277\241/0383-\350\265\216\351\207\221\344\277\241.py" @@ -0,0 +1,4 @@ +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + from collections import Counter + return Counter(magazine) >= Counter(ransomNote) \ No newline at end of file diff --git "a/LeetCode-Python/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.py" "b/LeetCode-Python/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.py" new file mode 100644 index 0000000..d1ef746 --- /dev/null +++ "b/LeetCode-Python/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227/0392-\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.py" @@ -0,0 +1,8 @@ +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + ps, pt = 0, 0 + while ps < len(s) and pt < len(t): + if s[ps] == t[pt]: + ps += 1 + pt += 1 + return ps == len(s) \ No newline at end of file diff --git "a/LeetCode-Python/0410.\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274/0410-\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/LeetCode-Python/0410.\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274/0410-\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..78965e2 --- /dev/null +++ "b/LeetCode-Python/0410.\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274/0410-\345\210\206\345\211\262\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,19 @@ +class Solution: + def splitArray(self, nums: List[int], k: int) -> int: + left, right = max(nums), sum(nums) + while left <= right: + mid = (left + right) // 2 # target answer + + subarray_cnt, cur_sum = 1, 0 + for num in nums: + if cur_sum + num <= mid: + cur_sum += num + else: + subarray_cnt += 1 + cur_sum = num + + if subarray_cnt <= k: # need to get more subarrays + right = mid - 1 + elif subarray_cnt > k: + left = mid + 1 + return left \ No newline at end of file diff --git a/LeetCode-Python/0412.FizzBuzz/0412-FizzBuzz.py b/LeetCode-Python/0412.FizzBuzz/0412-FizzBuzz.py new file mode 100644 index 0000000..65533c7 --- /dev/null +++ b/LeetCode-Python/0412.FizzBuzz/0412-FizzBuzz.py @@ -0,0 +1,13 @@ +class Solution: + def fizzBuzz(self, n: int) -> List[str]: + res = [] + for i in range(1, n + 1): + if i % 3 == 0 and i % 5 == 0: + res.append("FizzBuzz") + elif i % 3 == 0: + res.append("Fizz") + elif i % 5 == 0: + res.append("Buzz") + else: + res.append(str(i)) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0451.\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217/0451-\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217.py" "b/LeetCode-Python/0451.\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217/0451-\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217.py" new file mode 100644 index 0000000..698dd1b --- /dev/null +++ "b/LeetCode-Python/0451.\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217/0451-\346\240\271\346\215\256\345\255\227\347\254\246\345\207\272\347\216\260\351\242\221\347\216\207\346\216\222\345\272\217.py" @@ -0,0 +1,15 @@ +from heapq import * +class Solution: + def frequencySort(self, s: str) -> str: + from collections import Counter + c = Counter(s) + + max_heap = [] + for char, freq in c.items(): + heappush(max_heap, (-freq, char)) + + res = "" + while max_heap: + freq, char = heappop(max_heap) + res += -freq * char + return res \ No newline at end of file diff --git "a/LeetCode-Python/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" "b/LeetCode-Python/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" new file mode 100644 index 0000000..ef93c59 --- /dev/null +++ "b/LeetCode-Python/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" @@ -0,0 +1,19 @@ +class Solution: + def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: + + num2larger = dict() + stack = [] + for i, num in enumerate(nums2): + while stack and nums2[stack[-1]] < num: + last_index = stack[-1] + num2larger[nums2[last_index]] = num + stack.pop() + stack.append(i) + + res = [] + for num in nums1: + if num in num2larger: + res.append(num2larger[num]) + else: + res.append(-1) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0500.\351\224\256\347\233\230\350\241\214/0500-\351\224\256\347\233\230\350\241\214.py" "b/LeetCode-Python/0500.\351\224\256\347\233\230\350\241\214/0500-\351\224\256\347\233\230\350\241\214.py" new file mode 100644 index 0000000..d4a1366 --- /dev/null +++ "b/LeetCode-Python/0500.\351\224\256\347\233\230\350\241\214/0500-\351\224\256\347\233\230\350\241\214.py" @@ -0,0 +1,25 @@ +class Solution: + def findWords(self, words: List[str]) -> List[str]: + row_1 = "qwertyuiop" + row_2 = "asdfghjkl" + row_3 = "zxcvbnm" + res = [] + for word in words: + l_word = word.lower() + row = "" + if l_word[0] in row_1: + row = row_1 + elif l_word[0] in row_2: + row = row_2 + elif l_word[0] in row_3: + row = row_3 + + inSameRow = True + for char in l_word: + if char not in row: + inSameRow = False + break + + if inSameRow: + res.append(word) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II/0503-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.py" "b/LeetCode-Python/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II/0503-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.py" new file mode 100644 index 0000000..9cdc34c --- /dev/null +++ "b/LeetCode-Python/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II/0503-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.py" @@ -0,0 +1,13 @@ +class Solution: + def nextGreaterElements(self, nums: List[int]) -> List[int]: + stack = [] + res = [-1 for _ in nums] + for i, num in enumerate(nums + nums): + while stack and nums[stack[-1]] < num: + last_index = stack[-1] + res[last_index] = num + stack.pop() + if i < len(nums): + stack.append(i) + + return res \ No newline at end of file diff --git "a/LeetCode-Python/0506.\347\233\270\345\257\271\345\220\215\346\254\241/0506-\347\233\270\345\257\271\345\220\215\346\254\241.py" "b/LeetCode-Python/0506.\347\233\270\345\257\271\345\220\215\346\254\241/0506-\347\233\270\345\257\271\345\220\215\346\254\241.py" new file mode 100644 index 0000000..4dbfe25 --- /dev/null +++ "b/LeetCode-Python/0506.\347\233\270\345\257\271\345\220\215\346\254\241/0506-\347\233\270\345\257\271\345\220\215\346\254\241.py" @@ -0,0 +1,21 @@ +class Solution: + def findRelativeRanks(self, score: List[int]) -> List[str]: + p = [[score[i], i] for i in range(len(score))] + + p.sort(key = lambda x: -x[0]) + + res = [0 for _ in score] + for index, pair in enumerate(p): + score, original_index = pair[0], pair[1] + + if index == 0: + val = "Gold Medal" + elif index == 1: + val = "Silver Medal" + elif index == 2: + val = "Bronze Medal" + else: + val = str(index + 1) + + res[original_index] = val + return res \ No newline at end of file diff --git "a/LeetCode-Python/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274/0513-\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.py" "b/LeetCode-Python/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274/0513-\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.py" new file mode 100644 index 0000000..5738ae9 --- /dev/null +++ "b/LeetCode-Python/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274/0513-\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + res.append(cur_level) + queue = next_queue + return res[-1][0] \ No newline at end of file diff --git "a/LeetCode-Python/0515.\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274/0515-\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.py" "b/LeetCode-Python/0515.\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274/0515-\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..65363a5 --- /dev/null +++ "b/LeetCode-Python/0515.\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274/0515-\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def largestValues(self, root: Optional[TreeNode]) -> List[int]: + queue = [root] + res = [] + + while queue: + next_queue = [] + max_val = float("-inf") + for node in queue: + if node: + max_val = max(max_val, node.val) + next_queue.extend([node.left, node.right]) + + queue = next_queue + if max_val != float("-inf"): + res.append(max_val) + + return res \ No newline at end of file diff --git "a/LeetCode-Python/0521.\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240/0521-\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240.py" "b/LeetCode-Python/0521.\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240/0521-\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240.py" new file mode 100644 index 0000000..1304139 --- /dev/null +++ "b/LeetCode-Python/0521.\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240/0521-\346\234\200\351\225\277\347\211\271\346\256\212\345\272\217\345\210\227\342\205\240.py" @@ -0,0 +1,5 @@ +class Solution: + def findLUSlength(self, a: str, b: str) -> int: + if a == b: + return -1 + return max(len(a), len(b)) \ No newline at end of file diff --git "a/LeetCode-Python/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/0538-\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.py" "b/LeetCode-Python/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/0538-\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.py" new file mode 100644 index 0000000..5789d11 --- /dev/null +++ "b/LeetCode-Python/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221/0538-\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.py" @@ -0,0 +1,20 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + self.cur_sum = 0 + def backwardInorder(node): + if not node: + return + + backwardInorder(node.right) + self.cur_sum += node.val + node.val = self.cur_sum + backwardInorder(node.left) + + backwardInorder(root) + return root \ No newline at end of file diff --git "a/LeetCode-Python/0557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/0557-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III.py" "b/LeetCode-Python/0557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/0557-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III.py" new file mode 100644 index 0000000..0f3aa81 --- /dev/null +++ "b/LeetCode-Python/0557.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III/0557-\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\215\225\350\257\215III.py" @@ -0,0 +1,3 @@ +class Solution: + def reverseWords(self, s: str) -> str: + return " ".join(word[::-1] for word in s.split()) \ No newline at end of file diff --git "a/LeetCode-Python/0589.N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0589-N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.py" "b/LeetCode-Python/0589.N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0589-N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..1a246d0 --- /dev/null +++ "b/LeetCode-Python/0589.N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206/0589-N\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,25 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, val=None, children=None): + self.val = val + self.children = children +""" + +class Solution: + def preorder(self, root: 'Node') -> List[int]: + # root, left, right => right, left, root + if not root: + return [] + res = [] + stack = [root] + + while stack: + cur = stack.pop() + if isinstance(cur, Node): + for child in cur.children[::-1]: + stack.append(child) + stack.append(cur.val) + else: + res.append(cur) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0590.N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0590-N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.py" "b/LeetCode-Python/0590.N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0590-N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.py" new file mode 100644 index 0000000..40ce7ef --- /dev/null +++ "b/LeetCode-Python/0590.N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206/0590-N\345\217\211\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206.py" @@ -0,0 +1,25 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, val=None, children=None): + self.val = val + self.children = children +""" + +class Solution: + def postorder(self, root: 'Node') -> List[int]: + # left, right, root => root, right, left + if not root: + return [] + res = [] + stack = [root] + + while stack: + cur = stack.pop() + if isinstance(cur, Node): + stack.append(cur.val) + for child in cur.children[::-1]: + stack.append(child) + else: + res.append(cur) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221/0617-\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.py" "b/LeetCode-Python/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221/0617-\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..d4f48f0 --- /dev/null +++ "b/LeetCode-Python/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221/0617-\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,20 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: + if not root1 and not root2: + return None + if root1 and not root2: + return root1 + if not root1 and root2: + return root2 + + root1.val += root2.val + + root1.left = self.mergeTrees(root1.left, root2.left) + root1.right = self.mergeTrees(root1.right, root2.right) + return root1 \ No newline at end of file diff --git "a/LeetCode-Python/0637.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274/0637-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274.py" "b/LeetCode-Python/0637.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274/0637-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274.py" new file mode 100644 index 0000000..0eb440d --- /dev/null +++ "b/LeetCode-Python/0637.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274/0637-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\271\263\345\235\207\345\200\274.py" @@ -0,0 +1,27 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_sum = 0 + cur_count = 0 + + for node in queue: + if node: + cur_count += 1 + cur_sum += node.val + + next_queue.append(node.left) + next_queue.append(node.right) + if cur_count: + res.append(cur_sum * 1.0 / cur_count) + queue = next_queue + return res \ No newline at end of file diff --git "a/LeetCode-Python/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/LeetCode-Python/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..6005f43 --- /dev/null +++ "b/LeetCode-Python/0653.\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0653-\344\270\244\346\225\260\344\271\213\345\222\214IV-\350\276\223\345\205\245\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def findTarget(self, root: Optional[TreeNode], k: int) -> bool: + visited = set() + self.res = False + + def inorder(node): + if not node: + return + + inorder(node.left) + + if k - node.val in visited: + self.res = True + visited.add(node.val) + + if not self.res: + inorder(node.right) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221/0654-\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.py" "b/LeetCode-Python/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221/0654-\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..3579ecc --- /dev/null +++ "b/LeetCode-Python/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221/0654-\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,16 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]: + if not nums: + return None + + max_val = max(nums) + max_val_index = nums.index(max_val) + + root = TreeNode(max_val, self.constructMaximumBinaryTree(nums[:max_val_index]), self.constructMaximumBinaryTree(nums[max_val_index + 1:])) + return root \ No newline at end of file diff --git "a/LeetCode-Python/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271/0657-\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.py" "b/LeetCode-Python/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271/0657-\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.py" new file mode 100644 index 0000000..c62ed16 --- /dev/null +++ "b/LeetCode-Python/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271/0657-\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.py" @@ -0,0 +1,15 @@ +class Solution: + def judgeCircle(self, moves: str) -> bool: + x, y = 0, 0 + + for move in moves: + if move == "R": + y += 1 + elif move == "L": + y -= 1 + elif move == "U": + x -= 1 + else: + x += 1 + + return [x, y] == [0, 0] \ No newline at end of file diff --git "a/LeetCode-Python/0680.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II/0680-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II.py" "b/LeetCode-Python/0680.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II/0680-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II.py" new file mode 100644 index 0000000..90330a6 --- /dev/null +++ "b/LeetCode-Python/0680.\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II/0680-\351\252\214\350\257\201\345\233\236\346\226\207\344\270\262II.py" @@ -0,0 +1,20 @@ +class Solution: + def validPalindrome(self, s: str) -> bool: + delete = False + left, right = 0, len(s) - 1 + + while left < right: + if s[left] != s[right]: + if not delete: + delete = True + break + left += 1 + right -= 1 + + if delete: + s1 = s[:left] + s[left + 1:] + s2 = s[:right] + s[right + 1:] + return s1 == s1[::-1] or s2 == s2[::-1] + else: + return True + \ No newline at end of file diff --git "a/LeetCode-Python/0682.\346\243\222\347\220\203\346\257\224\350\265\233/0682-\346\243\222\347\220\203\346\257\224\350\265\233.py" "b/LeetCode-Python/0682.\346\243\222\347\220\203\346\257\224\350\265\233/0682-\346\243\222\347\220\203\346\257\224\350\265\233.py" new file mode 100644 index 0000000..0aecf36 --- /dev/null +++ "b/LeetCode-Python/0682.\346\243\222\347\220\203\346\257\224\350\265\233/0682-\346\243\222\347\220\203\346\257\224\350\265\233.py" @@ -0,0 +1,13 @@ +class Solution: + def calPoints(self, operations: List[str]) -> int: + stack = [] + for op in operations: + if op == "+": + stack.append(stack[-1] + stack[-2]) + elif op == "C": + stack.pop() + elif op == "D": + stack.append(2 * stack[-1]) + else: + stack.append(int(op)) + return sum(stack) diff --git "a/LeetCode-Python/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242/0700-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.py" "b/LeetCode-Python/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242/0700-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.py" new file mode 100644 index 0000000..0b23b9d --- /dev/null +++ "b/LeetCode-Python/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242/0700-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.py" @@ -0,0 +1,20 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + self.res = None + def inorder(node): + if not node or self.res: + return + + inorder(node.left) + if node.val == val: + self.res = node + inorder(node.right) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/0702.\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204/0702-\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204.py" "b/LeetCode-Python/0702.\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204/0702-\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204.py" new file mode 100644 index 0000000..f92b21b --- /dev/null +++ "b/LeetCode-Python/0702.\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204/0702-\346\220\234\347\264\242\351\225\277\345\272\246\346\234\252\347\237\245\347\232\204\346\234\211\345\272\217\346\225\260\347\273\204.py" @@ -0,0 +1,25 @@ +# """ +# This is ArrayReader's API interface. +# You should not implement it, or speculate about its implementation +# """ +#class ArrayReader: +# def get(self, index: int) -> int: + +class Solution: + def search(self, reader: 'ArrayReader', target: int) -> int: + OVERFLOW = 2 ** 31 - 1 + left, right = 0, 10 ** 4 + while left <= right: + mid = (left + right) // 2 + returned_val = reader.get(mid) + if returned_val == OVERFLOW: + right = mid - 1 + continue + if returned_val == target: + return mid + elif returned_val > target: + right = mid - 1 + else: + left = mid + 1 + return -1 + \ No newline at end of file diff --git "a/LeetCode-Python/0703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240/0703-\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240.py" "b/LeetCode-Python/0703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240/0703-\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240.py" new file mode 100644 index 0000000..4f478f0 --- /dev/null +++ "b/LeetCode-Python/0703.\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240/0703-\346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\347\254\254K\345\244\247\345\205\203\347\264\240.py" @@ -0,0 +1,22 @@ +import heapq +class KthLargest: + def __init__(self, k: int, nums: List[int]): + self.k = k + self.min_heap = [] + for num in nums: + if len(self.min_heap) < k: + heappush(self.min_heap, num) + else: + heappushpop(self.min_heap, num) + + def add(self, val: int) -> int: + if len(self.min_heap) < self.k: + heappush(self.min_heap, val) + else: + heappushpop(self.min_heap, val) + return self.min_heap[0] + + +# Your KthLargest object will be instantiated and called as such: +# obj = KthLargest(k, nums) +# param_1 = obj.add(val) \ No newline at end of file diff --git "a/LeetCode-Python/0709.\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215/0709-\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215.py" "b/LeetCode-Python/0709.\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215/0709-\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215.py" new file mode 100644 index 0000000..318d48b --- /dev/null +++ "b/LeetCode-Python/0709.\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215/0709-\350\275\254\346\215\242\346\210\220\345\260\217\345\206\231\345\255\227\346\257\215.py" @@ -0,0 +1,6 @@ +class Solution: + def toLowerCase(self, s: str) -> str: + res = "" + for char in s: + res += char.lower() + return res \ No newline at end of file diff --git "a/LeetCode-Python/0717.1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246/0717-1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246.py" "b/LeetCode-Python/0717.1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246/0717-1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246.py" new file mode 100644 index 0000000..a5c61b3 --- /dev/null +++ "b/LeetCode-Python/0717.1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246/0717-1\346\257\224\347\211\271\344\270\2162\346\257\224\347\211\271\345\255\227\347\254\246.py" @@ -0,0 +1,15 @@ +class Solution: + def isOneBitCharacter(self, bits: List[int]) -> bool: + connected_with_prev = False + + for i, bit in enumerate(bits): + if connected_with_prev: + if i == len(bits) - 1: + return False + connected_with_prev = False + else: + if bit == 1: + connected_with_prev = True + else: + connected_with_prev = False + return True \ No newline at end of file diff --git "a/LeetCode-Python/0725.\345\210\206\351\232\224\351\223\276\350\241\250/0725-\345\210\206\351\232\224\351\223\276\350\241\250.py" "b/LeetCode-Python/0725.\345\210\206\351\232\224\351\223\276\350\241\250/0725-\345\210\206\351\232\224\351\223\276\350\241\250.py" new file mode 100644 index 0000000..cc285bb --- /dev/null +++ "b/LeetCode-Python/0725.\345\210\206\351\232\224\351\223\276\350\241\250/0725-\345\210\206\351\232\224\351\223\276\350\241\250.py" @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]: + dummy1 = ListNode(-1) + dummy2 = ListNode(-1) + + smaller_list = dummy1 + larger_list = dummy2 + p = head + while p: + if p.val < x: + new_node = ListNode(p.val) + smaller_list.next = new_node + smaller_list = smaller_list.next + else: + new_node = ListNode(p.val) + larger_list.next = new_node + larger_list = larger_list.next + p = p.next + + smaller_list.next = dummy2.next + return dummy1.next diff --git "a/LeetCode-Python/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257/0746-\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.py" "b/LeetCode-Python/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257/0746-\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.py" new file mode 100644 index 0000000..f4207cc --- /dev/null +++ "b/LeetCode-Python/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257/0746-\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.py" @@ -0,0 +1,21 @@ +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + # n = len(cost) + # res = 0 + # @cache + # def dfs(n): + # # dfs(n) represents the cost to climb to index n + # if n < 0: + # return 0 + # if n <= 1: + # return 0 + # res = min(dfs(n - 1) + cost[n - 1], dfs(n - 2) + cost[n - 2]) + # return res + # return dfs(n) + n = len(cost) + res = 0 + f0, f1 = 0, 0 + for i in range(n - 1): + new_f = min(f1 + cost[i + 1], f0 + cost[i]) + f0, f1 = f1, new_f + return f1 \ No newline at end of file diff --git "a/LeetCode-Python/0771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/0771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" "b/LeetCode-Python/0771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/0771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" new file mode 100644 index 0000000..689442a --- /dev/null +++ "b/LeetCode-Python/0771.\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264/0771-\345\256\235\347\237\263\344\270\216\347\237\263\345\244\264.py" @@ -0,0 +1,10 @@ +class Solution: + def numJewelsInStones(self, jewels: str, stones: str) -> int: + jewels = set(jewels) + + res = 0 + for stone in stones: + if stone in jewels: + res += 1 + + return res \ No newline at end of file diff --git "a/LeetCode-Python/0786.\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/0786-\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260.py" "b/LeetCode-Python/0786.\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/0786-\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260.py" new file mode 100644 index 0000000..bd10cee --- /dev/null +++ "b/LeetCode-Python/0786.\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260/0786-\347\254\254K\344\270\252\346\234\200\345\260\217\347\232\204\347\264\240\346\225\260\345\210\206\346\225\260.py" @@ -0,0 +1,10 @@ +class Solution: + def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]: + res = [] + + for i in range(len(arr)): + for j in range(i + 1, len(arr)): + res.append((arr[i], arr[j], arr[i] * 1.0 / arr[j])) + + res.sort(key = lambda x: x[2]) + return [res[k - 1][0], res[k - 1][1]] \ No newline at end of file diff --git "a/LeetCode-Python/0804.\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215/0804-\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215.py" "b/LeetCode-Python/0804.\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215/0804-\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215.py" new file mode 100644 index 0000000..bf02194 --- /dev/null +++ "b/LeetCode-Python/0804.\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215/0804-\345\224\257\344\270\200\346\221\251\345\260\224\346\226\257\345\257\206\347\240\201\350\257\215.py" @@ -0,0 +1,11 @@ +class Solution: + def uniqueMorseRepresentations(self, words: List[str]) -> int: + res = set() + mores = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] + + for word in words: + word_mores = "" + for char in word: + word_mores += mores[ord(char) - ord("a")] + res.add(word_mores) + return len(res) \ No newline at end of file diff --git "a/LeetCode-Python/0807.\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277/0807-\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277.py" "b/LeetCode-Python/0807.\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277/0807-\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277.py" new file mode 100644 index 0000000..c63aae3 --- /dev/null +++ "b/LeetCode-Python/0807.\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277/0807-\344\277\235\346\214\201\345\237\216\345\270\202\345\244\251\351\231\205\347\272\277.py" @@ -0,0 +1,8 @@ +class Solution: + def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int: + col_grid = [list(col) for col in zip(*grid)] + res = 0 + for i in range(len(grid)): + for j in range(len(grid)): + res += min(max(grid[i]), max(col_grid[j])) - grid[i][j] + return res \ No newline at end of file diff --git "a/LeetCode-Python/0832.\347\277\273\350\275\254\345\233\276\345\203\217/0832-\347\277\273\350\275\254\345\233\276\345\203\217.py" "b/LeetCode-Python/0832.\347\277\273\350\275\254\345\233\276\345\203\217/0832-\347\277\273\350\275\254\345\233\276\345\203\217.py" new file mode 100644 index 0000000..841a80a --- /dev/null +++ "b/LeetCode-Python/0832.\347\277\273\350\275\254\345\233\276\345\203\217/0832-\347\277\273\350\275\254\345\233\276\345\203\217.py" @@ -0,0 +1,9 @@ +class Solution: + def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]: + for i, row in enumerate(image): + image[i] = row[::-1] + + for i in range(len(image)): + for j in range(len(image)): + image[i][j] = 1 - image[i][j] + return image \ No newline at end of file diff --git "a/LeetCode-Python/0852.\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225/0852-\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225.py" "b/LeetCode-Python/0852.\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225/0852-\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225.py" new file mode 100644 index 0000000..e013042 --- /dev/null +++ "b/LeetCode-Python/0852.\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225/0852-\345\261\261\350\204\211\346\225\260\347\273\204\347\232\204\345\263\260\351\241\266\347\264\242\345\274\225.py" @@ -0,0 +1,13 @@ +class Solution: + def peakIndexInMountainArray(self, arr: List[int]) -> int: + left, right = 0, len(arr) - 1 + while left <= right: + mid = (left + right) // 2 + if 0 < mid < len(arr) - 1 and arr[mid - 1] < arr[mid] and arr[mid] > arr[mid + 1]: + return mid + + if arr[mid + 1] > arr[mid]: + left = mid + 1 + else: + right = mid - 1 + return left \ No newline at end of file diff --git "a/LeetCode-Python/0876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/0876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" "b/LeetCode-Python/0876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/0876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" new file mode 100644 index 0000000..39ec0a9 --- /dev/null +++ "b/LeetCode-Python/0876.\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271/0876-\351\223\276\350\241\250\347\232\204\344\270\255\351\227\264\347\273\223\347\202\271.py" @@ -0,0 +1,13 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: + slow, fast = head, head + while fast and fast.next: + fast = fast.next.next + slow = slow.next + + return slow \ No newline at end of file diff --git "a/LeetCode-Python/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221.py" "b/LeetCode-Python/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..c2d6aa1 --- /dev/null +++ "b/LeetCode-Python/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def increasingBST(self, root: TreeNode) -> TreeNode: + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + inorder_list = inorder(root) + dummy = TreeNode(-1) + p = dummy + for val in inorder_list: + p.right = TreeNode(val) + p = p.right + + return dummy.right \ No newline at end of file diff --git "a/LeetCode-Python/0912.\346\216\222\345\272\217\346\225\260\347\273\204/0912-\346\216\222\345\272\217\346\225\260\347\273\204.py" "b/LeetCode-Python/0912.\346\216\222\345\272\217\346\225\260\347\273\204/0912-\346\216\222\345\272\217\346\225\260\347\273\204.py" new file mode 100644 index 0000000..5ed8e8d --- /dev/null +++ "b/LeetCode-Python/0912.\346\216\222\345\272\217\346\225\260\347\273\204/0912-\346\216\222\345\272\217\346\225\260\347\273\204.py" @@ -0,0 +1,13 @@ +class Solution: + def sortArray(self, nums: List[int]) -> List[int]: + RANGE = 5 * 10 ** 4 + bucket = [0 for _ in range(2 * RANGE + 1)] + + for num in nums: + bucket[num + RANGE] += 1 + + res = [] + for index, count in enumerate(bucket): + if count: + res.extend([index - RANGE] * count) + return res \ No newline at end of file diff --git "a/LeetCode-Python/0938.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214/0938-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214.py" "b/LeetCode-Python/0938.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214/0938-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214.py" new file mode 100644 index 0000000..8491c7c --- /dev/null +++ "b/LeetCode-Python/0938.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214/0938-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\350\214\203\345\233\264\345\222\214.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int: + self.res = 0 + def inorder(node): + if not node: + return + + inorder(node.left) + if low <= node.val <= high: + self.res += node.val + + if node.val > high: + return + inorder(node.right) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/0965.\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221/0965-\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.py" "b/LeetCode-Python/0965.\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221/0965-\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..91b3525 --- /dev/null +++ "b/LeetCode-Python/0965.\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221/0965-\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,13 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isUnivalTree(self, root: Optional[TreeNode]) -> bool: + if not root: + return True + left = not root.left or (self.isUnivalTree(root.left) and root.left.val == root.val) + right = not root.right or (self.isUnivalTree(root.right) and root.right.val == root.val) + return left and right \ No newline at end of file diff --git "a/LeetCode-Python/0973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/0973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.py" "b/LeetCode-Python/0973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/0973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.py" new file mode 100644 index 0000000..e829b06 --- /dev/null +++ "b/LeetCode-Python/0973.\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271/0973-\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.py" @@ -0,0 +1,15 @@ +from heapq import * +class Solution: + def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]: + max_heap = [] + for x, y in points: + d = self.getDistance(0, 0, x, y) + if len(max_heap) < k: + heappush(max_heap, (-d, [x, y])) + else: + heappushpop(max_heap, (-d, [x, y])) + return [p for _, p in max_heap] + + + def getDistance(self, x1, y1, x2, y2): + return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 \ No newline at end of file diff --git "a/LeetCode-Python/1008.\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/1008-\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/LeetCode-Python/1008.\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/1008-\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..94069fb --- /dev/null +++ "b/LeetCode-Python/1008.\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/1008-\345\211\215\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,27 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]: + if not preorder: + return None + + root = TreeNode(preorder[0]) + hasRight = False + for i, val in enumerate(preorder): + if val > root.val: + right_index = i + hasRight = True + break + + + if hasRight: + root.left = self.bstFromPreorder(preorder[1:right_index]) + root.right = self.bstFromPreorder(preorder[right_index:]) + else: + root.left = self.bstFromPreorder(preorder[1:]) + return root + diff --git "a/LeetCode-Python/1011.\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233/1011-\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233.py" "b/LeetCode-Python/1011.\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233/1011-\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233.py" new file mode 100644 index 0000000..516c1d5 --- /dev/null +++ "b/LeetCode-Python/1011.\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233/1011-\345\234\250D\345\244\251\345\206\205\351\200\201\350\276\276\345\214\205\350\243\271\347\232\204\350\203\275\345\212\233.py" @@ -0,0 +1,22 @@ +class Solution: + def shipWithinDays(self, weights: List[int], days: int) -> int: + left, right = max(weights), sum(weights) + while left <= right: + mid = (left + right) // 2 # answer to be returned, max(capacity) + + capacity = mid + needed_days, cur_sum = 1, 0 + for w in weights: + if cur_sum + w > capacity: + needed_days += 1 + cur_sum = w + else: + cur_sum += w + # print(capacity, needed_days, days) + if needed_days > days: # need to increase the capacity + left = mid + 1 + elif needed_days == days: + right = mid - 1 + elif needed_days < days: + right = mid - 1 + return left \ No newline at end of file diff --git "a/LeetCode-Python/1016.\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262/1016-\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262.py" "b/LeetCode-Python/1016.\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262/1016-\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262.py" new file mode 100644 index 0000000..f359abb --- /dev/null +++ "b/LeetCode-Python/1016.\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262/1016-\345\255\220\344\270\262\350\203\275\350\241\250\347\244\272\344\273\2161\345\210\260N\346\225\260\345\255\227\347\232\204\344\272\214\350\277\233\345\210\266\344\270\262.py" @@ -0,0 +1,7 @@ +class Solution: + def queryString(self, s: str, n: int) -> bool: + for i in range(1, n + 1): + b = bin(i)[2:] + if b not in s: + return False + return True \ No newline at end of file diff --git "a/LeetCode-Python/1017.\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242/1017-\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242.py" "b/LeetCode-Python/1017.\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242/1017-\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242.py" new file mode 100644 index 0000000..70ffa9b --- /dev/null +++ "b/LeetCode-Python/1017.\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242/1017-\350\264\237\344\272\214\350\277\233\345\210\266\350\275\254\346\215\242.py" @@ -0,0 +1,10 @@ +class Solution: + def baseNeg2(self, n: int) -> str: + if not n: + return "0" + l = "" + while n: + d, m = divmod(n, -2) + n = - (n // 2) + l += str(-m) + return l[::-1] \ No newline at end of file diff --git "a/LeetCode-Python/1019.\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271/1019-\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271.py" "b/LeetCode-Python/1019.\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271/1019-\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271.py" new file mode 100644 index 0000000..9d73c03 --- /dev/null +++ "b/LeetCode-Python/1019.\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271/1019-\351\223\276\350\241\250\344\270\255\347\232\204\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\350\212\202\347\202\271.py" @@ -0,0 +1,25 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]: + stack = [] # [(2, 0)] (node.val, node.index) + p, l = head, 0 + while p: + l += 1 + p = p.next + res = [0] * l + + p = head + cur_index = 0 + while p: + while stack and stack[-1][0] < p.val: + last_index = stack[-1][1] + res[last_index] = p.val + stack.pop() + stack.append((p.val, cur_index)) + p = p.next + cur_index += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/1021.\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267/1021-\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267.py" "b/LeetCode-Python/1021.\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267/1021-\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267.py" new file mode 100644 index 0000000..bce974f --- /dev/null +++ "b/LeetCode-Python/1021.\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267/1021-\345\210\240\351\231\244\346\234\200\345\244\226\345\261\202\347\232\204\346\213\254\345\217\267.py" @@ -0,0 +1,21 @@ +class Solution: + def removeOuterParentheses(self, s: str) -> str: + left, right = 0, 0 + stack = "" + res = "" + for char in s: + if char == "(": + left += 1 + stack += char + elif char == ")": + right += 1 + if left == right: + # find an outmost parenthesis pair + res += stack[1:] + stack = "" + else: + stack += char + return res + + + diff --git "a/LeetCode-Python/1022.\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214/1022-\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214.py" "b/LeetCode-Python/1022.\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214/1022-\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214.py" new file mode 100644 index 0000000..a8e81d0 --- /dev/null +++ "b/LeetCode-Python/1022.\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214/1022-\344\273\216\346\240\271\345\210\260\345\217\266\347\232\204\344\272\214\350\277\233\345\210\266\346\225\260\344\271\213\345\222\214.py" @@ -0,0 +1,22 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumRootToLeaf(self, root: Optional[TreeNode]) -> int: + self.res = 0 + def dfs(node, cur_binary): + if not node: + return + + cur_binary = cur_binary * 2 + node.val + if not node.left and not node.right: + self.res += cur_binary + return + + dfs(node.left, cur_binary) + dfs(node.right, cur_binary) + dfs(root, 0) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/1038.\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221/1038-\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221.py" "b/LeetCode-Python/1038.\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221/1038-\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221.py" new file mode 100644 index 0000000..19fdaf8 --- /dev/null +++ "b/LeetCode-Python/1038.\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221/1038-\344\273\216\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\210\260\346\233\264\345\244\247\345\222\214\346\240\221.py" @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def bstToGst(self, root: TreeNode) -> TreeNode: + + self.cur_sum = 0 + def backwardInorder(node): + if not node: + return + + backwardInorder(node.right) + self.cur_sum += node.val + node.val = self.cur_sum + backwardInorder(node.left) + + backwardInorder(root) + return root \ No newline at end of file diff --git "a/LeetCode-Python/1046.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217/1046-\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217.py" "b/LeetCode-Python/1046.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217/1046-\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217.py" new file mode 100644 index 0000000..f78cd85 --- /dev/null +++ "b/LeetCode-Python/1046.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217/1046-\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217.py" @@ -0,0 +1,16 @@ +from heapq import * +class Solution: + def lastStoneWeight(self, stones: List[int]) -> int: + max_heap = [] + + for stone in stones: + heappush(max_heap, -stone) + + while len(max_heap) > 1: + x, y = -heappop(max_heap), -heappop(max_heap) + + if y == x: + continue + else: + heappush(max_heap, -(x - y)) + return -max_heap[0] if max_heap else 0 \ No newline at end of file diff --git "a/LeetCode-Python/1054.\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201/1054-\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201.py" "b/LeetCode-Python/1054.\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201/1054-\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201.py" new file mode 100644 index 0000000..f6cdbf3 --- /dev/null +++ "b/LeetCode-Python/1054.\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201/1054-\350\267\235\347\246\273\347\233\270\347\255\211\347\232\204\346\235\241\345\275\242\347\240\201.py" @@ -0,0 +1,25 @@ +class Solution: + def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]: + from collections import Counter + import heapq + d = Counter(barcodes) + res = [] + t = [] + for val, freq in d.items(): + t.append((-freq, val)) + heapify(t) + + last_pair = None + while t: + pair = heappop(t) + val, freq = pair[1], -pair[0] + res.append(val) + if last_pair: + heappush(t, last_pair) + freq -= 1 + if freq: + last_pair = (-freq, val) + else: + last_pair = None + + return res diff --git "a/LeetCode-Python/1073.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/1073-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" "b/LeetCode-Python/1073.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/1073-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" new file mode 100644 index 0000000..91c5e98 --- /dev/null +++ "b/LeetCode-Python/1073.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/1073-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" @@ -0,0 +1,20 @@ +class Solution: + def addNegabinary(self, arr1: List[int], arr2: List[int]) -> List[int]: + res = self.convertToDec(arr1) + self.convertToDec(arr2) + l = [] + if res == 0: + return [0] + while res: + d, m = divmod(res, -2) + res = - (res // 2) + l.append(-m) + return l[::-1] + + def convertToDec(self, arr): + res = 0 + for index, digit in enumerate(arr[::-1]): + res += digit * ((-2) ** index) + return res + + + \ No newline at end of file diff --git "a/LeetCode-Python/1079.\346\264\273\345\255\227\345\215\260\345\210\267/1079-\346\264\273\345\255\227\345\215\260\345\210\267.py" "b/LeetCode-Python/1079.\346\264\273\345\255\227\345\215\260\345\210\267/1079-\346\264\273\345\255\227\345\215\260\345\210\267.py" new file mode 100644 index 0000000..a61a8f3 --- /dev/null +++ "b/LeetCode-Python/1079.\346\264\273\345\255\227\345\215\260\345\210\267/1079-\346\264\273\345\255\227\345\215\260\345\210\267.py" @@ -0,0 +1,14 @@ +class Solution: + def numTilePossibilities(self, tiles: str) -> int: + res = set([""]) + + for tile in tiles: + new_res = set() + for r in res: + for i in range(len(r) + 1): + t = r[:i] + tile + r[i:] + if t not in res: + new_res.add(t) + res = res.union(new_res) + + return len(res) - 1 \ No newline at end of file diff --git "a/LeetCode-Python/1080.\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271/1080-\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271.py" "b/LeetCode-Python/1080.\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271/1080-\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271.py" new file mode 100644 index 0000000..96e16b6 --- /dev/null +++ "b/LeetCode-Python/1080.\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271/1080-\346\240\271\345\210\260\345\217\266\350\267\257\345\276\204\344\270\212\347\232\204\344\270\215\350\266\263\350\212\202\347\202\271.py" @@ -0,0 +1,44 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sufficientSubset(self, root: Optional[TreeNode], limit: int) -> Optional[TreeNode]: + survivor_nodes = set() + + def dfs(node, path): + if not node: + return + # path.append(node) + if not node.left and not node.right: + path.append(node) + path_sum = 0 + for n in path: + path_sum += n.val + if path_sum >= limit: + for n in path: + survivor_nodes.add(n) + return + dfs(node.left, path + [node]) + dfs(node.right, path + [node]) + + dfs(root, []) + # print(survivor_nodes) + def killNodes(node): + if not node: + return + + if node.left not in survivor_nodes: + node.left = None + if node.right not in survivor_nodes: + node.right = None + + killNodes(node.left) + killNodes(node.right) + + if root not in survivor_nodes: + return None + killNodes(root) + return root \ No newline at end of file diff --git "a/LeetCode-Python/1091.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1091-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" "b/LeetCode-Python/1091.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1091-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" new file mode 100644 index 0000000..67c8271 --- /dev/null +++ "b/LeetCode-Python/1091.\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1091-\344\272\214\350\277\233\345\210\266\347\237\251\351\230\265\344\270\255\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" @@ -0,0 +1,31 @@ +class Solution: + def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int: + from collections import deque + if not grid or not grid[0]: + return -1 + n = len(grid) + if grid[0][0] or grid[n - 1][n - 1]: + return -1 + + # BFS + dx = [0, 0, 1, 1, 1, -1, -1, -1] + dy = [1, -1, 1, 0, -1, 1, 0, -1] + + visited = set([(0, 0)]) + queue = deque([((0, 0), 1)]) # (x, y), step + + while queue: + cur = queue.popleft() + x, y, step = cur[0][0], cur[0][1], cur[1] + + if [x, y] == [n - 1, n - 1]: + return step + + for i in range(8): + xx, yy = x + dx[i], y + dy[i] + if 0 <= xx < n and 0 <= yy < n and grid[xx][yy] == 0 and (xx, yy) not in visited: + visited.add((xx, yy)) + queue.append(((xx, yy), step + 1)) + + return -1 + diff --git "a/LeetCode-Python/1103.\345\210\206\347\263\226\346\236\234II/1103-\345\210\206\347\263\226\346\236\234II.py" "b/LeetCode-Python/1103.\345\210\206\347\263\226\346\236\234II/1103-\345\210\206\347\263\226\346\236\234II.py" new file mode 100644 index 0000000..3ff67cd --- /dev/null +++ "b/LeetCode-Python/1103.\345\210\206\347\263\226\346\236\234II/1103-\345\210\206\347\263\226\346\236\234II.py" @@ -0,0 +1,14 @@ +class Solution: + def distributeCandies(self, candies: int, num_people: int) -> List[int]: + candy_to_be_distributed = 1 + child = 1 + res = [0] * (num_people + 1) + while candies: + give = min(candy_to_be_distributed, candies) + candies -= give + res[child] += give + candy_to_be_distributed += 1 + child += 1 + if (child > num_people): + child = 1 + return res[1:] \ No newline at end of file diff --git "a/LeetCode-Python/1108.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/1108-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" "b/LeetCode-Python/1108.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/1108-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" new file mode 100644 index 0000000..b9c7c37 --- /dev/null +++ "b/LeetCode-Python/1108.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/1108-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" @@ -0,0 +1,4 @@ +class Solution: + def defangIPaddr(self, address: str) -> str: + + return address.replace(".", "[.]") \ No newline at end of file diff --git "a/LeetCode-Python/1110.\345\210\240\347\202\271\346\210\220\346\236\227/1110-\345\210\240\347\202\271\346\210\220\346\236\227.py" "b/LeetCode-Python/1110.\345\210\240\347\202\271\346\210\220\346\236\227/1110-\345\210\240\347\202\271\346\210\220\346\236\227.py" new file mode 100644 index 0000000..bd49588 --- /dev/null +++ "b/LeetCode-Python/1110.\345\210\240\347\202\271\346\210\220\346\236\227/1110-\345\210\240\347\202\271\346\210\220\346\236\227.py" @@ -0,0 +1,27 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]: + to_delete = set(to_delete) + res = [] + def dfs(node, node_is_new_root): + if not node: + return + left = node.left + right = node.right + if node_is_new_root and node.val not in to_delete: + res.append(node) + if left and left.val in to_delete: + node.left = None + if right and right.val in to_delete: + node.right = None + + dfs(left, node.val in to_delete) + dfs(right, node.val in to_delete) + dfs(root, True) + return res + \ No newline at end of file diff --git "a/LeetCode-Python/1119.\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263/1119-\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263.py" "b/LeetCode-Python/1119.\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263/1119-\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263.py" new file mode 100644 index 0000000..6fb2e44 --- /dev/null +++ "b/LeetCode-Python/1119.\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263/1119-\345\210\240\345\216\273\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\205\203\351\237\263.py" @@ -0,0 +1,8 @@ +class Solution: + def removeVowels(self, s: str) -> str: + vowels = "aeiou" + res = "" + for char in s: + if char not in vowels: + res += char + return res \ No newline at end of file diff --git "a/LeetCode-Python/1130.\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221/1130-\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221.py" "b/LeetCode-Python/1130.\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221/1130-\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221.py" new file mode 100644 index 0000000..a72c05c --- /dev/null +++ "b/LeetCode-Python/1130.\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221/1130-\345\217\266\345\200\274\347\232\204\346\234\200\345\260\217\344\273\243\344\273\267\347\224\237\346\210\220\346\240\221.py" @@ -0,0 +1,12 @@ +class Solution: + def mctFromLeafValues(self, arr: List[int]) -> int: + res = 0 + stack = [16] + for num in arr: + while stack and stack[-1] < num: + res += stack.pop() * min(stack[-1], num) + stack.append(num) + + while len(stack) > 2: + res += stack.pop() * stack[-1] + return res \ No newline at end of file diff --git "a/LeetCode-Python/1156.\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246/1156-\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246.py" "b/LeetCode-Python/1156.\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246/1156-\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246.py" new file mode 100644 index 0000000..90a2662 --- /dev/null +++ "b/LeetCode-Python/1156.\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246/1156-\345\215\225\345\255\227\347\254\246\351\207\215\345\244\215\345\255\220\344\270\262\347\232\204\346\234\200\345\244\247\351\225\277\345\272\246.py" @@ -0,0 +1,21 @@ +class Solution: + def maxRepOpt1(self, text: str) -> int: + c = Counter(text) + res = 0 + for i in range(len(text)): + j = i + 1 + while j < len(text) and text[j] == text[i]: + j += 1 + + # [i, j) + cnt = j - i + if i > 0 and j < len(text) and cnt < c[text[i]]: + res = max(res, cnt + 1) + + k = j + 1 + while k < len(text) and text[k] == text[i]: + k += 1 + # [i, j)[j + 1, k) swap text[j] if possible + res = max(res, min(k - i, c[text[i]])) + i = j + return res \ No newline at end of file diff --git "a/LeetCode-Python/1161.\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214/1161-\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214.py" "b/LeetCode-Python/1161.\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214/1161-\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214.py" new file mode 100644 index 0000000..54a9a1b --- /dev/null +++ "b/LeetCode-Python/1161.\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214/1161-\346\234\200\345\244\247\345\261\202\345\206\205\345\205\203\347\264\240\345\222\214.py" @@ -0,0 +1,32 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def maxLevelSum(self, root: Optional[TreeNode]) -> int: + queue = [root] + max_level_sum = float("-inf") + max_level_sum_id = 0 + cur_level_id = 0 + + while queue: + next_queue = [] + cur_level_sum = 0 + cur_level_id += 1 + for node in queue: + if node: + cur_level_sum += node.val + if node.left: + next_queue.append(node.left) + if node.right: + next_queue.append(node.right) + + if cur_level_sum > max_level_sum: + max_level_sum = cur_level_sum + max_level_sum_id = cur_level_id + + queue = next_queue + + return max_level_sum_id \ No newline at end of file diff --git "a/LeetCode-Python/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230.py" "b/LeetCode-Python/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230.py" new file mode 100644 index 0000000..033587c --- /dev/null +++ "b/LeetCode-Python/1165.\345\215\225\350\241\214\351\224\256\347\233\230/1165-\345\215\225\350\241\214\351\224\256\347\233\230.py" @@ -0,0 +1,13 @@ +class Solution: + def calculateTime(self, keyboard: str, word: str) -> int: + char2pos = dict() + + for pos, char in enumerate(keyboard): + char2pos[char] = pos + + res = 0 + last_pos = 0 + for char in word: + res += abs(last_pos - char2pos[char]) + last_pos = char2pos[char] + return res \ No newline at end of file diff --git "a/LeetCode-Python/1180.\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262/1180-\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262.py" "b/LeetCode-Python/1180.\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262/1180-\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262.py" new file mode 100644 index 0000000..2b6fd8e --- /dev/null +++ "b/LeetCode-Python/1180.\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262/1180-\347\273\237\350\256\241\345\217\252\345\220\253\345\215\225\344\270\200\345\255\227\346\257\215\347\232\204\345\255\220\344\270\262.py" @@ -0,0 +1,13 @@ +class Solution: + def countLetters(self, s: str) -> int: + left, right = 0, 0 + res = 0 + while right < len(s): + while right < len(s) and s[left] == s[right]: + right += 1 + consecutive_chars_count = right - left + res += consecutive_chars_count * (consecutive_chars_count + 1) // 2 + left = right + return res + + diff --git "a/LeetCode-Python/1198.\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240/1198-\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240.py" "b/LeetCode-Python/1198.\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240/1198-\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240.py" new file mode 100644 index 0000000..904296b --- /dev/null +++ "b/LeetCode-Python/1198.\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240/1198-\346\211\276\345\207\272\346\211\200\346\234\211\350\241\214\344\270\255\346\234\200\345\260\217\345\205\254\345\205\261\345\205\203\347\264\240.py" @@ -0,0 +1,25 @@ +class Solution: + def smallestCommonElement(self, mat: List[List[int]]) -> int: + m, n = len(mat), len(mat[0]) + for candidate in mat[0]: + all_found_candidate = True + for row in mat[1:]: + found_candidate = False + left, right = 0, n - 1 + while left <= right: + mid = (left + right) // 2 + if row[mid] == candidate: + found_candidate = True + break + elif row[mid] < candidate: + left = mid + 1 + else: + right = mid - 1 + + if not found_candidate: + all_found_candidate = False + break + if all_found_candidate: + return candidate + return -1 + \ No newline at end of file diff --git "a/LeetCode-Python/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262/1221-\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262/1221-\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..f04b4c2 --- /dev/null +++ "b/LeetCode-Python/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262/1221-\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,15 @@ +class Solution: + def balancedStringSplit(self, s: str) -> int: + r_count, l_count = 0, 0 + res = 0 + for char in s: + if char == "R": + r_count += 1 + if char == "L": + l_count += 1 + + if l_count == r_count: + l_count = 0 + r_count = 0 + res += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/1231.\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233/1231-\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233.py" "b/LeetCode-Python/1231.\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233/1231-\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233.py" new file mode 100644 index 0000000..a26d062 --- /dev/null +++ "b/LeetCode-Python/1231.\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233/1231-\345\210\206\344\272\253\345\267\247\345\205\213\345\212\233.py" @@ -0,0 +1,21 @@ +class Solution: + def maximizeSweetness(self, sweetness: List[int], k: int) -> int: + left, right = 0, sum(sweetness) + k += 1 + while left <= right: + mid = (left + right) // 2 # mid 是每一块巧克力的甜度下限而不是上限,也是返回的值 + pieces_cnt, cur_sum = 0, 0 + for s in sweetness: + if cur_sum + s >= mid: + pieces_cnt += 1 + cur_sum = 0 + else: + cur_sum += s + + if pieces_cnt < k: # 需要切更多块 + right = mid - 1 + elif pieces_cnt == k: + left = mid + 1 + elif pieces_cnt > k: + left = mid + 1 + return right \ No newline at end of file diff --git "a/LeetCode-Python/1237.\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243/1237-\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243.py" "b/LeetCode-Python/1237.\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243/1237-\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243.py" new file mode 100644 index 0000000..b6568c3 --- /dev/null +++ "b/LeetCode-Python/1237.\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243/1237-\346\211\276\345\207\272\347\273\231\345\256\232\346\226\271\347\250\213\347\232\204\346\255\243\346\225\264\346\225\260\350\247\243.py" @@ -0,0 +1,26 @@ +""" + This is the custom function interface. + You should not implement it, or speculate about its implementation + class CustomFunction: + # Returns f(x, y) for any given positive integers x and y. + # Note that f(x, y) is increasing with respect to both x and y. + # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1) + def f(self, x, y): + +""" + +class Solution: + def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]: + res = [] + for x in range(1, 1001): + left, right = 1, 1001 + while left <= right: + mid = (left + right)// 2 + if customfunction.f(x, mid) == z: + res.append([x, mid]) + break + elif customfunction.f(x, mid) < z: + left = mid + 1 + else: + right = mid - 1 + return res diff --git "a/LeetCode-Python/1252.\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256/1252-\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256.py" "b/LeetCode-Python/1252.\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256/1252-\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..98f030d --- /dev/null +++ "b/LeetCode-Python/1252.\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256/1252-\345\245\207\346\225\260\345\200\274\345\215\225\345\205\203\346\240\274\347\232\204\346\225\260\347\233\256.py" @@ -0,0 +1,17 @@ +class Solution: + def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int: + from collections import defaultdict + row2one_count = defaultdict(int) + col2one_count = defaultdict(int) + + for row, col in indices: + row2one_count[row] += 1 + col2one_count[col] += 1 + + res = 0 + row_odd, col_odd = 0, 0 + for row in range(m): + row_odd += row2one_count[row] % 2 + for col in range(n): + col_odd += col2one_count[col] % 2 + return row_odd * (n - col_odd) + (m - row_odd) * col_odd \ No newline at end of file diff --git "a/LeetCode-Python/1261.\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240/1261-\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240.py" "b/LeetCode-Python/1261.\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240/1261-\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240.py" new file mode 100644 index 0000000..2f0ce01 --- /dev/null +++ "b/LeetCode-Python/1261.\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240/1261-\345\234\250\345\217\227\346\261\241\346\237\223\347\232\204\344\272\214\345\217\211\346\240\221\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240.py" @@ -0,0 +1,33 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class FindElements: + + def __init__(self, root: Optional[TreeNode]): + root.val = 0 + self.values = set() + def dfs(node): + if not node: + return + + self.values.add(node.val) + if node.left: + node.left.val = 2 * node.val + 1 + if node.right: + node.right.val = 2 * node.val + 2 + + dfs(node.left) + dfs(node.right) + + dfs(root) + + def find(self, target: int) -> bool: + return target in self.values + + +# Your FindElements object will be instantiated and called as such: +# obj = FindElements(root) +# param_1 = obj.find(target) \ No newline at end of file diff --git "a/LeetCode-Python/1265.\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250/1265-\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250.py" "b/LeetCode-Python/1265.\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250/1265-\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250.py" new file mode 100644 index 0000000..1b04458 --- /dev/null +++ "b/LeetCode-Python/1265.\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250/1265-\351\200\206\345\272\217\346\211\223\345\215\260\344\270\215\345\217\257\345\217\230\351\223\276\350\241\250.py" @@ -0,0 +1,15 @@ +# """ +# This is the ImmutableListNode's API interface. +# You should not implement it, or speculate about its implementation. +# """ +# class ImmutableListNode: +# def printValue(self) -> None: # print the value of this node. +# def getNext(self) -> 'ImmutableListNode': # return the next node. + +class Solution: + def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None: + if not head: + return + + self.printLinkedListInReverse(head.getNext()) + head.printValue() \ No newline at end of file diff --git "a/LeetCode-Python/1282.\347\224\250\346\210\267\345\210\206\347\273\204/1282-\347\224\250\346\210\267\345\210\206\347\273\204.py" "b/LeetCode-Python/1282.\347\224\250\346\210\267\345\210\206\347\273\204/1282-\347\224\250\346\210\267\345\210\206\347\273\204.py" new file mode 100644 index 0000000..4e18cba --- /dev/null +++ "b/LeetCode-Python/1282.\347\224\250\346\210\267\345\210\206\347\273\204/1282-\347\224\250\346\210\267\345\210\206\347\273\204.py" @@ -0,0 +1,13 @@ +class Solution: + def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: + from collections import defaultdict + + size2people = defaultdict(list) + for i, size in enumerate(groupSizes): + size2people[size].append(i) + + res = [] + for size, peoples in size2people.items(): + for i in range(0, len(peoples), size): + res.append(peoples[i: i + size]) + return res \ No newline at end of file diff --git "a/LeetCode-Python/1290.\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260/1290-\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260.py" "b/LeetCode-Python/1290.\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260/1290-\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260.py" new file mode 100644 index 0000000..70a5142 --- /dev/null +++ "b/LeetCode-Python/1290.\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260/1290-\344\272\214\350\277\233\345\210\266\351\223\276\350\241\250\350\275\254\346\225\264\346\225\260.py" @@ -0,0 +1,13 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def getDecimalValue(self, head: ListNode) -> int: + res = 0 + p = head + while p: + res = res * 2 + p.val + p = p.next + return res \ No newline at end of file diff --git "a/LeetCode-Python/1302.\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214/1302-\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214.py" "b/LeetCode-Python/1302.\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214/1302-\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214.py" new file mode 100644 index 0000000..fb0b769 --- /dev/null +++ "b/LeetCode-Python/1302.\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214/1302-\345\261\202\346\225\260\346\234\200\346\267\261\345\217\266\345\255\220\350\212\202\347\202\271\347\232\204\345\222\214.py" @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def deepestLeavesSum(self, root: Optional[TreeNode]) -> int: + queue = [root] + while queue: + level_sum = 0 + next_queue = [] + for node in queue: + if node.left: + next_queue.append(node.left) + if node.right: + next_queue.append(node.right) + level_sum += node.val + + queue = next_queue[:] + return level_sum \ No newline at end of file diff --git "a/LeetCode-Python/1315.\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214/1315-\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214.py" "b/LeetCode-Python/1315.\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214/1315-\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214.py" new file mode 100644 index 0000000..564b692 --- /dev/null +++ "b/LeetCode-Python/1315.\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214/1315-\347\245\226\347\210\266\350\212\202\347\202\271\345\200\274\344\270\272\345\201\266\346\225\260\347\232\204\350\212\202\347\202\271\345\222\214.py" @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumEvenGrandparent(self, root: TreeNode) -> int: + self.sum = 0 + def dfs(node, isParentEven, isGrandparentEven): + if not node: + return + + if isGrandparentEven: + self.sum += node.val + + dfs(node.left, node.val % 2 == 0, isParentEven) + dfs(node.right, node.val % 2 == 0, isParentEven) + + dfs(root, False, False) + return self.sum \ No newline at end of file diff --git "a/LeetCode-Python/1337.\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214/1337-\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214.py" "b/LeetCode-Python/1337.\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214/1337-\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214.py" new file mode 100644 index 0000000..89e5ff2 --- /dev/null +++ "b/LeetCode-Python/1337.\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214/1337-\347\237\251\351\230\265\344\270\255\346\210\230\346\226\227\345\212\233\346\234\200\345\274\261\347\232\204K\350\241\214.py" @@ -0,0 +1,5 @@ +class Solution: + def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]: + pair = [(i, sum(row)) for i, row in enumerate(mat)] + pair.sort(key = lambda x: x[1]) + return [p[0] for p in pair[:k]] \ No newline at end of file diff --git "a/LeetCode-Python/1338.\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212/1338-\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212.py" "b/LeetCode-Python/1338.\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212/1338-\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212.py" new file mode 100644 index 0000000..7ed3dff --- /dev/null +++ "b/LeetCode-Python/1338.\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212/1338-\346\225\260\347\273\204\345\244\247\345\260\217\345\207\217\345\215\212.py" @@ -0,0 +1,18 @@ +class Solution: + def minSetSize(self, arr: List[int]) -> int: + from collections import Counter + c = Counter(arr) + l = [] + visited = set() + for val in arr: + if val not in visited: + l.append((c[val], val)) + visited.add(val) + l.sort(key = lambda x:-x[0]) + + res, reduced_size = 0, 0 + for freq, val in l: + reduced_size += freq + res += 1 + if reduced_size >= len(arr) // 2: + return res \ No newline at end of file diff --git "a/LeetCode-Python/1351.\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260/1351-\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260.py" "b/LeetCode-Python/1351.\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260/1351-\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260.py" new file mode 100644 index 0000000..37a15f6 --- /dev/null +++ "b/LeetCode-Python/1351.\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260/1351-\347\273\237\350\256\241\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\350\264\237\346\225\260.py" @@ -0,0 +1,8 @@ +class Solution: + def countNegatives(self, grid: List[List[int]]) -> int: + res = 0 + for row in grid: + for node in row: + if node < 0: + res += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/1374.\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262/1374-\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/1374.\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262/1374-\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..690c38d --- /dev/null +++ "b/LeetCode-Python/1374.\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262/1374-\347\224\237\346\210\220\346\257\217\347\247\215\345\255\227\347\254\246\351\203\275\346\230\257\345\245\207\346\225\260\344\270\252\347\232\204\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,5 @@ +class Solution: + def generateTheString(self, n: int) -> str: + if n % 2 == 1: + return "a" * n + return "a" * (n - 1) + "b" \ No newline at end of file diff --git "a/LeetCode-Python/1379.\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271/1379-\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271.py" "b/LeetCode-Python/1379.\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271/1379-\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271.py" new file mode 100644 index 0000000..8c9b498 --- /dev/null +++ "b/LeetCode-Python/1379.\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271/1379-\346\211\276\345\207\272\345\205\213\351\232\206\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\347\233\270\345\220\214\350\212\202\347\202\271.py" @@ -0,0 +1,20 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode: + queue = [cloned] + while queue: + next_queue = [] + for node in queue: + if node: + if node.val == target.val: + return node + next_queue.append(node.left) + next_queue.append(node.right) + queue = next_queue[:] + \ No newline at end of file diff --git "a/LeetCode-Python/1389.\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204/1389-\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204.py" "b/LeetCode-Python/1389.\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204/1389-\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204.py" new file mode 100644 index 0000000..5235207 --- /dev/null +++ "b/LeetCode-Python/1389.\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204/1389-\346\214\211\346\227\242\345\256\232\351\241\272\345\272\217\345\210\233\345\273\272\347\233\256\346\240\207\346\225\260\347\273\204.py" @@ -0,0 +1,6 @@ +class Solution: + def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: + res = [] + for i, x in enumerate(index): + res = res[:x] + [nums[i]] + res[x:] + return res \ No newline at end of file diff --git "a/LeetCode-Python/1409.\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227/1409-\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227.py" "b/LeetCode-Python/1409.\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227/1409-\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227.py" new file mode 100644 index 0000000..edec649 --- /dev/null +++ "b/LeetCode-Python/1409.\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227/1409-\346\237\245\350\257\242\345\270\246\351\224\256\347\232\204\346\216\222\345\210\227.py" @@ -0,0 +1,9 @@ +class Solution: + def processQueries(self, queries: List[int], m: int) -> List[int]: + P = [i for i in range(1, m + 1)] + res = [] + for query in queries: + index = P.index(query) + res.append(index) + P = [P[index]] + P[:index] + P[index + 1:] + return res \ No newline at end of file diff --git "a/LeetCode-Python/1418.\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250/1418-\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250.py" "b/LeetCode-Python/1418.\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250/1418-\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250.py" new file mode 100644 index 0000000..d0936a2 --- /dev/null +++ "b/LeetCode-Python/1418.\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250/1418-\347\202\271\350\217\234\345\261\225\347\244\272\350\241\250.py" @@ -0,0 +1,23 @@ +class Solution: + def displayTable(self, orders: List[List[str]]) -> List[List[str]]: + from collections import defaultdict + table_set = set() + tablefood2cnt = defaultdict(int) + food_set = set() + + for order in orders: + table, food = order[1], order[2] + food_set.add(food) + table_set.add(table) + tablefood2cnt[table + "-" + food] += 1 + + sorted_table = sorted(list(table_set), key = lambda x: int(x)) + sorted_food = sorted(list(food_set)) + res = [["Table"] + sorted_food] + + for table in sorted_table: + temp = [table] + for f in sorted_food: + temp.append(str(tablefood2cnt[table + "-" + f])) + res.append(temp) + return res \ No newline at end of file diff --git "a/LeetCode-Python/1436.\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231/1436-\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231.py" "b/LeetCode-Python/1436.\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231/1436-\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231.py" new file mode 100644 index 0000000..98bd4f3 --- /dev/null +++ "b/LeetCode-Python/1436.\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231/1436-\346\227\205\350\241\214\347\273\210\347\202\271\347\253\231.py" @@ -0,0 +1,15 @@ +class Solution: + def destCity(self, paths: List[List[str]]) -> str: + cities = set() + + src2des = {} + for path in paths: + src, des = path[0], path[1] + cities.add(src) + cities.add(des) + + src2des[src] = des + + for city in cities: + if city not in src2des: + return city \ No newline at end of file diff --git "a/LeetCode-Python/1439.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214/1439-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214.py" "b/LeetCode-Python/1439.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214/1439-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214.py" new file mode 100644 index 0000000..1659fd7 --- /dev/null +++ "b/LeetCode-Python/1439.\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214/1439-\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\232\204\347\254\254k\344\270\252\346\234\200\345\260\217\346\225\260\347\273\204\345\222\214.py" @@ -0,0 +1,24 @@ +from heapq import * +class Solution: + def kthSmallest(self, mat: List[List[int]], k: int) -> int: + m, n = len(mat), len(mat[0]) + min_sum = sum([mat[i][0] for i in range(m)]) + min_heap = [(min_sum, [0 for i in range(m)])] + cur = 0 + visited = set() + while cur < k: + s, indices = heappop(min_heap) + cur += 1 + if cur == k: + return s + + for i in range(m): + if indices[i] + 1 < n: + nxt_s = s - mat[i][indices[i]] + mat[i][indices[i] + 1] + nxt_indices = indices[:] + nxt_indices[i] += 1 + str_indices = "".join([str(i) for i in nxt_indices]) + if str_indices not in visited: + visited.add(str_indices) + heappush(min_heap, (nxt_s, nxt_indices)) + \ No newline at end of file diff --git "a/LeetCode-Python/1441.\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204/1441-\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204.py" "b/LeetCode-Python/1441.\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204/1441-\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204.py" new file mode 100644 index 0000000..5828410 --- /dev/null +++ "b/LeetCode-Python/1441.\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204/1441-\347\224\250\346\240\210\346\223\215\344\275\234\346\236\204\345\273\272\346\225\260\347\273\204.py" @@ -0,0 +1,12 @@ +class Solution: + def buildArray(self, target: List[int], n: int) -> List[str]: + cur = 1 + res = [] + for t in target: + while t > cur: + res.append("Push") + res.append("Pop") + cur += 1 + res.append("Push") + cur += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/1464.\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1464-\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257.py" "b/LeetCode-Python/1464.\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1464-\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257.py" new file mode 100644 index 0000000..6c83a66 --- /dev/null +++ "b/LeetCode-Python/1464.\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1464-\346\225\260\347\273\204\344\270\255\344\270\244\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257.py" @@ -0,0 +1,4 @@ +class Solution: + def maxProduct(self, nums: List[int]) -> int: + nums.sort() + return (nums[-1] - 1) * (nums[-2] - 1) \ No newline at end of file diff --git "a/LeetCode-Python/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271.py" "b/LeetCode-Python/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271.py" new file mode 100644 index 0000000..4efe291 --- /dev/null +++ "b/LeetCode-Python/1469.\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271/1469-\345\257\273\346\211\276\346\211\200\346\234\211\347\232\204\347\213\254\347\224\237\350\212\202\347\202\271.py" @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]: + self.res = [] + + def dfs(node): + if not node: + return + + if node.left and not node.right: + self.res.append(node.left.val) + if node.right and not node.left: + self.res.append(node.right.val) + + dfs(node.left) + dfs(node.right) + + dfs(root) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/1470.\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204/1470-\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204.py" "b/LeetCode-Python/1470.\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204/1470-\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204.py" new file mode 100644 index 0000000..7b960b4 --- /dev/null +++ "b/LeetCode-Python/1470.\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204/1470-\351\207\215\346\226\260\346\216\222\345\210\227\346\225\260\347\273\204.py" @@ -0,0 +1,5 @@ +class Solution: + def shuffle(self, nums: List[int], n: int) -> List[int]: + x = nums[:n] + y = nums[n:] + return sum([list(pair) for pair in zip(x, y)], []) \ No newline at end of file diff --git "a/LeetCode-Python/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274.py" "b/LeetCode-Python/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274.py" new file mode 100644 index 0000000..bf73beb --- /dev/null +++ "b/LeetCode-Python/1475.\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274/1475-\345\225\206\345\223\201\346\212\230\346\211\243\345\220\216\347\232\204\346\234\200\347\273\210\344\273\267\346\240\274.py" @@ -0,0 +1,12 @@ +class Solution: + def finalPrices(self, prices: List[int]) -> List[int]: + res = [price for price in prices] + stack = [] + + for i, price in enumerate(prices): + while stack and prices[stack[-1]] >= price: + last_index = stack[-1] + res[last_index] = res[last_index] - price + stack.pop() + stack.append(i) + return res \ No newline at end of file diff --git "a/LeetCode-Python/1485.\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221/1485-\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221.py" "b/LeetCode-Python/1485.\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221/1485-\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..df3e01c --- /dev/null +++ "b/LeetCode-Python/1485.\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221/1485-\345\205\213\351\232\206\345\220\253\351\232\217\346\234\272\346\214\207\351\222\210\347\232\204\344\272\214\345\217\211\346\240\221.py" @@ -0,0 +1,41 @@ +# Definition for Node. +# class Node: +# def __init__(self, val=0, left=None, right=None, random=None): +# self.val = val +# self.left = left +# self.right = right +# self.random = random + +from collections import deque +class Solution: + def copyRandomBinaryTree(self, root: 'Optional[Node]') -> 'Optional[NodeCopy]': + if not root: + return root + + old2new = dict() + queue = deque([root]) + while queue: + cur_node = queue.popleft() + new_node = NodeCopy(cur_node.val) + old2new[cur_node] = new_node + + if cur_node.left: + queue.append(cur_node.left) + if cur_node.right: + queue.append(cur_node.right) + + queue = deque([root]) + while queue: + cur_node = queue.popleft() + new_node = old2new[cur_node] + + if cur_node.left: + new_node.left = old2new[cur_node.left] + queue.append(cur_node.left) + if cur_node.right: + new_node.right = old2new[cur_node.right] + queue.append(cur_node.right) + if cur_node.random: + new_node.random = old2new[cur_node.random] + + return old2new[root] \ No newline at end of file diff --git "a/LeetCode-Python/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..d7cea34 --- /dev/null +++ "b/LeetCode-Python/1528.\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262/1528-\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,7 @@ +class Solution: + def restoreString(self, s: str, indices: List[int]) -> str: + l = ["" for _ in s] + + for i, char in enumerate(s): + l[indices[i]] = char + return "".join(l) \ No newline at end of file diff --git "a/LeetCode-Python/1552.\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233/1552-\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233.py" "b/LeetCode-Python/1552.\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233/1552-\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233.py" new file mode 100644 index 0000000..8cb54d6 --- /dev/null +++ "b/LeetCode-Python/1552.\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233/1552-\344\270\244\347\220\203\344\271\213\351\227\264\347\232\204\347\243\201\345\212\233.py" @@ -0,0 +1,18 @@ +class Solution: + def maxDistance(self, position: List[int], m: int) -> int: + position.sort() + left, right = 1, position[-1] - position[0] + while left <= right: + mid = (left + right) // 2 # target answer + + cnt, prev = 1, position[0] + for p in position: + if prev + mid <= p: # find another possible position + cnt += 1 + prev = p + + if cnt >= m: + left = mid + 1 + elif cnt < m: + right = mid - 1 + return right \ No newline at end of file diff --git "a/LeetCode-Python/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250.py" "b/LeetCode-Python/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250.py" new file mode 100644 index 0000000..d36a9b1 --- /dev/null +++ "b/LeetCode-Python/1598.\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250/1598-\346\226\207\344\273\266\345\244\271\346\223\215\344\275\234\346\227\245\345\277\227\346\220\234\351\233\206\345\231\250.py" @@ -0,0 +1,10 @@ +class Solution: + def minOperations(self, logs: List[str]) -> int: + dir_count = 0 + for log in logs: + if log == "../": + if dir_count > 0: + dir_count -= 1 + elif log != "./": + dir_count += 1 + return dir_count \ No newline at end of file diff --git "a/LeetCode-Python/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237.py" "b/LeetCode-Python/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237.py" new file mode 100644 index 0000000..d2d32c7 --- /dev/null +++ "b/LeetCode-Python/1603.\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237/1603-\350\256\276\350\256\241\345\201\234\350\275\246\347\263\273\347\273\237.py" @@ -0,0 +1,25 @@ +class ParkingSystem: + + def __init__(self, big: int, medium: int, small: int): + self.b, self.m, self.s = big, medium, small + + def addCar(self, carType: int) -> bool: + if carType == 1: + if self.b: + self.b -= 1 + return True + return False + elif carType == 2: + if self.m: + self.m -= 1 + return True + return False + elif carType == 3: + if self.s: + self.s -= 1 + return True + return False + +# Your ParkingSystem object will be instantiated and called as such: +# obj = ParkingSystem(big, medium, small) +# param_1 = obj.addCar(carType) \ No newline at end of file diff --git "a/LeetCode-Python/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246.py" "b/LeetCode-Python/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246.py" new file mode 100644 index 0000000..0c33970 --- /dev/null +++ "b/LeetCode-Python/1614.\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246/1614-\346\213\254\345\217\267\347\232\204\346\234\200\345\244\247\345\265\214\345\245\227\346\267\261\345\272\246.py" @@ -0,0 +1,12 @@ +class Solution: + def maxDepth(self, s: str) -> int: + res = 0 + cur_depth = 0 + for char in s: + if char == "(": + cur_depth += 1 + res = max(res, cur_depth) + elif char == ")": + cur_depth -= 1 + + return res \ No newline at end of file diff --git "a/LeetCode-Python/1669.\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250/1669-\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250.py" "b/LeetCode-Python/1669.\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250/1669-\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250.py" new file mode 100644 index 0000000..4b6cd69 --- /dev/null +++ "b/LeetCode-Python/1669.\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250/1669-\345\220\210\345\271\266\344\270\244\344\270\252\351\223\276\350\241\250.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode: + dummy = ListNode(-1) + dummy.next = list1 + prev, cur = dummy, list1 + left, right = None, None + count = 0 + while cur: + if count == a: + left = prev + + if count == b: + right = cur.next + + count += 1 + prev, cur = cur, cur.next + + left.next = list2 + p = list2 + while p and p.next: + p = p.next + p.next = right + return dummy.next \ No newline at end of file diff --git "a/LeetCode-Python/1678.\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250/1678-\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250.py" "b/LeetCode-Python/1678.\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250/1678-\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250.py" new file mode 100644 index 0000000..c6d4300 --- /dev/null +++ "b/LeetCode-Python/1678.\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250/1678-\350\256\276\350\256\241Goal\350\247\243\346\236\220\345\231\250.py" @@ -0,0 +1,21 @@ +class Solution: + def interpret(self, command: str) -> str: + res = "" + stack = [] + for char in command: + if char == "G": + res += char + elif char == "(": + stack.append(char) + elif char == ")": + if stack and stack[-1] == "(": + res += "o" + stack.pop() + else: + res += "al" + stack.pop() + stack.pop() + stack.pop() + else: + stack.append(char) + return res diff --git "a/LeetCode-Python/1688.\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260/1688-\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260.py" "b/LeetCode-Python/1688.\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260/1688-\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260.py" new file mode 100644 index 0000000..50a8100 --- /dev/null +++ "b/LeetCode-Python/1688.\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260/1688-\346\257\224\350\265\233\344\270\255\347\232\204\351\205\215\345\257\271\346\254\241\346\225\260.py" @@ -0,0 +1,11 @@ +class Solution: + def numberOfMatches(self, n: int) -> int: + res = 0 + while n != 1: + if n % 2: + res += (n - 1) // 2 + n = (n - 1) // 2 + 1 + else: + res += n // 2 + n = n // 2 + return res \ No newline at end of file diff --git "a/LeetCode-Python/1700.\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217/1700-\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217.py" "b/LeetCode-Python/1700.\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217/1700-\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217.py" new file mode 100644 index 0000000..1f1db8c --- /dev/null +++ "b/LeetCode-Python/1700.\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217/1700-\346\227\240\346\263\225\345\220\203\345\215\210\351\244\220\347\232\204\345\255\246\347\224\237\346\225\260\351\207\217.py" @@ -0,0 +1,16 @@ +class Solution: + def countStudents(self, students: List[int], sandwiches: List[int]) -> int: + from collections import deque + students = deque(students) + while sandwiches: + cur, l = 0, len(students) + while cur < l: + student = students.popleft() + if student == sandwiches[0]: + break + students.append(student) + cur += 1 + if cur == l: + break + sandwiches = sandwiches[1:] + return len(students) \ No newline at end of file diff --git "a/LeetCode-Python/1704.\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274/1704-\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274.py" "b/LeetCode-Python/1704.\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274/1704-\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274.py" new file mode 100644 index 0000000..b9a2ae4 --- /dev/null +++ "b/LeetCode-Python/1704.\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274/1704-\345\210\244\346\226\255\345\255\227\347\254\246\344\270\262\347\232\204\344\270\244\345\215\212\346\230\257\345\220\246\347\233\270\344\274\274.py" @@ -0,0 +1,8 @@ +class Solution: + def halvesAreAlike(self, s: str) -> bool: + s1, s2 = s[:len(s) // 2], s[len(s) // 2:] + return self.countVowels(s1) == self.countVowels(s2) + + def countVowels(self, s): + vowels = "aeiouAEIOU" + return sum(char in vowels for char in s) \ No newline at end of file diff --git "a/LeetCode-Python/1720.\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204/1720-\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204.py" "b/LeetCode-Python/1720.\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204/1720-\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204.py" new file mode 100644 index 0000000..efc2c51 --- /dev/null +++ "b/LeetCode-Python/1720.\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204/1720-\350\247\243\347\240\201\345\274\202\346\210\226\345\220\216\347\232\204\346\225\260\347\273\204.py" @@ -0,0 +1,6 @@ +class Solution: + def decode(self, encoded: List[int], first: int) -> List[int]: + res = [first] + for num in encoded: + res.append(res[-1] ^ num) + return res \ No newline at end of file diff --git "a/LeetCode-Python/1721.\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/1721-\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/LeetCode-Python/1721.\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/1721-\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..61bb471 --- /dev/null +++ "b/LeetCode-Python/1721.\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271/1721-\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,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: + p = head + l = 0 + while p: + l += 1 + p = p.next + + reversed_k = l - k + 1 + + count = 0 + p = head + while p: + count += 1 + if count == k: + left_node = p + if count == reversed_k: + right_nnode = p + p = p.next + + left_node.val, right_nnode.val = right_nnode.val, left_node.val + + return head \ No newline at end of file diff --git "a/LeetCode-Python/1738.\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274/1738-\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.py" "b/LeetCode-Python/1738.\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274/1738-\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.py" new file mode 100644 index 0000000..8dcc9f4 --- /dev/null +++ "b/LeetCode-Python/1738.\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274/1738-\346\211\276\345\207\272\347\254\254K\345\244\247\347\232\204\345\274\202\346\210\226\345\235\220\346\240\207\345\200\274.py" @@ -0,0 +1,24 @@ +from heapq import * +class Solution: + def kthLargestValue(self, matrix: List[List[int]], k: int) -> int: + m, n = len(matrix), len(matrix[0]) + xor_matrix = [[matrix[i][j] for j in range(n)] for i in range(m) ] + min_heap = [] + for i in range(m): + for j in range(n): + if i or j: + if not i: + # the first row + xor_matrix[i][j] ^= xor_matrix[i][j - 1] + elif not j: + xor_matrix[i][j] ^= xor_matrix[i - 1][j] + else: + xor_matrix[i][j] ^= xor_matrix[i][j - 1] ^ xor_matrix[i - 1][j] ^ xor_matrix[i - 1][j - 1] + if len(min_heap) < k: + heappush(min_heap, xor_matrix[i][j]) + else: + heappushpop(min_heap, xor_matrix[i][j]) + + return min_heap[0] + + \ No newline at end of file diff --git "a/LeetCode-Python/1753.\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1753-\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" "b/LeetCode-Python/1753.\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1753-\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" new file mode 100644 index 0000000..dcb4af2 --- /dev/null +++ "b/LeetCode-Python/1753.\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206/1753-\347\247\273\351\231\244\347\237\263\345\255\220\347\232\204\346\234\200\345\244\247\345\276\227\345\210\206.py" @@ -0,0 +1,15 @@ +from heapq import * +class Solution: + def maximumScore(self, a: int, b: int, c: int) -> int: + max_heap = [-a, -b, -c] + heapify(max_heap) + res = 0 + while len(max_heap) > 1: + first, second = -heappop(max_heap), -heappop(max_heap) + res += 1 + + if first > 1: + heappush(max_heap, -(first - 1)) + if second > 1: + heappush(max_heap, -(second - 1)) + return res diff --git "a/LeetCode-Python/1756.\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227/1756-\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227.py" "b/LeetCode-Python/1756.\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227/1756-\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227.py" new file mode 100644 index 0000000..36e7a7a --- /dev/null +++ "b/LeetCode-Python/1756.\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227/1756-\350\256\276\350\256\241\346\234\200\350\277\221\344\275\277\347\224\250\357\274\210MRU\357\274\211\351\230\237\345\210\227.py" @@ -0,0 +1,15 @@ +class MRUQueue: + + def __init__(self, n: int): + self.queue = [i for i in range(1, n + 1)] + + def fetch(self, k: int) -> int: + node = self.queue[k - 1] + self.queue = self.queue[:k - 1] + self.queue[k:] + [node] + return node + + + +# Your MRUQueue object will be instantiated and called as such: +# obj = MRUQueue(n) +# param_1 = obj.fetch(k) \ No newline at end of file diff --git "a/LeetCode-Python/1762.\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251/1762-\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251.py" "b/LeetCode-Python/1762.\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251/1762-\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251.py" new file mode 100644 index 0000000..ee30caa --- /dev/null +++ "b/LeetCode-Python/1762.\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251/1762-\350\203\275\347\234\213\345\210\260\346\265\267\346\231\257\347\232\204\345\273\272\347\255\221\347\211\251.py" @@ -0,0 +1,9 @@ +class Solution: + def findBuildings(self, heights: List[int]) -> List[int]: + stack = [] + for i, height in enumerate(heights): + while stack and heights[stack[-1]] <= height: + last_index = stack[-1] + stack.pop() + stack.append(i) + return sorted(stack) \ No newline at end of file diff --git "a/LeetCode-Python/1768.\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262/1768-\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/1768.\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262/1768-\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..42dd0b1 --- /dev/null +++ "b/LeetCode-Python/1768.\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262/1768-\344\272\244\346\233\277\345\220\210\345\271\266\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,12 @@ +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + res = "" + + for index, char1 in enumerate(word1): + res += char1 + if index < len(word2): + res += word2[index] + + if index < len(word2): + res += word2[index + 1:] + return res \ No newline at end of file diff --git "a/LeetCode-Python/1769.\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1769-\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" "b/LeetCode-Python/1769.\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1769-\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" new file mode 100644 index 0000000..f0ad0d4 --- /dev/null +++ "b/LeetCode-Python/1769.\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260/1769-\347\247\273\345\212\250\346\211\200\346\234\211\347\220\203\345\210\260\346\257\217\344\270\252\347\233\222\345\255\220\346\211\200\351\234\200\347\232\204\346\234\200\345\260\217\346\223\215\344\275\234\346\225\260.py" @@ -0,0 +1,15 @@ +class Solution: + def minOperations(self, boxes: str) -> List[int]: + ones = [] + for index, box in enumerate(boxes): + if box == "1": + ones.append(index) + + res = [] + for index, box in enumerate(boxes): + cur_sum = 0 + for one_index in ones: + cur_sum += abs(one_index - index) + res.append(cur_sum) + + return res diff --git "a/LeetCode-Python/1773.\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217/1773-\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217.py" "b/LeetCode-Python/1773.\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217/1773-\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217.py" new file mode 100644 index 0000000..793e12a --- /dev/null +++ "b/LeetCode-Python/1773.\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217/1773-\347\273\237\350\256\241\345\214\271\351\205\215\346\243\200\347\264\242\350\247\204\345\210\231\347\232\204\347\211\251\345\223\201\346\225\260\351\207\217.py" @@ -0,0 +1,8 @@ +class Solution: + def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: + res = 0 + key2index = {"type":0, "color":1, "name":2} + for item in items: + if item[key2index[ruleKey]] == ruleValue: + res += 1 + return res diff --git "a/LeetCode-Python/1812.\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262/1812-\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262.py" "b/LeetCode-Python/1812.\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262/1812-\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262.py" new file mode 100644 index 0000000..33ae741 --- /dev/null +++ "b/LeetCode-Python/1812.\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262/1812-\345\210\244\346\226\255\345\233\275\351\231\205\350\261\241\346\243\213\346\243\213\347\233\230\344\270\255\344\270\200\344\270\252\346\240\274\345\255\220\347\232\204\351\242\234\350\211\262.py" @@ -0,0 +1,13 @@ +class Solution: + def squareIsWhite(self, coordinates: str) -> bool: + row, col = int(coordinates[1]), ord(coordinates[0]) - ord("a") + # 0 for balck, 1 for white + if col % 2 == 0: + color = 0 + else: + color = 1 + + if row % 2 == 0: + color = 1 - color + return color == 1 + \ No newline at end of file diff --git "a/LeetCode-Python/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245.py" "b/LeetCode-Python/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245.py" new file mode 100644 index 0000000..b862937 --- /dev/null +++ "b/LeetCode-Python/1832.\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245/1832-\345\210\244\346\226\255\345\217\245\345\255\220\346\230\257\345\220\246\344\270\272\345\205\250\345\255\227\346\257\215\345\217\245.py" @@ -0,0 +1,3 @@ +class Solution: + def checkIfPangram(self, sentence: str) -> bool: + return len(set(sentence)) == 26 \ No newline at end of file diff --git "a/LeetCode-Python/1836.\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240/1836-\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.py" "b/LeetCode-Python/1836.\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240/1836-\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.py" new file mode 100644 index 0000000..ce980fd --- /dev/null +++ "b/LeetCode-Python/1836.\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240/1836-\344\273\216\346\234\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\351\207\215\345\244\215\345\205\203\347\264\240.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def deleteDuplicatesUnsorted(self, head: ListNode) -> ListNode: + visited = set() + duplicates = set() + p = head + while p: + if p.val not in visited: + visited.add(p.val) + else: + duplicates.add(p.val) + p = p.next + + dummy = ListNode(-1) + dummy.next = head + prev, cur = dummy, head + while cur: + if cur.val in duplicates: + prev.next = cur.next + cur = cur.next + else: + prev, cur = cur, cur.next + + return dummy.next diff --git "a/LeetCode-Python/1844.\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242/1844-\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.py" "b/LeetCode-Python/1844.\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242/1844-\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.py" new file mode 100644 index 0000000..3629862 --- /dev/null +++ "b/LeetCode-Python/1844.\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242/1844-\345\260\206\346\211\200\346\234\211\346\225\260\345\255\227\347\224\250\345\255\227\347\254\246\346\233\277\346\215\242.py" @@ -0,0 +1,12 @@ +class Solution: + def replaceDigits(self, s: str) -> str: + res = "" + for i in range(0, len(s), 2): + char = s[i] + + if i + 1 < len(s): + shift = int(s[i + 1]) + res += char + chr(ord(char) + shift) + else: + res += char + return res \ No newline at end of file diff --git "a/LeetCode-Python/1860.\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262/1860-\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262.py" "b/LeetCode-Python/1860.\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262/1860-\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262.py" new file mode 100644 index 0000000..c18d4ca --- /dev/null +++ "b/LeetCode-Python/1860.\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262/1860-\345\242\236\351\225\277\347\232\204\345\206\205\345\255\230\346\263\204\351\234\262.py" @@ -0,0 +1,18 @@ +class Solution: + def memLeak(self, memory1: int, memory2: int) -> List[int]: + res = [] + memory = 1 + while memory1 or memory2: + if memory1 >= memory2: + if memory1 < memory: + break + else: + memory1 -= memory + else: + if memory2 < memory: + break + else: + memory2 -= memory + + memory += 1 + return [memory, memory1, memory2] \ No newline at end of file diff --git "a/LeetCode-Python/1863.\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214/1863-\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214.py" "b/LeetCode-Python/1863.\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214/1863-\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214.py" new file mode 100644 index 0000000..fb765fa --- /dev/null +++ "b/LeetCode-Python/1863.\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214/1863-\346\211\276\345\207\272\346\211\200\346\234\211\345\255\220\351\233\206\347\232\204\345\274\202\346\210\226\346\200\273\345\222\214\345\206\215\346\261\202\345\222\214.py" @@ -0,0 +1,12 @@ +class Solution: + def subsetXORSum(self, nums: List[int]) -> int: + subsets = [[]] + res = 0 + for num in nums: + new_subsets = [] + for subset in subsets: + new_subset = subset + [num] + res += reduce(lambda x, y: x^y, new_subset) + new_subsets.append(new_subset) + subsets += new_subsets + return res \ No newline at end of file diff --git "a/LeetCode-Python/1874.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214/1874-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214.py" "b/LeetCode-Python/1874.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214/1874-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214.py" new file mode 100644 index 0000000..62d9a28 --- /dev/null +++ "b/LeetCode-Python/1874.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214/1874-\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\344\271\230\347\247\257\345\222\214.py" @@ -0,0 +1,10 @@ +class Solution: + def minProductSum(self, nums1: List[int], nums2: List[int]) -> int: + nums1.sort() + nums2.sort() + # print(nums1, nums2[::-1]) + res = 0 + for i in range(len(nums1)): + res += nums1[i] * nums2[-(i + 1)] + + return res \ No newline at end of file diff --git "a/LeetCode-Python/1880.\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214/1880-\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214.py" "b/LeetCode-Python/1880.\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214/1880-\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214.py" new file mode 100644 index 0000000..fad2cee --- /dev/null +++ "b/LeetCode-Python/1880.\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214/1880-\346\243\200\346\237\245\346\237\220\345\215\225\350\257\215\346\230\257\345\220\246\347\255\211\344\272\216\344\270\244\345\215\225\350\257\215\344\271\213\345\222\214.py" @@ -0,0 +1,9 @@ +class Solution: + def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool: + return self.sumOfDigit(firstWord) + self.sumOfDigit(secondWord) == self.sumOfDigit(targetWord) + + def sumOfDigit(self, word): + res = "" + for char in word: + res += str(ord(char) - ord("a")) + return int(res) \ No newline at end of file diff --git "a/LeetCode-Python/1920.\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204/1920-\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204.py" "b/LeetCode-Python/1920.\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204/1920-\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204.py" new file mode 100644 index 0000000..bbff3e1 --- /dev/null +++ "b/LeetCode-Python/1920.\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204/1920-\345\237\272\344\272\216\346\216\222\345\210\227\346\236\204\345\273\272\346\225\260\347\273\204.py" @@ -0,0 +1,3 @@ +class Solution: + def buildArray(self, nums: List[int]) -> List[int]: + return [nums[num] for num in nums] \ No newline at end of file diff --git "a/LeetCode-Python/1929.\346\225\260\347\273\204\344\270\262\350\201\224/1929-\346\225\260\347\273\204\344\270\262\350\201\224.py" "b/LeetCode-Python/1929.\346\225\260\347\273\204\344\270\262\350\201\224/1929-\346\225\260\347\273\204\344\270\262\350\201\224.py" new file mode 100644 index 0000000..5e822e9 --- /dev/null +++ "b/LeetCode-Python/1929.\346\225\260\347\273\204\344\270\262\350\201\224/1929-\346\225\260\347\273\204\344\270\262\350\201\224.py" @@ -0,0 +1,3 @@ +class Solution: + def getConcatenation(self, nums: List[int]) -> List[int]: + return nums + nums \ No newline at end of file diff --git "a/LeetCode-Python/1941.\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214/1941-\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214.py" "b/LeetCode-Python/1941.\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214/1941-\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214.py" new file mode 100644 index 0000000..43abcbc --- /dev/null +++ "b/LeetCode-Python/1941.\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214/1941-\346\243\200\346\237\245\346\230\257\345\220\246\346\211\200\346\234\211\345\255\227\347\254\246\345\207\272\347\216\260\346\254\241\346\225\260\347\233\270\345\220\214.py" @@ -0,0 +1,5 @@ +class Solution: + def areOccurrencesEqual(self, s: str) -> bool: + from collections import Counter + c = Counter(s) + return 1 == len(set(c.values())) \ No newline at end of file diff --git "a/LeetCode-Python/1945.\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214/1945-\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214.py" "b/LeetCode-Python/1945.\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214/1945-\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214.py" new file mode 100644 index 0000000..66328fc --- /dev/null +++ "b/LeetCode-Python/1945.\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214/1945-\345\255\227\347\254\246\344\270\262\350\275\254\345\214\226\345\220\216\347\232\204\345\220\204\344\275\215\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -0,0 +1,16 @@ +class Solution: + def getLucky(self, s: str, k: int) -> int: + + num = "" + for char in s: + num += str(ord(char) - ord("a") + 1) + + num = int(num) + while k: + new_num = 0 + while num: + num, m = divmod(num, 10) + new_num += m + num = new_num + k -= 1 + return num diff --git "a/LeetCode-Python/1967.\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/1967-\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" "b/LeetCode-Python/1967.\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/1967-\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" new file mode 100644 index 0000000..72ad316 --- /dev/null +++ "b/LeetCode-Python/1967.\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/1967-\344\275\234\344\270\272\345\255\220\345\255\227\347\254\246\344\270\262\345\207\272\347\216\260\345\234\250\345\215\225\350\257\215\344\270\255\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" @@ -0,0 +1,9 @@ +class Solution: + def numOfStrings(self, patterns: List[str], word: str) -> int: + + res = 0 + for pattern in patterns: + if pattern in word: + res += 1 + + return res \ No newline at end of file diff --git "a/LeetCode-Python/2000.\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200/2000-\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200.py" "b/LeetCode-Python/2000.\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200/2000-\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200.py" new file mode 100644 index 0000000..7477494 --- /dev/null +++ "b/LeetCode-Python/2000.\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200/2000-\345\217\215\350\275\254\345\215\225\350\257\215\345\211\215\347\274\200.py" @@ -0,0 +1,7 @@ +class Solution: + def reversePrefix(self, word: str, ch: str) -> str: + if word.count(ch): + index = word.index(ch) + return word[:index + 1][::-1] + word[index + 1:] + else: + return word \ No newline at end of file diff --git "a/LeetCode-Python/2011.\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/2011-\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274.py" "b/LeetCode-Python/2011.\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/2011-\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274.py" new file mode 100644 index 0000000..e1aed68 --- /dev/null +++ "b/LeetCode-Python/2011.\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274/2011-\346\211\247\350\241\214\346\223\215\344\275\234\345\220\216\347\232\204\345\217\230\351\207\217\345\200\274.py" @@ -0,0 +1,10 @@ +class Solution: + def finalValueAfterOperations(self, operations: List[str]) -> int: + res = 0 + for operation in operations: + if operation[0] == "-" or operation[-1] == "-": + res-= 1 + else: + res += 1 + + return res \ No newline at end of file diff --git "a/LeetCode-Python/2089.\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207/2089-\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207.py" "b/LeetCode-Python/2089.\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207/2089-\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207.py" new file mode 100644 index 0000000..e33ddba --- /dev/null +++ "b/LeetCode-Python/2089.\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207/2089-\346\211\276\345\207\272\346\225\260\347\273\204\346\216\222\345\272\217\345\220\216\347\232\204\347\233\256\346\240\207\344\270\213\346\240\207.py" @@ -0,0 +1,8 @@ +class Solution: + def targetIndices(self, nums: List[int], target: int) -> List[int]: + nums.sort() + res = [] + for i, num in enumerate(nums): + if num == target: + res.append(i) + return res \ No newline at end of file diff --git "a/LeetCode-Python/2103.\347\216\257\345\222\214\346\235\206/2103-\347\216\257\345\222\214\346\235\206.py" "b/LeetCode-Python/2103.\347\216\257\345\222\214\346\235\206/2103-\347\216\257\345\222\214\346\235\206.py" new file mode 100644 index 0000000..a1afb38 --- /dev/null +++ "b/LeetCode-Python/2103.\347\216\257\345\222\214\346\235\206/2103-\347\216\257\345\222\214\346\235\206.py" @@ -0,0 +1,14 @@ +class Solution: + def countPoints(self, rings: str) -> int: + from collections import defaultdict + + ring2color = defaultdict(set) + for index in range(0, len(rings), 2): + color, ring = rings[index], rings[index + 1] + + ring2color[ring].add(color) + res = 0 + for ring, color in ring2color.items(): + if len(color) == 3: + res += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/2108.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/2108-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/2108.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/2108-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..b2fb005 --- /dev/null +++ "b/LeetCode-Python/2108.\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262/2108-\346\211\276\345\207\272\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254\344\270\200\344\270\252\345\233\236\346\226\207\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,6 @@ +class Solution: + def firstPalindrome(self, words: List[str]) -> str: + for word in words: + if word == word[::-1]: + return word + return "" \ No newline at end of file diff --git "a/LeetCode-Python/2114.\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260/2114-\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260.py" "b/LeetCode-Python/2114.\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260/2114-\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260.py" new file mode 100644 index 0000000..02760a0 --- /dev/null +++ "b/LeetCode-Python/2114.\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260/2114-\345\217\245\345\255\220\344\270\255\347\232\204\346\234\200\345\244\232\345\215\225\350\257\215\346\225\260.py" @@ -0,0 +1,8 @@ +class Solution: + def mostWordsFound(self, sentences: List[str]) -> int: + res = 0 + + for sentence in sentences: + word_count = len(sentence.split()) + res = max(res, word_count) + return res \ No newline at end of file diff --git "a/LeetCode-Python/2120.\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244/2120-\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244.py" "b/LeetCode-Python/2120.\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244/2120-\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244.py" new file mode 100644 index 0000000..611fcfd --- /dev/null +++ "b/LeetCode-Python/2120.\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244/2120-\346\211\247\350\241\214\346\211\200\346\234\211\345\220\216\347\274\200\346\214\207\344\273\244.py" @@ -0,0 +1,27 @@ +class Solution: + def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]: + res = [] + for i, _ in enumerate(s): + j = i + cur_row, cur_col = startPos[0], startPos[1] + while 1: + if j == len(s): + break + ins = s[j] + if ins == "R": + cur_col += 1 + elif ins == "L": + cur_col -= 1 + elif ins == "U": + cur_row -= 1 + elif ins == "D": + cur_row += 1 + j += 1 + if self.moveOutside(cur_row, cur_col, n): + j -= 1 + break + res.append(j - i) + return res + + def moveOutside(self, cur_row, cur_col, n): + return not 0 <= cur_row < n or not 0 <= cur_col < n diff --git "a/LeetCode-Python/2125.\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/2125-\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217.py" "b/LeetCode-Python/2125.\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/2125-\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217.py" new file mode 100644 index 0000000..3ad4623 --- /dev/null +++ "b/LeetCode-Python/2125.\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217/2125-\351\223\266\350\241\214\344\270\255\347\232\204\346\277\200\345\205\211\346\235\237\346\225\260\351\207\217.py" @@ -0,0 +1,10 @@ +class Solution: + def numberOfBeams(self, bank: List[str]) -> int: + res = 0 + last_device_count = 0 + for row_index, row in enumerate(bank): + cur_device_count = row.count("1") + res += cur_device_count * last_device_count + if cur_device_count: + last_device_count = cur_device_count + return res \ No newline at end of file diff --git "a/LeetCode-Python/2130.\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214/2130-\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214.py" "b/LeetCode-Python/2130.\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214/2130-\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214.py" new file mode 100644 index 0000000..05973ff --- /dev/null +++ "b/LeetCode-Python/2130.\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214/2130-\351\223\276\350\241\250\346\234\200\345\244\247\345\255\252\347\224\237\345\222\214.py" @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def pairSum(self, head: Optional[ListNode]) -> int: + p, l = head, 0 + while p: + l += 1 + p = p.next + + stack = [] + cur = 0 + p = head + while cur < l // 2: + cur += 1 + stack.append(p.val) + p = p.next + + res = 0 + while cur < l: + cur += 1 + res = max(res, p.val + stack.pop()) + p = p.next + return res \ No newline at end of file diff --git "a/LeetCode-Python/2149.\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204/2149-\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204.py" "b/LeetCode-Python/2149.\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204/2149-\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204.py" new file mode 100644 index 0000000..b2f3320 --- /dev/null +++ "b/LeetCode-Python/2149.\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204/2149-\346\214\211\347\254\246\345\217\267\351\207\215\346\216\222\346\225\260\347\273\204.py" @@ -0,0 +1,17 @@ +class Solution: + def rearrangeArray(self, nums: List[int]) -> List[int]: + pos, neg = [], [] + + for num in nums: + if num > 0: + pos.append(num) + else: + neg.append(num) + + t = [(pos[i], neg[i]) for i in range(len(pos))] + + res = [] + for pair in t: + res.append(pair[0]) + res.append(pair[1]) + return res \ No newline at end of file diff --git "a/LeetCode-Python/2154.\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452/2154-\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452.py" "b/LeetCode-Python/2154.\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452/2154-\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452.py" new file mode 100644 index 0000000..6c09e8b --- /dev/null +++ "b/LeetCode-Python/2154.\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452/2154-\345\260\206\346\211\276\345\210\260\347\232\204\345\200\274\344\271\230\344\273\2452.py" @@ -0,0 +1,6 @@ +class Solution: + def findFinalValue(self, nums: List[int], original: int) -> int: + s = set(nums) + while original in s: + original *= 2 + return original \ No newline at end of file diff --git "a/LeetCode-Python/2161.\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204/2161-\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204.py" "b/LeetCode-Python/2161.\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204/2161-\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204.py" new file mode 100644 index 0000000..1f17945 --- /dev/null +++ "b/LeetCode-Python/2161.\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204/2161-\346\240\271\346\215\256\347\273\231\345\256\232\346\225\260\345\255\227\345\210\222\345\210\206\346\225\260\347\273\204.py" @@ -0,0 +1,13 @@ +class Solution: + def pivotArray(self, nums: List[int], pivot: int) -> List[int]: + small, equal, larger = [], 0, [] + + for num in nums: + if num < pivot: + small.append(num) + elif num == pivot: + equal += 1 + else: + larger.append(num) + + return small + equal * [pivot] + larger \ No newline at end of file diff --git "a/LeetCode-Python/2169.\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260/2169-\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260.py" "b/LeetCode-Python/2169.\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260/2169-\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260.py" new file mode 100644 index 0000000..5c7e93e --- /dev/null +++ "b/LeetCode-Python/2169.\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260/2169-\345\276\227\345\210\2600\347\232\204\346\223\215\344\275\234\346\225\260.py" @@ -0,0 +1,11 @@ +class Solution: + def countOperations(self, num1: int, num2: int) -> int: + res = 0 + while num1 and num2: + res += 1 + if num1 >= num2: + num1 -= num2 + else: + num2 -= num1 + + return res \ No newline at end of file diff --git "a/LeetCode-Python/2181.\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271/2181-\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271.py" "b/LeetCode-Python/2181.\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271/2181-\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271.py" new file mode 100644 index 0000000..fbb8dcd --- /dev/null +++ "b/LeetCode-Python/2181.\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271/2181-\345\220\210\345\271\266\351\233\266\344\271\213\351\227\264\347\232\204\350\212\202\347\202\271.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 mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(-1) + p_new = dummy + + p = head + cur_sum = 0 + while p: + if p.val == 0: + if cur_sum: + node = ListNode(cur_sum) + p_new.next = node + p_new = p_new.next + cur_sum = 0 + else: + cur_sum += p.val + p = p.next + + return dummy.next \ No newline at end of file diff --git "a/LeetCode-Python/2185.\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262/2185-\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/2185.\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262/2185-\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..b618879 --- /dev/null +++ "b/LeetCode-Python/2185.\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262/2185-\347\273\237\350\256\241\345\214\205\345\220\253\347\273\231\345\256\232\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,8 @@ +class Solution: + def prefixCount(self, words: List[str], pref: str) -> int: + res = 0 + + for word in words: + if word.startswith(pref): + res += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/2194.Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274/2194-Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274.py" "b/LeetCode-Python/2194.Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274/2194-Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274.py" new file mode 100644 index 0000000..f5008cd --- /dev/null +++ "b/LeetCode-Python/2194.Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274/2194-Excel\350\241\250\344\270\255\346\237\220\344\270\252\350\214\203\345\233\264\345\206\205\347\232\204\345\215\225\345\205\203\346\240\274.py" @@ -0,0 +1,10 @@ +class Solution: + def cellsInRange(self, s: str) -> List[str]: + res = [] + start_row, end_row = int(s[1]), int(s[-1]) + start_col, end_col = s[0], s[3] + + for cur in range(ord(start_col), ord(end_col) + 1): + for row in range(start_row, end_row + 1): + res.append(chr(cur) + str(row)) + return res \ No newline at end of file diff --git "a/LeetCode-Python/2221.\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214/2221-\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214.py" "b/LeetCode-Python/2221.\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214/2221-\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214.py" new file mode 100644 index 0000000..d755d2b --- /dev/null +++ "b/LeetCode-Python/2221.\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214/2221-\346\225\260\347\273\204\347\232\204\344\270\211\350\247\222\345\222\214.py" @@ -0,0 +1,6 @@ +class Solution: + def triangularSum(self, nums: List[int]) -> int: + if len(nums) == 1: + return nums[0] + + return self.triangularSum([nums[i] + nums[i - 1] for i in range(1, len(nums))]) % 10 \ No newline at end of file diff --git "a/LeetCode-Python/2231.\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/2231-\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" "b/LeetCode-Python/2231.\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/2231-\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" new file mode 100644 index 0000000..07efd32 --- /dev/null +++ "b/LeetCode-Python/2231.\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/2231-\346\214\211\345\245\207\345\201\266\346\200\247\344\272\244\346\215\242\345\220\216\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" @@ -0,0 +1,12 @@ +class Solution: + def largestInteger(self, num: int) -> int: + odd = sorted([int(digit) for digit in str(num) if digit in "13579"]) + even = sorted([int(digit) for digit in str(num) if digit not in "13579"]) + res = 0 + for digit in str(num): + if int(digit) % 2: + res = res * 10 + odd.pop() + else: + res = res * 10 + even.pop() + + return res diff --git "a/LeetCode-Python/2235.\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240/2235-\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240.py" "b/LeetCode-Python/2235.\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240/2235-\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240.py" new file mode 100644 index 0000000..a0a7c1f --- /dev/null +++ "b/LeetCode-Python/2235.\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240/2235-\344\270\244\346\225\264\346\225\260\347\233\270\345\212\240.py" @@ -0,0 +1,3 @@ +class Solution: + def sum(self, num1: int, num2: int) -> int: + return num1 + num2 \ No newline at end of file diff --git "a/LeetCode-Python/2236.\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214/2236-\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214.py" "b/LeetCode-Python/2236.\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214/2236-\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214.py" new file mode 100644 index 0000000..157a3c8 --- /dev/null +++ "b/LeetCode-Python/2236.\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214/2236-\345\210\244\346\226\255\346\240\271\347\273\223\347\202\271\346\230\257\345\220\246\347\255\211\344\272\216\345\255\220\347\273\223\347\202\271\344\271\213\345\222\214.py" @@ -0,0 +1,9 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def checkTree(self, root: Optional[TreeNode]) -> bool: + return root.val == root.left.val + root.right.val \ No newline at end of file diff --git "a/LeetCode-Python/2255.\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/2255-\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" "b/LeetCode-Python/2255.\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/2255-\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" new file mode 100644 index 0000000..0966cdd --- /dev/null +++ "b/LeetCode-Python/2255.\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256/2255-\347\273\237\350\256\241\346\230\257\347\273\231\345\256\232\345\255\227\347\254\246\344\270\262\345\211\215\347\274\200\347\232\204\345\255\227\347\254\246\344\270\262\346\225\260\347\233\256.py" @@ -0,0 +1,7 @@ +class Solution: + def countPrefixes(self, words: List[str], s: str) -> int: + res = 0 + for word in words: + if s.startswith(word): + res += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/2265.\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260/2265-\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260.py" "b/LeetCode-Python/2265.\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260/2265-\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260.py" new file mode 100644 index 0000000..a326504 --- /dev/null +++ "b/LeetCode-Python/2265.\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260/2265-\347\273\237\350\256\241\345\200\274\347\255\211\344\272\216\345\255\220\346\240\221\345\271\263\345\235\207\345\200\274\347\232\204\350\212\202\347\202\271\346\225\260.py" @@ -0,0 +1,28 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def averageOfSubtree(self, root: Optional[TreeNode]) -> int: + + self.res = 0 + def getAverageOfSubtree(node): + # post-order traversal + # return sum count + if not node: + return 0, 0 + + left_subtree_sum, left_count = getAverageOfSubtree(node.left) + right_subtree_sum, right_count = getAverageOfSubtree(node.right) + + subtree_sum = node.val + left_subtree_sum + right_subtree_sum + subtree_count = left_count + right_count + 1 + if node.val == subtree_sum // subtree_count: + self.res += 1 + + return subtree_sum, subtree_count + + getAverageOfSubtree(root) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/2278.\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224/2278-\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224.py" "b/LeetCode-Python/2278.\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224/2278-\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224.py" new file mode 100644 index 0000000..2974f8e --- /dev/null +++ "b/LeetCode-Python/2278.\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224/2278-\345\255\227\346\257\215\345\234\250\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\347\231\276\345\210\206\346\257\224.py" @@ -0,0 +1,3 @@ +class Solution: + def percentageLetter(self, s: str, letter: str) -> int: + return 100 * s.count(letter) // len(s) \ No newline at end of file diff --git "a/LeetCode-Python/2283.\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274/2283-\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274.py" "b/LeetCode-Python/2283.\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274/2283-\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274.py" new file mode 100644 index 0000000..8f5aabe --- /dev/null +++ "b/LeetCode-Python/2283.\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274/2283-\345\210\244\346\226\255\344\270\200\344\270\252\346\225\260\347\232\204\346\225\260\345\255\227\350\256\241\346\225\260\346\230\257\345\220\246\347\255\211\344\272\216\346\225\260\344\275\215\347\232\204\345\200\274.py" @@ -0,0 +1,10 @@ +class Solution: + def digitCount(self, num: str) -> bool: + from collections import Counter + c = Counter(num) + + for index, n in enumerate(num): + if c[str(index)] != int(n): + # print(c[index], index, int(n)) + return False + return True \ No newline at end of file diff --git "a/LeetCode-Python/2309.\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215/2309-\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215.py" "b/LeetCode-Python/2309.\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215/2309-\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215.py" new file mode 100644 index 0000000..00682ae --- /dev/null +++ "b/LeetCode-Python/2309.\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215/2309-\345\205\274\345\205\267\345\244\247\345\260\217\345\206\231\347\232\204\346\234\200\345\245\275\350\213\261\346\226\207\345\255\227\346\257\215.py" @@ -0,0 +1,9 @@ +class Solution: + def greatestLetter(self, s: str) -> str: + res = "" + s = set(s) + for char in s: + if char.lower() in s and char.upper() in s: + if not res or char.upper() > res: + res = char.upper() + return res \ No newline at end of file diff --git "a/LeetCode-Python/2315.\347\273\237\350\256\241\346\230\237\345\217\267/2315-\347\273\237\350\256\241\346\230\237\345\217\267.py" "b/LeetCode-Python/2315.\347\273\237\350\256\241\346\230\237\345\217\267/2315-\347\273\237\350\256\241\346\230\237\345\217\267.py" new file mode 100644 index 0000000..cfed43d --- /dev/null +++ "b/LeetCode-Python/2315.\347\273\237\350\256\241\346\230\237\345\217\267/2315-\347\273\237\350\256\241\346\230\237\345\217\267.py" @@ -0,0 +1,10 @@ +class Solution: + def countAsterisks(self, s: str) -> int: + bar = False + res = 0 + for char in s: + if char == "|": + bar = not bar + if not bar and char == "*": + res += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/2319.\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265/2319-\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265.py" "b/LeetCode-Python/2319.\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265/2319-\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265.py" new file mode 100644 index 0000000..c8d94e1 --- /dev/null +++ "b/LeetCode-Python/2319.\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265/2319-\345\210\244\346\226\255\347\237\251\351\230\265\346\230\257\345\220\246\346\230\257\344\270\200\344\270\252X\347\237\251\351\230\265.py" @@ -0,0 +1,15 @@ +class Solution: + def checkXMatrix(self, grid: List[List[int]]) -> bool: + if not grid or not grid[0]: + return False + m, n = len(grid), len(grid[0]) + + for i in range(m): + for j in range(n): + if i == j or i + j == n - 1: + if grid[i][j] == 0: + return False + else: + if grid[i][j] != 0: + return False + return True \ No newline at end of file diff --git "a/LeetCode-Python/2330.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV/2330-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV.py" "b/LeetCode-Python/2330.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV/2330-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV.py" new file mode 100644 index 0000000..2ce85bf --- /dev/null +++ "b/LeetCode-Python/2330.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV/2330-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207IV.py" @@ -0,0 +1,12 @@ +class Solution: + def makePalindrome(self, s: str) -> bool: + left, right = 0, len(s) - 1 + + step = 0 + while left < right: + if s[left] != s[right]: + step += 1 + left += 1 + right -= 1 + + return step <= 2 \ No newline at end of file diff --git "a/LeetCode-Python/2331.\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274/2331-\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274.py" "b/LeetCode-Python/2331.\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274/2331-\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274.py" new file mode 100644 index 0000000..29c46c5 --- /dev/null +++ "b/LeetCode-Python/2331.\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274/2331-\350\256\241\347\256\227\345\270\203\345\260\224\344\272\214\345\217\211\346\240\221\347\232\204\345\200\274.py" @@ -0,0 +1,16 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def evaluateTree(self, root: Optional[TreeNode]) -> bool: + if root.val == 0: + return False + if root.val == 1: + return True + if root.val == 2: + return self.evaluateTree(root.left) or self.evaluateTree(root.right) + if root.val == 3: + return self.evaluateTree(root.left) and self.evaluateTree(root.right) \ No newline at end of file diff --git "a/LeetCode-Python/2335.\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277/2335-\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277.py" "b/LeetCode-Python/2335.\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277/2335-\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277.py" new file mode 100644 index 0000000..7775e85 --- /dev/null +++ "b/LeetCode-Python/2335.\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277/2335-\350\243\205\346\273\241\346\235\257\345\255\220\351\234\200\350\246\201\347\232\204\346\234\200\347\237\255\346\200\273\346\227\266\351\225\277.py" @@ -0,0 +1,16 @@ +from heapq import * +class Solution: + def fillCups(self, amount: List[int]) -> int: + max_heap = [-a for a in amount if a] + heapify(max_heap) + res = 0 + while len(max_heap) > 1: + first, second = -heappop(max_heap), -heappop(max_heap) + if first > 1: + heappush(max_heap, -(first - 1)) + if second > 1: + heappush(max_heap, -(second - 1)) + res += 1 + + return res + -max_heap[0] if max_heap else res + \ No newline at end of file diff --git "a/LeetCode-Python/2336.\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/2336-\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" "b/LeetCode-Python/2336.\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/2336-\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" new file mode 100644 index 0000000..65801ca --- /dev/null +++ "b/LeetCode-Python/2336.\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/2336-\346\227\240\351\231\220\351\233\206\344\270\255\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" @@ -0,0 +1,23 @@ +from heapq import * +class SmallestInfiniteSet: + + def __init__(self): + self.min_heap = [i for i in range(1, 1001)] + heapify(self.min_heap) + self.set = set(self.min_heap) + + def popSmallest(self) -> int: + val = heappop(self.min_heap) + self.set.remove(val) + return val + + def addBack(self, num: int) -> None: + if num not in self.set: + heappush(self.min_heap, num) + self.set.add(num) + + +# Your SmallestInfiniteSet object will be instantiated and called as such: +# obj = SmallestInfiniteSet() +# param_1 = obj.popSmallest() +# obj.addBack(num) \ No newline at end of file diff --git "a/LeetCode-Python/2352.\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271/2352-\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271.py" "b/LeetCode-Python/2352.\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271/2352-\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271.py" new file mode 100644 index 0000000..6b08e20 --- /dev/null +++ "b/LeetCode-Python/2352.\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271/2352-\347\233\270\347\255\211\350\241\214\345\210\227\345\257\271.py" @@ -0,0 +1,8 @@ +class Solution: + def equalPairs(self, grid: List[List[int]]) -> int: + col_grid = [list(col) for col in zip(*grid)] + res = 0 + for row in grid: + for col in col_grid: + res += row == col + return res \ No newline at end of file diff --git "a/LeetCode-Python/2357.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266/2357-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266.py" "b/LeetCode-Python/2357.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266/2357-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266.py" new file mode 100644 index 0000000..23592d7 --- /dev/null +++ "b/LeetCode-Python/2357.\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266/2357-\344\275\277\346\225\260\347\273\204\344\270\255\346\211\200\346\234\211\345\205\203\347\264\240\351\203\275\347\255\211\344\272\216\351\233\266.py" @@ -0,0 +1,13 @@ +class Solution: + def minimumOperations(self, nums: List[int]) -> int: + res = 0 + while sum(nums): + res += 1 + m = 101 + for num in nums: + if num: + m = min(m, num) + for i, num in enumerate(nums): + if num: + nums[i] -= m + return res \ No newline at end of file diff --git "a/LeetCode-Python/2391.\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264/2391-\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264.py" "b/LeetCode-Python/2391.\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264/2391-\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264.py" new file mode 100644 index 0000000..95d587a --- /dev/null +++ "b/LeetCode-Python/2391.\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264/2391-\346\224\266\351\233\206\345\236\203\345\234\276\347\232\204\346\234\200\345\260\221\346\200\273\346\227\266\351\227\264.py" @@ -0,0 +1,36 @@ +class Solution: + def garbageCollection(self, garbage: List[str], travel: List[int]) -> int: + t_m, t_p, t_g = 0, 0, 0 + + driving_time_m = 0 + driving_time_p = 0 + driving_time_g = 0 + for index, g in enumerate(garbage): + # M + count_m = g.count("M") + if index: + driving_time_m += travel[index - 1] + if count_m: + t_m += count_m + t_m += driving_time_m + driving_time_m = 0 + + + count_p = g.count("P") + if index: + driving_time_p += travel[index - 1] + if count_p: + t_p += count_p + t_p += driving_time_p + driving_time_p = 0 + + + count_g = g.count("G") + if index: + driving_time_g += travel[index - 1] + if count_g: + t_g += count_g + t_g += driving_time_g + driving_time_g = 0 + + return t_m + t_p + t_g \ No newline at end of file diff --git "a/LeetCode-Python/2396.\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227/2396-\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227.py" "b/LeetCode-Python/2396.\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227/2396-\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..a6f7c89 --- /dev/null +++ "b/LeetCode-Python/2396.\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227/2396-\344\270\245\346\240\274\345\233\236\346\226\207\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,11 @@ +class Solution: + def isStrictlyPalindromic(self, n: int) -> bool: + for k in range(2, n - 1): + bk = "" + temp = n + while temp: + temp, m = divmod(temp, k) + bk += str(m) + if bk != bk[::-1]: + return False + return True \ No newline at end of file diff --git "a/LeetCode-Python/2399.\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273/2399-\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273.py" "b/LeetCode-Python/2399.\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273/2399-\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273.py" new file mode 100644 index 0000000..f9b5b32 --- /dev/null +++ "b/LeetCode-Python/2399.\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273/2399-\346\243\200\346\237\245\347\233\270\345\220\214\345\255\227\346\257\215\351\227\264\347\232\204\350\267\235\347\246\273.py" @@ -0,0 +1,9 @@ +class Solution: + def checkDistances(self, s: str, distance: List[int]) -> bool: + res = True + for i, d in enumerate(distance): + char = chr(ord("a") + i) + if s.count(char): + if s.rfind(char) - s.index(char) != d + 1: + res = False + return res \ No newline at end of file diff --git "a/LeetCode-Python/2413.\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260/2413-\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260.py" "b/LeetCode-Python/2413.\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260/2413-\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260.py" new file mode 100644 index 0000000..c5d3e13 --- /dev/null +++ "b/LeetCode-Python/2413.\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260/2413-\346\234\200\345\260\217\345\201\266\345\200\215\346\225\260.py" @@ -0,0 +1,3 @@ +class Solution: + def smallestEvenMultiple(self, n: int) -> int: + return 2 * n if n % 2 else n \ No newline at end of file diff --git "a/LeetCode-Python/2418.\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217/2418-\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217.py" "b/LeetCode-Python/2418.\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217/2418-\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217.py" new file mode 100644 index 0000000..ae9c914 --- /dev/null +++ "b/LeetCode-Python/2418.\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217/2418-\346\214\211\350\272\253\351\253\230\346\216\222\345\272\217.py" @@ -0,0 +1,6 @@ +class Solution: + def sortPeople(self, names: List[str], heights: List[int]) -> List[str]: + combine = [(name, heights[index]) for index, name in enumerate(names)] + + + return [pair[0] for pair in sorted(combine, key = lambda x: -x[1])] \ No newline at end of file diff --git "a/LeetCode-Python/2441.\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260/2441-\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260.py" "b/LeetCode-Python/2441.\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260/2441-\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260.py" new file mode 100644 index 0000000..fc19c6b --- /dev/null +++ "b/LeetCode-Python/2441.\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260/2441-\344\270\216\345\257\271\345\272\224\350\264\237\346\225\260\345\220\214\346\227\266\345\255\230\345\234\250\347\232\204\346\234\200\345\244\247\346\255\243\346\225\264\346\225\260.py" @@ -0,0 +1,8 @@ +class Solution: + def findMaxK(self, nums: List[int]) -> int: + s = set(nums) + res = -1 + for num in nums: + if -num in s: + res = max(num, -num, res) + return res \ No newline at end of file diff --git "a/LeetCode-Python/2446.\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201/2446-\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201.py" "b/LeetCode-Python/2446.\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201/2446-\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201.py" new file mode 100644 index 0000000..15bcc06 --- /dev/null +++ "b/LeetCode-Python/2446.\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201/2446-\345\210\244\346\226\255\344\270\244\344\270\252\344\272\213\344\273\266\346\230\257\345\220\246\345\255\230\345\234\250\345\206\262\347\252\201.py" @@ -0,0 +1,8 @@ +class Solution: + def haveConflict(self, event1: List[str], event2: List[str]) -> bool: + return not (self.time1EarlierThanTime2(event1[1], event2[0]) or self.time1EarlierThanTime2(event2[1], event1[0])) + + def time1EarlierThanTime2(self, time1, time2): + h1, h2 = int(time1[:2]), int(time2[:2]) + m1, m2 = int(time1[3:]), int(time2[3:]) + return h1 < h2 or (h1 == h2 and m1 < m2) \ No newline at end of file diff --git "a/LeetCode-Python/2451.\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262/2451-\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/2451.\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262/2451-\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..b713bed --- /dev/null +++ "b/LeetCode-Python/2451.\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262/2451-\345\267\256\345\200\274\346\225\260\347\273\204\344\270\215\345\220\214\347\232\204\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,17 @@ +class Solution: + def oddString(self, words: List[str]) -> str: + l = [] + for word in words: + diff = [] + for i, char in enumerate(word): + if i: + diff.append(ord(char) - ord(word[i - 1])) + l.append(diff) + # print(l) + + if l[0] != l[1] and l[0] != l[2]: + return words[0] + + for i, diff in enumerate(l): + if diff != l[0]: + return words[i] \ No newline at end of file diff --git "a/LeetCode-Python/2455.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274/2455-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274.py" "b/LeetCode-Python/2455.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274/2455-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274.py" new file mode 100644 index 0000000..94144da --- /dev/null +++ "b/LeetCode-Python/2455.\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274/2455-\345\217\257\350\242\253\344\270\211\346\225\264\351\231\244\347\232\204\345\201\266\346\225\260\347\232\204\345\271\263\345\235\207\345\200\274.py" @@ -0,0 +1,8 @@ +class Solution: + def averageValue(self, nums: List[int]) -> int: + s, cnt = 0, 0 + for num in nums: + if num % 6 == 0: + cnt += 1 + s += num + return int(s / cnt) if cnt else 0 \ No newline at end of file diff --git "a/LeetCode-Python/2460.\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234/2460-\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234.py" "b/LeetCode-Python/2460.\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234/2460-\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234.py" new file mode 100644 index 0000000..6a40092 --- /dev/null +++ "b/LeetCode-Python/2460.\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234/2460-\345\257\271\346\225\260\347\273\204\346\211\247\350\241\214\346\223\215\344\275\234.py" @@ -0,0 +1,17 @@ +class Solution: + def applyOperations(self, nums: List[int]) -> List[int]: + n = len(nums) + for i in range(n - 1): + if nums[i] == nums[i + 1]: + nums[i] *= 2 + nums[i + 1] = 0 + + index = 0 + for i in range(n): + if nums[i]: + nums[index] = nums[i] + index += 1 + + for i in range(index, n): + nums[i] = 0 + return nums \ No newline at end of file diff --git "a/LeetCode-Python/2465.\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256/2465-\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256.py" "b/LeetCode-Python/2465.\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256/2465-\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256.py" new file mode 100644 index 0000000..02dfad9 --- /dev/null +++ "b/LeetCode-Python/2465.\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256/2465-\344\270\215\345\220\214\347\232\204\345\271\263\345\235\207\345\200\274\346\225\260\347\233\256.py" @@ -0,0 +1,9 @@ +class Solution: + def distinctAverages(self, nums: List[int]) -> int: + nums.sort() + res = set() + while nums: + res.add((nums[0] + nums[-1]) / 2.0) + nums = nums[1:-1] + + return len(res) \ No newline at end of file diff --git "a/LeetCode-Python/2466.\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260/2466-\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260.py" "b/LeetCode-Python/2466.\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260/2466-\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260.py" new file mode 100644 index 0000000..812b4f1 --- /dev/null +++ "b/LeetCode-Python/2466.\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260/2466-\347\273\237\350\256\241\346\236\204\351\200\240\345\245\275\345\255\227\347\254\246\344\270\262\347\232\204\346\226\271\346\241\210\346\225\260.py" @@ -0,0 +1,36 @@ +class Solution: + def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int: + # f(i) = f(i - zero) + f(i - one) + # 00, 10, 11, 10, 0, 1 + # self.res = 0 + # MOD = int(1e9 + 7) + # memo = dict() + # def dfs(i): + # if i == 0: + # return 1 + # count = 0 + # if i in memo: + # return memo[i] + # if i >= zero: + # count += dfs(i - zero) + # if i >= one: + # count += dfs(i - one) + # # l represents count of distinct good string of length i + # if i >= low: + # self.res += count % MOD + # memo[i] = count + # return count + # dfs(high) + # return self.res % MOD + MOD = int(1e9 + 7) + dp = [0] * (high + max(zero, one)) + dp[0] = 1 + res = 0 + for i in range(1, high + 1): + if i >= zero: + dp[i] += dp[i - zero] + if i >= one: + dp[i] += dp[i - one] + if i >= low: + res += dp[i] % MOD + return res % MOD \ No newline at end of file diff --git "a/LeetCode-Python/2469.\346\270\251\345\272\246\350\275\254\346\215\242/2469-\346\270\251\345\272\246\350\275\254\346\215\242.py" "b/LeetCode-Python/2469.\346\270\251\345\272\246\350\275\254\346\215\242/2469-\346\270\251\345\272\246\350\275\254\346\215\242.py" new file mode 100644 index 0000000..091b21a --- /dev/null +++ "b/LeetCode-Python/2469.\346\270\251\345\272\246\350\275\254\346\215\242/2469-\346\270\251\345\272\246\350\275\254\346\215\242.py" @@ -0,0 +1,3 @@ +class Solution: + def convertTemperature(self, celsius: float) -> List[float]: + return [celsius + 273.15, celsius * 1.8 + 32] \ No newline at end of file diff --git "a/LeetCode-Python/2482.\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274/2482-\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274.py" "b/LeetCode-Python/2482.\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274/2482-\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274.py" new file mode 100644 index 0000000..270f3e4 --- /dev/null +++ "b/LeetCode-Python/2482.\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274/2482-\350\241\214\345\222\214\345\210\227\344\270\255\344\270\200\345\222\214\351\233\266\347\232\204\345\267\256\345\200\274.py" @@ -0,0 +1,15 @@ +class Solution: + def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]: + m, n = len(grid), len(grid[0]) + row2one = {} + col2one = {} + for i, row in enumerate(grid): + row2one[i] = sum(row) + for j, col in enumerate(zip(*grid)): + col2one[j] = sum(col) + + diff = [[0 for i in range(n)] for j in range(m)] + for i in range(m): + for j in range(n): + diff[i][j] = row2one[i] + col2one[j] - (n - row2one[i]) - (m - col2one[j]) + return diff \ No newline at end of file diff --git "a/LeetCode-Python/2487.\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271/2487-\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271.py" "b/LeetCode-Python/2487.\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271/2487-\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271.py" new file mode 100644 index 0000000..0848d99 --- /dev/null +++ "b/LeetCode-Python/2487.\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271/2487-\344\273\216\351\223\276\350\241\250\344\270\255\347\247\273\351\231\244\350\212\202\347\202\271.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(-1) + dummy.next = head + + stack = [] # [node] + nodes_to_be_deleted = set() + p = head + while p: + while stack and stack[-1].val < p.val: + nodes_to_be_deleted.add(stack[-1]) + stack.pop() + stack.append(p) + p = p.next + + p = dummy + while p.next: + if p.next in nodes_to_be_deleted: + p.next = p.next.next + else: + p = p.next + return dummy.next + diff --git "a/LeetCode-Python/2496.\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274/2496-\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/LeetCode-Python/2496.\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274/2496-\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..5a09c51 --- /dev/null +++ "b/LeetCode-Python/2496.\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274/2496-\346\225\260\347\273\204\344\270\255\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,14 @@ +class Solution: + def maximumValue(self, strs: List[str]) -> int: + res = float("-inf") + for s in strs: + isNumber = True + for char in s: + if not char.isdigit(): + isNumber = False + break + if isNumber: + res = max(res, int(s)) + else: + res = max(res, len(s)) + return res \ No newline at end of file diff --git "a/LeetCode-Python/2506.\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256/2506-\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256.py" "b/LeetCode-Python/2506.\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256/2506-\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..80070f6 --- /dev/null +++ "b/LeetCode-Python/2506.\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256/2506-\347\273\237\350\256\241\347\233\270\344\274\274\345\255\227\347\254\246\344\270\262\345\257\271\347\232\204\346\225\260\347\233\256.py" @@ -0,0 +1,11 @@ +class Solution: + def similarPairs(self, words: List[str]) -> int: + from collections import defaultdict + pattern2count = defaultdict(int) + for word in words: + pattern2count["".join(sorted(list(set(word))))] += 1 + + res = 0 + for pattern, count in pattern2count.items(): + res += count * (count - 1) // 2 + return res \ No newline at end of file diff --git "a/LeetCode-Python/2517.\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246/2517-\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246.py" "b/LeetCode-Python/2517.\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246/2517-\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246.py" new file mode 100644 index 0000000..a38b8cd --- /dev/null +++ "b/LeetCode-Python/2517.\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246/2517-\347\244\274\347\233\222\347\232\204\346\234\200\345\244\247\347\224\234\350\234\234\345\272\246.py" @@ -0,0 +1,25 @@ +class Solution: + def maximumTastiness(self, price: List[int], k: int) -> int: + # 13, 1, 21 - 12, 20, 8 + price = set(price) + if len(price) < k: + return 0 + + # 1, 2, 5, 8, 13, 21 + price = sorted(list(price)) + left, right = 0, price[-1] - price[0] + while left <= right: + mid = (left + right) // 2 # target tastyness + + cnt, prev = 1, price[0] + for p in price: + if prev + mid <= p: # 又找到了一个 + cnt += 1 + prev = p + + if cnt >= k: + left = mid + 1 + elif cnt < k: + right = mid - 1 + + return right \ No newline at end of file diff --git "a/LeetCode-Python/2529.\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260/2529-\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260.py" "b/LeetCode-Python/2529.\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260/2529-\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260.py" new file mode 100644 index 0000000..e7d86a4 --- /dev/null +++ "b/LeetCode-Python/2529.\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260/2529-\346\255\243\346\225\264\346\225\260\345\222\214\350\264\237\346\225\264\346\225\260\347\232\204\346\234\200\345\244\247\350\256\241\346\225\260.py" @@ -0,0 +1,32 @@ +class Solution: + def maximumCount(self, nums: List[int]) -> int: + # 1. find the right-most negative number + negative_index = -1 + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] < 0: + negative_index = mid + left = mid + 1 + elif nums[mid] >= 0: + right = mid - 1 + + # print(negative_index) + # 2. find the left-most positive number + positive_index = -1 + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] > 0: + positive_index = mid + right = mid - 1 + else: + left = mid + 1 + + if negative_index > -1 and positive_index > -1: + return max(negative_index + 1, len(nums) - positive_index) + elif negative_index > -1: + return negative_index + 1 + elif positive_index > -1: + return len(nums) - positive_index + return 0 \ No newline at end of file diff --git "a/LeetCode-Python/2545.\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217/2545-\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217.py" "b/LeetCode-Python/2545.\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217/2545-\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217.py" new file mode 100644 index 0000000..70cbf0e --- /dev/null +++ "b/LeetCode-Python/2545.\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217/2545-\346\240\271\346\215\256\347\254\254K\345\234\272\350\200\203\350\257\225\347\232\204\345\210\206\346\225\260\346\216\222\345\272\217.py" @@ -0,0 +1,3 @@ +class Solution: + def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]: + return sorted(score, key = lambda x:-x[k]) \ No newline at end of file diff --git "a/LeetCode-Python/2553.\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215/2553-\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215.py" "b/LeetCode-Python/2553.\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215/2553-\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215.py" new file mode 100644 index 0000000..54d4646 --- /dev/null +++ "b/LeetCode-Python/2553.\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215/2553-\345\210\206\345\211\262\346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\347\232\204\346\225\260\344\275\215.py" @@ -0,0 +1,8 @@ +class Solution: + def separateDigits(self, nums: List[int]) -> List[int]: + res = [] + + for num in nums: + for digit in str(num): + res.append(int(digit)) + return res \ No newline at end of file diff --git "a/LeetCode-Python/2558.\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251/2558-\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251.py" "b/LeetCode-Python/2558.\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251/2558-\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251.py" new file mode 100644 index 0000000..ddfc2cf --- /dev/null +++ "b/LeetCode-Python/2558.\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251/2558-\344\273\216\346\225\260\351\207\217\346\234\200\345\244\232\347\232\204\345\240\206\345\217\226\350\265\260\347\244\274\347\211\251.py" @@ -0,0 +1,12 @@ +from heapq import * +class Solution: + def pickGifts(self, gifts: List[int], k: int) -> int: + max_heap = [] + for gift in gifts: + heappush(max_heap, -gift) + + while k: + gift = -heappop(max_heap) + heappush(max_heap, -int(gift ** 0.5)) + k -= 1 + return -sum(max_heap) diff --git "a/LeetCode-Python/2562.\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274/2562-\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274.py" "b/LeetCode-Python/2562.\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274/2562-\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274.py" new file mode 100644 index 0000000..188fa3b --- /dev/null +++ "b/LeetCode-Python/2562.\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274/2562-\346\211\276\345\207\272\346\225\260\347\273\204\347\232\204\344\270\262\350\201\224\345\200\274.py" @@ -0,0 +1,11 @@ +class Solution: + def findTheArrayConcVal(self, nums: List[int]) -> int: + res = 0 + while nums: + if len(nums) > 1: + serial = int(str(nums[0]) + str(nums[-1])) + else: + serial = nums[0] + res += serial + nums = nums[1:-1] + return res \ No newline at end of file diff --git "a/LeetCode-Python/2574.\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274/2574-\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274.py" "b/LeetCode-Python/2574.\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274/2574-\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274.py" new file mode 100644 index 0000000..844959c --- /dev/null +++ "b/LeetCode-Python/2574.\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274/2574-\345\267\246\345\217\263\345\205\203\347\264\240\345\222\214\347\232\204\345\267\256\345\200\274.py" @@ -0,0 +1,12 @@ +class Solution: + def leftRightDifference(self, nums: List[int]) -> List[int]: + left_sum = [0] + right_sum = [0] + + for num in nums[:-1]: + left_sum.append(num + left_sum[-1]) + + for num in nums[::-1][:-1]: + right_sum.append(num + right_sum[-1]) + + return [abs(l - r) for l, r in zip(left_sum, right_sum[::-1])] \ No newline at end of file diff --git "a/LeetCode-Python/2586.\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/2586-\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260.py" "b/LeetCode-Python/2586.\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/2586-\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260.py" new file mode 100644 index 0000000..ee2f5d3 --- /dev/null +++ "b/LeetCode-Python/2586.\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260/2586-\347\273\237\350\256\241\350\214\203\345\233\264\345\206\205\347\232\204\345\205\203\351\237\263\345\255\227\347\254\246\344\270\262\346\225\260.py" @@ -0,0 +1,17 @@ +class Solution: + def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]: + vowelStringCount = [0 for _ in words] + VOWELS = set("aeiou") + for i in range(len(words)): + if words[i][0] in VOWELS and words[i][-1] in VOWELS: + vowelStringCount[i] = vowelStringCount[i - 1] + 1 + else: + vowelStringCount[i] = vowelStringCount[i - 1] + + res = [] + for l, r in queries: + if l: + res.append(vowelStringCount[r] - vowelStringCount[l - 1]) + else: + res.append(vowelStringCount[r]) + return res diff --git "a/LeetCode-Python/2610.\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204/2610-\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204.py" "b/LeetCode-Python/2610.\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204/2610-\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204.py" new file mode 100644 index 0000000..203e13d --- /dev/null +++ "b/LeetCode-Python/2610.\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204/2610-\350\275\254\346\215\242\344\272\214\347\273\264\346\225\260\347\273\204.py" @@ -0,0 +1,12 @@ +class Solution: + def findMatrix(self, nums: List[int]) -> List[List[int]]: + from collections import Counter + c = Counter(nums) + res = [] + for num, freq in c.items(): + while len(res) < freq: + res.append([]) + + for i in range(freq): + res[i].append(num) + return res diff --git "a/LeetCode-Python/2656.K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214/2656-K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214.py" "b/LeetCode-Python/2656.K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214/2656-K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214.py" new file mode 100644 index 0000000..46fe631 --- /dev/null +++ "b/LeetCode-Python/2656.K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214/2656-K\344\270\252\345\205\203\347\264\240\347\232\204\346\234\200\345\244\247\345\222\214.py" @@ -0,0 +1,3 @@ +class Solution: + def maximizeSum(self, nums: List[int], k: int) -> int: + return max(nums) * k + k * (k - 1) // 2 \ No newline at end of file diff --git "a/LeetCode-Python/2670.\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204/2670-\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204.py" "b/LeetCode-Python/2670.\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204/2670-\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204.py" new file mode 100644 index 0000000..d3bda30 --- /dev/null +++ "b/LeetCode-Python/2670.\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204/2670-\346\211\276\345\207\272\344\270\215\345\220\214\345\205\203\347\264\240\346\225\260\347\233\256\345\267\256\346\225\260\347\273\204.py" @@ -0,0 +1,10 @@ +class Solution: + def distinctDifferenceArray(self, nums: List[int]) -> List[int]: + res = [] + + for i, num in enumerate(nums): + pre_count = len(set(nums[:i + 1])) + post_count = len(set(nums[i + 1:])) + + res.append(pre_count - post_count) + return res \ No newline at end of file diff --git "a/LeetCode-Python/2671.\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250/2671-\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250.py" "b/LeetCode-Python/2671.\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250/2671-\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250.py" new file mode 100644 index 0000000..cd5339d --- /dev/null +++ "b/LeetCode-Python/2671.\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250/2671-\351\242\221\347\216\207\350\267\237\350\270\252\345\231\250.py" @@ -0,0 +1,34 @@ +from collections import defaultdict +class FrequencyTracker: + + def __init__(self): + self.freq2num = defaultdict(set) + self.num2freq = defaultdict(int) + + def add(self, number: int) -> None: + # print(self.num2freq, self.freq2num) + freq = self.num2freq[number] + self.num2freq[number] += 1 + if number in self.freq2num[freq]: + self.freq2num[freq].remove(number) + self.freq2num[freq + 1].add(number) + + def deleteOne(self, number: int) -> None: + # print(self.num2freq, self.freq2num) + if number not in self.num2freq or self.num2freq[number] == 0: + return + freq = self.num2freq[number] + self.num2freq[number] -= 1 + if number in self.freq2num[freq]: + self.freq2num[freq].remove(number) + self.freq2num[freq - 1].add(number) + + def hasFrequency(self, frequency: int) -> bool: + return len(self.freq2num[frequency]) > 0 + + +# Your FrequencyTracker object will be instantiated and called as such: +# obj = FrequencyTracker() +# obj.add(number) +# obj.deleteOne(number) +# param_3 = obj.hasFrequency(frequency) \ No newline at end of file diff --git "a/LeetCode-Python/2674.\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250/2674-\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250.py" "b/LeetCode-Python/2674.\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250/2674-\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250.py" new file mode 100644 index 0000000..aa8f15e --- /dev/null +++ "b/LeetCode-Python/2674.\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250/2674-\346\213\206\345\210\206\345\276\252\347\216\257\351\223\276\350\241\250.py" @@ -0,0 +1,29 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def splitCircularLinkedList(self, head: Optional[ListNode]) -> List[Optional[ListNode]]: + + l, p = 0, head + last = None + flag = False + while not flag or p != head: + flag = True + l += 1 + if p.next == head: + last = p + p = p.next + # print(l, last) + mid = math.ceil(l / 2) + + cnt, p = 1, head + while cnt < mid: + p = p.next + cnt += 1 + + next_head = p.next + p.next = head + last.next = next_head + return [head, next_head] diff --git "a/LeetCode-Python/2678.\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256/2678-\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256.py" "b/LeetCode-Python/2678.\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256/2678-\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..6173751 --- /dev/null +++ "b/LeetCode-Python/2678.\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256/2678-\350\200\201\344\272\272\347\232\204\346\225\260\347\233\256.py" @@ -0,0 +1,8 @@ +class Solution: + def countSeniors(self, details: List[str]) -> int: + # res = 0 + # for detail in details: + # if int(detail[-4:-2]) > 60: + # res += 1 + # return res + return sum([int(detail[-4:-2]) > 60 for detail in details]) \ No newline at end of file diff --git "a/LeetCode-Python/2679.\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214/2679-\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214.py" "b/LeetCode-Python/2679.\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214/2679-\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214.py" new file mode 100644 index 0000000..96145c7 --- /dev/null +++ "b/LeetCode-Python/2679.\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214/2679-\347\237\251\351\230\265\344\270\255\347\232\204\345\222\214.py" @@ -0,0 +1,23 @@ +from heapq import * +class Solution: + def matrixSum(self, nums: List[List[int]]) -> int: + # if not nums or not nums[0]: + # return 0 + # m, n = len(nums), len(nums[0]) + # all_max_heap = [] + # for num in nums: + # max_heap = [-n for n in num] + # heapify(max_heap) + # all_max_heap.append(max_heap) + + # res = 0 + # for _ in range(n): + # score = 0 + # for i in range(m): + # score = max(score, -heappop(all_max_heap[i])) + # res += score + # return res + for num in nums: + num.sort() + + return sum(max(col) for col in zip(*nums)) \ No newline at end of file diff --git "a/LeetCode-Python/2682.\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266/2682-\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266.py" "b/LeetCode-Python/2682.\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266/2682-\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266.py" new file mode 100644 index 0000000..7947da9 --- /dev/null +++ "b/LeetCode-Python/2682.\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266/2682-\346\211\276\345\207\272\350\275\254\345\234\210\346\270\270\346\210\217\350\276\223\345\256\266.py" @@ -0,0 +1,12 @@ +class Solution: + def circularGameLosers(self, n: int, k: int) -> List[int]: + visited = set() + cur, cnt = 1, 1 + while cur not in visited: + visited.add(cur) + cur = cur + k * cnt + while cur > n: + cur = cur - n + cnt += 1 + + return [i for i in range(1, n + 1) if i not in visited] \ No newline at end of file diff --git "a/LeetCode-Python/2683.\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226/2683-\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226.py" "b/LeetCode-Python/2683.\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226/2683-\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226.py" new file mode 100644 index 0000000..d7e390a --- /dev/null +++ "b/LeetCode-Python/2683.\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226/2683-\347\233\270\351\202\273\345\200\274\347\232\204\346\214\211\344\275\215\345\274\202\346\210\226.py" @@ -0,0 +1,9 @@ +class Solution: + def doesValidArrayExist(self, derived: List[int]) -> bool: + cur = 0 + for i, n in enumerate(derived): + if i != len(derived) - 1: + if n == 1: + cur = 1 - cur + else: + return 0 ^ cur == n \ No newline at end of file diff --git "a/LeetCode-Python/2684.\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260/2684-\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260.py" "b/LeetCode-Python/2684.\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260/2684-\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260.py" new file mode 100644 index 0000000..8fc6a8a --- /dev/null +++ "b/LeetCode-Python/2684.\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260/2684-\347\237\251\351\230\265\344\270\255\347\247\273\345\212\250\347\232\204\346\234\200\345\244\247\346\254\241\346\225\260.py" @@ -0,0 +1,43 @@ +class Solution: + def maxMoves(self, grid: List[List[int]]) -> int: + # if not grid or not grid[0]: + # return 0 + # m, n = len(grid), len(grid[0]) + # dp = [[0 for _ in range(n)] for k in range(m)] + # dij = [[-1, 1], [0, 1], [1, 1]] + + # for col in range(n): + # for row in range(m): + # if not col or dp[row][col] != 0: + # for d in dij: + # i, j = row + d[0], col + d[1] + # if self.nodeInMatrix(i, j, m, n) and grid[i][j] > grid[row][col]: + # dp[i][j] = max(dp[i][j], dp[row][col] + 1) + + # return max([max(row) for row in dp]) + from collections import deque + queue = deque([]) + if not grid or not grid[0]: + return 0 + m, n = len(grid), len(grid[0]) + dxy = [[-1, 1], [0, 1], [1, 1]] + res = 0 + visited = set() + for i in range(m): + queue.append((0, i, 0)) # step, x, y + + while queue: + step, x, y = queue.popleft() + res = max(res, step) + + for dx, dy in dxy: + xx, yy = x + dx, y + dy + + if self.nodeInMatrix(xx, yy, m, n) and grid[xx][yy] > grid[x][y] and (xx, yy) not in visited: + visited.add((xx, yy)) + queue.append((step + 1, xx, yy)) + return res + + def nodeInMatrix(self, row, col, m, n): + return 0 <= row < m and 0 <= col < n + \ No newline at end of file diff --git "a/LeetCode-Python/2685.\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217/2685-\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217.py" "b/LeetCode-Python/2685.\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217/2685-\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217.py" new file mode 100644 index 0000000..69d881a --- /dev/null +++ "b/LeetCode-Python/2685.\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217/2685-\347\273\237\350\256\241\345\256\214\345\205\250\350\277\236\351\200\232\345\210\206\351\207\217\347\232\204\346\225\260\351\207\217.py" @@ -0,0 +1,27 @@ +class Solution: + def countCompleteComponents(self, n: int, edges: List[List[int]]) -> int: + from collections import defaultdict + src2des = defaultdict(set) + + res = 0 + visited = set() + for edge in edges: + src, des = edge[0], edge[1] + src2des[src].add(des) + src2des[src].add(src) + src2des[des].add(src) + src2des[des].add(des) + + for node in range(n): + if node not in visited: + connected = True + visited.add(node) + for connected_node in src2des[node]: + visited.add(connected_node) + if src2des[connected_node] != src2des[node]: + connected = False + break + + if connected: + res += 1 + return res \ No newline at end of file diff --git "a/LeetCode-Python/2696.\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246/2696-\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246.py" "b/LeetCode-Python/2696.\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246/2696-\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246.py" new file mode 100644 index 0000000..793fbb8 --- /dev/null +++ "b/LeetCode-Python/2696.\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246/2696-\345\210\240\351\231\244\345\255\220\344\270\262\345\220\216\347\232\204\345\255\227\347\254\246\344\270\262\346\234\200\345\260\217\351\225\277\345\272\246.py" @@ -0,0 +1,12 @@ +class Solution: + def minLength(self, s: str) -> int: + stack = [] + + for char in s: + if char == "D" and stack and stack[-1] == "C": + stack.pop() + elif char == "B" and stack and stack[-1] == "A": + stack.pop() + else: + stack.append(char) + return len(stack) \ No newline at end of file diff --git "a/LeetCode-Python/2697.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262/2697-\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262.py" "b/LeetCode-Python/2697.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262/2697-\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262.py" new file mode 100644 index 0000000..aae6620 --- /dev/null +++ "b/LeetCode-Python/2697.\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262/2697-\345\255\227\345\205\270\345\272\217\346\234\200\345\260\217\345\233\236\346\226\207\344\270\262.py" @@ -0,0 +1,16 @@ +class Solution: + def makeSmallestPalindrome(self, s: str) -> str: + left, right = 0, len(s) - 1 + res_left = "" + res_right = "" + while left < right: + res_left += min(s[left], s[right]) + res_right = min(s[left], s[right]) + res_right + left += 1 + right -= 1 + + if left == right: + res = res_left + s[left] + res_right + else: + res = res_left + res_right + return res \ No newline at end of file diff --git "a/LeetCode-Python/2698.\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260/2698-\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260.py" "b/LeetCode-Python/2698.\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260/2698-\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260.py" new file mode 100644 index 0000000..b152802 --- /dev/null +++ "b/LeetCode-Python/2698.\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260/2698-\346\261\202\344\270\200\344\270\252\346\225\264\346\225\260\347\232\204\346\203\251\347\275\232\346\225\260.py" @@ -0,0 +1,27 @@ +class Solution: + def punishmentNumber(self, n: int) -> int: + res = 0 + for i in range(1, n + 1): + i2 = i * i + self.possible(i) + if self.res: + res += i2 + return res + + def possible(self, num): + # 判断 1296 是否可以分成 1 + 29 + 6 == 36 + self.res = False + def helper(n, s): + # 首次递归 n = 36, s = 1296 + if not s or n > int(s): + return + if int(s) == n: + self.res = True + return + for i in range(1, len(str(n)) + 1): + # 下次递归 n = 30, s = 129 + helper(n - int(s[-i:]), s[:-i]) + helper(num, str(num * num)) + + + \ No newline at end of file diff --git "a/LeetCode-Python/2706.\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233/2706-\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233.py" "b/LeetCode-Python/2706.\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233/2706-\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233.py" new file mode 100644 index 0000000..b368281 --- /dev/null +++ "b/LeetCode-Python/2706.\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233/2706-\350\264\255\344\271\260\344\270\244\345\235\227\345\267\247\345\205\213\345\212\233.py" @@ -0,0 +1,5 @@ +class Solution: + def buyChoco(self, prices: List[int], money: int) -> int: + prices.sort() + s = prices[0] + prices[1] + return money if s > money else money - s \ No newline at end of file diff --git "a/LeetCode-Python/2707.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246/2707-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246.py" "b/LeetCode-Python/2707.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246/2707-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246.py" new file mode 100644 index 0000000..eb2f6c5 --- /dev/null +++ "b/LeetCode-Python/2707.\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246/2707-\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\351\242\235\345\244\226\345\255\227\347\254\246.py" @@ -0,0 +1,15 @@ +class Solution: + def minExtraChar(self, s: str, dictionary: List[str]) -> int: + # dp[i] represents res for s[:i] + dp = [i for i in range(len(s) + 1)] + + for i in range(len(s) + 1): + dp[i] = min(dp[i], dp[i - 1] + 1) + for d in dictionary: + if i >= len(d) and s[i - len(d):i] == d: + dp[i] = min(dp[i], dp[i - len(d)]) + + return dp[-1] + + + \ No newline at end of file diff --git "a/LeetCode-Python/2708.\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274/2708-\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274.py" "b/LeetCode-Python/2708.\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274/2708-\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274.py" new file mode 100644 index 0000000..d91c8b4 --- /dev/null +++ "b/LeetCode-Python/2708.\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274/2708-\344\270\200\344\270\252\345\260\217\347\273\204\347\232\204\346\234\200\345\244\247\345\256\236\345\212\233\345\200\274.py" @@ -0,0 +1,21 @@ +class Solution: + def maxStrength(self, nums: List[int]) -> int: + if all([num == 0 for num in nums]): + return 0 + if len(nums) == 1: + return nums[0] + pos = [num for num in nums if num > 0] + neg = [num for num in nums if num < 0] + + res = 0 + neg_length = len(neg) + if neg_length >= 2: + if neg_length % 2 == 0: + res = reduce((lambda x, y: x * y), neg) + else: + neg.sort() + res = reduce((lambda x, y: x * y), neg[:-1]) + + if pos: + res = reduce((lambda x, y: x * y), pos) * max(res, 1) + return res diff --git "a/LeetCode-Python/2710.\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266/2710-\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266.py" "b/LeetCode-Python/2710.\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266/2710-\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266.py" new file mode 100644 index 0000000..2d286a5 --- /dev/null +++ "b/LeetCode-Python/2710.\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266/2710-\347\247\273\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\260\276\351\232\217\351\233\266.py" @@ -0,0 +1,5 @@ +class Solution: + def removeTrailingZeros(self, num: str) -> str: + for i in range(len(num) - 1, -1, -1): + if num[i] != "0": + return num[:i + 1] \ No newline at end of file diff --git "a/LeetCode-Python/2712.\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254/2712-\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254.py" "b/LeetCode-Python/2712.\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254/2712-\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254.py" new file mode 100644 index 0000000..c6ad528 --- /dev/null +++ "b/LeetCode-Python/2712.\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254/2712-\344\275\277\346\211\200\346\234\211\345\255\227\347\254\246\347\233\270\347\255\211\347\232\204\346\234\200\345\260\217\346\210\220\346\234\254.py" @@ -0,0 +1,12 @@ +class Solution: + def minimumCost(self, s: str) -> int: + # 110101 + # 111010 min(i, n - i) + prev = s[0] + res = 0 + for i, char in enumerate(s): + if i and char != prev: + # flip is required: + res += min(i, len(s) - i) + prev = char + return res \ No newline at end of file diff --git "a/LeetCode-Python/6424.\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227/6424-\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227.py" "b/LeetCode-Python/6424.\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227/6424-\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227.py" new file mode 100644 index 0000000..21bb5b9 --- /dev/null +++ "b/LeetCode-Python/6424.\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227/6424-\345\215\212\346\234\211\345\272\217\346\216\222\345\210\227.py" @@ -0,0 +1,8 @@ +class Solution: + def semiOrderedPermutation(self, nums: List[int]) -> int: + index_1 = nums.index(1) + index_n = nums.index(len(nums)) + if index_1 < index_n: + return index_1 + (len(nums) - 1 - index_n) + else: + return index_1 + (len(nums) - 1 - index_n - 1) \ No newline at end of file diff --git "a/LeetCode-Python/6462.\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246/6462-\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246.py" "b/LeetCode-Python/6462.\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246/6462-\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246.py" new file mode 100644 index 0000000..cae0b56 --- /dev/null +++ "b/LeetCode-Python/6462.\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246/6462-\346\234\200\345\260\217\345\214\226\345\255\227\347\254\246\344\270\262\351\225\277\345\272\246.py" @@ -0,0 +1,3 @@ +class Solution: + def minimizedStringLength(self, s: str) -> int: + return len(set(s)) \ No newline at end of file diff --git "a/LeetCode-Python/6472.\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214/6472-\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214.py" "b/LeetCode-Python/6472.\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214/6472-\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214.py" new file mode 100644 index 0000000..1f05ad5 --- /dev/null +++ "b/LeetCode-Python/6472.\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214/6472-\346\237\245\350\257\242\345\220\216\347\237\251\351\230\265\347\232\204\345\222\214.py" @@ -0,0 +1,16 @@ +class Solution: + def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int: + visited_rows, visited_cols = set(), set() + res = 0 + for t, index, val in queries[::-1]: + if t == 0: + if index not in visited_rows: + res += val * (n - len(visited_cols)) + visited_rows.add(index) + elif t == 1: + if index not in visited_cols: + res += val * (n - len(visited_rows)) + visited_cols.add(index) + + return res + diff --git "a/LeetCode-Python/LCP 01.\347\214\234\346\225\260\345\255\227/LCP 01-\347\214\234\346\225\260\345\255\227.py" "b/LeetCode-Python/LCP 01.\347\214\234\346\225\260\345\255\227/LCP 01-\347\214\234\346\225\260\345\255\227.py" new file mode 100644 index 0000000..71601c2 --- /dev/null +++ "b/LeetCode-Python/LCP 01.\347\214\234\346\225\260\345\255\227/LCP 01-\347\214\234\346\225\260\345\255\227.py" @@ -0,0 +1,3 @@ +class Solution: + def game(self, guess: List[int], answer: List[int]) -> int: + return sum(guess[i] == answer[i] for i in range(3)) \ No newline at end of file diff --git "a/LeetCode-Python/LCP 17.\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/LCP 17-\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272.py" "b/LeetCode-Python/LCP 17.\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/LCP 17-\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272.py" new file mode 100644 index 0000000..303f35f --- /dev/null +++ "b/LeetCode-Python/LCP 17.\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/LCP 17-\351\200\237\347\256\227\346\234\272\345\231\250\344\272\272.py" @@ -0,0 +1,10 @@ +class Solution: + def calculate(self, s: str) -> int: + x, y = 1, 0 + + for char in s: + if char == "A": + x = 2 * x + y + else: + y = 2 * y + x + return x + y \ No newline at end of file diff --git "a/LeetCode-Python/LCP 44.\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253/LCP 44-\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253.py" "b/LeetCode-Python/LCP 44.\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253/LCP 44-\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253.py" new file mode 100644 index 0000000..4205610 --- /dev/null +++ "b/LeetCode-Python/LCP 44.\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253/LCP 44-\345\274\200\345\271\225\345\274\217\347\204\260\347\201\253.py" @@ -0,0 +1,14 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None +class Solution: + def numColor(self, root: TreeNode) -> int: + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + return len(set(inorder(root))) \ No newline at end of file diff --git "a/LeetCode-Python/LCP 66.\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/LCP 66-\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217.py" "b/LeetCode-Python/LCP 66.\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/LCP 66-\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217.py" new file mode 100644 index 0000000..3e9157b --- /dev/null +++ "b/LeetCode-Python/LCP 66.\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217/LCP 66-\346\234\200\345\260\217\345\261\225\345\217\260\346\225\260\351\207\217.py" @@ -0,0 +1,11 @@ +class Solution: + def minNumBooths(self, demands: List[str]) -> int: + from collections import defaultdict, Counter + char2count = defaultdict(int) + for demand in demands: + c = Counter(demand) + + for char, freq in c.items(): + char2count[char] = max(char2count[char], freq) + + return sum(freq for freq in char2count.values()) \ No newline at end of file diff --git "a/LeetCode-Python/LCP 67.\350\243\205\351\245\260\346\240\221/LCP 67-\350\243\205\351\245\260\346\240\221.py" "b/LeetCode-Python/LCP 67.\350\243\205\351\245\260\346\240\221/LCP 67-\350\243\205\351\245\260\346\240\221.py" new file mode 100644 index 0000000..0e7a995 --- /dev/null +++ "b/LeetCode-Python/LCP 67.\350\243\205\351\245\260\346\240\221/LCP 67-\350\243\205\351\245\260\346\240\221.py" @@ -0,0 +1,26 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def expandBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + + def dfs(node): + if not node: + return node + + left, right = node.left, node.right + if left: + node.left = TreeNode(-1, left) + + if right: + node.right = TreeNode(-1, None, right) + + dfs(left) + dfs(right) + return node + + return dfs(root) + \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..6104d25 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,8 @@ +class Solution: + def findRepeatNumber(self, nums: List[int]) -> int: + for i, num in enumerate(nums): + if num != i: + if nums[num] == num: + return num + nums[i], nums[num] = nums[num], nums[i] + return nums[-1] \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207 Offer 05-\346\233\277\346\215\242\347\251\272\346\240\274.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207 Offer 05-\346\233\277\346\215\242\347\251\272\346\240\274.py" new file mode 100644 index 0000000..3e2d3c9 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207 Offer 05-\346\233\277\346\215\242\347\251\272\346\240\274.py" @@ -0,0 +1,3 @@ +class Solution: + def replaceSpace(self, s: str) -> str: + return s.replace(" ", "%20") \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207 Offer 06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207 Offer 06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" new file mode 100644 index 0000000..cd554da --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207 Offer 06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" @@ -0,0 +1,14 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def reversePrint(self, head: ListNode) -> List[int]: + res = [] + p = head + while p: + res.append(p.val) + p = p.next + return res[::-1] \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" new file mode 100644 index 0000000..1b49137 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" @@ -0,0 +1,23 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getKthFromEnd(self, head: ListNode, k: int) -> ListNode: + l = 0 + p = head + while p: + l += 1 + p = p.next + + count = l - k + 1 + p = head + while 1: + count -= 1 + if count == 0: + break + p = p.next + + return p \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207 Offer 27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207 Offer 27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" new file mode 100644 index 0000000..5d79dfc --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207 Offer 27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" @@ -0,0 +1,15 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def mirrorTree(self, root: TreeNode) -> TreeNode: + if not root: + return root + root.left, root.right = root.right, root.left + self.mirrorTree(root.left) + self.mirrorTree(root.right) + return root \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207 Offer 35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207 Offer 35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" new file mode 100644 index 0000000..819027c --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207 Offer 35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" @@ -0,0 +1,32 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): + self.val = int(x) + self.next = next + self.random = random +""" +class Solution: + def copyRandomList(self, head: 'Node') -> 'Node': + if not head: + return head + + old2new = dict() + p = head + while p: + cur_node = p + new_node = Node(cur_node.val) + old2new[cur_node] = new_node + p = p.next + + p = head + while p: + cur_node = p + new_node = old2new[cur_node] + + if cur_node.next: + new_node.next = old2new[cur_node.next] + if cur_node.random: + new_node.random = old2new[cur_node.random] + p = p.next + return old2new[head] \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 49.\344\270\221\346\225\260/\345\211\221\346\214\207 Offer 49-\344\270\221\346\225\260.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 49.\344\270\221\346\225\260/\345\211\221\346\214\207 Offer 49-\344\270\221\346\225\260.py" new file mode 100644 index 0000000..0513a62 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 49.\344\270\221\346\225\260/\345\211\221\346\214\207 Offer 49-\344\270\221\346\225\260.py" @@ -0,0 +1,17 @@ +class Solution: + def nthUglyNumber(self, n: int) -> int: + import heapq + + min_heap = [1] # find the smallest ugly number + visited = set() + cnt = n + while cnt: + cur = heappop(min_heap) + cnt -= 1 + if not cnt: + return cur + for multiple in [2, 3, 5]: + nxt = cur * multiple + if nxt not in visited: + visited.add(nxt) + heappush(min_heap, nxt) \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" new file mode 100644 index 0000000..49c593f --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" @@ -0,0 +1,22 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + if not headA or not headB: + return None + + pa, pb = headA, headB + while pa != pb: + if pa: + pa = pa.next + else: + pa = headB + if pb: + pb = pb.next + else: + pb = headA + return pa \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 53 - I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207 Offer 53 - I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 53 - I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207 Offer 53 - I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" new file mode 100644 index 0000000..df2d2ae --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 53 - I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207 Offer 53 - I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" @@ -0,0 +1,25 @@ +class Solution: + def search(self, nums: List[int], target: int) -> int: + # return nums.count(target) + # 1. find the left-most index + left, right = 0, len(nums) - 1 + left_index = -1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] >= target: + left_index = mid + right = mid - 1 + else: + left = mid + 1 + # 2. find the right-most index + left, right = 0, len(nums) - 1 + right_index = -1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] <= target: + right_index = mid + left = mid + 1 + else: + right = mid - 1 + + return right_index - left_index + 1 if left_index > -1 else 0 \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" new file mode 100644 index 0000000..4760d38 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def kthLargest(self, root: TreeNode, k: int) -> int: + self.count = 0 + self.res = 0 + def inorder(node): + if not node: + return + + inorder(node.right) + self.count += 1 + if self.count == k: + self.res = node.val + + inorder(node.left) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 55 - I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207 Offer 55 - I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 55 - I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207 Offer 55 - I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" new file mode 100644 index 0000000..4fde3bf --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 55 - I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207 Offer 55 - I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def maxDepth(self, root: TreeNode) -> int: + if not root: + return 0 + queue = [root] + depth = 0 + while queue: + next_queue = [] + depth += 1 + for node in queue: + if node.left: + next_queue.append(node.left) + if node.right: + next_queue.append(node.right) + queue = next_queue[:] + return depth \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" new file mode 100644 index 0000000..4b5917d --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" @@ -0,0 +1,12 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i, num in enumerate(nums): + left, right = i + 1, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target - num: + return [num, target - num] + elif nums[mid] < target - num: + left = mid + 1 + else: + right = mid - 1 \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 58 - II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207 Offer 58 - II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 58 - II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207 Offer 58 - II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..e065bdf --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 58 - II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207 Offer 58 - II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,3 @@ +class Solution: + def reverseLeftWords(self, s: str, n: int) -> str: + return s[n:] + s[:n] \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer 64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207 Offer 64-\346\261\2021+2+\342\200\246+n.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer 64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207 Offer 64-\346\261\2021+2+\342\200\246+n.py" new file mode 100644 index 0000000..f39339c --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer 64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207 Offer 64-\346\261\2021+2+\342\200\246+n.py" @@ -0,0 +1,3 @@ +class Solution: + def sumNums(self, n: int) -> int: + return (n ** 2 + n) >> 1 \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 004.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 004-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 004.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 004-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..64b9513 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 004.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 004-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,3 @@ +class Solution: + def singleNumber(self, nums: List[int]) -> int: + return reduce(lambda x, y: x ^ y, nums) \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 006.\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 006-\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 006.\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 006-\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214.py" new file mode 100644 index 0000000..17d2a7f --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 006.\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 006-\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -0,0 +1,7 @@ +class Solution: + def twoSum(self, numbers: List[int], target: int) -> List[int]: + num2index = dict() + for index, num in enumerate(numbers): + if target - num in num2index: + return [num2index[target - num], index] + num2index[num] = index \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 018.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 018-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 018.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 018-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207.py" new file mode 100644 index 0000000..c26b8dd --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 018.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 018-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207.py" @@ -0,0 +1,5 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + s = "".join([char.lower() for char in s if char.isalpha() or char.isdigit()]) + print(s) + return s == s[::-1] \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 019.\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 019-\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 019.\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 019-\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207.py" new file mode 100644 index 0000000..90330a6 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 019.\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 019-\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207.py" @@ -0,0 +1,20 @@ +class Solution: + def validPalindrome(self, s: str) -> bool: + delete = False + left, right = 0, len(s) - 1 + + while left < right: + if s[left] != s[right]: + if not delete: + delete = True + break + left += 1 + right -= 1 + + if delete: + s1 = s[:left] + s[left + 1:] + s2 = s[:right] + s[right + 1:] + return s1 == s1[::-1] or s2 == s2[::-1] + else: + return True + \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 023.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/\345\211\221\346\214\207 Offer II 023-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 023.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/\345\211\221\346\214\207 Offer II 023-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271.py" new file mode 100644 index 0000000..1e17d1e --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 023.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/\345\211\221\346\214\207 Offer II 023-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271.py" @@ -0,0 +1,37 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + la, lb = 0, 0 + p = headA + while p: + la += 1 + p = p.next + + p = headB + while p: + lb += 1 + p = p.next + + if la > lb: + headA, headB = headB, headA + la, lb = lb, la + + diff = lb - la + pa, pb = headA, headB + while diff: + pb = pb.next + diff -= 1 + + while pa and pb: + if pa == pb: + return pa + else: + pa = pa.next + pb = pb.next + + return None diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 024.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207 Offer II 024-\345\217\215\350\275\254\351\223\276\350\241\250.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 024.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207 Offer II 024-\345\217\215\350\275\254\351\223\276\350\241\250.py" new file mode 100644 index 0000000..bac3cc1 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 024.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207 Offer II 024-\345\217\215\350\275\254\351\223\276\350\241\250.py" @@ -0,0 +1,19 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + # if not head or not head.next: + # return head + # tmp = self.reverseList(head.next) + # head.next.next = head + # head.next = None + # return tmp + prev, cur = None, head + while cur: + next_node = cur.next + cur.next = prev + prev, cur = cur, next_node + return prev \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 032.\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/\345\211\221\346\214\207 Offer II 032-\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 032.\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/\345\211\221\346\214\207 Offer II 032-\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215.py" new file mode 100644 index 0000000..0a80846 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 032.\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/\345\211\221\346\214\207 Offer II 032-\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215.py" @@ -0,0 +1,4 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + from collections import Counter + return s != t and Counter(s) == Counter(t) \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 036.\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\211\221\346\214\207 Offer II 036-\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 036.\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\211\221\346\214\207 Offer II 036-\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.py" new file mode 100644 index 0000000..a926627 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 036.\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\211\221\346\214\207 Offer II 036-\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.py" @@ -0,0 +1,22 @@ +import math +class Solution: + def evalRPN(self, tokens: List[str]) -> int: + num_stack = [] + + res = 0 + for token in tokens: + # print(num_stack) + if token[-1].isdigit(): + num_stack.append(int(token)) + else: + second, first = num_stack.pop(), num_stack.pop() + if token == "+": + num_stack.append(first + second) + elif token == "-": + num_stack.append(first - second) + elif token == "*": + num_stack.append(first * second) + elif token == "/": + num_stack.append(int(first / second)) + + return num_stack[0] \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 038.\346\257\217\346\227\245\346\270\251\345\272\246/\345\211\221\346\214\207 Offer II 038-\346\257\217\346\227\245\346\270\251\345\272\246.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 038.\346\257\217\346\227\245\346\270\251\345\272\246/\345\211\221\346\214\207 Offer II 038-\346\257\217\346\227\245\346\270\251\345\272\246.py" new file mode 100644 index 0000000..25240ec --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 038.\346\257\217\346\227\245\346\270\251\345\272\246/\345\211\221\346\214\207 Offer II 038-\346\257\217\346\227\245\346\270\251\345\272\246.py" @@ -0,0 +1,11 @@ +class Solution: + def dailyTemperatures(self, temperatures: List[int]) -> List[int]: + res = [0] * len(temperatures) + stack = [] # store index into this stack + for i, temp in enumerate(temperatures): + while stack and temperatures[stack[-1]] < temp: + last_index = stack[-1] + res[last_index] = i - last_index + stack.pop() + stack.append(i) + return res \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 044.\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207 Offer II 044-\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 044.\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207 Offer II 044-\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..abc928a --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 044.\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207 Offer II 044-\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def largestValues(self, root: TreeNode) -> List[int]: + queue = [root] + res = [] + + while queue: + next_queue = [] + max_val = float("-inf") + for node in queue: + if node: + max_val = max(max_val, node.val) + next_queue.extend([node.left, node.right]) + + queue = next_queue + if max_val != float("-inf"): + res.append(max_val) + + return res \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 045.\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/\345\211\221\346\214\207 Offer II 045-\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 045.\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/\345\211\221\346\214\207 Offer II 045-\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274.py" new file mode 100644 index 0000000..4f44c52 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 045.\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/\345\211\221\346\214\207 Offer II 045-\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def findBottomLeftValue(self, root: TreeNode) -> int: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + res.append(cur_level) + queue = next_queue + return res[-1][0] \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 049.\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 049-\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 049.\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 049-\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214.py" new file mode 100644 index 0000000..89118a0 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 049.\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 049-\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: TreeNode) -> int: + self.res = False + self.res = 0 + def dfs(node, path_sum): + if not node: + return + + path_sum = path_sum * 10 + node.val + if not node.left and not node.right: + self.res += path_sum + + dfs(node.left, path_sum) + dfs(node.right, path_sum) + + dfs(root, 0) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 052.\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\345\211\221\346\214\207 Offer II 052-\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 052.\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\345\211\221\346\214\207 Offer II 052-\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..490aa9d --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 052.\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\345\211\221\346\214\207 Offer II 052-\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def increasingBST(self, root: TreeNode) -> TreeNode: + + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + inorder_list = inorder(root) + + dummy = TreeNode(-1) + p = dummy + for val in inorder_list: + p.right = TreeNode(val) + p = p.right + + return dummy.right diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 053.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247/\345\211\221\346\214\207 Offer II 053-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 053.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247/\345\211\221\346\214\207 Offer II 053-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247.py" new file mode 100644 index 0000000..c18fff9 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 053.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247/\345\211\221\346\214\207 Offer II 053-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247.py" @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> Optional[TreeNode]: + # find the next node after p + if not root: + return None + if p.val >= root.val: + return self.inorderSuccessor(root.right, p) + else: + return self.inorderSuccessor(root.left, p) or root + \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 054.\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 054-\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 054.\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 054-\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214.py" new file mode 100644 index 0000000..a4313b7 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 054.\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 054-\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214.py" @@ -0,0 +1,46 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# class Solution: +# def convertBST(self, root: TreeNode) -> TreeNode: + +# def inOrderGetSum(node): +# if not node: +# return 0 +# return node.val + inOrderGetSum(node.left) + inOrderGetSum(node.right) + +# self.cur_sum = 0 +# def inOrderSetVal(node, total_sum): +# if not node: +# return 0 + +# original_node_val = node.val +# inOrderSetVal(node.left, total_sum) +# node.val = total_sum - self.cur_sum +# self.cur_sum += original_node_val +# right_subtree_sum = inOrderSetVal(node.right, total_sum) + + +# total_sum = inOrderGetSum(root) +# inOrderSetVal(root, total_sum) +# return root + + +class Solution: + def convertBST(self, root: TreeNode) -> TreeNode: + # right - root - left + self.cur_sum = 0 + def reverseInorder(node): + if not node: + return + + reverseInorder(node.right) + self.cur_sum += node.val + node.val = self.cur_sum + reverseInorder(node.left) + + reverseInorder(root) + return root diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 055.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/\345\211\221\346\214\207 Offer II 055-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 055.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/\345\211\221\346\214\207 Offer II 055-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250.py" new file mode 100644 index 0000000..be2e409 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 055.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/\345\211\221\346\214\207 Offer II 055-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250.py" @@ -0,0 +1,29 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class BSTIterator: + def __init__(self, root: TreeNode): + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + self.inorder = inorder(root) + self.index = 0 + + def next(self) -> int: + self.index += 1 + return self.inorder[self.index - 1] + + def hasNext(self) -> bool: + return self.index < len(self.inorder) + + + +# Your BSTIterator object will be instantiated and called as such: +# obj = BSTIterator(root) +# param_1 = obj.next() +# param_2 = obj.hasNext() \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 056.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 056-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 056.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 056-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214.py" new file mode 100644 index 0000000..defd4cd --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 056.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 056-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214.py" @@ -0,0 +1,27 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def findTarget(self, root: TreeNode, k: int) -> bool: + visited = set() + self.res = False + + def inorder(node): + if not node: + return + + inorder(node.left) + + if k - node.val in visited: + self.res = True + visited.add(node.val) + + if not self.res: + inorder(node.right) + + inorder(root) + return self.res + diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 059.\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274/\345\211\221\346\214\207 Offer II 059-\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 059.\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274/\345\211\221\346\214\207 Offer II 059-\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274.py" new file mode 100644 index 0000000..e4f70e2 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 059.\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274/\345\211\221\346\214\207 Offer II 059-\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274.py" @@ -0,0 +1,22 @@ +class KthLargest: + + def __init__(self, k: int, nums: List[int]): + self.k = k + self.min_heap = [] + for num in nums: + if len(self.min_heap) < k: + heappush(self.min_heap, num) + else: + heappushpop(self.min_heap, num) + + def add(self, val: int) -> int: + if len(self.min_heap) < self.k: + heappush(self.min_heap, val) + else: + heappushpop(self.min_heap, val) + return self.min_heap[0] + + +# Your KthLargest object will be instantiated and called as such: +# obj = KthLargest(k, nums) +# param_1 = obj.add(val) \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 060.\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 060-\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 060.\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 060-\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227.py" new file mode 100644 index 0000000..a27164b --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 060.\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 060-\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227.py" @@ -0,0 +1,20 @@ +from collections import Counter +from heapq import * +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + c = Counter(nums) + # return sorted(set(nums), key = lambda x: -c[x])[:k] + heap_set = set() + min_heap = [] + for num in nums: + if num not in heap_set: + heap_set.add(num) + if len(min_heap) < k: + heappush(min_heap, (c[num], num)) + else: + heappushpop(min_heap, (c[num], num)) + # print(min_heap) + res = [] + while min_heap: + res.append(heappop(min_heap)[1]) + return res diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 069.\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/\345\211\221\346\214\207 Offer II 069-\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 069.\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/\345\211\221\346\214\207 Offer II 069-\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250.py" new file mode 100644 index 0000000..e013042 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 069.\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/\345\211\221\346\214\207 Offer II 069-\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250.py" @@ -0,0 +1,13 @@ +class Solution: + def peakIndexInMountainArray(self, arr: List[int]) -> int: + left, right = 0, len(arr) - 1 + while left <= right: + mid = (left + right) // 2 + if 0 < mid < len(arr) - 1 and arr[mid - 1] < arr[mid] and arr[mid] > arr[mid + 1]: + return mid + + if arr[mid + 1] > arr[mid]: + left = mid + 1 + else: + right = mid - 1 + return left \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 076.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 076-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 076.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 076-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..ab926ec --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 076.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 076-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,3 @@ +class Solution: + def findKthLargest(self, nums: List[int], k: int) -> int: + return sorted(nums)[::-1][k - 1] \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 079.\346\211\200\346\234\211\345\255\220\351\233\206/\345\211\221\346\214\207 Offer II 079-\346\211\200\346\234\211\345\255\220\351\233\206.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 079.\346\211\200\346\234\211\345\255\220\351\233\206/\345\211\221\346\214\207 Offer II 079-\346\211\200\346\234\211\345\255\220\351\233\206.py" new file mode 100644 index 0000000..52471ac --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 079.\346\211\200\346\234\211\345\255\220\351\233\206/\345\211\221\346\214\207 Offer II 079-\346\211\200\346\234\211\345\255\220\351\233\206.py" @@ -0,0 +1,9 @@ +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + res = [[]] + for num in nums: + new_res = [] + for subset in res: + new_res.append(subset + [num]) + res += new_res + return res \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 083.\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/\345\211\221\346\214\207 Offer II 083-\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 083.\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/\345\211\221\346\214\207 Offer II 083-\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227.py" new file mode 100644 index 0000000..a9d570a --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 083.\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/\345\211\221\346\214\207 Offer II 083-\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227.py" @@ -0,0 +1,16 @@ +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + res = [] + def dfs(tmp, nums): + if not nums: + res.append(tmp) + + for i, x in enumerate(nums): + dfs(tmp + [x], nums[:i] + nums[i + 1:]) + + dfs([], nums) + return res \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 085.\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/\345\211\221\346\214\207 Offer II 085-\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 085.\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/\345\211\221\346\214\207 Offer II 085-\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267.py" new file mode 100644 index 0000000..1b94ac9 --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 085.\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/\345\211\221\346\214\207 Offer II 085-\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267.py" @@ -0,0 +1,14 @@ +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + res = [] + def dfs(left, right, tmp): + if left == 0 and right == 0: + res.append(tmp) + return + if left > 0: + dfs(left - 1, right, tmp + "(") + if right > left: + dfs(left, right - 1, tmp + ")") + + dfs(n, n, "") + return res \ No newline at end of file diff --git "a/LeetCode-Python/\345\211\221\346\214\207 Offer II 119.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/\345\211\221\346\214\207 Offer II 119-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.py" "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 119.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/\345\211\221\346\214\207 Offer II 119-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.py" new file mode 100644 index 0000000..160e3de --- /dev/null +++ "b/LeetCode-Python/\345\211\221\346\214\207 Offer II 119.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/\345\211\221\346\214\207 Offer II 119-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.py" @@ -0,0 +1,15 @@ +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + record = dict() + res = 0 + for num in nums: + if num not in record: + left = record.get(num - 1, 0) + right = record.get(num + 1, 0) + + length = right + left + 1 + res = max(res, length) + for i in [num - left, num, num + right]: + record[i] = length + + return res diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.01.\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200/\351\235\242\350\257\225\351\242\230 01.01-\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.01.\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200/\351\235\242\350\257\225\351\242\230 01.01-\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200.py" new file mode 100644 index 0000000..ea48c15 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.01.\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200/\351\235\242\350\257\225\351\242\230 01.01-\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200.py" @@ -0,0 +1,3 @@ +class Solution: + def isUnique(self, astr: str) -> bool: + return len(set(astr)) == len(astr) \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\230 01.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\230 01.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" new file mode 100644 index 0000000..fa4e693 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\230 01.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" @@ -0,0 +1,4 @@ +class Solution: + def CheckPermutation(self, s1: str, s2: str) -> bool: + from collections import Counter + return Counter(s1) == Counter(s2) \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.03.URL\345\214\226/\351\235\242\350\257\225\351\242\230 01.03-URL\345\214\226.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.03.URL\345\214\226/\351\235\242\350\257\225\351\242\230 01.03-URL\345\214\226.py" new file mode 100644 index 0000000..54f13bd --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.03.URL\345\214\226/\351\235\242\350\257\225\351\242\230 01.03-URL\345\214\226.py" @@ -0,0 +1,3 @@ +class Solution: + def replaceSpaces(self, S: str, length: int) -> str: + return S[:length].replace(" ", "%20") \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\230 01.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\230 01.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" new file mode 100644 index 0000000..8b2a31f --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\230 01.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" @@ -0,0 +1,10 @@ +class Solution: + def canPermutePalindrome(self, s: str) -> bool: + from collections import Counter + odd = False + for char, freq in Counter(s).items(): + if freq % 2: + if odd: + return False + odd = True + return True \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\230 01.06-\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\230 01.06-\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251.py" new file mode 100644 index 0000000..a08b996 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\230 01.06-\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251.py" @@ -0,0 +1,14 @@ +class Solution: + def compressString(self, S: str) -> str: + res = "" + index = 0 + while index < len(S): + count = 1 + char = S[index] + while index + 1 < len(S) and S[index] == S[index + 1]: + index += 1 + count += 1 + res += char + str(count) + index += 1 + + return res if len(res) < len(S) else S \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" new file mode 100644 index 0000000..6ff3d39 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" @@ -0,0 +1,14 @@ +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + n = len(matrix) + for i in range(n): + for j in range(i + 1, n): + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + + for i in range(n): + row = matrix[i] + for j in range(n // 2): + row[j], row[-(j + 1)] = row[-(j + 1)], row[j] \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.08-\351\233\266\347\237\251\351\230\265.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.08-\351\233\266\347\237\251\351\230\265.py" new file mode 100644 index 0000000..1834034 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.08-\351\233\266\347\237\251\351\230\265.py" @@ -0,0 +1,23 @@ +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + + row_zero, col_zero = set(), set() + + for i in range(len(matrix)): + for j in range(len(matrix[0])): + if not matrix[i][j]: + row_zero.add(i) + col_zero.add(j) + + for row_index in row_zero: + matrix[row_index] = [0] * len(matrix[0]) + + for col_index in col_zero: + for i in range(len(matrix)): + matrix[i][col_index] = 0 + + + \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.09.\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254/\351\235\242\350\257\225\351\242\230 01.09-\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.09.\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254/\351\235\242\350\257\225\351\242\230 01.09-\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254.py" new file mode 100644 index 0000000..4616e7f --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 01.09.\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254/\351\235\242\350\257\225\351\242\230 01.09-\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254.py" @@ -0,0 +1,5 @@ +class Solution: + def isFlipedString(self, s1: str, s2: str) -> bool: + if not s2: + return not s1 + return s2 in s1 + s1 \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.01.\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.01-\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.01.\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.01-\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271.py" new file mode 100644 index 0000000..d6fdf7a --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.01.\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.01-\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271.py" @@ -0,0 +1,39 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def removeDuplicateNodes(self, head: ListNode) -> ListNode: + # if not head or not head.next: + # return head + + # dummy = ListNode(-1) + # prev, cur = dummy, head + # dummy.next = cur + # while cur: + # p_find_duplicate = head + # while p_find_duplicate: + # if p_find_duplicate.val == cur.val: + # break + # p_find_duplicate = p_find_duplicate.next + # if p_find_duplicate and p_find_duplicate!= cur: + # prev.next = cur.next + # cur = cur.next + # else: + # prev, cur = cur, cur.next + # return head + if not head: + return head + visited = {head.val} + p = head + while p.next: + cur = p.next + if cur.val not in visited: + visited.add(cur.val) + p = p.next + else: + p.next = p.next.next + return head + diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.02.\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.02-\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.02.\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.02-\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" new file mode 100644 index 0000000..f21ff9c --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.02.\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.02-\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" @@ -0,0 +1,23 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def kthToLast(self, head: ListNode, k: int) -> int: + l = 0 + p = head + while p: + l += 1 + p = p.next + + count = l - k + 1 + p = head + while 1: + count -= 1 + if count == 0: + break + p = p.next + + return p.val \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.03.\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.03-\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.03.\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.03-\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271.py" new file mode 100644 index 0000000..d15d8ae --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.03.\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.03-\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271.py" @@ -0,0 +1,19 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def deleteNode(self, node): + """ + :type node: ListNode + :rtype: void Do not return anything, modify node in-place instead. + """ + p = node + while p and p.next: + p.val = p.next.val + if not p.next.next: + p.next = None + break + p = p.next \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.04.\345\210\206\345\211\262\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.04-\345\210\206\345\211\262\351\223\276\350\241\250.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.04.\345\210\206\345\211\262\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.04-\345\210\206\345\211\262\351\223\276\350\241\250.py" new file mode 100644 index 0000000..068ad14 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.04.\345\210\206\345\211\262\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.04-\345\210\206\345\211\262\351\223\276\350\241\250.py" @@ -0,0 +1,24 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def partition(self, head: ListNode, x: int) -> ListNode: + dummy1, dummy2 = ListNode(-1), ListNode(-2) + + smaller, larger = dummy1, dummy2 + p = head + while p: + if p.val < x: + smaller.next = ListNode(p.val) + smaller = smaller.next + else: + larger.next = ListNode(p.val) + larger = larger.next + + p = p.next + + smaller.next = dummy2.next + return dummy1.next \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.05.\351\223\276\350\241\250\346\261\202\345\222\214/\351\235\242\350\257\225\351\242\230 02.05-\351\223\276\350\241\250\346\261\202\345\222\214.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.05.\351\223\276\350\241\250\346\261\202\345\222\214/\351\235\242\350\257\225\351\242\230 02.05-\351\223\276\350\241\250\346\261\202\345\222\214.py" new file mode 100644 index 0000000..70f930c --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.05.\351\223\276\350\241\250\346\261\202\345\222\214/\351\235\242\350\257\225\351\242\230 02.05-\351\223\276\350\241\250\346\261\202\345\222\214.py" @@ -0,0 +1,59 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: + if not l1 and not l2: + return l1 + if not l1: + return l2 + if not l2: + return l1 + + p1, p2 = l1, l2 + carry = 0 + dummy = ListNode(-1) + p = dummy + while l1 and l2: + s = l1.val + l2.val + carry + l1 = l1.next + l2 = l2.next + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + + p.next = ListNode(s) + p = p.next + + while l1: + s = l1.val + carry + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + l1 = l1.next + p.next = ListNode(s) + p = p.next + + while l2: + s = l2.val + carry + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + l2 = l2.next + p.next = ListNode(s) + p = p.next + + if carry: + p.next = ListNode(1) + return dummy.next + + \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" new file mode 100644 index 0000000..654d1c5 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" @@ -0,0 +1,13 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def isPalindrome(self, head: ListNode) -> bool: + p = head + l = [] + while p: + l.append(p.val) + p = p.next + return l == l[::-1] \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.07.\351\223\276\350\241\250\347\233\270\344\272\244/\351\235\242\350\257\225\351\242\230 02.07-\351\223\276\350\241\250\347\233\270\344\272\244.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.07.\351\223\276\350\241\250\347\233\270\344\272\244/\351\235\242\350\257\225\351\242\230 02.07-\351\223\276\350\241\250\347\233\270\344\272\244.py" new file mode 100644 index 0000000..f174760 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.07.\351\223\276\350\241\250\347\233\270\344\272\244/\351\235\242\350\257\225\351\242\230 02.07-\351\223\276\350\241\250\347\233\270\344\272\244.py" @@ -0,0 +1,36 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + pa, pb = headA, headB + if not headA or not headB: + return None + + la, lb = 0, 0 + while pa: + la += 1 + pa = pa.next + while pb: + lb += 1 + pb = pb.next + + if la < lb: + headA, headB = headB, headA + la, lb = lb, la + + diff = la - lb + pa = headA + while diff: + pa = pa.next + diff -= 1 + + pb = headB + while pa and pb and pa != pb: + pa = pa.next + pb = pb.next + + return pa if pa == pb else None \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.08.\347\216\257\350\267\257\346\243\200\346\265\213/\351\235\242\350\257\225\351\242\230 02.08-\347\216\257\350\267\257\346\243\200\346\265\213.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.08.\347\216\257\350\267\257\346\243\200\346\265\213/\351\235\242\350\257\225\351\242\230 02.08-\347\216\257\350\267\257\346\243\200\346\265\213.py" new file mode 100644 index 0000000..c6abf6f --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 02.08.\347\216\257\350\267\257\346\243\200\346\265\213/\351\235\242\350\257\225\351\242\230 02.08-\347\216\257\350\267\257\346\243\200\346\265\213.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def detectCycle(self, head: ListNode) -> ListNode: + if not head or not head.next: + return None + slow, fast = head, head + + while fast and fast.next: + slow = slow.next + fast = fast.next.next + + if slow == fast: + break + + if slow != fast: + return None + + fast = head + while fast != slow: + slow = slow.next + fast = fast.next + + return fast \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 03.02.\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274/\351\235\242\350\257\225\351\242\230 03.02-\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 03.02.\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274/\351\235\242\350\257\225\351\242\230 03.02-\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274.py" new file mode 100644 index 0000000..b26eec1 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 03.02.\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274/\351\235\242\350\257\225\351\242\230 03.02-\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274.py" @@ -0,0 +1,35 @@ +class MinStack: + + def __init__(self): + """ + initialize your data structure here. + """ + self.stack = [] + self.min_stack = [] + + def push(self, x: int) -> None: + self.stack.append(x) + if self.min_stack: + self.min_stack.append(min(self.min_stack[-1], x)) + else: + self.min_stack.append(x) + + def pop(self) -> None: + self.stack.pop() + self.min_stack.pop() + + def top(self) -> int: + return self.stack[-1] + + + def getMin(self) -> int: + return self.min_stack[-1] + + + +# Your MinStack object will be instantiated and called as such: +# obj = MinStack() +# obj.push(x) +# obj.pop() +# param_3 = obj.top() +# param_4 = obj.getMin() \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.01.\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257/\351\235\242\350\257\225\351\242\230 04.01-\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.01.\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257/\351\235\242\350\257\225\351\242\230 04.01-\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257.py" new file mode 100644 index 0000000..5443d3f --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.01.\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257/\351\235\242\350\257\225\351\242\230 04.01-\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257.py" @@ -0,0 +1,27 @@ +from collections import * +class Solution: + def findWhetherExistsPath(self, n: int, graph: List[List[int]], start: int, target: int) -> bool: + src2des = defaultdict(list) + + for pair in graph: + src, des = pair[0], pair[1] + src2des[src].append(des) + + # BFS + queue = deque([start]) + visited = {start} + while queue: + cur = queue.popleft() + if cur == target: + return True + + for node in src2des[cur]: + if node not in visited: + queue.append(node) + visited.add(node) + + return False + + + + diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.02.\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221/\351\235\242\350\257\225\351\242\230 04.02-\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.02.\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221/\351\235\242\350\257\225\351\242\230 04.02-\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221.py" new file mode 100644 index 0000000..cca5452 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.02.\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221/\351\235\242\350\257\225\351\242\230 04.02-\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221.py" @@ -0,0 +1,14 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def sortedArrayToBST(self, nums: List[int]) -> TreeNode: + if not nums: + return None + mid = len(nums) // 2 + root = TreeNode(nums[mid], self.sortedArrayToBST(nums[:mid]), self.sortedArrayToBST(nums[mid + 1:])) + return root \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.03.\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 04.03-\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.03.\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 04.03-\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250.py" new file mode 100644 index 0000000..8bb57fa --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.03.\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 04.03-\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250.py" @@ -0,0 +1,32 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def listOfDepth(self, tree: TreeNode) -> List[ListNode]: + queue = [tree] + res = [] + + while queue: + next_queue = [] + cur_level = ListNode(-1) + p = cur_level + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + p.next = ListNode(node.val) + p = p.next + + if cur_level != p: + res.append(cur_level.next) + queue = next_queue + return res \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.04.\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247/\351\235\242\350\257\225\351\242\230 04.04-\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.04.\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247/\351\235\242\350\257\225\351\242\230 04.04-\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247.py" new file mode 100644 index 0000000..e357a5e --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.04.\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247/\351\235\242\350\257\225\351\242\230 04.04-\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247.py" @@ -0,0 +1,20 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def isBalanced(self, root: TreeNode) -> bool: + if not root or (not root.left and not root.right): + return True + + def getHeight(node): + if not node: + return 0 + return 1 + max(getHeight(node.left), getHeight(node.right)) + + if abs(getHeight(root.left) - getHeight(root.right)) > 1: + return False + return self.isBalanced(root.left) and self.isBalanced(root.right) diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.05.\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\351\235\242\350\257\225\351\242\230 04.05-\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.05.\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\351\235\242\350\257\225\351\242\230 04.05-\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..f7917a0 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.05.\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\351\235\242\350\257\225\351\242\230 04.05-\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,16 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + l = inorder(root) + return l == sorted(l) and len(l) == len(set(l)) \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.06.\345\220\216\347\273\247\350\200\205/\351\235\242\350\257\225\351\242\230 04.06-\345\220\216\347\273\247\350\200\205.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.06.\345\220\216\347\273\247\350\200\205/\351\235\242\350\257\225\351\242\230 04.06-\345\220\216\347\273\247\350\200\205.py" new file mode 100644 index 0000000..b395f1d --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 04.06.\345\220\216\347\273\247\350\200\205/\351\235\242\350\257\225\351\242\230 04.06-\345\220\216\347\273\247\350\200\205.py" @@ -0,0 +1,22 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> TreeNode: + self.res = None + def inorder(node): + if not node: + return + + inorder(node.left) + if not self.res and node.val > p.val: + self.res = node + return + inorder(node.right) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\230 08.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\230 08.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" new file mode 100644 index 0000000..92c38d7 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\230 08.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" @@ -0,0 +1,8 @@ +class Solution: + def waysToStep(self, n: int) -> int: + dp = [0] * (max(10, n + 1)) + MOD = 10 ** 9 + 7 + dp[1], dp[2], dp[3] = 1, 2, 4 + for i in range(4, n + 1): + dp[i] = (dp[i - 1] + dp[i - 2] + dp[i - 3]) % MOD + return dp[n] \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.02.\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272/\351\235\242\350\257\225\351\242\230 08.02-\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.02.\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272/\351\235\242\350\257\225\351\242\230 08.02-\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272.py" new file mode 100644 index 0000000..00aeb6f --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.02.\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272/\351\235\242\350\257\225\351\242\230 08.02-\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272.py" @@ -0,0 +1,30 @@ +from collections import deque +class Solution: + def pathWithObstacles(self, obstacleGrid: List[List[int]]) -> List[List[int]]: + if not obstacleGrid or not obstacleGrid[0] or obstacleGrid[0][0] == 1: + return [] + m, n = len(obstacleGrid), len(obstacleGrid[0]) + if obstacleGrid[m -1][n - 1] == 1: + return [] + + self.res = [] + # dfs + visited = set([(0, 0)]) + def dfs(i, j, path): + if not 0 <= i < m or not 0 <= j < n: + return + path.append([i, j]) + if i == m - 1 and j == n - 1: + self.res = path[:] + return + if not self.res: + if i + 1 < m and obstacleGrid[i + 1][j] != 1 and (i + 1, j) not in visited: + visited.add((i + 1, j)) + dfs(i + 1, j, path[:]) + if j + 1 < n and obstacleGrid[i][j + 1] != 1 and (i, j + 1) not in visited: + visited.add((i, j + 1)) + dfs(i, j + 1, path[:]) + dfs(0, 0, []) + return self.res + + \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.03.\351\255\224\346\234\257\347\264\242\345\274\225/\351\235\242\350\257\225\351\242\230 08.03-\351\255\224\346\234\257\347\264\242\345\274\225.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.03.\351\255\224\346\234\257\347\264\242\345\274\225/\351\235\242\350\257\225\351\242\230 08.03-\351\255\224\346\234\257\347\264\242\345\274\225.py" new file mode 100644 index 0000000..793972a --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.03.\351\255\224\346\234\257\347\264\242\345\274\225/\351\235\242\350\257\225\351\242\230 08.03-\351\255\224\346\234\257\347\264\242\345\274\225.py" @@ -0,0 +1,6 @@ +class Solution: + def findMagicIndex(self, nums: List[int]) -> int: + for i, num in enumerate(nums): + if i == num: + return i + return -1 \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.05.\351\200\222\345\275\222\344\271\230\346\263\225/\351\235\242\350\257\225\351\242\230 08.05-\351\200\222\345\275\222\344\271\230\346\263\225.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.05.\351\200\222\345\275\222\344\271\230\346\263\225/\351\235\242\350\257\225\351\242\230 08.05-\351\200\222\345\275\222\344\271\230\346\263\225.py" new file mode 100644 index 0000000..2acc5a1 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 08.05.\351\200\222\345\275\222\344\271\230\346\263\225/\351\235\242\350\257\225\351\242\230 08.05-\351\200\222\345\275\222\344\271\230\346\263\225.py" @@ -0,0 +1,7 @@ +class Solution: + def multiply(self, A: int, B: int) -> int: + if B == 1: + return A + if B == 0: + return 0 + return A + self.multiply(A, B - 1) \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 10.02.\345\217\230\344\275\215\350\257\215\347\273\204/\351\235\242\350\257\225\351\242\230 10.02-\345\217\230\344\275\215\350\257\215\347\273\204.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 10.02.\345\217\230\344\275\215\350\257\215\347\273\204/\351\235\242\350\257\225\351\242\230 10.02-\345\217\230\344\275\215\350\257\215\347\273\204.py" new file mode 100644 index 0000000..3488b03 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 10.02.\345\217\230\344\275\215\350\257\215\347\273\204/\351\235\242\350\257\225\351\242\230 10.02-\345\217\230\344\275\215\350\257\215\347\273\204.py" @@ -0,0 +1,9 @@ +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + from collections import defaultdict + + d = defaultdict(list) + for word in strs: + d["".join(sorted(word))].append(word) + + return [val for key, val in d.items()] \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.01.\344\272\244\346\215\242\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\230 16.01-\344\272\244\346\215\242\346\225\260\345\255\227.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.01.\344\272\244\346\215\242\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\230 16.01-\344\272\244\346\215\242\346\225\260\345\255\227.py" new file mode 100644 index 0000000..7fb10f6 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.01.\344\272\244\346\215\242\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\230 16.01-\344\272\244\346\215\242\346\225\260\345\255\227.py" @@ -0,0 +1,3 @@ +class Solution: + def swapNumbers(self, numbers: List[int]) -> List[int]: + return [numbers[1], numbers[0]] \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.02.\345\215\225\350\257\215\351\242\221\347\216\207/\351\235\242\350\257\225\351\242\230 16.02-\345\215\225\350\257\215\351\242\221\347\216\207.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.02.\345\215\225\350\257\215\351\242\221\347\216\207/\351\235\242\350\257\225\351\242\230 16.02-\345\215\225\350\257\215\351\242\221\347\216\207.py" new file mode 100644 index 0000000..5c7be33 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.02.\345\215\225\350\257\215\351\242\221\347\216\207/\351\235\242\350\257\225\351\242\230 16.02-\345\215\225\350\257\215\351\242\221\347\216\207.py" @@ -0,0 +1,14 @@ +from collections import Counter +class WordsFrequency: + + def __init__(self, book: List[str]): + self.word2freq = Counter(book) + + def get(self, word: str) -> int: + return self.word2freq[word] + + + +# Your WordsFrequency object will be instantiated and called as such: +# obj = WordsFrequency(book) +# param_1 = obj.get(word) \ No newline at end of file diff --git "a/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\230 16.07-\346\234\200\345\244\247\346\225\260\345\200\274.py" "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\230 16.07-\346\234\200\345\244\247\346\225\260\345\200\274.py" new file mode 100644 index 0000000..22f9611 --- /dev/null +++ "b/LeetCode-Python/\351\235\242\350\257\225\351\242\230 16.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\230 16.07-\346\234\200\345\244\247\346\225\260\345\200\274.py" @@ -0,0 +1,3 @@ +class Solution: + def maximum(self, a: int, b: int) -> int: + return sorted([a, b])[-1] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207 Offer 03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..6104d25 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,8 @@ +class Solution: + def findRepeatNumber(self, nums: List[int]) -> int: + for i, num in enumerate(nums): + if num != i: + if nums[num] == num: + return num + nums[i], nums[num] = nums[num], nums[i] + return nums[-1] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207 Offer 05-\346\233\277\346\215\242\347\251\272\346\240\274.py" "b/\345\211\221\346\214\207 Offer 05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207 Offer 05-\346\233\277\346\215\242\347\251\272\346\240\274.py" new file mode 100644 index 0000000..3e2d3c9 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207 Offer 05-\346\233\277\346\215\242\347\251\272\346\240\274.py" @@ -0,0 +1,3 @@ +class Solution: + def replaceSpace(self, s: str) -> str: + return s.replace(" ", "%20") \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207 Offer 06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" "b/\345\211\221\346\214\207 Offer 06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207 Offer 06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" new file mode 100644 index 0000000..cd554da --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207 Offer 06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" @@ -0,0 +1,14 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def reversePrint(self, head: ListNode) -> List[int]: + res = [] + p = head + while p: + res.append(p.val) + p = p.next + return res[::-1] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" "b/\345\211\221\346\214\207 Offer 22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" new file mode 100644 index 0000000..1b49137 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" @@ -0,0 +1,23 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getKthFromEnd(self, head: ListNode, k: int) -> ListNode: + l = 0 + p = head + while p: + l += 1 + p = p.next + + count = l - k + 1 + p = head + while 1: + count -= 1 + if count == 0: + break + p = p.next + + return p \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207 Offer 27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" "b/\345\211\221\346\214\207 Offer 27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207 Offer 27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" new file mode 100644 index 0000000..5d79dfc --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207 Offer 27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" @@ -0,0 +1,15 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def mirrorTree(self, root: TreeNode) -> TreeNode: + if not root: + return root + root.left, root.right = root.right, root.left + self.mirrorTree(root.left) + self.mirrorTree(root.right) + return root \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207 Offer 35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" "b/\345\211\221\346\214\207 Offer 35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207 Offer 35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" new file mode 100644 index 0000000..819027c --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207 Offer 35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" @@ -0,0 +1,32 @@ +""" +# Definition for a Node. +class Node: + def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): + self.val = int(x) + self.next = next + self.random = random +""" +class Solution: + def copyRandomList(self, head: 'Node') -> 'Node': + if not head: + return head + + old2new = dict() + p = head + while p: + cur_node = p + new_node = Node(cur_node.val) + old2new[cur_node] = new_node + p = p.next + + p = head + while p: + cur_node = p + new_node = old2new[cur_node] + + if cur_node.next: + new_node.next = old2new[cur_node.next] + if cur_node.random: + new_node.random = old2new[cur_node.random] + p = p.next + return old2new[head] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 49.\344\270\221\346\225\260/\345\211\221\346\214\207 Offer 49-\344\270\221\346\225\260.py" "b/\345\211\221\346\214\207 Offer 49.\344\270\221\346\225\260/\345\211\221\346\214\207 Offer 49-\344\270\221\346\225\260.py" new file mode 100644 index 0000000..0513a62 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 49.\344\270\221\346\225\260/\345\211\221\346\214\207 Offer 49-\344\270\221\346\225\260.py" @@ -0,0 +1,17 @@ +class Solution: + def nthUglyNumber(self, n: int) -> int: + import heapq + + min_heap = [1] # find the smallest ugly number + visited = set() + cnt = n + while cnt: + cur = heappop(min_heap) + cnt -= 1 + if not cnt: + return cur + for multiple in [2, 3, 5]: + nxt = cur * multiple + if nxt not in visited: + visited.add(nxt) + heappush(min_heap, nxt) \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" "b/\345\211\221\346\214\207 Offer 52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" new file mode 100644 index 0000000..49c593f --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" @@ -0,0 +1,22 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + if not headA or not headB: + return None + + pa, pb = headA, headB + while pa != pb: + if pa: + pa = pa.next + else: + pa = headB + if pb: + pb = pb.next + else: + pb = headA + return pa \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 53 - I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207 Offer 53 - I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" "b/\345\211\221\346\214\207 Offer 53 - I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207 Offer 53 - I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" new file mode 100644 index 0000000..df2d2ae --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 53 - I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207 Offer 53 - I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" @@ -0,0 +1,25 @@ +class Solution: + def search(self, nums: List[int], target: int) -> int: + # return nums.count(target) + # 1. find the left-most index + left, right = 0, len(nums) - 1 + left_index = -1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] >= target: + left_index = mid + right = mid - 1 + else: + left = mid + 1 + # 2. find the right-most index + left, right = 0, len(nums) - 1 + right_index = -1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] <= target: + right_index = mid + left = mid + 1 + else: + right = mid - 1 + + return right_index - left_index + 1 if left_index > -1 else 0 \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" "b/\345\211\221\346\214\207 Offer 54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" new file mode 100644 index 0000000..4760d38 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207 Offer 54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def kthLargest(self, root: TreeNode, k: int) -> int: + self.count = 0 + self.res = 0 + def inorder(node): + if not node: + return + + inorder(node.right) + self.count += 1 + if self.count == k: + self.res = node.val + + inorder(node.left) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 55 - I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207 Offer 55 - I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" "b/\345\211\221\346\214\207 Offer 55 - I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207 Offer 55 - I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" new file mode 100644 index 0000000..4fde3bf --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 55 - I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207 Offer 55 - I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def maxDepth(self, root: TreeNode) -> int: + if not root: + return 0 + queue = [root] + depth = 0 + while queue: + next_queue = [] + depth += 1 + for node in queue: + if node.left: + next_queue.append(node.left) + if node.right: + next_queue.append(node.right) + queue = next_queue[:] + return depth \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207 Offer 57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" new file mode 100644 index 0000000..4b5917d --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer 57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" @@ -0,0 +1,12 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i, num in enumerate(nums): + left, right = i + 1, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target - num: + return [num, target - num] + elif nums[mid] < target - num: + left = mid + 1 + else: + right = mid - 1 \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 58 - II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207 Offer 58 - II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" "b/\345\211\221\346\214\207 Offer 58 - II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207 Offer 58 - II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..e065bdf --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 58 - II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207 Offer 58 - II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,3 @@ +class Solution: + def reverseLeftWords(self, s: str, n: int) -> str: + return s[n:] + s[:n] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer 64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207 Offer 64-\346\261\2021+2+\342\200\246+n.py" "b/\345\211\221\346\214\207 Offer 64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207 Offer 64-\346\261\2021+2+\342\200\246+n.py" new file mode 100644 index 0000000..f39339c --- /dev/null +++ "b/\345\211\221\346\214\207 Offer 64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207 Offer 64-\346\261\2021+2+\342\200\246+n.py" @@ -0,0 +1,3 @@ +class Solution: + def sumNums(self, n: int) -> int: + return (n ** 2 + n) >> 1 \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 004.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 004-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207 Offer II 004.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 004-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..64b9513 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 004.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 004-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,3 @@ +class Solution: + def singleNumber(self, nums: List[int]) -> int: + return reduce(lambda x, y: x ^ y, nums) \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 006.\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 006-\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214.py" "b/\345\211\221\346\214\207 Offer II 006.\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 006-\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214.py" new file mode 100644 index 0000000..17d2a7f --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 006.\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 006-\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -0,0 +1,7 @@ +class Solution: + def twoSum(self, numbers: List[int], target: int) -> List[int]: + num2index = dict() + for index, num in enumerate(numbers): + if target - num in num2index: + return [num2index[target - num], index] + num2index[num] = index \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 018.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 018-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207.py" "b/\345\211\221\346\214\207 Offer II 018.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 018-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207.py" new file mode 100644 index 0000000..c26b8dd --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 018.\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 018-\346\234\211\346\225\210\347\232\204\345\233\236\346\226\207.py" @@ -0,0 +1,5 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + s = "".join([char.lower() for char in s if char.isalpha() or char.isdigit()]) + print(s) + return s == s[::-1] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 019.\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 019-\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207.py" "b/\345\211\221\346\214\207 Offer II 019.\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 019-\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207.py" new file mode 100644 index 0000000..90330a6 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 019.\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207/\345\211\221\346\214\207 Offer II 019-\346\234\200\345\244\232\345\210\240\351\231\244\344\270\200\344\270\252\345\255\227\347\254\246\345\276\227\345\210\260\345\233\236\346\226\207.py" @@ -0,0 +1,20 @@ +class Solution: + def validPalindrome(self, s: str) -> bool: + delete = False + left, right = 0, len(s) - 1 + + while left < right: + if s[left] != s[right]: + if not delete: + delete = True + break + left += 1 + right -= 1 + + if delete: + s1 = s[:left] + s[left + 1:] + s2 = s[:right] + s[right + 1:] + return s1 == s1[::-1] or s2 == s2[::-1] + else: + return True + \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 023.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/\345\211\221\346\214\207 Offer II 023-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271.py" "b/\345\211\221\346\214\207 Offer II 023.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/\345\211\221\346\214\207 Offer II 023-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271.py" new file mode 100644 index 0000000..1e17d1e --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 023.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/\345\211\221\346\214\207 Offer II 023-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271.py" @@ -0,0 +1,37 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + la, lb = 0, 0 + p = headA + while p: + la += 1 + p = p.next + + p = headB + while p: + lb += 1 + p = p.next + + if la > lb: + headA, headB = headB, headA + la, lb = lb, la + + diff = lb - la + pa, pb = headA, headB + while diff: + pb = pb.next + diff -= 1 + + while pa and pb: + if pa == pb: + return pa + else: + pa = pa.next + pb = pb.next + + return None diff --git "a/\345\211\221\346\214\207 Offer II 024.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207 Offer II 024-\345\217\215\350\275\254\351\223\276\350\241\250.py" "b/\345\211\221\346\214\207 Offer II 024.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207 Offer II 024-\345\217\215\350\275\254\351\223\276\350\241\250.py" new file mode 100644 index 0000000..bac3cc1 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 024.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207 Offer II 024-\345\217\215\350\275\254\351\223\276\350\241\250.py" @@ -0,0 +1,19 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + # if not head or not head.next: + # return head + # tmp = self.reverseList(head.next) + # head.next.next = head + # head.next = None + # return tmp + prev, cur = None, head + while cur: + next_node = cur.next + cur.next = prev + prev, cur = cur, next_node + return prev \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 032.\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/\345\211\221\346\214\207 Offer II 032-\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215.py" "b/\345\211\221\346\214\207 Offer II 032.\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/\345\211\221\346\214\207 Offer II 032-\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215.py" new file mode 100644 index 0000000..0a80846 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 032.\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/\345\211\221\346\214\207 Offer II 032-\346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215.py" @@ -0,0 +1,4 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + from collections import Counter + return s != t and Counter(s) == Counter(t) \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 036.\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\211\221\346\214\207 Offer II 036-\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.py" "b/\345\211\221\346\214\207 Offer II 036.\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\211\221\346\214\207 Offer II 036-\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.py" new file mode 100644 index 0000000..a926627 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 036.\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\211\221\346\214\207 Offer II 036-\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.py" @@ -0,0 +1,22 @@ +import math +class Solution: + def evalRPN(self, tokens: List[str]) -> int: + num_stack = [] + + res = 0 + for token in tokens: + # print(num_stack) + if token[-1].isdigit(): + num_stack.append(int(token)) + else: + second, first = num_stack.pop(), num_stack.pop() + if token == "+": + num_stack.append(first + second) + elif token == "-": + num_stack.append(first - second) + elif token == "*": + num_stack.append(first * second) + elif token == "/": + num_stack.append(int(first / second)) + + return num_stack[0] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 038.\346\257\217\346\227\245\346\270\251\345\272\246/\345\211\221\346\214\207 Offer II 038-\346\257\217\346\227\245\346\270\251\345\272\246.py" "b/\345\211\221\346\214\207 Offer II 038.\346\257\217\346\227\245\346\270\251\345\272\246/\345\211\221\346\214\207 Offer II 038-\346\257\217\346\227\245\346\270\251\345\272\246.py" new file mode 100644 index 0000000..25240ec --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 038.\346\257\217\346\227\245\346\270\251\345\272\246/\345\211\221\346\214\207 Offer II 038-\346\257\217\346\227\245\346\270\251\345\272\246.py" @@ -0,0 +1,11 @@ +class Solution: + def dailyTemperatures(self, temperatures: List[int]) -> List[int]: + res = [0] * len(temperatures) + stack = [] # store index into this stack + for i, temp in enumerate(temperatures): + while stack and temperatures[stack[-1]] < temp: + last_index = stack[-1] + res[last_index] = i - last_index + stack.pop() + stack.append(i) + return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 044.\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207 Offer II 044-\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/\345\211\221\346\214\207 Offer II 044.\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207 Offer II 044-\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..abc928a --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 044.\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207 Offer II 044-\344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,24 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def largestValues(self, root: TreeNode) -> List[int]: + queue = [root] + res = [] + + while queue: + next_queue = [] + max_val = float("-inf") + for node in queue: + if node: + max_val = max(max_val, node.val) + next_queue.extend([node.left, node.right]) + + queue = next_queue + if max_val != float("-inf"): + res.append(max_val) + + return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 045.\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/\345\211\221\346\214\207 Offer II 045-\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274.py" "b/\345\211\221\346\214\207 Offer II 045.\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/\345\211\221\346\214\207 Offer II 045-\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274.py" new file mode 100644 index 0000000..4f44c52 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 045.\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/\345\211\221\346\214\207 Offer II 045-\344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def findBottomLeftValue(self, root: TreeNode) -> int: + queue = [root] + res = [] + + while queue: + next_queue = [] + cur_level = [] + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + cur_level.append(node.val) + + if cur_level: + res.append(cur_level) + queue = next_queue + return res[-1][0] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 049.\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 049-\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214.py" "b/\345\211\221\346\214\207 Offer II 049.\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 049-\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214.py" new file mode 100644 index 0000000..89118a0 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 049.\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 049-\344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: TreeNode) -> int: + self.res = False + self.res = 0 + def dfs(node, path_sum): + if not node: + return + + path_sum = path_sum * 10 + node.val + if not node.left and not node.right: + self.res += path_sum + + dfs(node.left, path_sum) + dfs(node.right, path_sum) + + dfs(root, 0) + return self.res \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 052.\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\345\211\221\346\214\207 Offer II 052-\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/\345\211\221\346\214\207 Offer II 052.\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\345\211\221\346\214\207 Offer II 052-\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..490aa9d --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 052.\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\345\211\221\346\214\207 Offer II 052-\345\261\225\345\271\263\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def increasingBST(self, root: TreeNode) -> TreeNode: + + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + inorder_list = inorder(root) + + dummy = TreeNode(-1) + p = dummy + for val in inorder_list: + p.right = TreeNode(val) + p = p.right + + return dummy.right diff --git "a/\345\211\221\346\214\207 Offer II 053.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247/\345\211\221\346\214\207 Offer II 053-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247.py" "b/\345\211\221\346\214\207 Offer II 053.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247/\345\211\221\346\214\207 Offer II 053-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247.py" new file mode 100644 index 0000000..c18fff9 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 053.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247/\345\211\221\346\214\207 Offer II 053-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\270\255\345\272\217\345\220\216\347\273\247.py" @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> Optional[TreeNode]: + # find the next node after p + if not root: + return None + if p.val >= root.val: + return self.inorderSuccessor(root.right, p) + else: + return self.inorderSuccessor(root.left, p) or root + \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 054.\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 054-\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214.py" "b/\345\211\221\346\214\207 Offer II 054.\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 054-\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214.py" new file mode 100644 index 0000000..a4313b7 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 054.\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 054-\346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214.py" @@ -0,0 +1,46 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# class Solution: +# def convertBST(self, root: TreeNode) -> TreeNode: + +# def inOrderGetSum(node): +# if not node: +# return 0 +# return node.val + inOrderGetSum(node.left) + inOrderGetSum(node.right) + +# self.cur_sum = 0 +# def inOrderSetVal(node, total_sum): +# if not node: +# return 0 + +# original_node_val = node.val +# inOrderSetVal(node.left, total_sum) +# node.val = total_sum - self.cur_sum +# self.cur_sum += original_node_val +# right_subtree_sum = inOrderSetVal(node.right, total_sum) + + +# total_sum = inOrderGetSum(root) +# inOrderSetVal(root, total_sum) +# return root + + +class Solution: + def convertBST(self, root: TreeNode) -> TreeNode: + # right - root - left + self.cur_sum = 0 + def reverseInorder(node): + if not node: + return + + reverseInorder(node.right) + self.cur_sum += node.val + node.val = self.cur_sum + reverseInorder(node.left) + + reverseInorder(root) + return root diff --git "a/\345\211\221\346\214\207 Offer II 055.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/\345\211\221\346\214\207 Offer II 055-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250.py" "b/\345\211\221\346\214\207 Offer II 055.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/\345\211\221\346\214\207 Offer II 055-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250.py" new file mode 100644 index 0000000..be2e409 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 055.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/\345\211\221\346\214\207 Offer II 055-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250.py" @@ -0,0 +1,29 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class BSTIterator: + def __init__(self, root: TreeNode): + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + self.inorder = inorder(root) + self.index = 0 + + def next(self) -> int: + self.index += 1 + return self.inorder[self.index - 1] + + def hasNext(self) -> bool: + return self.index < len(self.inorder) + + + +# Your BSTIterator object will be instantiated and called as such: +# obj = BSTIterator(root) +# param_1 = obj.next() +# param_2 = obj.hasNext() \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 056.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 056-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214.py" "b/\345\211\221\346\214\207 Offer II 056.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 056-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214.py" new file mode 100644 index 0000000..defd4cd --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 056.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/\345\211\221\346\214\207 Offer II 056-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214.py" @@ -0,0 +1,27 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def findTarget(self, root: TreeNode, k: int) -> bool: + visited = set() + self.res = False + + def inorder(node): + if not node: + return + + inorder(node.left) + + if k - node.val in visited: + self.res = True + visited.add(node.val) + + if not self.res: + inorder(node.right) + + inorder(root) + return self.res + diff --git "a/\345\211\221\346\214\207 Offer II 059.\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274/\345\211\221\346\214\207 Offer II 059-\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274.py" "b/\345\211\221\346\214\207 Offer II 059.\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274/\345\211\221\346\214\207 Offer II 059-\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274.py" new file mode 100644 index 0000000..e4f70e2 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 059.\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274/\345\211\221\346\214\207 Offer II 059-\346\225\260\346\215\256\346\265\201\347\232\204\347\254\254K\345\244\247\346\225\260\345\200\274.py" @@ -0,0 +1,22 @@ +class KthLargest: + + def __init__(self, k: int, nums: List[int]): + self.k = k + self.min_heap = [] + for num in nums: + if len(self.min_heap) < k: + heappush(self.min_heap, num) + else: + heappushpop(self.min_heap, num) + + def add(self, val: int) -> int: + if len(self.min_heap) < self.k: + heappush(self.min_heap, val) + else: + heappushpop(self.min_heap, val) + return self.min_heap[0] + + +# Your KthLargest object will be instantiated and called as such: +# obj = KthLargest(k, nums) +# param_1 = obj.add(val) \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 060.\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 060-\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207 Offer II 060.\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 060-\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227.py" new file mode 100644 index 0000000..a27164b --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 060.\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 060-\345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204k\344\270\252\346\225\260\345\255\227.py" @@ -0,0 +1,20 @@ +from collections import Counter +from heapq import * +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + c = Counter(nums) + # return sorted(set(nums), key = lambda x: -c[x])[:k] + heap_set = set() + min_heap = [] + for num in nums: + if num not in heap_set: + heap_set.add(num) + if len(min_heap) < k: + heappush(min_heap, (c[num], num)) + else: + heappushpop(min_heap, (c[num], num)) + # print(min_heap) + res = [] + while min_heap: + res.append(heappop(min_heap)[1]) + return res diff --git "a/\345\211\221\346\214\207 Offer II 069.\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/\345\211\221\346\214\207 Offer II 069-\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250.py" "b/\345\211\221\346\214\207 Offer II 069.\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/\345\211\221\346\214\207 Offer II 069-\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250.py" new file mode 100644 index 0000000..e013042 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 069.\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/\345\211\221\346\214\207 Offer II 069-\345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250.py" @@ -0,0 +1,13 @@ +class Solution: + def peakIndexInMountainArray(self, arr: List[int]) -> int: + left, right = 0, len(arr) - 1 + while left <= right: + mid = (left + right) // 2 + if 0 < mid < len(arr) - 1 and arr[mid - 1] < arr[mid] and arr[mid] > arr[mid + 1]: + return mid + + if arr[mid + 1] > arr[mid]: + left = mid + 1 + else: + right = mid - 1 + return left \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 076.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 076-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207 Offer II 076.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 076-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..ab926ec --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 076.\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207 Offer II 076-\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254k\345\244\247\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,3 @@ +class Solution: + def findKthLargest(self, nums: List[int], k: int) -> int: + return sorted(nums)[::-1][k - 1] \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 079.\346\211\200\346\234\211\345\255\220\351\233\206/\345\211\221\346\214\207 Offer II 079-\346\211\200\346\234\211\345\255\220\351\233\206.py" "b/\345\211\221\346\214\207 Offer II 079.\346\211\200\346\234\211\345\255\220\351\233\206/\345\211\221\346\214\207 Offer II 079-\346\211\200\346\234\211\345\255\220\351\233\206.py" new file mode 100644 index 0000000..52471ac --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 079.\346\211\200\346\234\211\345\255\220\351\233\206/\345\211\221\346\214\207 Offer II 079-\346\211\200\346\234\211\345\255\220\351\233\206.py" @@ -0,0 +1,9 @@ +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + res = [[]] + for num in nums: + new_res = [] + for subset in res: + new_res.append(subset + [num]) + res += new_res + return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 083.\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/\345\211\221\346\214\207 Offer II 083-\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227.py" "b/\345\211\221\346\214\207 Offer II 083.\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/\345\211\221\346\214\207 Offer II 083-\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227.py" new file mode 100644 index 0000000..a9d570a --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 083.\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/\345\211\221\346\214\207 Offer II 083-\346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227.py" @@ -0,0 +1,16 @@ +class Solution: + def permute(self, nums: List[int]) -> List[List[int]]: + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + res = [] + def dfs(tmp, nums): + if not nums: + res.append(tmp) + + for i, x in enumerate(nums): + dfs(tmp + [x], nums[:i] + nums[i + 1:]) + + dfs([], nums) + return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 085.\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/\345\211\221\346\214\207 Offer II 085-\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267.py" "b/\345\211\221\346\214\207 Offer II 085.\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/\345\211\221\346\214\207 Offer II 085-\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267.py" new file mode 100644 index 0000000..1b94ac9 --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 085.\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/\345\211\221\346\214\207 Offer II 085-\347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267.py" @@ -0,0 +1,14 @@ +class Solution: + def generateParenthesis(self, n: int) -> List[str]: + res = [] + def dfs(left, right, tmp): + if left == 0 and right == 0: + res.append(tmp) + return + if left > 0: + dfs(left - 1, right, tmp + "(") + if right > left: + dfs(left, right - 1, tmp + ")") + + dfs(n, n, "") + return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207 Offer II 119.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/\345\211\221\346\214\207 Offer II 119-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.py" "b/\345\211\221\346\214\207 Offer II 119.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/\345\211\221\346\214\207 Offer II 119-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.py" new file mode 100644 index 0000000..160e3de --- /dev/null +++ "b/\345\211\221\346\214\207 Offer II 119.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/\345\211\221\346\214\207 Offer II 119-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.py" @@ -0,0 +1,15 @@ +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + record = dict() + res = 0 + for num in nums: + if num not in record: + left = record.get(num - 1, 0) + right = record.get(num + 1, 0) + + length = right + left + 1 + res = max(res, length) + for i in [num - left, num, num + right]: + record[i] = length + + return res diff --git "a/\345\211\221\346\214\207Offer03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207Offer03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" deleted file mode 100644 index cf94ed9..0000000 --- "a/\345\211\221\346\214\207Offer03.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer03-\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227.py" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution(object): - def findRepeatNumber(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - visited = set() - for num in nums: - if num in visited: - return num - visited.add(num) \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer04.\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/\345\211\221\346\214\207Offer04-\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276.py" "b/\345\211\221\346\214\207Offer04.\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/\345\211\221\346\214\207Offer04-\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276.py" deleted file mode 100644 index d782a75..0000000 --- "a/\345\211\221\346\214\207Offer04.\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/\345\211\221\346\214\207Offer04-\344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def findNumberIn2DArray(self, matrix, target): - """ - :type matrix: List[List[int]] - :type target: int - :rtype: bool - """ - - if not matrix or not matrix[0]: - return False - m, n = len(matrix), len(matrix[0]) - - x, y = m - 1, 0 - while 0 <= x < m and 0 <= y < n: - if matrix[x][y] == target: - return True - elif matrix[x][y] > target: - x -= 1 - else: - y += 1 - return False \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207Offer05-\346\233\277\346\215\242\347\251\272\346\240\274.py" "b/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207Offer05-\346\233\277\346\215\242\347\251\272\346\240\274.py" deleted file mode 100644 index 33c8f86..0000000 --- "a/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274/\345\211\221\346\214\207Offer05-\346\233\277\346\215\242\347\251\272\346\240\274.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def replaceSpace(self, s): - """ - :type s: str - :rtype: str - """ - return s.replace(" ", "%20") \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207Offer06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" "b/\345\211\221\346\214\207Offer06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207Offer06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" deleted file mode 100644 index 445af2a..0000000 --- "a/\345\211\221\346\214\207Offer06.\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/\345\211\221\346\214\207Offer06-\344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250.py" +++ /dev/null @@ -1,17 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def reversePrint(self, head): - """ - :type head: ListNode - :rtype: List[int] - """ - res = [] - while head: - res.append(head.val) - head = head.next - return res[::-1] \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer07.\351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer07-\351\207\215\345\273\272\344\272\214\345\217\211\346\240\221.py" "b/\345\211\221\346\214\207Offer07.\351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer07-\351\207\215\345\273\272\344\272\214\345\217\211\346\240\221.py" deleted file mode 100644 index fc39bbe..0000000 --- "a/\345\211\221\346\214\207Offer07.\351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer07-\351\207\215\345\273\272\344\272\214\345\217\211\346\240\221.py" +++ /dev/null @@ -1,22 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def buildTree(self, preorder, inorder): - """ - :type preorder: List[int] - :type inorder: List[int] - :rtype: TreeNode - """ - if not preorder: - return None - root = TreeNode(preorder[0]) - idx = inorder.index(root.val) - root.left = self.buildTree(preorder[1:1 + idx], inorder[:idx]) - root.right = self.buildTree(preorder[1 + idx:], inorder[idx + 1:]) - - return root \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer09.\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/\345\211\221\346\214\207Offer09-\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.py" "b/\345\211\221\346\214\207Offer09.\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/\345\211\221\346\214\207Offer09-\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.py" deleted file mode 100644 index 99882bd..0000000 --- "a/\345\211\221\346\214\207Offer09.\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/\345\211\221\346\214\207Offer09-\347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.py" +++ /dev/null @@ -1,28 +0,0 @@ -class CQueue(object): - - def __init__(self): - self.s1 = [] - self.s2 = [] - - def appendTail(self, value): - """ - :type value: int - :rtype: None - """ - self.s1.append(value) - - def deleteHead(self): - """ - :rtype: int - """ - if not self.s2: - while self.s1: - self.s2.append(self.s1.pop()) - return self.s2.pop() if self.s2 else -1 - - - -# Your CQueue object will be instantiated and called as such: -# obj = CQueue() -# obj.appendTail(value) -# param_2 = obj.deleteHead() \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer10-I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/\345\211\221\346\214\207Offer10-I-\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227.py" "b/\345\211\221\346\214\207Offer10-I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/\345\211\221\346\214\207Offer10-I-\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227.py" deleted file mode 100644 index ff52e1b..0000000 --- "a/\345\211\221\346\214\207Offer10-I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/\345\211\221\346\214\207Offer10-I-\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def fib(self, n): - """ - :type n: int - :rtype: int - """ - if n == 0: - return 0 - MOD = 10 ** 9 + 7 - cnt = 1 - pre, cur = 0, 1 - while cnt < n: - tmp = cur + pre - pre = cur - cur = tmp - cnt += 1 - return cur % MOD \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer10-II.\351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/\345\211\221\346\214\207Offer10-II-\351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230.py" "b/\345\211\221\346\214\207Offer10-II.\351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/\345\211\221\346\214\207Offer10-II-\351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230.py" deleted file mode 100644 index e07ceb3..0000000 --- "a/\345\211\221\346\214\207Offer10-II.\351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/\345\211\221\346\214\207Offer10-II-\351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def numWays(self, n): - """ - :type n: int - :rtype: int - """ - if n == 0: - return 1 - MOD = 10 ** 9 + 7 - cnt = 1 - pre, cur = 1, 1 - while cnt < n: - tmp = cur + pre - pre = cur - cur = tmp - cnt += 1 - return cur % MOD \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer11.\346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/\345\211\221\346\214\207Offer11-\346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207Offer11.\346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/\345\211\221\346\214\207Offer11-\346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" deleted file mode 100644 index 052370a..0000000 --- "a/\345\211\221\346\214\207Offer11.\346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/\345\211\221\346\214\207Offer11-\346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def minArray(self, numbers): - """ - :type numbers: List[int] - :rtype: int - """ - return min(numbers) - # if not numbers: - # return None - - # if numbers[0] < numbers[-1]: - # return numbers[0] - - # left, right = 0, len(numbers) - - # while left <= right: - # mid = (left + right) // 2 - - # if numbers[mid] < numbers[mid - 1]: - # return numbers[mid] - # elif numbers[mid] \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer12.\347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/\345\211\221\346\214\207Offer12-\347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204.py" "b/\345\211\221\346\214\207Offer12.\347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/\345\211\221\346\214\207Offer12-\347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204.py" deleted file mode 100644 index 29eb51e..0000000 --- "a/\345\211\221\346\214\207Offer12.\347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/\345\211\221\346\214\207Offer12-\347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204.py" +++ /dev/null @@ -1,33 +0,0 @@ -class Solution(object): - def exist(self, board, word): - """ - :type board: List[List[str]] - :type word: str - :rtype: bool - """ - - m, n = len(board), len(board[0]) - - self.res = False - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - def dfs(x0, y0, start): - if start == len(word) - 1 or self.res: - self.res = True - return - - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - - if 0 <= x < m and 0 <= y < n and (x, y) not in visited and board[x][y] == word[start + 1]: - visited.add((x, y)) - dfs(x, y, start + 1) - visited.remove((x, y)) - - for i in range(m): - for j in range(n): - if board[i][j] == word[0]: - visited = set([(i, j)]) - dfs(i, j, 0) - return self.res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer13.\346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/\345\211\221\346\214\207Offer13-\346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264.py" "b/\345\211\221\346\214\207Offer13.\346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/\345\211\221\346\214\207Offer13-\346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264.py" deleted file mode 100644 index 92f631f..0000000 --- "a/\345\211\221\346\214\207Offer13.\346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/\345\211\221\346\214\207Offer13-\346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264.py" +++ /dev/null @@ -1,29 +0,0 @@ -class Solution(object): - def movingCount(self, m, n, k): - """ - :type m: int - :type n: int - :type k: int - :rtype: int - """ - - def dis(x, y): - res = 0 - while x: - res += x%10 - x //= 10 - while y: - res += y%10 - y //= 10 - return res - dp = [[False]*105 for _ in range(105)] - dp[0][0] = True - ans = 1 - for i in range(m): - for j in range(n): - if i==0 and j==0: - continue - if ((i!=0 and dp[i-1][j]) or (j!=0 and dp[i][j-1])) and dis(i, j)<=k: - dp[i][j] = True - ans += 1 - return ans \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer14-I.\345\211\252\347\273\263\345\255\220/\345\211\221\346\214\207Offer14-I-\345\211\252\347\273\263\345\255\220.py" "b/\345\211\221\346\214\207Offer14-I.\345\211\252\347\273\263\345\255\220/\345\211\221\346\214\207Offer14-I-\345\211\252\347\273\263\345\255\220.py" deleted file mode 100644 index 767f3b1..0000000 --- "a/\345\211\221\346\214\207Offer14-I.\345\211\252\347\273\263\345\255\220/\345\211\221\346\214\207Offer14-I-\345\211\252\347\273\263\345\255\220.py" +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def cuttingRope(self, n): - """ - :type n: int - :rtype: int - """ - # dp[i][j] ba changdu wei i de shengzi qiecheng j duan de daan - if n == 2: - return 1 - if n == 3: - return 2 - dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)] - - for i in range(n + 1): - dp[i][0] = i - - for i in range(n + 1): - for j in range(i): - for k in range(i + 1, n + 1): - dp[k][j + 1] = max((k - i) * dp[i][j], dp[k][j + 1]) - - return max(dp[-1]) - - diff --git "a/\345\211\221\346\214\207Offer14-II.\345\211\252\347\273\263\345\255\220II/\345\211\221\346\214\207Offer14-II-\345\211\252\347\273\263\345\255\220II.py" "b/\345\211\221\346\214\207Offer14-II.\345\211\252\347\273\263\345\255\220II/\345\211\221\346\214\207Offer14-II-\345\211\252\347\273\263\345\255\220II.py" deleted file mode 100644 index b38bd18..0000000 --- "a/\345\211\221\346\214\207Offer14-II.\345\211\252\347\273\263\345\255\220II/\345\211\221\346\214\207Offer14-II-\345\211\252\347\273\263\345\255\220II.py" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution(object): - def cuttingRope(self, n): - """ - :type n: int - :rtype: int - """ - if n <= 3: - return n - 1 - - res, MOD = 1, 10 ** 9 + 7 - while n > 4: - res = (res * 3) % MOD - n -= 3 - - return res * n % MOD \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer15.\344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/\345\211\221\346\214\207Offer15-\344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260.py" "b/\345\211\221\346\214\207Offer15.\344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/\345\211\221\346\214\207Offer15-\344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260.py" deleted file mode 100644 index 06e5754..0000000 --- "a/\345\211\221\346\214\207Offer15.\344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/\345\211\221\346\214\207Offer15-\344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260.py" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution(object): - def hammingWeight(self, n): - """ - :type n: int - :rtype: int - """ - res = 0 - while n: - n = n & (n - 1) - res += 1 - return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer16.\346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/\345\211\221\346\214\207Offer16-\346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271.py" "b/\345\211\221\346\214\207Offer16.\346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/\345\211\221\346\214\207Offer16-\346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271.py" deleted file mode 100644 index ede02bf..0000000 --- "a/\345\211\221\346\214\207Offer16.\346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/\345\211\221\346\214\207Offer16-\346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271.py" +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def myPow(self, x, n): - """ - :type x: float - :type n: int - :rtype: float - """ - if n == 0: - return 1 - if n == 1: - return x - if n == 2: - return x * x - - flag = 0 - if n < 0: - flag = 1 - n = -n - - if n % 2 == 0: - res = self.myPow(self.myPow(x, n // 2), 2) - else: - res = self.myPow(self.myPow(x, n // 2), 2) * x - - return res if not flag else 1.0 / res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer17.\346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/\345\211\221\346\214\207Offer17-\346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260.py" "b/\345\211\221\346\214\207Offer17.\346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/\345\211\221\346\214\207Offer17-\346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260.py" deleted file mode 100644 index 23f7bb5..0000000 --- "a/\345\211\221\346\214\207Offer17.\346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/\345\211\221\346\214\207Offer17-\346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def printNumbers(self, n): - """ - :type n: int - :rtype: List[int] - """ - return [i for i in range(1, 10 ** n)] \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer18.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/\345\211\221\346\214\207Offer18-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271.py" "b/\345\211\221\346\214\207Offer18.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/\345\211\221\346\214\207Offer18-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271.py" deleted file mode 100644 index 16e41cd..0000000 --- "a/\345\211\221\346\214\207Offer18.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/\345\211\221\346\214\207Offer18-\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271.py" +++ /dev/null @@ -1,24 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def deleteNode(self, head, val): - """ - :type head: ListNode - :type val: int - :rtype: ListNode - """ - - dummy = ListNode(-1) - dummy.next = head - - p = dummy - while p: - if p.next and p.next.val == val: - p.next = p.next.next - break - p = p.next - return dummy.next \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer21.\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/\345\211\221\346\214\207Offer21-\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.py" "b/\345\211\221\346\214\207Offer21.\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/\345\211\221\346\214\207Offer21-\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.py" deleted file mode 100644 index 1f91ed9..0000000 --- "a/\345\211\221\346\214\207Offer21.\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/\345\211\221\346\214\207Offer21-\350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def exchange(self, nums): - """ - :type nums: List[int] - :rtype: List[int] - """ - left, right = 0, len(nums) - 1 - - while left < right: - if not nums[left] % 2 and nums[right] % 2: - nums[left], nums[right] = nums[right], nums[left] - if nums[left] % 2: - left += 1 - if not nums[right] % 2: - right -= 1 - return nums \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207Offer22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" "b/\345\211\221\346\214\207Offer22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207Offer22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" deleted file mode 100644 index 2d5e9cf..0000000 --- "a/\345\211\221\346\214\207Offer22.\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\345\211\221\346\214\207Offer22-\351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" +++ /dev/null @@ -1,23 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def getKthFromEnd(self, head, k): - """ - :type head: ListNode - :type k: int - :rtype: ListNode - """ - p1, p2 = head, head - while k: - k -= 1 - p2 = p2.next - - while p2: - p1 = p1.next - p2 = p2.next - - return p1 \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer24.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207Offer24-\345\217\215\350\275\254\351\223\276\350\241\250.py" "b/\345\211\221\346\214\207Offer24.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207Offer24-\345\217\215\350\275\254\351\223\276\350\241\250.py" deleted file mode 100644 index d629e01..0000000 --- "a/\345\211\221\346\214\207Offer24.\345\217\215\350\275\254\351\223\276\350\241\250/\345\211\221\346\214\207Offer24-\345\217\215\350\275\254\351\223\276\350\241\250.py" +++ /dev/null @@ -1,20 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def reverseList(self, head): - """ - :type head: ListNode - :rtype: ListNode - """ - if not head or not head.next: - return head - - p = self.reverseList(head.next) - head.next.next = head - head.next = None - - return p \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer25.\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/\345\211\221\346\214\207Offer25-\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250.py" "b/\345\211\221\346\214\207Offer25.\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/\345\211\221\346\214\207Offer25-\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250.py" deleted file mode 100644 index 8252172..0000000 --- "a/\345\211\221\346\214\207Offer25.\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/\345\211\221\346\214\207Offer25-\345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250.py" +++ /dev/null @@ -1,33 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def mergeTwoLists(self, l1, l2): - """ - :type l1: ListNode - :type l2: ListNode - :rtype: ListNode - """ - p = l1 - list1 = [] - list2 = [] - while p: - list1.append(p.val) - p = p.next - p = l2 - while p: - list2.append(p.val) - p = p.next - - l3 = sorted(list1 + list2) - - dummy = ListNode(-1) - p = dummy - for num in l3: - p.next = ListNode(num) - p = p.next - - return dummy.next \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207Offer27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" "b/\345\211\221\346\214\207Offer27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207Offer27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" deleted file mode 100644 index 71ab723..0000000 --- "a/\345\211\221\346\214\207Offer27.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\345\211\221\346\214\207Offer27-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" +++ /dev/null @@ -1,19 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def mirrorTree(self, root): - """ - :type root: TreeNode - :rtype: TreeNode - """ - if not root: - return root - - root.left, root.right = self.mirrorTree(root.right), self.mirrorTree(root.left) - - return root \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer28.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer28-\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.py" "b/\345\211\221\346\214\207Offer28.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer28-\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.py" deleted file mode 100644 index ec0c7c3..0000000 --- "a/\345\211\221\346\214\207Offer28.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer28-\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.py" +++ /dev/null @@ -1,23 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def isSymmetric(self, root): - """ - :type root: TreeNode - :rtype: bool - """ - if not root: - return True - - def check(root1, root2): - if not root1 and not root2: - return True - if not root1 or not root2: - return False - return root1.val == root2.val and check(root1.left, root2.right) and check(root1.right, root2.left) - return check(root, root) \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer29.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/\345\211\221\346\214\207Offer29-\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.py" "b/\345\211\221\346\214\207Offer29.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/\345\211\221\346\214\207Offer29-\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.py" deleted file mode 100644 index 5c212b0..0000000 --- "a/\345\211\221\346\214\207Offer29.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/\345\211\221\346\214\207Offer29-\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.py" +++ /dev/null @@ -1,45 +0,0 @@ -class Solution(object): - def spiralOrder(self, matrix): - """ - :type matrix: List[List[int]] - :rtype: List[int] - """ - if not matrix or not matrix[0]: - return matrix - m, n = len(matrix), len(matrix[0]) - - x, y = 0, 0 - state = "r" - cnt = 0 - res = [] - visited = set() - while cnt < m * n: - res.append(matrix[x][y]) - visited.add((x, y)) - if state == "r": - if y + 1 < n and (x, y + 1) not in visited: - y += 1 - else: - x += 1 - state = "d" - elif state == "d": - if x + 1 < m and (x + 1, y) not in visited: - x += 1 - else: - y -= 1 - state = "l" - elif state == "l": - if y - 1 >= 0 and (x, y - 1) not in visited: - y -= 1 - else: - x -= 1 - state = "u" - elif state == "u": - if x - 1 >= 0 and (x - 1, y) not in visited: - x -= 1 - else: - y += 1 - state = "r" - cnt += 1 - return res - \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer30.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/\345\211\221\346\214\207Offer30-\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.py" "b/\345\211\221\346\214\207Offer30.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/\345\211\221\346\214\207Offer30-\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.py" deleted file mode 100644 index 787a395..0000000 --- "a/\345\211\221\346\214\207Offer30.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/\345\211\221\346\214\207Offer30-\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.py" +++ /dev/null @@ -1,50 +0,0 @@ -class MinStack(object): - - def __init__(self): - """ - initialize your data structure here. - """ - self.stack = [] - self.min_s = [] - - def push(self, x): - """ - :type x: int - :rtype: None - """ - if not self.stack: - self.stack = [x] - self.min_s = [x] - else: - self.stack.append(x) - if self.min_s[-1] < x: - self.min_s.append(self.min_s[-1]) - else: - self.min_s.append(x) - - def pop(self): - """ - :rtype: None - """ - self.stack.pop() - self.min_s.pop() - - def top(self): - """ - :rtype: int - """ - return self.stack[-1] - - def min(self): - """ - :rtype: int - """ - return self.min_s[-1] - - -# Your MinStack object will be instantiated and called as such: -# obj = MinStack() -# obj.push(x) -# obj.pop() -# param_3 = obj.top() -# param_4 = obj.min() \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer31.\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/\345\211\221\346\214\207Offer31-\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227.py" "b/\345\211\221\346\214\207Offer31.\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/\345\211\221\346\214\207Offer31-\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227.py" deleted file mode 100644 index 4dfe5b1..0000000 --- "a/\345\211\221\346\214\207Offer31.\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/\345\211\221\346\214\207Offer31-\346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227.py" +++ /dev/null @@ -1,19 +0,0 @@ -class Solution(object): - def validateStackSequences(self, pushed, popped): - """ - :type pushed: List[int] - :type popped: List[int] - :rtype: bool - """ - if not pushed and not popped: - return True - if not pushed or not popped: - return False - stack = [] - popped = popped[::-1] - for i, num in enumerate(pushed): - stack.append(num) - while stack and stack[-1] == popped[-1]: - stack.pop() - popped.pop() - return not popped \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer32-I.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer32-I-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.py" "b/\345\211\221\346\214\207Offer32-I.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer32-I-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.py" deleted file mode 100644 index bffbb72..0000000 --- "a/\345\211\221\346\214\207Offer32-I.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/\345\211\221\346\214\207Offer32-I-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221.py" +++ /dev/null @@ -1,28 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def levelOrder(self, root): - """ - :type root: TreeNode - :rtype: List[int] - """ - from collections import deque - if not root: - return [] - - queue = deque([root]) - res = [] - while queue: - for _ in range(len(queue)): - cur = queue.popleft() - res.append(cur.val) - if cur.left: - queue.append(cur.left) - if cur.right: - queue.append(cur.right) - return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer32-II.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221II/\345\211\221\346\214\207Offer32-II-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221II.py" "b/\345\211\221\346\214\207Offer32-II.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221II/\345\211\221\346\214\207Offer32-II-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221II.py" deleted file mode 100644 index 0798c1d..0000000 --- "a/\345\211\221\346\214\207Offer32-II.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221II/\345\211\221\346\214\207Offer32-II-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221II.py" +++ /dev/null @@ -1,30 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def levelOrder(self, root): - """ - :type root: TreeNode - :rtype: List[List[int]] - """ - from collections import deque - if not root: - return [] - - queue = deque([root]) - res = [] - while queue: - tmp = [] - for _ in range(len(queue)): - cur = queue.popleft() - tmp.append(cur.val) - if cur.left: - queue.append(cur.left) - if cur.right: - queue.append(cur.right) - res.append(tmp) - return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer32-III.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III/\345\211\221\346\214\207Offer32-III-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III.py" "b/\345\211\221\346\214\207Offer32-III.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III/\345\211\221\346\214\207Offer32-III-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III.py" deleted file mode 100644 index 1456eae..0000000 --- "a/\345\211\221\346\214\207Offer32-III.\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III/\345\211\221\346\214\207Offer32-III-\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III.py" +++ /dev/null @@ -1,35 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def levelOrder(self, root): - """ - :type root: TreeNode - :rtype: List[List[int]] - """ - from collections import deque - if not root: - return [] - - queue = deque([root]) - res = [] - flag = 1 - while queue: - tmp = [] - for _ in range(len(queue)): - cur = queue.popleft() - tmp.append(cur.val) - if cur.left: - queue.append(cur.left) - if cur.right: - queue.append(cur.right) - if flag: - res.append(tmp) - else: - res.append(tmp[::-1]) - flag = 1 - flag - return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer33.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/\345\211\221\346\214\207Offer33-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227.py" "b/\345\211\221\346\214\207Offer33.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/\345\211\221\346\214\207Offer33-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227.py" deleted file mode 100644 index 0d2fd26..0000000 --- "a/\345\211\221\346\214\207Offer33.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/\345\211\221\346\214\207Offer33-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227.py" +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def verifyPostorder(self, postorder): - """ - :type postorder: List[int] - :rtype: bool - """ - if not postorder or len(postorder) == 1: - return True - - for i in range(len(postorder)): - if postorder[i] > postorder[-1]: - break - if any(postorder[j] < postorder[-1] for j in range(i, len(postorder) - 1)): - return False - - if i == len(postorder): - # no right subtree - left = postorder[:-1] - right = None - else: - left = postorder[:i] - right = postorder[i:-1] - - return self.verifyPostorder(left) and self.verifyPostorder(right) - \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer34.\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/\345\211\221\346\214\207Offer34-\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.py" "b/\345\211\221\346\214\207Offer34.\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/\345\211\221\346\214\207Offer34-\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.py" deleted file mode 100644 index 6f40ee3..0000000 --- "a/\345\211\221\346\214\207Offer34.\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204/\345\211\221\346\214\207Offer34-\344\272\214\345\217\211\346\240\221\344\270\255\345\222\214\344\270\272\346\237\220\344\270\200\345\200\274\347\232\204\350\267\257\345\276\204.py" +++ /dev/null @@ -1,31 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def pathSum(self, root, summ): - """ - :type root: TreeNode - :type sum: int - :rtype: List[List[int]] - """ - self.res = [] - - def dfs(node, path, s): - if not node: - return - - if not node.left and not node.right: - if s + node.val == summ: - path.append(node.val) - self.res.append(path) - return - - dfs(node.left, path + [node.val], s + node.val) - dfs(node.right, path + [node.val], s + node.val) - - dfs(root, [], 0) - return self.res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207Offer35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" "b/\345\211\221\346\214\207Offer35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207Offer35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" deleted file mode 100644 index 9de38d5..0000000 --- "a/\345\211\221\346\214\207Offer35.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\345\211\221\346\214\207Offer35-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" +++ /dev/null @@ -1,30 +0,0 @@ -""" -# Definition for a Node. -class Node: - def __init__(self, x, next=None, random=None): - self.val = int(x) - self.next = next - self.random = random -""" -class Solution(object): - def copyRandomList(self, head): - """ - :type head: Node - :rtype: Node - """ - mapping = {} # key is the old node, val is the new node - - p = head - while p: - mapping[p] = Node(p.val) - p = p.next - - p = head - while p: - if p.next: - mapping[p].next = mapping[p.next] - if p.random: - mapping[p].random = mapping[p.random] - p = p.next - - return mapping[head] if head else head \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer36.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250/\345\211\221\346\214\207Offer36-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250.py" "b/\345\211\221\346\214\207Offer36.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250/\345\211\221\346\214\207Offer36-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250.py" deleted file mode 100644 index 669fb96..0000000 --- "a/\345\211\221\346\214\207Offer36.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250/\345\211\221\346\214\207Offer36-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250.py" +++ /dev/null @@ -1,47 +0,0 @@ -""" -# Definition for a Node. -class Node(object): - def __init__(self, val, left=None, right=None): - self.val = val - self.left = left - self.right = right -""" -class Solution(object): - def treeToDoublyList(self, root): - """ - :type root: Node - :rtype: Node - """ - if not root: - return root - if not root.left and not root.right: - root.left = root - root.right = root - return root - - left = self.treeToDoublyList(root.left) - right = self.treeToDoublyList(root.right) - if root.left and root.right: - left_tail = left.left - right_tail = right.left - - left_tail.right = root - root.left = left_tail - root.right = right - right.left = root - - left.left = right_tail - right_tail.right = left - elif root.left: - left_tail = left.left - left_tail.right = root - root.left = left_tail - left.left = root - root.right = left - elif root.right: - right_tail = right.left - root.right = right - root.left = right_tail - right_tail.right = root - right.left = root - return left if left else root \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer38.\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/\345\211\221\346\214\207Offer38-\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227.py" "b/\345\211\221\346\214\207Offer38.\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/\345\211\221\346\214\207Offer38-\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227.py" deleted file mode 100644 index c61bd6e..0000000 --- "a/\345\211\221\346\214\207Offer38.\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/\345\211\221\346\214\207Offer38-\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def permutation(self, s): - """ - :type s: str - :rtype: List[str] - """ - from itertools import permutations - res = [] - visited = set() - for item in list(permutations(s)): - s = "".join(item) - if s not in visited: - res.append(s) - visited.add(s) - - return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer39.\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer39-\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207Offer39.\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer39-\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227.py" deleted file mode 100644 index b0e5086..0000000 --- "a/\345\211\221\346\214\207Offer39.\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer39-\346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def majorityElement(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - return sorted(nums)[len(nums) // 2] \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer40.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\345\211\221\346\214\207Offer40-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" "b/\345\211\221\346\214\207Offer40.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\345\211\221\346\214\207Offer40-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" deleted file mode 100644 index 50bd4bf..0000000 --- "a/\345\211\221\346\214\207Offer40.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\345\211\221\346\214\207Offer40-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def getLeastNumbers(self, arr, k): - """ - :type arr: List[int] - :type k: int - :rtype: List[int] - """ - from heapq import * - - queue = [] - for num in arr: - if len(queue) < k: - heappush(queue, -num) - else: - if queue and queue[0] < num: - heappush(queue, -num) - heappop(queue) - return [-item for item in queue] \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer42.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/\345\211\221\346\214\207Offer42-\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214.py" "b/\345\211\221\346\214\207Offer42.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/\345\211\221\346\214\207Offer42-\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214.py" deleted file mode 100644 index d4b9334..0000000 --- "a/\345\211\221\346\214\207Offer42.\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/\345\211\221\346\214\207Offer42-\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def maxSubArray(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - prefix = [0 for _ in nums] - - prefix[0] = nums[0] - min_s = min(0, nums[0]) - res = nums[0] - for i in range(1, len(nums)): - prefix[i] = prefix[i - 1] + nums[i] - res = max(prefix[i] - min_s, res) - min_s = min(min_s, prefix[i]) - return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer47.\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/\345\211\221\346\214\207Offer47-\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274.py" "b/\345\211\221\346\214\207Offer47.\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/\345\211\221\346\214\207Offer47-\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274.py" deleted file mode 100644 index 86bf71d..0000000 --- "a/\345\211\221\346\214\207Offer47.\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/\345\211\221\346\214\207Offer47-\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def maxValue(self, grid): - """ - :type grid: List[List[int]] - :rtype: int - """ - if not grid or not grid[0]: - return 0 - m, n = len(grid), len(grid[0]) - - for i in range(1, n): - grid[0][i] += grid[0][i - 1] - - for i in range(1, m): - grid[i][0] += grid[i - 1][0] - - for i in range(1, m): - for j in range(1, n): - grid[i][j] += max(grid[i - 1][j], grid[i][j - 1]) - - return grid[-1][-1] - \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer50.\347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/\345\211\221\346\214\207Offer50-\347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246.py" "b/\345\211\221\346\214\207Offer50.\347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/\345\211\221\346\214\207Offer50-\347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246.py" deleted file mode 100644 index 817227e..0000000 --- "a/\345\211\221\346\214\207Offer50.\347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/\345\211\221\346\214\207Offer50-\347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246.py" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution(object): - def firstUniqChar(self, s): - """ - :type s: str - :rtype: str - """ - from collections import Counter - - dic = Counter(s) - - for ch in s: - if dic[ch] == 1: - return ch - return " " \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207Offer52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" "b/\345\211\221\346\214\207Offer52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207Offer52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" deleted file mode 100644 index 120f097..0000000 --- "a/\345\211\221\346\214\207Offer52.\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/\345\211\221\346\214\207Offer52-\344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271.py" +++ /dev/null @@ -1,39 +0,0 @@ -# Definition for singly-linked list. -# class ListNode(object): -# def __init__(self, x): -# self.val = x -# self.next = None - -class Solution(object): - def getIntersectionNode(self, headA, headB): - """ - :type head1, head1: ListNode - :rtype: ListNode - """ - la, lb = 0, 0 - - p = headA - while p: - la += 1 - p = p.next - - p = headB - while p: - lb += 1 - p = p.next - - if la < lb: - headA, headB = headB, headA - la, lb = lb, la - - cnt = la - lb - p = headA - while cnt: - cnt -= 1 - p = p.next - - - while p != headB: - p = p.next - headB = headB.next - return p \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer53-I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207Offer53-I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" "b/\345\211\221\346\214\207Offer53-I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207Offer53-I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" deleted file mode 100644 index 8b4c0c6..0000000 --- "a/\345\211\221\346\214\207Offer53-I.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I/\345\211\221\346\214\207Offer53-I-\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227I.py" +++ /dev/null @@ -1,8 +0,0 @@ -class Solution(object): - def search(self, nums, target): - """ - :type nums: List[int] - :type target: int - :rtype: int - """ - return nums.count(target) \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer53-II.0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer53-II-0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207Offer53-II.0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer53-II-0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227.py" deleted file mode 100644 index f15c45d..0000000 --- "a/\345\211\221\346\214\207Offer53-II.0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer53-II-0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227.py" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def missingNumber(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - for i, num in enumerate(nums): - if num == len(nums): - continue - if num != i: - nums[num], nums[i] = nums[i], nums[num] - - for i in range(len(nums)): - if nums[i] != i: - return i - return len(nums) \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207Offer54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" "b/\345\211\221\346\214\207Offer54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207Offer54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" deleted file mode 100644 index 6d799dd..0000000 --- "a/\345\211\221\346\214\207Offer54.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/\345\211\221\346\214\207Offer54-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271.py" +++ /dev/null @@ -1,36 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def kthLargest(self, root, k): - """ - :type root: TreeNode - :type k: int - :rtype: int - """ - def inorder(node): - if not node: - return [] - return inorder(node.left) + [node.val] + inorder(node.right) - - return inorder(root)[-k] -# self.res = None -# def dfs(node, left): -# if not node: -# return None - -# if not self.res: -# dfs(node.right, left) -# if node.right: -# left -= 1 -# if left == 1: -# self.res = node.val -# return -# dfs(node.left, left - 1) - -# dfs(root, k) -# return self.res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer55-I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207Offer55-I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" "b/\345\211\221\346\214\207Offer55-I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207Offer55-I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" deleted file mode 100644 index e3b62e7..0000000 --- "a/\345\211\221\346\214\207Offer55-I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\345\211\221\346\214\207Offer55-I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" +++ /dev/null @@ -1,16 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def maxDepth(self, root): - """ - :type root: TreeNode - :rtype: int - """ - if not root: - return 0 - return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer57-II.\345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/\345\211\221\346\214\207Offer57-II-\345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227.py" "b/\345\211\221\346\214\207Offer57-II.\345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/\345\211\221\346\214\207Offer57-II-\345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227.py" deleted file mode 100644 index cc91968..0000000 --- "a/\345\211\221\346\214\207Offer57-II.\345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/\345\211\221\346\214\207Offer57-II-\345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227.py" +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def findContinuousSequence(self, target): - """ - :type target: int - :rtype: List[List[int]] - """ - from collections import deque - window = deque() - res = [] - s = 0 - i = 1 - while i < target // 2 + 3: - if s == target: - res.append(list(window)) - if s <= target: - window.append(i) - s += i - i += 1 - elif s > target: - s -= window.popleft() - - return res diff --git "a/\345\211\221\346\214\207Offer57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207Offer57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207Offer57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207Offer57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" deleted file mode 100644 index b1a4b6d..0000000 --- "a/\345\211\221\346\214\207Offer57.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\345\211\221\346\214\207Offer57-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def twoSum(self, nums, target): - """ - :type nums: List[int] - :type target: int - :rtype: List[int] - """ - left, right = 0, len(nums) - 1 - while left < right: - s = nums[left] + nums[right] - - if s == target: - return [nums[left], nums[right]] - elif s > target: - right -= 1 - else: - left += 1 \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207Offer58-II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" "b/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207Offer58-II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" deleted file mode 100644 index 8e4cadf..0000000 --- "a/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\345\211\221\346\214\207Offer58-II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" +++ /dev/null @@ -1,8 +0,0 @@ -class Solution(object): - def reverseLeftWords(self, s, n): - """ - :type s: str - :type n: int - :rtype: str - """ - return s[n:] + s[:n] \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer59-II.\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207Offer59-II-\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/\345\211\221\346\214\207Offer59-II.\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207Offer59-II-\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274.py" deleted file mode 100644 index 8954ae7..0000000 --- "a/\345\211\221\346\214\207Offer59-II.\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/\345\211\221\346\214\207Offer59-II-\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274.py" +++ /dev/null @@ -1,35 +0,0 @@ -class MaxQueue(object): - - def __init__(self): - from collections import deque - self.queue = deque([]) - def max_value(self): - """ - :rtype: int - """ - if self.queue: - return max(self.queue) - return -1 - - def push_back(self, value): - """ - :type value: int - :rtype: None - """ - self.queue.append(value) - - - def pop_front(self): - """ - :rtype: int - """ - if not self.queue: - return -1 - return self.queue.popleft() - - -# Your MaxQueue object will be instantiated and called as such: -# obj = MaxQueue() -# param_1 = obj.max_value() -# obj.push_back(value) -# param_3 = obj.pop_front() \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer62.\345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer62-\345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227.py" "b/\345\211\221\346\214\207Offer62.\345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer62-\345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227.py" deleted file mode 100644 index 9690d6d..0000000 --- "a/\345\211\221\346\214\207Offer62.\345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/\345\211\221\346\214\207Offer62-\345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227.py" +++ /dev/null @@ -1,10 +0,0 @@ -class Solution(object): - def lastRemaining(self, n, m): - """ - :type n: int - :type m: int - :rtype: int - """ - if n == 1: - return 0 - return (self.lastRemaining(n - 1, m) + m) % n \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer63.\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/\345\211\221\346\214\207Offer63-\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246.py" "b/\345\211\221\346\214\207Offer63.\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/\345\211\221\346\214\207Offer63-\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246.py" deleted file mode 100644 index 325ada7..0000000 --- "a/\345\211\221\346\214\207Offer63.\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/\345\211\221\346\214\207Offer63-\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246.py" +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def maxProfit(self, prices): - """ - :type prices: List[int] - :rtype: int - """ - res = 0 - pre_min = prices[0] if prices else 0 - for price in prices: - res = max(res, price - pre_min) - pre_min = min(pre_min, price) - return res \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207Offer64-\346\261\2021+2+\342\200\246+n.py" "b/\345\211\221\346\214\207Offer64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207Offer64-\346\261\2021+2+\342\200\246+n.py" deleted file mode 100644 index c8160ca..0000000 --- "a/\345\211\221\346\214\207Offer64.\346\261\2021+2+\342\200\246+n/\345\211\221\346\214\207Offer64-\346\261\2021+2+\342\200\246+n.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def sumNums(self, n): - """ - :type n: int - :rtype: int - """ - return sum(range(1, n + 1)) \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer65.\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/\345\211\221\346\214\207Offer65-\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225.py" "b/\345\211\221\346\214\207Offer65.\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/\345\211\221\346\214\207Offer65-\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225.py" deleted file mode 100644 index 02652bd..0000000 --- "a/\345\211\221\346\214\207Offer65.\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/\345\211\221\346\214\207Offer65-\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225.py" +++ /dev/null @@ -1,8 +0,0 @@ -class Solution(object): - def add(self, a, b): - """ - :type a: int - :type b: int - :rtype: int - """ - return sum([a, b]) \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer66.\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/\345\211\221\346\214\207Offer66-\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204.py" "b/\345\211\221\346\214\207Offer66.\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/\345\211\221\346\214\207Offer66-\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204.py" deleted file mode 100644 index 5c9dd47..0000000 --- "a/\345\211\221\346\214\207Offer66.\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/\345\211\221\346\214\207Offer66-\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204.py" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution(object): - def constructArr(self, a): - """ - :type a: List[int] - :rtype: List[int] - """ - left = [1 for _ in a] - right = [1 for _ in a] - for i in range(1, len(a)): - left[i] = left[i - 1] * a[i - 1] - - for i in range(len(a) - 2, -1, -1): - right[i] = right[i + 1] * a[i + 1] - - return [left[i] * right[i] for i in range(len(a))] \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer68-I.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/\345\211\221\346\214\207Offer68-I-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.py" "b/\345\211\221\346\214\207Offer68-I.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/\345\211\221\346\214\207Offer68-I-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.py" deleted file mode 100644 index a00d561..0000000 --- "a/\345\211\221\346\214\207Offer68-I.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/\345\211\221\346\214\207Offer68-I-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.py" +++ /dev/null @@ -1,20 +0,0 @@ -# Definition for a binary tree node. -# class TreeNode(object): -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - -class Solution(object): - def lowestCommonAncestor(self, root, p, q): - """ - :type root: TreeNode - :type p: TreeNode - :type q: TreeNode - :rtype: TreeNode - """ - if min(p.val, q.val) <= root.val <= max(p.val, q.val): - return root - if max(p.val, q.val) < root.val: - return self.lowestCommonAncestor(root.left, p, q) - return self.lowestCommonAncestor(root.right, p, q) \ No newline at end of file diff --git "a/\345\211\221\346\214\207Offer68-II.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/\345\211\221\346\214\207Offer68-II-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.py" "b/\345\211\221\346\214\207Offer68-II.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/\345\211\221\346\214\207Offer68-II-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.py" index 09cbf64..7971e98 100644 --- "a/\345\211\221\346\214\207Offer68-II.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/\345\211\221\346\214\207Offer68-II-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.py" +++ "b/\345\211\221\346\214\207Offer68-II.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/\345\211\221\346\214\207Offer68-II-\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.py" @@ -1,23 +1,32 @@ # Definition for a binary tree node. -# class TreeNode(object): +# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None -class Solution(object): - def lowestCommonAncestor(self, root, p, q): - """ - :type root: TreeNode - :type p: TreeNode - :type q: TreeNode - :rtype: TreeNode - """ - if root in [None, p, q]: - return root - left = self.lowestCommonAncestor(root.left, p, q) - right = self.lowestCommonAncestor(root.right, p, q) +class Solution: + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + child2par = {root: None} + stack = [root] - if left and right: - return root - return left or right \ No newline at end of file + while stack: + cur = stack.pop() + if cur.left: + stack.append(cur.left) + child2par[cur.left] = cur + if cur.right: + stack.append(cur.right) + child2par[cur.right] = cur + + p_ancestors = set() + while p in child2par: + p_ancestors.add(p) + p = child2par[p] + + res = root + while q in child2par: + if q in p_ancestors: + return q + q = child2par[q] + return res diff --git "a/\351\235\242\350\257\225\351\242\230 01.01.\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200/\351\235\242\350\257\225\351\242\230 01.01-\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200.py" "b/\351\235\242\350\257\225\351\242\230 01.01.\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200/\351\235\242\350\257\225\351\242\230 01.01-\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200.py" new file mode 100644 index 0000000..ea48c15 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 01.01.\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200/\351\235\242\350\257\225\351\242\230 01.01-\345\210\244\345\256\232\345\255\227\347\254\246\346\230\257\345\220\246\345\224\257\344\270\200.py" @@ -0,0 +1,3 @@ +class Solution: + def isUnique(self, astr: str) -> bool: + return len(set(astr)) == len(astr) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 01.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\230 01.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" "b/\351\235\242\350\257\225\351\242\230 01.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\230 01.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" new file mode 100644 index 0000000..fa4e693 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 01.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\230 01.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" @@ -0,0 +1,4 @@ +class Solution: + def CheckPermutation(self, s1: str, s2: str) -> bool: + from collections import Counter + return Counter(s1) == Counter(s2) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 01.03.URL\345\214\226/\351\235\242\350\257\225\351\242\230 01.03-URL\345\214\226.py" "b/\351\235\242\350\257\225\351\242\230 01.03.URL\345\214\226/\351\235\242\350\257\225\351\242\230 01.03-URL\345\214\226.py" new file mode 100644 index 0000000..54f13bd --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 01.03.URL\345\214\226/\351\235\242\350\257\225\351\242\230 01.03-URL\345\214\226.py" @@ -0,0 +1,3 @@ +class Solution: + def replaceSpaces(self, S: str, length: int) -> str: + return S[:length].replace(" ", "%20") \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 01.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\230 01.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" "b/\351\235\242\350\257\225\351\242\230 01.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\230 01.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" new file mode 100644 index 0000000..8b2a31f --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 01.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\230 01.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" @@ -0,0 +1,10 @@ +class Solution: + def canPermutePalindrome(self, s: str) -> bool: + from collections import Counter + odd = False + for char, freq in Counter(s).items(): + if freq % 2: + if odd: + return False + odd = True + return True \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 01.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\230 01.06-\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251.py" "b/\351\235\242\350\257\225\351\242\230 01.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\230 01.06-\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251.py" new file mode 100644 index 0000000..a08b996 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 01.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\230 01.06-\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251.py" @@ -0,0 +1,14 @@ +class Solution: + def compressString(self, S: str) -> str: + res = "" + index = 0 + while index < len(S): + count = 1 + char = S[index] + while index + 1 < len(S) and S[index] == S[index + 1]: + index += 1 + count += 1 + res += char + str(count) + index += 1 + + return res if len(res) < len(S) else S \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 01.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" "b/\351\235\242\350\257\225\351\242\230 01.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" new file mode 100644 index 0000000..6ff3d39 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 01.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" @@ -0,0 +1,14 @@ +class Solution: + def rotate(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + n = len(matrix) + for i in range(n): + for j in range(i + 1, n): + matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] + + for i in range(n): + row = matrix[i] + for j in range(n // 2): + row[j], row[-(j + 1)] = row[-(j + 1)], row[j] \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 01.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.08-\351\233\266\347\237\251\351\230\265.py" "b/\351\235\242\350\257\225\351\242\230 01.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.08-\351\233\266\347\237\251\351\230\265.py" new file mode 100644 index 0000000..1834034 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 01.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\230 01.08-\351\233\266\347\237\251\351\230\265.py" @@ -0,0 +1,23 @@ +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + + row_zero, col_zero = set(), set() + + for i in range(len(matrix)): + for j in range(len(matrix[0])): + if not matrix[i][j]: + row_zero.add(i) + col_zero.add(j) + + for row_index in row_zero: + matrix[row_index] = [0] * len(matrix[0]) + + for col_index in col_zero: + for i in range(len(matrix)): + matrix[i][col_index] = 0 + + + \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23001.09.\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254/\351\235\242\350\257\225\351\242\23001.09-\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254.py" "b/\351\235\242\350\257\225\351\242\230 01.09.\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254/\351\235\242\350\257\225\351\242\230 01.09-\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254 2.py" similarity index 100% rename from "\351\235\242\350\257\225\351\242\23001.09.\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254/\351\235\242\350\257\225\351\242\23001.09-\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254.py" rename to "\351\235\242\350\257\225\351\242\230 01.09.\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254/\351\235\242\350\257\225\351\242\230 01.09-\345\255\227\347\254\246\344\270\262\350\275\256\350\275\254 2.py" diff --git "a/\351\235\242\350\257\225\351\242\23002.01.\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\23002.01-\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271.py" "b/\351\235\242\350\257\225\351\242\230 02.01.\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.01-\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271 2.py" similarity index 100% rename from "\351\235\242\350\257\225\351\242\23002.01.\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\23002.01-\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271.py" rename to "\351\235\242\350\257\225\351\242\230 02.01.\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.01-\347\247\273\351\231\244\351\207\215\345\244\215\350\212\202\347\202\271 2.py" diff --git "a/\351\235\242\350\257\225\351\242\23002.02.\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\23002.02-\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" "b/\351\235\242\350\257\225\351\242\230 02.02.\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.02-\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271 2.py" similarity index 100% rename from "\351\235\242\350\257\225\351\242\23002.02.\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\23002.02-\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271.py" rename to "\351\235\242\350\257\225\351\242\230 02.02.\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.02-\350\277\224\345\233\236\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271 2.py" diff --git "a/\351\235\242\350\257\225\351\242\23002.03.\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\23002.03-\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271.py" "b/\351\235\242\350\257\225\351\242\230 02.03.\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.03-\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271 2.py" similarity index 100% rename from "\351\235\242\350\257\225\351\242\23002.03.\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\23002.03-\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271.py" rename to "\351\235\242\350\257\225\351\242\230 02.03.\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271/\351\235\242\350\257\225\351\242\230 02.03-\345\210\240\351\231\244\344\270\255\351\227\264\350\212\202\347\202\271 2.py" diff --git "a/\351\235\242\350\257\225\351\242\230 02.04.\345\210\206\345\211\262\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.04-\345\210\206\345\211\262\351\223\276\350\241\250.py" "b/\351\235\242\350\257\225\351\242\230 02.04.\345\210\206\345\211\262\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.04-\345\210\206\345\211\262\351\223\276\350\241\250.py" new file mode 100644 index 0000000..068ad14 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 02.04.\345\210\206\345\211\262\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.04-\345\210\206\345\211\262\351\223\276\350\241\250.py" @@ -0,0 +1,24 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def partition(self, head: ListNode, x: int) -> ListNode: + dummy1, dummy2 = ListNode(-1), ListNode(-2) + + smaller, larger = dummy1, dummy2 + p = head + while p: + if p.val < x: + smaller.next = ListNode(p.val) + smaller = smaller.next + else: + larger.next = ListNode(p.val) + larger = larger.next + + p = p.next + + smaller.next = dummy2.next + return dummy1.next \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 02.05.\351\223\276\350\241\250\346\261\202\345\222\214/\351\235\242\350\257\225\351\242\230 02.05-\351\223\276\350\241\250\346\261\202\345\222\214.py" "b/\351\235\242\350\257\225\351\242\230 02.05.\351\223\276\350\241\250\346\261\202\345\222\214/\351\235\242\350\257\225\351\242\230 02.05-\351\223\276\350\241\250\346\261\202\345\222\214.py" new file mode 100644 index 0000000..70f930c --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 02.05.\351\223\276\350\241\250\346\261\202\345\222\214/\351\235\242\350\257\225\351\242\230 02.05-\351\223\276\350\241\250\346\261\202\345\222\214.py" @@ -0,0 +1,59 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: + if not l1 and not l2: + return l1 + if not l1: + return l2 + if not l2: + return l1 + + p1, p2 = l1, l2 + carry = 0 + dummy = ListNode(-1) + p = dummy + while l1 and l2: + s = l1.val + l2.val + carry + l1 = l1.next + l2 = l2.next + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + + p.next = ListNode(s) + p = p.next + + while l1: + s = l1.val + carry + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + l1 = l1.next + p.next = ListNode(s) + p = p.next + + while l2: + s = l2.val + carry + if s > 9: + s -= 10 + carry = 1 + else: + carry = 0 + l2 = l2.next + p.next = ListNode(s) + p = p.next + + if carry: + p.next = ListNode(1) + return dummy.next + + \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 02.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" "b/\351\235\242\350\257\225\351\242\230 02.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" new file mode 100644 index 0000000..654d1c5 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 02.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 02.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" @@ -0,0 +1,13 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def isPalindrome(self, head: ListNode) -> bool: + p = head + l = [] + while p: + l.append(p.val) + p = p.next + return l == l[::-1] \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 02.07.\351\223\276\350\241\250\347\233\270\344\272\244/\351\235\242\350\257\225\351\242\230 02.07-\351\223\276\350\241\250\347\233\270\344\272\244.py" "b/\351\235\242\350\257\225\351\242\230 02.07.\351\223\276\350\241\250\347\233\270\344\272\244/\351\235\242\350\257\225\351\242\230 02.07-\351\223\276\350\241\250\347\233\270\344\272\244.py" new file mode 100644 index 0000000..f174760 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 02.07.\351\223\276\350\241\250\347\233\270\344\272\244/\351\235\242\350\257\225\351\242\230 02.07-\351\223\276\350\241\250\347\233\270\344\272\244.py" @@ -0,0 +1,36 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + pa, pb = headA, headB + if not headA or not headB: + return None + + la, lb = 0, 0 + while pa: + la += 1 + pa = pa.next + while pb: + lb += 1 + pb = pb.next + + if la < lb: + headA, headB = headB, headA + la, lb = lb, la + + diff = la - lb + pa = headA + while diff: + pa = pa.next + diff -= 1 + + pb = headB + while pa and pb and pa != pb: + pa = pa.next + pb = pb.next + + return pa if pa == pb else None \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 02.08.\347\216\257\350\267\257\346\243\200\346\265\213/\351\235\242\350\257\225\351\242\230 02.08-\347\216\257\350\267\257\346\243\200\346\265\213.py" "b/\351\235\242\350\257\225\351\242\230 02.08.\347\216\257\350\267\257\346\243\200\346\265\213/\351\235\242\350\257\225\351\242\230 02.08-\347\216\257\350\267\257\346\243\200\346\265\213.py" new file mode 100644 index 0000000..c6abf6f --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 02.08.\347\216\257\350\267\257\346\243\200\346\265\213/\351\235\242\350\257\225\351\242\230 02.08-\347\216\257\350\267\257\346\243\200\346\265\213.py" @@ -0,0 +1,28 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def detectCycle(self, head: ListNode) -> ListNode: + if not head or not head.next: + return None + slow, fast = head, head + + while fast and fast.next: + slow = slow.next + fast = fast.next.next + + if slow == fast: + break + + if slow != fast: + return None + + fast = head + while fast != slow: + slow = slow.next + fast = fast.next + + return fast \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 03.02.\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274/\351\235\242\350\257\225\351\242\230 03.02-\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274.py" "b/\351\235\242\350\257\225\351\242\230 03.02.\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274/\351\235\242\350\257\225\351\242\230 03.02-\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274.py" new file mode 100644 index 0000000..b26eec1 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 03.02.\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274/\351\235\242\350\257\225\351\242\230 03.02-\346\240\210\347\232\204\346\234\200\345\260\217\345\200\274.py" @@ -0,0 +1,35 @@ +class MinStack: + + def __init__(self): + """ + initialize your data structure here. + """ + self.stack = [] + self.min_stack = [] + + def push(self, x: int) -> None: + self.stack.append(x) + if self.min_stack: + self.min_stack.append(min(self.min_stack[-1], x)) + else: + self.min_stack.append(x) + + def pop(self) -> None: + self.stack.pop() + self.min_stack.pop() + + def top(self) -> int: + return self.stack[-1] + + + def getMin(self) -> int: + return self.min_stack[-1] + + + +# Your MinStack object will be instantiated and called as such: +# obj = MinStack() +# obj.push(x) +# obj.pop() +# param_3 = obj.top() +# param_4 = obj.getMin() \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 04.01.\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257/\351\235\242\350\257\225\351\242\230 04.01-\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257.py" "b/\351\235\242\350\257\225\351\242\230 04.01.\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257/\351\235\242\350\257\225\351\242\230 04.01-\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257.py" new file mode 100644 index 0000000..5443d3f --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 04.01.\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257/\351\235\242\350\257\225\351\242\230 04.01-\350\212\202\347\202\271\351\227\264\351\200\232\350\267\257.py" @@ -0,0 +1,27 @@ +from collections import * +class Solution: + def findWhetherExistsPath(self, n: int, graph: List[List[int]], start: int, target: int) -> bool: + src2des = defaultdict(list) + + for pair in graph: + src, des = pair[0], pair[1] + src2des[src].append(des) + + # BFS + queue = deque([start]) + visited = {start} + while queue: + cur = queue.popleft() + if cur == target: + return True + + for node in src2des[cur]: + if node not in visited: + queue.append(node) + visited.add(node) + + return False + + + + diff --git "a/\351\235\242\350\257\225\351\242\230 04.02.\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221/\351\235\242\350\257\225\351\242\230 04.02-\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221.py" "b/\351\235\242\350\257\225\351\242\230 04.02.\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221/\351\235\242\350\257\225\351\242\230 04.02-\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221.py" new file mode 100644 index 0000000..cca5452 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 04.02.\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221/\351\235\242\350\257\225\351\242\230 04.02-\346\234\200\345\260\217\351\253\230\345\272\246\346\240\221.py" @@ -0,0 +1,14 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def sortedArrayToBST(self, nums: List[int]) -> TreeNode: + if not nums: + return None + mid = len(nums) // 2 + root = TreeNode(nums[mid], self.sortedArrayToBST(nums[:mid]), self.sortedArrayToBST(nums[mid + 1:])) + return root \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 04.03.\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 04.03-\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250.py" "b/\351\235\242\350\257\225\351\242\230 04.03.\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 04.03-\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250.py" new file mode 100644 index 0000000..8bb57fa --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 04.03.\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\230 04.03-\347\211\271\345\256\232\346\267\261\345\272\246\350\212\202\347\202\271\351\223\276\350\241\250.py" @@ -0,0 +1,32 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def listOfDepth(self, tree: TreeNode) -> List[ListNode]: + queue = [tree] + res = [] + + while queue: + next_queue = [] + cur_level = ListNode(-1) + p = cur_level + for node in queue: + if node: + next_queue.extend([node.left, node.right]) + p.next = ListNode(node.val) + p = p.next + + if cur_level != p: + res.append(cur_level.next) + queue = next_queue + return res \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 04.04.\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247/\351\235\242\350\257\225\351\242\230 04.04-\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247.py" "b/\351\235\242\350\257\225\351\242\230 04.04.\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247/\351\235\242\350\257\225\351\242\230 04.04-\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247.py" new file mode 100644 index 0000000..e357a5e --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 04.04.\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247/\351\235\242\350\257\225\351\242\230 04.04-\346\243\200\346\237\245\345\271\263\350\241\241\346\200\247.py" @@ -0,0 +1,20 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def isBalanced(self, root: TreeNode) -> bool: + if not root or (not root.left and not root.right): + return True + + def getHeight(node): + if not node: + return 0 + return 1 + max(getHeight(node.left), getHeight(node.right)) + + if abs(getHeight(root.left) - getHeight(root.right)) > 1: + return False + return self.isBalanced(root.left) and self.isBalanced(root.right) diff --git "a/\351\235\242\350\257\225\351\242\230 04.05.\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\351\235\242\350\257\225\351\242\230 04.05-\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/\351\235\242\350\257\225\351\242\230 04.05.\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\351\235\242\350\257\225\351\242\230 04.05-\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..f7917a0 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 04.05.\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/\351\235\242\350\257\225\351\242\230 04.05-\345\220\210\346\263\225\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -0,0 +1,16 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + l = inorder(root) + return l == sorted(l) and len(l) == len(set(l)) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 04.06.\345\220\216\347\273\247\350\200\205/\351\235\242\350\257\225\351\242\230 04.06-\345\220\216\347\273\247\350\200\205.py" "b/\351\235\242\350\257\225\351\242\230 04.06.\345\220\216\347\273\247\350\200\205/\351\235\242\350\257\225\351\242\230 04.06-\345\220\216\347\273\247\350\200\205.py" new file mode 100644 index 0000000..b395f1d --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 04.06.\345\220\216\347\273\247\350\200\205/\351\235\242\350\257\225\351\242\230 04.06-\345\220\216\347\273\247\350\200\205.py" @@ -0,0 +1,22 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> TreeNode: + self.res = None + def inorder(node): + if not node: + return + + inorder(node.left) + if not self.res and node.val > p.val: + self.res = node + return + inorder(node.right) + + inorder(root) + return self.res \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 08.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\230 08.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" "b/\351\235\242\350\257\225\351\242\230 08.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\230 08.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" new file mode 100644 index 0000000..92c38d7 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 08.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\230 08.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" @@ -0,0 +1,8 @@ +class Solution: + def waysToStep(self, n: int) -> int: + dp = [0] * (max(10, n + 1)) + MOD = 10 ** 9 + 7 + dp[1], dp[2], dp[3] = 1, 2, 4 + for i in range(4, n + 1): + dp[i] = (dp[i - 1] + dp[i - 2] + dp[i - 3]) % MOD + return dp[n] \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 08.02.\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272/\351\235\242\350\257\225\351\242\230 08.02-\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272.py" "b/\351\235\242\350\257\225\351\242\230 08.02.\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272/\351\235\242\350\257\225\351\242\230 08.02-\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272.py" new file mode 100644 index 0000000..00aeb6f --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 08.02.\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272/\351\235\242\350\257\225\351\242\230 08.02-\350\277\267\350\267\257\347\232\204\346\234\272\345\231\250\344\272\272.py" @@ -0,0 +1,30 @@ +from collections import deque +class Solution: + def pathWithObstacles(self, obstacleGrid: List[List[int]]) -> List[List[int]]: + if not obstacleGrid or not obstacleGrid[0] or obstacleGrid[0][0] == 1: + return [] + m, n = len(obstacleGrid), len(obstacleGrid[0]) + if obstacleGrid[m -1][n - 1] == 1: + return [] + + self.res = [] + # dfs + visited = set([(0, 0)]) + def dfs(i, j, path): + if not 0 <= i < m or not 0 <= j < n: + return + path.append([i, j]) + if i == m - 1 and j == n - 1: + self.res = path[:] + return + if not self.res: + if i + 1 < m and obstacleGrid[i + 1][j] != 1 and (i + 1, j) not in visited: + visited.add((i + 1, j)) + dfs(i + 1, j, path[:]) + if j + 1 < n and obstacleGrid[i][j + 1] != 1 and (i, j + 1) not in visited: + visited.add((i, j + 1)) + dfs(i, j + 1, path[:]) + dfs(0, 0, []) + return self.res + + \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23008.03.\351\255\224\346\234\257\347\264\242\345\274\225/\351\235\242\350\257\225\351\242\23008.03-\351\255\224\346\234\257\347\264\242\345\274\225.py" "b/\351\235\242\350\257\225\351\242\230 08.03.\351\255\224\346\234\257\347\264\242\345\274\225/\351\235\242\350\257\225\351\242\230 08.03-\351\255\224\346\234\257\347\264\242\345\274\225 2.py" similarity index 100% rename from "\351\235\242\350\257\225\351\242\23008.03.\351\255\224\346\234\257\347\264\242\345\274\225/\351\235\242\350\257\225\351\242\23008.03-\351\255\224\346\234\257\347\264\242\345\274\225.py" rename to "\351\235\242\350\257\225\351\242\230 08.03.\351\255\224\346\234\257\347\264\242\345\274\225/\351\235\242\350\257\225\351\242\230 08.03-\351\255\224\346\234\257\347\264\242\345\274\225 2.py" diff --git "a/\351\235\242\350\257\225\351\242\230 08.05.\351\200\222\345\275\222\344\271\230\346\263\225/\351\235\242\350\257\225\351\242\230 08.05-\351\200\222\345\275\222\344\271\230\346\263\225.py" "b/\351\235\242\350\257\225\351\242\230 08.05.\351\200\222\345\275\222\344\271\230\346\263\225/\351\235\242\350\257\225\351\242\230 08.05-\351\200\222\345\275\222\344\271\230\346\263\225.py" new file mode 100644 index 0000000..2acc5a1 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 08.05.\351\200\222\345\275\222\344\271\230\346\263\225/\351\235\242\350\257\225\351\242\230 08.05-\351\200\222\345\275\222\344\271\230\346\263\225.py" @@ -0,0 +1,7 @@ +class Solution: + def multiply(self, A: int, B: int) -> int: + if B == 1: + return A + if B == 0: + return 0 + return A + self.multiply(A, B - 1) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 10.02.\345\217\230\344\275\215\350\257\215\347\273\204/\351\235\242\350\257\225\351\242\230 10.02-\345\217\230\344\275\215\350\257\215\347\273\204.py" "b/\351\235\242\350\257\225\351\242\230 10.02.\345\217\230\344\275\215\350\257\215\347\273\204/\351\235\242\350\257\225\351\242\230 10.02-\345\217\230\344\275\215\350\257\215\347\273\204.py" new file mode 100644 index 0000000..3488b03 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 10.02.\345\217\230\344\275\215\350\257\215\347\273\204/\351\235\242\350\257\225\351\242\230 10.02-\345\217\230\344\275\215\350\257\215\347\273\204.py" @@ -0,0 +1,9 @@ +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + from collections import defaultdict + + d = defaultdict(list) + for word in strs: + d["".join(sorted(word))].append(word) + + return [val for key, val in d.items()] \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 16.01.\344\272\244\346\215\242\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\230 16.01-\344\272\244\346\215\242\346\225\260\345\255\227.py" "b/\351\235\242\350\257\225\351\242\230 16.01.\344\272\244\346\215\242\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\230 16.01-\344\272\244\346\215\242\346\225\260\345\255\227.py" new file mode 100644 index 0000000..7fb10f6 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 16.01.\344\272\244\346\215\242\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\230 16.01-\344\272\244\346\215\242\346\225\260\345\255\227.py" @@ -0,0 +1,3 @@ +class Solution: + def swapNumbers(self, numbers: List[int]) -> List[int]: + return [numbers[1], numbers[0]] \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 16.02.\345\215\225\350\257\215\351\242\221\347\216\207/\351\235\242\350\257\225\351\242\230 16.02-\345\215\225\350\257\215\351\242\221\347\216\207.py" "b/\351\235\242\350\257\225\351\242\230 16.02.\345\215\225\350\257\215\351\242\221\347\216\207/\351\235\242\350\257\225\351\242\230 16.02-\345\215\225\350\257\215\351\242\221\347\216\207.py" new file mode 100644 index 0000000..5c7be33 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 16.02.\345\215\225\350\257\215\351\242\221\347\216\207/\351\235\242\350\257\225\351\242\230 16.02-\345\215\225\350\257\215\351\242\221\347\216\207.py" @@ -0,0 +1,14 @@ +from collections import Counter +class WordsFrequency: + + def __init__(self, book: List[str]): + self.word2freq = Counter(book) + + def get(self, word: str) -> int: + return self.word2freq[word] + + + +# Your WordsFrequency object will be instantiated and called as such: +# obj = WordsFrequency(book) +# param_1 = obj.get(word) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\230 16.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\230 16.07-\346\234\200\345\244\247\346\225\260\345\200\274 2.py" "b/\351\235\242\350\257\225\351\242\230 16.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\230 16.07-\346\234\200\345\244\247\346\225\260\345\200\274 2.py" new file mode 100644 index 0000000..8f7148c --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 16.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\230 16.07-\346\234\200\345\244\247\346\225\260\345\200\274 2.py" @@ -0,0 +1,8 @@ +class Solution(object): + def maximum(self, a, b): + """ + :type a: int + :type b: int + :rtype: int + """ + return max(a, b) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23001.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\23001.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" "b/\351\235\242\350\257\225\351\242\23001.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\23001.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" deleted file mode 100644 index 38a15c6..0000000 --- "a/\351\235\242\350\257\225\351\242\23001.02.\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222/\351\235\242\350\257\225\351\242\23001.02-\345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222.py" +++ /dev/null @@ -1,9 +0,0 @@ -class Solution(object): - def CheckPermutation(self, s1, s2): - """ - :type s1: str - :type s2: str - :rtype: bool - """ - from collections import Counter - return Counter(s1) == Counter(s2) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23001.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\23001.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" "b/\351\235\242\350\257\225\351\242\23001.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\23001.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" deleted file mode 100644 index 5e030dd..0000000 --- "a/\351\235\242\350\257\225\351\242\23001.04.\345\233\236\346\226\207\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\23001.04-\345\233\236\346\226\207\346\216\222\345\210\227.py" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution(object): - def canPermutePalindrome(self, s): - """ - :type s: str - :rtype: bool - """ - from collections import Counter - odd_cnt = 0 - for key, val in Counter(s).items(): - if val % 2: - odd_cnt += 1 - if odd_cnt > 1: - return False - return True \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23001.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23001.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" "b/\351\235\242\350\257\225\351\242\23001.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23001.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" deleted file mode 100644 index f674642..0000000 --- "a/\351\235\242\350\257\225\351\242\23001.07.\346\227\213\350\275\254\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23001.07-\346\227\213\350\275\254\347\237\251\351\230\265.py" +++ /dev/null @@ -1,7 +0,0 @@ -class Solution(object): - def rotate(self, matrix): - """ - :type matrix: List[List[int]] - :rtype: None Do not return anything, modify matrix in-place instead. - """ - matrix[::] = zip(*matrix[::-1]) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23002.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\23002.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" "b/\351\235\242\350\257\225\351\242\23002.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\23002.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" new file mode 100644 index 0000000..bc35489 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23002.06.\345\233\236\346\226\207\351\223\276\350\241\250/\351\235\242\350\257\225\351\242\23002.06-\345\233\236\346\226\207\351\223\276\350\241\250.py" @@ -0,0 +1,18 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None +class Solution(object): + def isPalindrome(self, head): + """ + :type head: ListNode + :rtype: bool + """ + s = [] + while head: + s.append(head.val) + head = head.next + return s == s[::-1] + + \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23008.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\23008.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" "b/\351\235\242\350\257\225\351\242\23008.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\23008.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" new file mode 100644 index 0000000..513e8ee --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23008.01.\344\270\211\346\255\245\351\227\256\351\242\230/\351\235\242\350\257\225\351\242\23008.01-\344\270\211\346\255\245\351\227\256\351\242\230.py" @@ -0,0 +1,14 @@ +class Solution(object): + def waysToStep(self, n): + """ + :type n: int + :rtype: int + """ + dp = [0 for _ in range(n + 3)] + dp[1] = 1 + dp[2] = 2 + dp[3] = 4 + MOD = 10 ** 9 + 7 + for i in range(4, n + 1): + dp[i] = (dp[i - 1] + dp[i - 2] + dp[i - 3]) % MOD + return dp[n] \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23016.03.\344\272\244\347\202\271/\351\235\242\350\257\225\351\242\23016.03-\344\272\244\347\202\271.py" "b/\351\235\242\350\257\225\351\242\23016.03.\344\272\244\347\202\271/\351\235\242\350\257\225\351\242\23016.03-\344\272\244\347\202\271.py" deleted file mode 100644 index 0a31d37..0000000 --- "a/\351\235\242\350\257\225\351\242\23016.03.\344\272\244\347\202\271/\351\235\242\350\257\225\351\242\23016.03-\344\272\244\347\202\271.py" +++ /dev/null @@ -1,4 +0,0 @@ -res=iter([[],[],[0.00000,0.00000],[],[],[0.00000,0.00000],[1.00000,1.00000],[-1.00000,1.00000],[0.50000,0.00000],[1.00000,1.00000],[-47.02740,44.44814],[41.45557,-8.39925],[-61.29711,4.58065],[-33.90141,-35.01408],[63.68807,47.68169],[-24.96573,11.01457],[-41.26415,5.77358],[-1.66921,-60.39657],[34.73304,5.15974],[58.29173,-63.09474],[-56.83871,47.25427],[14.63478,-5.23478],[-31.15152,44.00000],[-19.83569,-55.25496],[-64.28571,-17.19048],[-61.09417,-62.48430],[40.00000,-1.00000],[60.05573,48.22291],[59.00000,-60.00000],[-17.22973,-16.67568],[],[],[-42.72489,4.90218],[35.78820,28.54064],[-46.50000,-49.50000],[3.52174,-49.39130],[36.90900,-40.04929],[59.62688,39.26535],[24.55769,35.48077],[-32.85097,36.45848],[-17.84615,40.84615],[-39.91018,37.40719],[39.24000,-12.24000],[46.22727,-50.95455],[-24.92958,27.09859],[15.31721,22.37168],[],[-42.20046,-1.74829],[61.74777,-29.11851],[54.75000,17.62500],[19.59055,60.10236],[-29.29258,-27.07424],[-15.74064,-44.09904],[],[43.70236,20.15632],[-22.25570,-48.01013],[31.67398,-63.67085],[5.34247,-29.12204],[45.62625,34.71153],[]]) -class Solution: - def intersection(self, start1: List[int], end1: List[int], start2: List[int], end2: List[int]) -> List[float]: - return next(res) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23016.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\23016.07-\346\234\200\345\244\247\346\225\260\345\200\274.py" "b/\351\235\242\350\257\225\351\242\23016.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\23016.07-\346\234\200\345\244\247\346\225\260\345\200\274.py" index 8f7148c..c795505 100644 --- "a/\351\235\242\350\257\225\351\242\23016.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\23016.07-\346\234\200\345\244\247\346\225\260\345\200\274.py" +++ "b/\351\235\242\350\257\225\351\242\23016.07.\346\234\200\345\244\247\346\225\260\345\200\274/\351\235\242\350\257\225\351\242\23016.07-\346\234\200\345\244\247\346\225\260\345\200\274.py" @@ -5,4 +5,4 @@ def maximum(self, a, b): :type b: int :rtype: int """ - return max(a, b) \ No newline at end of file + return max(a, b) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23016.10.\347\224\237\345\255\230\344\272\272\346\225\260/\351\235\242\350\257\225\351\242\23016.10-\347\224\237\345\255\230\344\272\272\346\225\260.py" "b/\351\235\242\350\257\225\351\242\23016.10.\347\224\237\345\255\230\344\272\272\346\225\260/\351\235\242\350\257\225\351\242\23016.10-\347\224\237\345\255\230\344\272\272\346\225\260.py" deleted file mode 100644 index 82c78bd..0000000 --- "a/\351\235\242\350\257\225\351\242\23016.10.\347\224\237\345\255\230\344\272\272\346\225\260/\351\235\242\350\257\225\351\242\23016.10-\347\224\237\345\255\230\344\272\272\346\225\260.py" +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def maxAliveYear(self, birth, death): - """ - :type birth: List[int] - :type death: List[int] - :rtype: int - """ - bucket = [0 for _ in range(102)] - - for i in range(len(birth)): - bucket[birth[i] - 1900] += 1 - bucket[death[i] + 1 - 1900] -= 1 - - maxx, res = 0, 0 - for i in range(1, len(bucket)): - bucket[i] += bucket[i - 1] - if bucket[i] > maxx: - maxx = bucket[i] - res = i - - return res + 1900 \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23016.11.\350\267\263\346\260\264\346\235\277/\351\235\242\350\257\225\351\242\23016.11-\350\267\263\346\260\264\346\235\277.py" "b/\351\235\242\350\257\225\351\242\23016.11.\350\267\263\346\260\264\346\235\277/\351\235\242\350\257\225\351\242\23016.11-\350\267\263\346\260\264\346\235\277.py" new file mode 100644 index 0000000..e83177e --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23016.11.\350\267\263\346\260\264\346\235\277/\351\235\242\350\257\225\351\242\23016.11-\350\267\263\346\260\264\346\235\277.py" @@ -0,0 +1,14 @@ +class Solution(object): + def divingBoard(self, shorter, longer, k): + """ + :type shorter: int + :type longer: int + :type k: int + :rtype: List[int] + """ + res = [] + for i in range(k + 1): + s = shorter * (k - i) + longer * i + if not res or res[-1] != s: + res.append(s) + return res if k else [] \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23016.17.\350\277\236\347\273\255\346\225\260\345\210\227/\351\235\242\350\257\225\351\242\23016.17-\350\277\236\347\273\255\346\225\260\345\210\227.py" "b/\351\235\242\350\257\225\351\242\23016.17.\350\277\236\347\273\255\346\225\260\345\210\227/\351\235\242\350\257\225\351\242\23016.17-\350\277\236\347\273\255\346\225\260\345\210\227.py" new file mode 100644 index 0000000..c9af973 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23016.17.\350\277\236\347\273\255\346\225\260\345\210\227/\351\235\242\350\257\225\351\242\23016.17-\350\277\236\347\273\255\346\225\260\345\210\227.py" @@ -0,0 +1,10 @@ +class Solution(object): + def maxSubArray(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + dp = [0 for _ in nums] + for i, x in enumerate(nums): + dp[i] = max(x, dp[i - 1] + x) if i else x + return max(dp) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23016.25.LRU\347\274\223\345\255\230/\351\235\242\350\257\225\351\242\23016.25-LRU\347\274\223\345\255\230.py" "b/\351\235\242\350\257\225\351\242\23016.25.LRU\347\274\223\345\255\230/\351\235\242\350\257\225\351\242\23016.25-LRU\347\274\223\345\255\230.py" new file mode 100644 index 0000000..ee7f3bd --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23016.25.LRU\347\274\223\345\255\230/\351\235\242\350\257\225\351\242\23016.25-LRU\347\274\223\345\255\230.py" @@ -0,0 +1,78 @@ +class DLLNode(object): + def __init__(self, key, val, pre = None, next = None): + self.key = key + self.val = val + self.pre = pre + self.next = next + +class LRUCache(object): + def __init__(self, capacity): + """ + :type capacity: int + """ + self.capacity = capacity + self.size = 0 + self.head = DLLNode(-1, -1) + self.tail = DLLNode(-1, -1, self.head) + self.head.next = self.tail + self.dic = {} + + def get(self, key): + """ + :type key: int + :rtype: int + """ + if key in self.dic: + res = self.dic[key].val + self.moveToHead(self.dic[key]) # set this node to be most recently used + return res + return -1 + + def put(self, key, value): + """ + :type key: int + :type value: int + :rtype: None + """ + if key in self.dic: + self.dic[key].val = value + self.moveToHead(self.dic[key]) + else: + node = DLLNode(key, value) + self.dic[key] = node + if self.size < self.capacity: + self.size += 1 + else: + self.removeLastNode() + self.insertToHead(node) + + def insertToHead(self, node): + pre_first = self.head.next + self.head.next = node + node.pre = self.head + node.next = pre_first + pre_first.pre = node + + def moveToHead(self, node): + # 1. take it out + node.pre.next = node.next + node.next.pre = node.pre + # 2. insert it to the head + self.insertToHead(node) + + def removeLastNode(self): + pre_last = self.tail.pre + pre_second_last = pre_last.pre + + pre_second_last.next = self.tail + self.tail.pre = pre_second_last + + self.dic.pop(pre_last.key) + + + + +# Your LRUCache object will be instantiated and called as such: +# obj = LRUCache(capacity) +# param_1 = obj.get(key) +# obj.put(key,value) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23017.01.\344\270\215\347\224\250\345\212\240\345\217\267\347\232\204\345\212\240\346\263\225/\351\235\242\350\257\225\351\242\23017.01-\344\270\215\347\224\250\345\212\240\345\217\267\347\232\204\345\212\240\346\263\225.py" "b/\351\235\242\350\257\225\351\242\23017.01.\344\270\215\347\224\250\345\212\240\345\217\267\347\232\204\345\212\240\346\263\225/\351\235\242\350\257\225\351\242\23017.01-\344\270\215\347\224\250\345\212\240\345\217\267\347\232\204\345\212\240\346\263\225.py" deleted file mode 100644 index 02652bd..0000000 --- "a/\351\235\242\350\257\225\351\242\23017.01.\344\270\215\347\224\250\345\212\240\345\217\267\347\232\204\345\212\240\346\263\225/\351\235\242\350\257\225\351\242\23017.01-\344\270\215\347\224\250\345\212\240\345\217\267\347\232\204\345\212\240\346\263\225.py" +++ /dev/null @@ -1,8 +0,0 @@ -class Solution(object): - def add(self, a, b): - """ - :type a: int - :type b: int - :rtype: int - """ - return sum([a, b]) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23017.04.\346\266\210\345\244\261\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23017.04-\346\266\210\345\244\261\347\232\204\346\225\260\345\255\227.py" "b/\351\235\242\350\257\225\351\242\23017.04.\346\266\210\345\244\261\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23017.04-\346\266\210\345\244\261\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..7defbb7 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23017.04.\346\266\210\345\244\261\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23017.04-\346\266\210\345\244\261\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,10 @@ +class Solution(object): + def missingNumber(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + s = set(nums) + for i in range(len(nums) + 1): + if i not in s: + return i \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23017.16.\346\214\211\346\221\251\345\270\210/\351\235\242\350\257\225\351\242\23017.16-\346\214\211\346\221\251\345\270\210.py" "b/\351\235\242\350\257\225\351\242\23017.16.\346\214\211\346\221\251\345\270\210/\351\235\242\350\257\225\351\242\23017.16-\346\214\211\346\221\251\345\270\210.py" index bccbcff..109e90c 100644 --- "a/\351\235\242\350\257\225\351\242\23017.16.\346\214\211\346\221\251\345\270\210/\351\235\242\350\257\225\351\242\23017.16-\346\214\211\346\221\251\345\270\210.py" +++ "b/\351\235\242\350\257\225\351\242\23017.16.\346\214\211\346\221\251\345\270\210/\351\235\242\350\257\225\351\242\23017.16-\346\214\211\346\221\251\345\270\210.py" @@ -4,10 +4,11 @@ def massage(self, nums): :type nums: List[int] :rtype: int """ + if not nums: + return 0 + if len(nums) == 1: + return nums[0] dp = [0 for _ in nums] - for i in range(len(nums)): - dp[i] = nums[i] - for j in range(i - 1): - dp[i] = max(dp[i], dp[j] + nums[i]) - return max(dp) if nums else 0 \ No newline at end of file + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) + return dp[-1] \ No newline at end of file