diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..cef69cc Binary files /dev/null and b/.DS_Store differ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bb38db8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\ProgramData\\Anaconda2\\envs\\py3\\python.exe" +} \ No newline at end of file diff --git a/0001.two-sum/two-sum.cpp b/0001.two-sum/two-sum.cpp deleted file mode 100644 index b2f69fd..0000000 --- a/0001.two-sum/two-sum.cpp +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - unordered_map m; - vector res; - for (int i = 0; i < nums.size(); i++){ - int temp = target - nums[i]; - if (m.count(temp)){ - res.push_back(m[temp]); - res.push_back(i); - break; - } - m[nums[i]] = i; - } - return res; - } -}; \ No newline at end of file diff --git a/0001.two-sum/two-sum.md b/0001.two-sum/two-sum.md deleted file mode 100644 index 8457633..0000000 --- a/0001.two-sum/two-sum.md +++ /dev/null @@ -1,14 +0,0 @@ -

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

- -

You may assume that each input would have exactly one solution, and you may not use the same element twice.

- -

Example:

- -
-Given nums = [2, 7, 11, 15], target = 9,
-
-Because nums[0] + nums[1] = 2 + 7 = 9,
-return [0, 1].
-
- -

 

diff --git a/0001.two-sum/two-sum.py b/0001.two-sum/two-sum.py deleted file mode 100644 index f328bda..0000000 --- a/0001.two-sum/two-sum.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] - """ - for i in range(len(nums)): - for j in range(i + 1, len(nums)): - if nums[i] + nums[j] == target: - return [i,j] - \ 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.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" new file mode 100644 index 0000000..2201fce --- /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.py" @@ -0,0 +1,12 @@ +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 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/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/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..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.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" new file mode 100644 index 0000000..8a1f7f4 --- /dev/null +++ "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" @@ -0,0 +1,16 @@ +class Solution(object): + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + 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.median-of-two-sorted-arrays/median-of-two-sorted-arrays.md b/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.md deleted file mode 100644 index 5392ce0..0000000 --- a/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.md +++ /dev/null @@ -1,23 +0,0 @@ -

There are two sorted arrays nums1 and nums2 of size m and n respectively.

- -

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

- -

You may assume nums1 and nums2 cannot be both empty.

- -

Example 1:

- -
-nums1 = [1, 3]
-nums2 = [2]
-
-The median is 2.0
-
- -

Example 2:

- -
-nums1 = [1, 2]
-nums2 = [3, 4]
-
-The median is (2 + 3)/2 = 2.5
-
diff --git a/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.py b/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.py deleted file mode 100644 index 5dbbd99..0000000 --- a/0004.median-of-two-sorted-arrays/median-of-two-sorted-arrays.py +++ /dev/null @@ -1,57 +0,0 @@ -from heapq import * -class MedianFinder(object): -# άѣһ󶥶ѣһСѣСȴ󶥶Ҫ -# DZڵλsizeͬλѶ֮ͳ2 -# ֻһλͿsizeСǸѵĶѶ -# ½СѵҪСͰ󶥶 -# ½СѵҪ󣬾ͰС -# ѣʹsize Ϊ1 - def __init__(self): - """ - initialize your data structure here. - """ - self.max_h = list() - self.min_h = list() - heapify(self.max_h) - heapify(self.min_h) - - - def addNum(self, num): - """ - :type num: int - :rtype: None - """ - heappush(self.min_h, num) - heappush(self.max_h, -heappop(self.min_h)) - if len(self.max_h) > len(self.min_h): - heappush(self.min_h, -heappop(self.max_h)) - - def findMedian(self): - """ - :rtype: float - """ - max_len = len(self.max_h) - min_len = len(self.min_h) - if max_len == min_len: #ѡλ - return (self.min_h[0] + -self.max_h[0]) / 2. - else:#Сѵsize һ >= 󶥶ѵsizeԴ𰸾СѵĶѶ - return self.min_h[0] / 1. - -# Your MedianFinder object will be instantiated and called as such: -# obj = MedianFinder() -# obj.addNum(num) -# param_2 = obj.findMedian() - -class Solution(object): - def findMedianSortedArrays(self, nums1, nums2): - """ - :type nums1: List[int] - :type nums2: List[int] - :rtype: float - """ - mf = MedianFinder() - for num in nums1: - mf.addNum(num) - for num in nums2: - mf.addNum(num) - return mf.findMedian() \ No newline at end of file diff --git "a/0004.\345\257\273\346\211\276\344\270\244\344\270\252\346\234\211\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\234\211\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\234\211\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\234\211\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.py" index 5dbbd99..a36ac3f 100644 --- "a/0004.\345\257\273\346\211\276\344\270\244\344\270\252\346\234\211\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\234\211\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\234\211\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\234\211\345\272\217\346\225\260\347\273\204\347\232\204\344\270\255\344\275\215\346\225\260.py" @@ -1,46 +1,22 @@ from heapq import * -class MedianFinder(object): -# άѣһ󶥶ѣһСѣСȴ󶥶Ҫ -# DZڵλsizeͬλѶ֮ͳ2 -# ֻһλͿsizeСǸѵĶѶ -# ½СѵҪСͰ󶥶 -# ½СѵҪ󣬾ͰС -# ѣʹsize Ϊ1 +class DoubleHeap(object): def __init__(self): - """ - initialize your data structure here. - """ - self.max_h = list() - self.min_h = list() - heapify(self.max_h) - heapify(self.min_h) + self.maxh = [] + self.minh = [] + heapify(self.maxh) + heapify(self.minh) - - def addNum(self, num): - """ - :type num: int - :rtype: None - """ - heappush(self.min_h, num) - heappush(self.max_h, -heappop(self.min_h)) - if len(self.max_h) > len(self.min_h): - heappush(self.min_h, -heappop(self.max_h)) - - def findMedian(self): - """ - :rtype: float - """ - max_len = len(self.max_h) - min_len = len(self.min_h) - if max_len == min_len: #ѡλ - return (self.min_h[0] + -self.max_h[0]) / 2. - else:#Сѵsize һ >= 󶥶ѵsizeԴ𰸾СѵĶѶ - return self.min_h[0] / 1. + def insert(self, val): + heappush(self.minh, val) + heappush(self.maxh, -heappop(self.minh)) + if len(self.maxh) > len(self.minh): + heappush(self.minh, -heappop(self.maxh)) -# Your MedianFinder object will be instantiated and called as such: -# obj = MedianFinder() -# obj.addNum(num) -# param_2 = obj.findMedian() + def findMedian(self): + if len(self.maxh) == len(self.minh): + return (self.minh[0] - self.maxh[0]) / 2.0 + return self.minh[0]/1.0 + class Solution(object): def findMedianSortedArrays(self, nums1, nums2): @@ -49,9 +25,11 @@ def findMedianSortedArrays(self, nums1, nums2): :type nums2: List[int] :rtype: float """ - mf = MedianFinder() + dh = DoubleHeap() for num in nums1: - mf.addNum(num) + dh.insert(num) + for num in nums2: - mf.addNum(num) - return mf.findMedian() \ No newline at end of file + dh.insert(num) + + return dh.findMedian() \ 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 22d6540..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,24 +4,21 @@ def longestPalindrome(self, s): :type s: str :rtype: str """ - max_l = 0 res = "" - for i in range(0, len(s)): - #s[i] Ϊɢ - left, right = i, i - while(left >= 0 and right < len(s) and s[left] == s[right]): - if max_l < right - left + 1: - max_l = right - left + 1 - res = s[left:right + 1] - left -= 1 - right += 1 - - #s[i],s[i+1]Ϊɢ - left, right = i, i + 1 - while(left >= 0 and right < len(s) and s[left] == s[right]): - if max_l < right - left + 1: - max_l = right - left + 1 - res = s[left:right + 1] - left -= 1 - right += 1 + for i in range(len(s)): + 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/0006.Z\345\255\227\345\275\242\345\217\230\346\215\242/0006-Z\345\255\227\345\275\242\345\217\230\346\215\242.py" "b/0006.Z\345\255\227\345\275\242\345\217\230\346\215\242/0006-Z\345\255\227\345\275\242\345\217\230\346\215\242.py" index 345c29c..ddac202 100644 --- "a/0006.Z\345\255\227\345\275\242\345\217\230\346\215\242/0006-Z\345\255\227\345\275\242\345\217\230\346\215\242.py" +++ "b/0006.Z\345\255\227\345\275\242\345\217\230\346\215\242/0006-Z\345\255\227\345\275\242\345\217\230\346\215\242.py" @@ -1,35 +1,40 @@ class Solution(object): - def convert(self, s, n): + def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ - #ҹ - if n <= 1: + #һкһж 2 * (n - 1) + #ֱֱΣ 2 * (n - 1 - i) + #ֱֱΣ 2 * i + if not s or numRows == 1: return s - l = len(s) res = "" - for i in range(n): - tmp, index = "", i - if i in [0, n - 1]: - while(index < l): - - tmp += s[index] - index += 2 * (n - 1) + for idx in range(numRows): + if idx < len(s): + res += s[idx] + + if idx in [0, numRows - 1]: + tmp = idx + 2 *(numRows - 1) + while tmp < len(s): + res += s[tmp] + tmp += 2 *(numRows - 1) else: - state = "down" - while(index < l): - tmp += s[index] - if state == "down": - state = "up" - index += 2 * (n - 1 - i) - else: - state = "down" - index += 2 * i - res += tmp - + tmp = idx + 2 * (numRows - 1 - idx) + tri = "down" + while tmp < len(s): + res += s[tmp] + if tri == "up": + tmp += 2 * (numRows - 1 - idx) + tri = "down" + else: + tmp += 2 * idx + tri = "up" + return res - + + + \ No newline at end of file diff --git "a/0007.\346\225\264\346\225\260\345\217\215\350\275\254/0007-\346\225\264\346\225\260\345\217\215\350\275\254.py" "b/0007.\346\225\264\346\225\260\345\217\215\350\275\254/0007-\346\225\264\346\225\260\345\217\215\350\275\254.py" index ff7dd8e..82df21d 100644 --- "a/0007.\346\225\264\346\225\260\345\217\215\350\275\254/0007-\346\225\264\346\225\260\345\217\215\350\275\254.py" +++ "b/0007.\346\225\264\346\225\260\345\217\215\350\275\254/0007-\346\225\264\346\225\260\345\217\215\350\275\254.py" @@ -4,19 +4,16 @@ def reverse(self, x): :type x: int :rtype: int """ - - flag = 0 + INT_MIN = -2 **31 + INT_MAX = 2 ** 31 - 1 + op = 1 if x < 0: - flag = 1 - if flag: + op = -1 s = str(x)[1:] - s = s[::-1] - x = -1 *int(s) else: s = str(x) - s = s[::-1] - x = int(s) - if x < -1 * 2 **31 or x > 2** 31 -1: - return 0 - return x \ No newline at end of file + res = op * int(s[::-1]) + return res if INT_MIN <= res <= INT_MAX else 0 + + \ No newline at end of file diff --git "a/0008.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi)/0008-\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi).py" "b/0008.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi)/0008-\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi).py" index 422830d..f817929 100644 --- "a/0008.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi)/0008-\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi).py" +++ "b/0008.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi)/0008-\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260(atoi).py" @@ -5,32 +5,34 @@ def myAtoi(self, s): :rtype: int """ s = s.strip(" ") - if not len(s): #ų - return 0 - if s[0] not in ["+", "-"] and not s[0].isdigit(): #ųһǿַ + # print s + if not s or (s[0] not in ["+", "-"] and not s[0].isdigit()): return 0 + op = 1 - res = "" + tmp = "" for i, char in enumerate(s): - if i == 0 : + if i == 0: if char == "-": op = -1 continue elif char == "+": - continue - if char == " " or not char.isdigit(): + pass + continue + if char.isdigit(): + tmp += char + else: break - res += char - # print res, op - if len(res) > 0: - res = op * int(res) + # print tmp + if tmp: + res = op * int(tmp) else: - return 0 - INT_MIN = -2 **31 + res = 0 INT_MAX = 2 **31 - 1 + INT_MIN = -2 **31 if res > INT_MAX: return INT_MAX elif res < INT_MIN: return INT_MIN - return res - \ No newline at end of file + else: + 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/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260.py" "b/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260.py" index e40b713..fc986ad 100644 --- "a/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260.py" +++ "b/0009.\345\233\236\346\226\207\346\225\260/0009-\345\233\236\346\226\207\346\225\260.py" @@ -1,11 +1,17 @@ class Solution(object): def isPalindrome(self, x): + """ + :type x: int + :rtype: bool + """ + #2019.6.1 + xx = x + if x < 0: + return False - s = str(x) - if len(s) <= 1: - return True - print len(s) / 2 - for count in range(0, len(s)/2): - if s[count] != s[len(s)-1 - count]: - return False - return True \ No newline at end of file + 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/0011.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/0011-\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.py" "b/0011.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/0011-\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.py" index aef803f..15c142d 100644 --- "a/0011.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/0011-\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.py" +++ "b/0011.\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250/0011-\347\233\233\346\234\200\345\244\232\346\260\264\347\232\204\345\256\271\345\231\250.py" @@ -4,17 +4,13 @@ def maxArea(self, height): :type height: List[int] :rtype: int """ - lo, hi = 0, len(height) - 1 + left, right = 0, len(height) - 1 res = 0 - while(lo < hi): - if height[lo] > height[hi]: - area = height[hi] * (hi - lo) - hi -= 1 + while(left < right): + # print left, right, + res = max(res, (right - left) * min(height[left], height[right])) + if height[left] < height[right]: + left += 1 else: - area = height[lo] * (hi - lo) - lo += 1 - # print area - res = max(area, res) - - return res - \ No newline at end of file + right -= 1 + return res \ 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 3d15c44..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,12 +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/0014.\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200/0014-\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" "b/0014.\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200/0014-\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" index 8737a0d..058eeef 100644 --- "a/0014.\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200/0014-\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" +++ "b/0014.\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200/0014-\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.py" @@ -6,15 +6,13 @@ def longestCommonPrefix(self, strs): """ if not strs: return "" - strs.sort() - res = "" - for x, y in zip(strs[0], strs[-1]): + res = "" + pair = zip(strs[0], strs[-1]) + for x, y in pair: if x == y: res += x else: break - - return res - \ No newline at end of file + return res \ No newline at end of file diff --git "a/0015.\344\270\211\346\225\260\344\271\213\345\222\214/0015-\344\270\211\346\225\260\344\271\213\345\222\214.py" "b/0015.\344\270\211\346\225\260\344\271\213\345\222\214/0015-\344\270\211\346\225\260\344\271\213\345\222\214.py" index 5ed85e1..ffe7ff2 100644 --- "a/0015.\344\270\211\346\225\260\344\271\213\345\222\214/0015-\344\270\211\346\225\260\344\271\213\345\222\214.py" +++ "b/0015.\344\270\211\346\225\260\344\271\213\345\222\214/0015-\344\270\211\346\225\260\344\271\213\345\222\214.py" @@ -4,29 +4,30 @@ def threeSum(self, nums): :type nums: List[int] :rtype: List[List[int]] """ - #̶a,˫ָ֮Ϊ-a nums.sort() - l = len(nums) res = [] - for i, a in enumerate(nums): + for i, num in enumerate(nums): if i == 0 or nums[i] > nums[i - 1]: - #ʼ˫ָ - left, right = i + 1, len(nums) - 1 + # num + b + c == 0 + left = i + 1 + right = len(nums) - 1 + while(left < right): - s = a + nums[left] + nums[right] + s = num + nums[left] + nums[right] if s == 0: - tmp = [a, nums[left], nums[right]] - res.append(tmp) + res.append([num, nums[left], nums[right]]) left += 1 right -= 1 - while left < right and nums[left] == nums[left - 1]: + while(left < right and nums[left] == nums[left - 1]): left += 1 - while right > left and nums[right] == nums[right + 1]: + while(left < right and nums[right + 1] == nums[right]): right -= 1 - elif s < 0: - left += 1 + # break elif s > 0: right -= 1 - return res + else: + left += 1 - \ No newline at end of file + + return res + \ No newline at end of file diff --git "a/0016.\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214/0016-\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214.py" "b/0016.\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214/0016-\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214.py" index 4b5aaa9..e947337 100644 --- "a/0016.\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214/0016-\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214.py" +++ "b/0016.\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214/0016-\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214.py" @@ -6,19 +6,22 @@ def threeSumClosest(self, nums, target): :rtype: int """ nums.sort() - res = 99999999 + res = nums[0] + nums[1] + nums[2] for i, num in enumerate(nums): left, right = i + 1, len(nums) - 1 - while(left < right): + + while left < right: s = num + nums[left] + nums[right] - + # print s, res, abs(s - target), abs(res - target) if abs(s - target) < abs(res - target): res = s - if s == target: return s - elif s > target: - right -= 1 - else: + elif s < target: left += 1 - return res \ No newline at end of file + else: + right -= 1 + return res + + + \ No newline at end of file diff --git "a/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210/0017-\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.py" "b/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210/0017-\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.py" index 7b58b1c..935f397 100644 --- "a/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210/0017-\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.py" +++ "b/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210/0017-\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.py" @@ -4,20 +4,19 @@ def letterCombinations(self, digits): :type digits: str :rtype: List[str] """ + if not digits: + return [] mapping = {2:"abc", 3:"def", 4:"ghi", 5:"jkl", 6:"mno", 7:"pqrs", 8:"tuv", 9:"wxyz"} - + res = [] - for digit in digits: - temp = [] - n = int(digit) - for char in mapping[n]: - # print char - if not res: - temp.append(char) - else: - for item in res: - temp.append(item + char) - res = temp - # print res - + # for i, digit in enumerate(digits) + def dfs(nums, tmp): + if not nums: + res.append(tmp) + return + + for char in mapping[int(nums[0])]: + dfs(nums[1:], tmp + char) + + dfs(digits, "") return res \ No newline at end of file diff --git "a/0018.\345\233\233\346\225\260\344\271\213\345\222\214/0018-\345\233\233\346\225\260\344\271\213\345\222\214.py" "b/0018.\345\233\233\346\225\260\344\271\213\345\222\214/0018-\345\233\233\346\225\260\344\271\213\345\222\214.py" index 1fb6f76..16b0c4a 100644 --- "a/0018.\345\233\233\346\225\260\344\271\213\345\222\214/0018-\345\233\233\346\225\260\344\271\213\345\222\214.py" +++ "b/0018.\345\233\233\346\225\260\344\271\213\345\222\214/0018-\345\233\233\346\225\260\344\271\213\345\222\214.py" @@ -1,46 +1,37 @@ class Solution(object): - def fourSum(self, n, target): + def fourSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ - self.res = [] - n.sort() - # print n - def threeSum(nums,t, d): - """ - :type nums: List[int] - :rtype: List[List[int]] - """ - #̶a,˫ָ֮Ϊ-a - l = len(nums) - res = [] - for i, a in enumerate(nums): - if i == 0 or nums[i] > nums[i - 1]: - #ʼ˫ָ - left, right = i + 1, len(nums) - 1 + if len(nums) < 4: + return [] + nums.sort() + res = [] + def findThreeSum(num, l, t): + for i, n in enumerate(l): + if i == 0 or l[i] > l[i - 1]: + left, right = i + 1, len(l) - 1 while(left < right): - s = a + nums[left] + nums[right] - # print d, a, nums[left], nums[right] + s = n + l[left] + l[right] + if s == t: - tmp = [d,a, nums[left], nums[right]] - self.res.append(tmp) + res.append([num, n, l[left], l[right]]) left += 1 right -= 1 - while left < right and nums[left] == nums[left - 1]: + while(left < right and l[left] == l[left - 1]): left += 1 - while right > left and nums[right] == nums[right + 1]: + while(right > left and l[right] == l[right + 1]): right -= 1 - elif s < t: - left += 1 elif s > t: right -= 1 - - for i in range(len(n) - 3): - if i == 0 or n[i] > n[i - 1]: - # print n[i] - threeSum(n[i + 1:], target - n[i], n[i]) - + else: + left += 1 + + for i, num in enumerate(nums): + if i == 0 or nums[i] > nums[i - 1]: + findThreeSum(num, nums[i + 1:], target - num) - return self.res \ No newline at end of file + return res + \ No newline at end of file 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/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\350\212\202\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\350\212\202\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\350\212\202\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\350\212\202\347\202\271.py" index 45d8dba..eb41749 100644 --- "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\350\212\202\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\350\212\202\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\350\212\202\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\350\212\202\347\202\271.py" @@ -11,23 +11,19 @@ def removeNthFromEnd(self, head, n): :type n: int :rtype: ListNode """ - if not head.next: - return [] p = head - if n == 1: - while(p.next.next): - p = p.next - p.next = None - else: - fast = head - slow = head - for i in range(n-1): - fast = fast.next - print fast.val,slow.val - while(fast.next): - fast = fast.next - slow = slow.next - print fast.val,slow.val - slow.val = slow.next.val - slow.next = slow.next.next - return head \ No newline at end of file + slow, fast = p, p + while(n): + n -= 1 + fast = fast.next + if fast is None: + return p.next + while(fast and fast.next): + fast = fast.next + slow = slow.next + + slow.next = slow.next.next + + return p + + \ 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 5044c13..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,13 +4,13 @@ def isValid(self, s): :type s: str :rtype: bool """ - if len(s)%2: - return False - dic = {")":"(","]":"[","}":"{"} - stack = [None] - for item in s: - if item in dic and stack[-1] == dic[item]: - stack.pop() + dic = {")": "(", "]":"[", "}":"{"} + stack = [] + for ch in s: + if ch in ["(", "[", "{"]: + stack.append(ch) else: - stack.append(item) - return len(stack) == 1 + 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/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" "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" new file mode 100644 index 0000000..7e7be51 --- /dev/null +++ "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" @@ -0,0 +1,33 @@ +# 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 + """ + dummy = ListNode(-1) + + p = dummy + + while l1 and l2: + if l1.val <= l2.val: + p.next = ListNode(l1.val) + l1 = l1.next + else: + p.next = ListNode(l2.val) + l2 = l2.next + p = p.next + + if l1: + p.next = l1 + + if l2: + p.next = l2 + + return dummy.next \ No newline at end of file diff --git "a/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/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..7e7be51 --- /dev/null +++ "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.py" @@ -0,0 +1,33 @@ +# 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 + """ + dummy = ListNode(-1) + + p = dummy + + while l1 and l2: + if l1.val <= l2.val: + p.next = ListNode(l1.val) + l1 = l1.next + else: + p.next = ListNode(l2.val) + l2 = l2.next + p = p.next + + if l1: + p.next = l1 + + if l2: + p.next = l2 + + return dummy.next \ No newline at end of file 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/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/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" index 3801859..326c241 100644 --- "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.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.py" @@ -1,23 +1,22 @@ class Solution(object): - def generate(self, temp, left, right, result): - if (left == 0 and right == 0): - result.append(temp) - return - if (left > 0): - self.generate(temp + "(", left-1, right, result) - if (left < right): - self.generate(temp + ")", left, right - 1, result) - def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ - result = [] - self.generate("", n, n, result) - return result - - - + 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/0023.\345\220\210\345\271\266K\344\270\252\346\216\222\345\272\217\351\223\276\350\241\250/0023-\345\220\210\345\271\266K\344\270\252\346\216\222\345\272\217\351\223\276\350\241\250.py" "b/0023.\345\220\210\345\271\266K\344\270\252\346\216\222\345\272\217\351\223\276\350\241\250/0023-\345\220\210\345\271\266K\344\270\252\346\216\222\345\272\217\351\223\276\350\241\250.py" new file mode 100644 index 0000000..8581c27 --- /dev/null +++ "b/0023.\345\220\210\345\271\266K\344\270\252\346\216\222\345\272\217\351\223\276\350\241\250/0023-\345\220\210\345\271\266K\344\270\252\346\216\222\345\272\217\351\223\276\350\241\250.py" @@ -0,0 +1,30 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def mergeKLists(self, lists): + """ + :type lists: List[ListNode] + :rtype: ListNode + """ + from heapq import * + pq = [] + for i in range(len(lists)): + if lists[i]: + heappush(pq, (lists[i].val, i)) + lists[i] = lists[i].next + + dummy = ListNode(1) + p = dummy + while pq: + val, idx = heappop(pq) + p.next = ListNode(val) + p = p.next + if lists[idx]: + heappush(pq, (lists[idx].val, idx)) + lists[idx] = lists[idx].next + return dummy.next + \ 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/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/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..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.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\216\222\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\216\222\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\216\222\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\216\222\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" index e23482f..be1b92b 100644 --- "a/0026.\345\210\240\351\231\244\346\216\222\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\216\222\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\216\222\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\216\222\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" @@ -4,13 +4,14 @@ def removeDuplicates(self, nums): :type nums: List[int] :rtype: int """ - i = 0 for j in range(len(nums)): - if i < 1 or nums[j] != nums[j - 1]: + if j == 0 or nums[j] != nums[j - 1]: nums[i] = nums[j] i += 1 return i - - - \ No newline at end of file + + + + + \ 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/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/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" index 439086f..7f0c216 100644 --- "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.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.py" @@ -5,15 +5,17 @@ def removeElement(self, nums, val): :type val: int :rtype: int """ - i = 0 - while( i int: + if needle not in haystack: + return -1 + return haystack.index(needle) \ No newline at end of file diff --git a/0029.divide-two-integers/divide-two-integers.md b/0029.divide-two-integers/divide-two-integers.md deleted file mode 100644 index 3c1b6d6..0000000 --- a/0029.divide-two-integers/divide-two-integers.md +++ /dev/null @@ -1,25 +0,0 @@ -

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.

- -

Return the quotient after dividing dividend by divisor.

- -

The integer division should truncate toward zero.

- -

Example 1:

- -
-Input: dividend = 10, divisor = 3
-Output: 3
- -

Example 2:

- -
-Input: dividend = 7, divisor = -3
-Output: -2
- -

Note:

- -
    -
  • Both dividend and divisor will be 32-bit signed integers.
  • -
  • The divisor will never be 0.
  • -
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
  • -
diff --git a/0029.divide-two-integers/divide-two-integers.py b/0029.divide-two-integers/divide-two-integers.py deleted file mode 100644 index e89b241..0000000 --- a/0029.divide-two-integers/divide-two-integers.py +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def divide(self, dividend, divisor): - """ - :type dividend: int - :type divisor: int - :rtype: int - """ - # 㱻Լȥٸ - op = 1 - if (dividend > 0 and divisor < 0) or (dividend < 0 and divisor > 0): - op = -1 - - dividend, divisor = abs(dividend), abs(divisor) - res = 0 - # cnt = 1 - while(dividend >= divisor): - multidivisor, multi = divisor, 1 - while(dividend >= multidivisor): - res += multi - dividend -= multidivisor - multi = multi << 1 - multidivisor = multidivisor <<1 - print dividend, multidivisor, multi - print multi - - - INT_MIN = -(2 **31) - INT_MAX = 2 **31 - 1 - res *= op - - return res if INT_MIN <= res <= INT_MAX else INT_MAX \ No newline at end of file diff --git "a/0029.\344\270\244\346\225\260\347\233\270\351\231\244/0029-\344\270\244\346\225\260\347\233\270\351\231\244.py" "b/0029.\344\270\244\346\225\260\347\233\270\351\231\244/0029-\344\270\244\346\225\260\347\233\270\351\231\244.py" index 00c56c2..c4d873d 100644 --- "a/0029.\344\270\244\346\225\260\347\233\270\351\231\244/0029-\344\270\244\346\225\260\347\233\270\351\231\244.py" +++ "b/0029.\344\270\244\346\225\260\347\233\270\351\231\244/0029-\344\270\244\346\225\260\347\233\270\351\231\244.py" @@ -5,24 +5,23 @@ def divide(self, dividend, divisor): :type divisor: int :rtype: int """ - # 㱻Լȥٸ + multi = dividend * divisor op = 1 - if (dividend > 0 and divisor < 0) or (dividend < 0 and divisor > 0): + if multi == 0: + return 0 + elif multi <0: op = -1 - dividend, divisor = abs(dividend), abs(divisor) + multi = 1 res = 0 while(dividend >= divisor): - multidivisor, multi = divisor, 1 - while(dividend >= multidivisor): + tmp = multi * divisor + if dividend >= tmp: + dividend -= tmp res += multi - dividend -= multidivisor - multi = multi << 1 - multidivisor = multidivisor <<1 + multi += 1 + else: + multi = 1 - - INT_MIN = -(2 **31) - INT_MAX = 2 **31 - 1 - res *= op - - return res if INT_MIN <= res <= INT_MAX else INT_MAX \ No newline at end of file + res = res * op + return res if -2 ** 31 <= res <= 2 ** 31 - 1 else 2 ** 31 - 1 \ No newline at end of file diff --git "a/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227/0031-\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.py" "b/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227/0031-\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.py" index d44dc9d..5538d3c 100644 --- "a/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227/0031-\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.py" +++ "b/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227/0031-\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.py" @@ -4,21 +4,27 @@ def nextPermutation(self, nums): :type nums: List[int] :rtype: None Do not return anything, modify nums in-place instead. """ - #i numsβǰеͷ jnums[i - 1]СԪ - - l = len(nums) - for i in range(l - 1, -1, -1): - if nums[i - 1] < nums[i]:# ҵҪi - break - - if i == 0: # ǰһУ巭ת - nums[:] = nums[::-1] + if not nums or len(nums) == 1: return nums + # print nums, sorted(nums)[::-1] + if nums == sorted(nums)[::-1]: + nums[:] = nums[::-1] + return - for j in range(l - 1, i - 1, -1): - if nums[j] > nums[i - 1]: #ҵҪj + i = len(nums) - 1 + while(i - 1 >= 0 and nums[i - 1] >= nums[i]): + i -= 1 + i -= 1 + tmp = nums[i] + j = len(nums) - 1 + while(j >= i and nums[j] <= tmp): + j -= 1 + if nums[j] > tmp: break - nums[i - 1], nums[j] = nums[j], nums[i - 1] - nums[i:] = nums[i:][::-1] - return nums \ No newline at end of file + # print tmp, nums[j], nums[i] + nums[i], nums[j] = nums[j], nums[i] + # print nums + nums[i+ 1:] = nums[i + 1:][::-1] + return + \ No newline at end of file diff --git "a/0032.\346\234\200\351\225\277\346\234\211\346\225\210\346\213\254\345\217\267/0032-\346\234\200\351\225\277\346\234\211\346\225\210\346\213\254\345\217\267.py" "b/0032.\346\234\200\351\225\277\346\234\211\346\225\210\346\213\254\345\217\267/0032-\346\234\200\351\225\277\346\234\211\346\225\210\346\213\254\345\217\267.py" new file mode 100644 index 0000000..38e69fd --- /dev/null +++ "b/0032.\346\234\200\351\225\277\346\234\211\346\225\210\346\213\254\345\217\267/0032-\346\234\200\351\225\277\346\234\211\346\225\210\346\213\254\345\217\267.py" @@ -0,0 +1,19 @@ +class Solution(object): + def longestValidParentheses(self, s): + """ + :type s: str + :rtype: int + """ + stack = [-1] + res = 0 + for i, x in enumerate(s): + if x == "(": + stack.append(i) + else: + stack.pop() + if stack: + res = max(res, i - stack[-1]) + else: + stack.append(i) + + return res \ No newline at end of file diff --git "a/0033.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204/0033-\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204.py" "b/0033.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204/0033-\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204.py" index 4d944f0..e6c5746 100644 --- "a/0033.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204/0033-\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204.py" +++ "b/0033.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204/0033-\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204.py" @@ -5,39 +5,38 @@ def search(self, nums, target): :type target: int :rtype: int """ + #1. ת + #2. ȷtargetһȻ if not nums: return -1 if len(nums) == 1: return 0 if nums[0] == target else -1 - - lo, hi = 0, len(nums) - 1 - while(lo <= hi): - mid = (lo + hi) // 2 - # print mid, nums[mid] - if mid + 1 < len(nums) and nums[mid] > nums[mid +1]: + midposition = -1 + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + # print mid + if mid + 1 < len(nums) and nums[mid] > nums[mid + 1]: + midposition = mid break - if nums[mid] < nums[-1]: - hi = mid - 1 elif nums[mid] >= nums[0]: - lo = mid + 1 - - if lo > hi:#ûת - lo, hi = 0, len(nums) - 1 - else: + left = mid + 1 + elif nums[mid] <= nums[-1]: + right = mid - 1 + # print midposition + if midposition != -1: if target >= nums[0]: - lo, hi = 0, mid + left, right = 0, midposition else: - lo, hi = mid + 1, len(nums) - 1 - - while(lo <= hi): - # print lo, hi - mid = (lo + hi) // 2 + left, right = midposition + 1, len(nums) - 1 + else: + 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 + right = mid - 1 else: - lo = mid + 1 - - return -1 - \ No newline at end of file + left = mid + 1 + return -1 \ 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/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/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" index 1df165c..6916af6 100644 --- "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.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.py" @@ -6,43 +6,51 @@ def searchRange(self, nums, target): :rtype: List[int] """ lo, hi = 0, len(nums) - 1 - #ҵһtarget - while(lo <= hi): + + while lo <= hi: + mid = (lo + hi) // 2 + # print lo, hi, mid, nums if nums[mid] == target: break - elif nums[mid] > target: - hi = mid - 1 - else: + elif nums[mid] < target: lo = mid + 1 - if lo > hi: # + else: + hi = mid - 1 + + if lo > hi: return [-1, -1] - - midtarget = mid - lo, hi = 0, mid - leftpos = 0 - while(lo <= hi): - # print lo, hi - if (hi >= 1 and nums[hi - 1] != target) or hi == 0: #ҵ߽ҵͷ - leftpos = hi - break + 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: - hi = mid - elif nums[mid] < target: + if nums[mid] < target: lo = mid + 1 - - rightpos = 0 - lo, hi = midtarget, len(nums) - 1 - while(lo <= hi): - if (lo <= len(nums) - 2 and nums[lo + 1] != target) or lo == len(nums) - 1: #ҵұ߽ҵͷ - rightpos = lo - break - mid = (lo + hi + 1) // 2 - if nums[mid] == target: - lo = mid - elif nums[mid] > target: + 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] + - return [leftpos, rightpos] - \ No newline at end of file + \ 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 ad0dcc8..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,12 +5,14 @@ def searchInsert(self, nums, target): :type target: int :rtype: int """ - if not nums: - return 0 - l = len(nums) - for index in range(l): - if nums[index] >= target: - return index - return index + 1 + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 - \ No newline at end of file + 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/0036.valid-sudoku/valid-sudoku.md b/0036.valid-sudoku/valid-sudoku.md deleted file mode 100644 index 19504c0..0000000 --- a/0036.valid-sudoku/valid-sudoku.md +++ /dev/null @@ -1,59 +0,0 @@ -

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

- -
    -
  1. Each row must contain the digits 1-9 without repetition.
  2. -
  3. Each column must contain the digits 1-9 without repetition.
  4. -
  5. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
  6. -
- -


-A partially filled sudoku which is valid.

- -

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

- -

Example 1:

- -
-Input:
-[
-  ["5","3",".",".","7",".",".",".","."],
-  ["6",".",".","1","9","5",".",".","."],
-  [".","9","8",".",".",".",".","6","."],
-  ["8",".",".",".","6",".",".",".","3"],
-  ["4",".",".","8",".","3",".",".","1"],
-  ["7",".",".",".","2",".",".",".","6"],
-  [".","6",".",".",".",".","2","8","."],
-  [".",".",".","4","1","9",".",".","5"],
-  [".",".",".",".","8",".",".","7","9"]
-]
-Output: true
-
- -

Example 2:

- -
-Input:
-[
-  ["8","3",".",".","7",".",".",".","."],
-  ["6",".",".","1","9","5",".",".","."],
-  [".","9","8",".",".",".",".","6","."],
-  ["8",".",".",".","6",".",".",".","3"],
-  ["4",".",".","8",".","3",".",".","1"],
-  ["7",".",".",".","2",".",".",".","6"],
-  [".","6",".",".",".",".","2","8","."],
-  [".",".",".","4","1","9",".",".","5"],
-  [".",".",".",".","8",".",".","7","9"]
-]
-Output: false
-Explanation: Same as Example 1, except with the 5 in the top left corner being 
-    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
-
- -

Note:

- -
    -
  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • -
  • Only the filled cells need to be validated according to the mentioned rules.
  • -
  • The given board contain only digits 1-9 and the character '.'.
  • -
  • The given board size is always 9x9.
  • -
diff --git a/0036.valid-sudoku/valid-sudoku.py b/0036.valid-sudoku/valid-sudoku.py deleted file mode 100644 index a65dde4..0000000 --- a/0036.valid-sudoku/valid-sudoku.py +++ /dev/null @@ -1,21 +0,0 @@ -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) - - 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)]: - # print board[i][j], row[i], column[j], squre[(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]) - # print row[1] - return True \ 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 19f59d5..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,23 +1,15 @@ -class Solution(object): - def isValidSudoku(self, board): - """ - :type board: List[List[str]] - :rtype: bool - """ - from collections import defaultdict +class Solution: + def isValidSudoku(self, board: List[List[str]]) -> bool: row = defaultdict(set) - column = defaultdict(set) - squre = 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)]: - # print board[i][j], row[i], column[j], 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]) - - return True \ No newline at end of file + col[j].add(board[i][j]) + square[(i // 3, j // 3)].add(board[i][j]) + return True diff --git a/0037.sudoku-solver/sudoku-solver.md b/0037.sudoku-solver/sudoku-solver.md deleted file mode 100644 index 827ae0c..0000000 --- a/0037.sudoku-solver/sudoku-solver.md +++ /dev/null @@ -1,25 +0,0 @@ -

Write a program to solve a Sudoku puzzle by filling the empty cells.

- -

A sudoku solution must satisfy all of the following rules:

- -
    -
  1. Each of the digits 1-9 must occur exactly once in each row.
  2. -
  3. Each of the digits 1-9 must occur exactly once in each column.
  4. -
  5. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
  6. -
- -

Empty cells are indicated by the character '.'.

- -


-A sudoku puzzle...

- -


-...and its solution numbers marked in red.

- -

Note:

- -
    -
  • The given board contain only digits 1-9 and the character '.'.
  • -
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • -
  • The given board size is always 9x9.
  • -
diff --git a/0037.sudoku-solver/sudoku-solver.py b/0037.sudoku-solver/sudoku-solver.py deleted file mode 100644 index f598eae..0000000 --- a/0037.sudoku-solver/sudoku-solver.py +++ /dev/null @@ -1,45 +0,0 @@ -class Solution(object): - def solveSudoku(self, board): - """ - :type board: List[List[str]] - :rtype: None Do not return anything, modify board in-place instead. - """ - from collections import defaultdict - row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set) - fill_list = [] - 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")) - else: - fill_list.append([i, j]) - - self.result = [] - def backtrack(idx): - if idx == len(fill_list): - for row1 in board: - self.result.append(row1[:]) - return - if not self.result: - i, j = fill_list[idx][0], fill_list[idx][1] - for digit in range(1, 10): - if str(digit) in row[i] or str(digit) in column[j] or str(digit) in squre[(i // 3, j // 3)]: - continue - - board[i][j] = str(digit) - row[i].add(board[i][j]) - column[j].add(board[i][j]) - squre[(i // 3, j // 3)].add(board[i][j]) - - backtrack(idx + 1) - - row[i].remove(board[i][j]) - column[j].remove(board[i][j]) - squre[(i // 3, j // 3)].remove(board[i][j]) - - backtrack(0) - for i in range(9): - for j in range(9): - board[i][j] = self.result[i][j] 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 f598eae..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,45 +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) - fill_list = [] + 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")) - else: - fill_list.append([i, j]) - - self.result = [] - def backtrack(idx): - if idx == len(fill_list): - for row1 in board: - self.result.append(row1[:]) + 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 == 9 and y == 0: + if self.isValidSudoku(board): + self.res = copy.deepcopy(board) return - if not self.result: - i, j = fill_list[idx][0], fill_list[idx][1] - for digit in range(1, 10): - if str(digit) in row[i] or str(digit) in column[j] or str(digit) in squre[(i // 3, j // 3)]: - continue + + 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 - board[i][j] = str(digit) - row[i].add(board[i][j]) - column[j].add(board[i][j]) - squre[(i // 3, j // 3)].add(board[i][j]) + 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) - backtrack(idx + 1) + dfs(0, 0) + board[:] = self.res - row[i].remove(board[i][j]) - column[j].remove(board[i][j]) - squre[(i // 3, j // 3)].remove(board[i][j]) - backtrack(0) + 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): - board[i][j] = self.result[i][j] + 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 \ No newline at end of file diff --git "a/0038.\346\212\245\346\225\260/0038-\346\212\245\346\225\260.py" "b/0038.\346\212\245\346\225\260/0038-\346\212\245\346\225\260.py" new file mode 100644 index 0000000..529e56d --- /dev/null +++ "b/0038.\346\212\245\346\225\260/0038-\346\212\245\346\225\260.py" @@ -0,0 +1,20 @@ +class Solution(object): + def countAndSay(self, n): + """ + :type n: int + :rtype: str + """ + record = ["1"] + for i in range(1, n): + pre = record[i - 1] + idx = 0 + tmp = "" + while idx < len(pre): + cnt = 1 + while(idx + 1 < len(pre) and pre[idx] == pre[idx + 1]): + idx += 1 + cnt += 1 + tmp += str(cnt) + pre[idx] + idx += 1 + record.append(tmp) + return record[-1] \ No newline at end of file diff --git "a/0039.\347\273\204\345\220\210\346\200\273\345\222\214/0039-\347\273\204\345\220\210\346\200\273\345\222\214.py" "b/0039.\347\273\204\345\220\210\346\200\273\345\222\214/0039-\347\273\204\345\220\210\346\200\273\345\222\214.py" index 71662df..20f1d1f 100644 --- "a/0039.\347\273\204\345\220\210\346\200\273\345\222\214/0039-\347\273\204\345\220\210\346\200\273\345\222\214.py" +++ "b/0039.\347\273\204\345\220\210\346\200\273\345\222\214/0039-\347\273\204\345\220\210\346\200\273\345\222\214.py" @@ -1,15 +1,26 @@ class Solution(object): def combinationSum(self, candidates, target): + """ + :type candidates: List[int] + :type target: int + :rtype: List[List[int]] + """ res = [] candidates.sort() + def dfs(t, tmp): + if t < 0: + return + if t == 0: + tmp.sort() + if tmp not in res: + res.append(tmp) + return + + for i, x in enumerate(candidates): + if t - x < 0: + break + dfs(t - x, tmp + [x]) - def backtrack(remain, temp, start): - if not remain: #remainΪ0 - res.append(temp[:]) - else: - for i, n in enumerate(candidates[start:]): - if n > remain: - break - backtrack(remain-n, temp+[n], start+i) - backtrack(target, [], 0) + + dfs(target, []) return res \ No newline at end of file diff --git "a/0040.\347\273\204\345\220\210\346\200\273\345\222\214II/0040-\347\273\204\345\220\210\346\200\273\345\222\214II.py" "b/0040.\347\273\204\345\220\210\346\200\273\345\222\214II/0040-\347\273\204\345\220\210\346\200\273\345\222\214II.py" index 294916c..c70ce0a 100644 --- "a/0040.\347\273\204\345\220\210\346\200\273\345\222\214II/0040-\347\273\204\345\220\210\346\200\273\345\222\214II.py" +++ "b/0040.\347\273\204\345\220\210\346\200\273\345\222\214II/0040-\347\273\204\345\220\210\346\200\273\345\222\214II.py" @@ -1,32 +1,25 @@ class Solution(object): - def combinationSum2(self, c, t): + def combinationSum2(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ - res = list() - l = len(c) - def dfs(start, tmp): - s = sum(tmp) - if s == t: - tt = sorted(tmp) - if tt not in res: - res.append(tt[:]) + res = [] + candidates.sort() + def dfs(start, t, tmp): + if t == 0: + tmp.sort() + if tmp not in res: + res.append(tmp) return - if start >= l: - return - - for i in range(start, l): - - if c[i] > t or s + c[i] > t: - continue - - tmp.append(c[i]) - dfs(i + 1, tmp) - tmp.pop() - - for i,x in enumerate(c): - dfs(i, list()) + for i in range(start, len(candidates)): + x = candidates[i] + if t - x < 0: + break + dfs(i + 1, t - x, tmp + [x]) + + + dfs(0, target, []) return res \ No newline at end of file diff --git a/0041.first-missing-positive/first-missing-positive.md b/0041.first-missing-positive/first-missing-positive.md deleted file mode 100644 index b1879c7..0000000 --- a/0041.first-missing-positive/first-missing-positive.md +++ /dev/null @@ -1,26 +0,0 @@ -

Given an unsorted integer array, find the smallest missing positive integer.

- -

Example 1:

- -
-Input: [1,2,0]
-Output: 3
-
- -

Example 2:

- -
-Input: [3,4,-1,1]
-Output: 2
-
- -

Example 3:

- -
-Input: [7,8,9,11,12]
-Output: 1
-
- -

Note:

- -

Your algorithm should run in O(n) time and uses constant extra space.

diff --git a/0041.first-missing-positive/first-missing-positive.py b/0041.first-missing-positive/first-missing-positive.py deleted file mode 100644 index abcda2e..0000000 --- a/0041.first-missing-positive/first-missing-positive.py +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def firstMissingPositive(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - # ڴ1~n֮Ƿŵnums[i - 1]λ - - for i in range(len(nums)): - while 1 <= nums[i] <= len(nums) and nums[i] != nums[nums[i] - 1]: - # nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i] - nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] - - for i, x in enumerate(nums): - if x != i + 1: - return i + 1 - - return len(nums) + 1 \ No newline at end of file diff --git "a/0041.\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260/0041-\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260.py" "b/0041.\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260/0041-\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260.py" index abcda2e..5e1ac10 100644 --- "a/0041.\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260/0041-\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260.py" +++ "b/0041.\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260/0041-\347\274\272\345\244\261\347\232\204\347\254\254\344\270\200\344\270\252\346\255\243\346\225\260.py" @@ -4,15 +4,15 @@ def firstMissingPositive(self, nums): :type nums: List[int] :rtype: int """ - # ڴ1~n֮Ƿŵnums[i - 1]λ - - for i in range(len(nums)): - while 1 <= nums[i] <= len(nums) and nums[i] != nums[nums[i] - 1]: - # nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i] - nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] - - for i, x in enumerate(nums): - if x != i + 1: - return i + 1 - - return len(nums) + 1 \ No newline at end of file + if not nums: + return 1 + max_element = max(nums) + if max_element < 0: + return 1 + i = 1 + while i < max_element: + # for i in range(1, max_element): + if i not in nums: + return i + i += 1 + return max_element + 1 \ No newline at end of file diff --git a/0042.trapping-rain-water/trapping-rain-water.md b/0042.trapping-rain-water/trapping-rain-water.md deleted file mode 100644 index abc422e..0000000 --- a/0042.trapping-rain-water/trapping-rain-water.md +++ /dev/null @@ -1,10 +0,0 @@ -

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

- -


-The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

- -

Example:

- -
-Input: [0,1,0,2,1,0,1,3,2,1,2,1]
-Output: 6
diff --git a/0042.trapping-rain-water/trapping-rain-water.py b/0042.trapping-rain-water/trapping-rain-water.py deleted file mode 100644 index a5926c1..0000000 --- a/0042.trapping-rain-water/trapping-rain-water.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/0042.\346\216\245\351\233\250\346\260\264/0042-\346\216\245\351\233\250\346\260\264.py" "b/0042.\346\216\245\351\233\250\346\260\264/0042-\346\216\245\351\233\250\346\260\264.py" index a5926c1..2d3dc43 100644 --- "a/0042.\346\216\245\351\233\250\346\260\264/0042-\346\216\245\351\233\250\346\260\264.py" +++ "b/0042.\346\216\245\351\233\250\346\260\264/0042-\346\216\245\351\233\250\346\260\264.py" @@ -4,28 +4,21 @@ def trap(self, height): :type height: List[int] :rtype: int """ + height = [0] + height + [0] 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(1, len(height)): + left_max[i] = max(height[i], left_max[i - 1]) + + for i in range(len(height) - 2, -1, -1): + # print i + right_max[i] = max(height[i], right_max[i + 1]) + # print left_max, right_max + # res = [0 for _ in height] + res = 0 + for i in range(1, len(height) - 2): + res += min(left_max[i], right_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 + return res \ No newline at end of file diff --git "a/0043.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/0043-\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230.py" "b/0043.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/0043-\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230.py" index 7b07eb5..d1ddced 100644 --- "a/0043.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/0043-\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230.py" +++ "b/0043.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/0043-\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230.py" @@ -5,65 +5,68 @@ def multiply(self, num1, num2): :type num2: str :rtype: str """ - if len(num1) < len(num2): - num1, num2 = num2, num1 - if num1 == "0" or num2 == "0": + if num1 == "0" or num2 == "0": # return "0" - num2 = num2[::-1] - tmp, res = [],[] - for i, char in enumerate(num2): - tmp = self.stringMultiDigit(num1, int(char)) + "0" * i #㵱ǰλ˷Ľעĩβ0 - res = self.stringPlusString(res, tmp) #ϵǰλ + l1, l2 = len(num1), len(num2) + if l1 < l2: + num1, num2 = num2, num1 #num1ʼձnum2 + l1, l2 = l2, l1 - return "".join(res) + num2 = num2[::-1] + res = "0" + for i, digit in enumerate(num2): + tmp = self.StringMultiplyDigit(num1, int(digit)) + "0" * i #num1num2ĵǰλij˻ + res = self.StringPlusString(res, tmp) #restmpĺ + + return res - def stringMultiDigit(self,s, n): #һַһֵij˻ ַ - s = s[::-1] - l = [] - for char in s: - l.append(int(char)) - - for i, char in enumerate(l): - l[i] *= n + def StringMultiplyDigit(self,string, n): + #Ĺǣһַһij˻ַ + #Ϊ "123", 3 "369" + s = string[::-1] + res = [] + for i, char in enumerate(s): + num = int(char) + res.append(num * n) + res = self.CarrySolver(res) + res = res[::-1] + return "".join(str(x) for x in res) + + def CarrySolver(self, nums): + #Ĺǣеÿһλýλ + #[15, 27, 12], [5, 8, 4, 1] + i = 0 + while i < len(nums): + if nums[i] >= 10: + carrier = nums[i] // 10 + if i == len(nums) - 1: + nums.append(carrier) + else: + nums[i + 1] += carrier + nums[i] %= 10 + i += 1 + + return nums + + def StringPlusString(self, s1, s2): + #Ĺǣַĺ + #Ϊ123 456, Ϊ"579" + l1, l2 = len(s1), len(s2) + if l1 < l2: + s1, s2 = s2, s1 + l1, l2 = l2, l1 + s1 = [int(x) for x in s1] + s2 = [int(x) for x in s2] + s1, s2 = s1[::-1], s2[::-1] + for i, digit in enumerate(s2): + s1[i] += s2[i] - for i, char in enumerate(l): - while(l[i] > 9): - tmp = l[i] // 10 - l[i] -= tmp * 10 - if i == len(l) - 1: - l.append(0) - l[i + 1] += tmp - - return "".join(str(char) for char in l[::-1]) + s1 = self.CarrySolver(s1) + s1 = s1[::-1] + return "".join(str(x) for x in s1) - def stringPlusString(self,s1, s2): #ַӷַ - # print s1, s2 - s1, s2 = s1[::-1], s2[::-1] - l1, l2 = [], [] - for char in s1: - l1.append(int(char)) - for char in s2: - l2.append(int(char)) - # tmp = [] - if len(l1) < len(l2): - l1, l2 = l2, l1 - i = 0 - for i in range(len(l2)): - l1[i] += l2[i] - if l1[i] > 9: - l1[i] -= 10 - if i == len(l1) - 1: - l1.append(0) - l1[i + 1] += 1 - i += 1 - if i < len(l1) - 1: #һλܵĽλ - if l1[i] > 9: - l1[i] -= 10 - if i == len(l1) - 1: - l1.append(0) - l1[i + 1] += 1 - - return "".join(str(char) for char in l1[::-1]) - \ No newline at end of file + + + \ No newline at end of file diff --git "a/0044.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/0044-\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215.py" "b/0044.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/0044-\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215.py" new file mode 100644 index 0000000..6e5f9dd --- /dev/null +++ "b/0044.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/0044-\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215.py" @@ -0,0 +1,35 @@ +class Solution(object): + def isMatch(self, s, p): + """ + :type s: str + :type p: str + :rtype: bool + """ + tmpp = "" + for i, char in enumerate(p): + if p[i] != "*": + tmpp += p[i] + else: + if i == 0 or p[i] != tmpp[-1]: + tmpp += "*" + p = tmpp[:] + + memo = {} + def find(i, j): + if (i, j) not in memo: + ans = False + if j == len(p): + ans = i == len(s) + elif i < len(s): + if p[j] in [s[i], "?"]: + ans = find(i + 1, j + 1) + elif p[j] == "*": + ans = find(i, j + 1) or find(i + 1, j) + elif p[j] == "*": + ans = find(i, j + 1) + memo[i, j] = ans + return memo[i, j] + + return find(0, 0) + + \ 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/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227.py" "b/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227.py" index 457bb71..f2a991c 100644 --- "a/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227.py" +++ "b/0046.\345\205\250\346\216\222\345\210\227/0046-\345\205\250\346\216\222\345\210\227.py" @@ -1,22 +1,16 @@ - class Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ - res, l = list(), len(nums) - def dfs(n, tmp): - if len(tmp) == l: - res.append(tmp[:]) - return - for i, x in enumerate(n): - tmp.append(x) - dfs(n[:i] + n[i + 1:], tmp) - tmp.pop() - - - for i, x in enumerate(nums): - dfs(nums[:i] + nums[i + 1:], [x]) + 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/0047.\345\205\250\346\216\222\345\210\227II/0047-\345\205\250\346\216\222\345\210\227II.py" "b/0047.\345\205\250\346\216\222\345\210\227II/0047-\345\205\250\346\216\222\345\210\227II.py" index fcb677f..08a58bd 100644 --- "a/0047.\345\205\250\346\216\222\345\210\227II/0047-\345\205\250\346\216\222\345\210\227II.py" +++ "b/0047.\345\205\250\346\216\222\345\210\227II/0047-\345\205\250\346\216\222\345\210\227II.py" @@ -4,19 +4,16 @@ def permuteUnique(self, nums): :type nums: List[int] :rtype: List[List[int]] """ - res, l = list(), len(nums) - def dfs(n, tmp): - if len(tmp) == l: - if tmp not in res: - res.append(tmp[:]) - return - for i, x in enumerate(n): - tmp.append(x) - dfs(n[:i] + n[i + 1:], tmp) - tmp.pop() - - - for i, x in enumerate(nums): - dfs(nums[:i] + nums[i + 1:], [x]) - + nums.sort() + res = [] + record = dict() + def dfs(tmp, nums): + if not nums: + res.append(tmp) + + for i, x in enumerate(nums): + if i == 0 or nums[i] != nums[i - 1]: + 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/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/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..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.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/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/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" index e9abe00..6998b4a 100644 --- "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.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.py" @@ -4,13 +4,15 @@ def groupAnagrams(self, strs): :type strs: List[str] :rtype: List[List[str]] """ - hashmap = dict() - for s in strs: - t = "".join(sorted(s)) - - if t in hashmap: - hashmap[t].append(s) + record = dict() + + for word in strs: + tmp = tuple(sorted(word)) + # print tmp + if tmp in record: + record[tmp].append(word) else: - hashmap[t] = [s] + record[tmp] = [word] + return [val for key, val in record.items()] - return hashmap.values() \ No newline at end of file + \ No newline at end of file diff --git a/0050.Pow(x,n)/0050-Pow(x,n).py b/0050.Pow(x,n)/0050-Pow(x,n).py index ef94423..47442e6 100644 --- a/0050.Pow(x,n)/0050-Pow(x,n).py +++ b/0050.Pow(x,n)/0050-Pow(x,n).py @@ -5,13 +5,4 @@ def myPow(self, x, n): :type n: int :rtype: float """ - i = abs(n) - res = 1.0 - while(i != 0): - if i % 2: - res *= x - x *= x - # print i, res - i /= 2 - return res if n > 0 else 1/res - \ No newline at end of file + return x ** n \ No newline at end of file diff --git "a/0051.N\347\232\207\345\220\216/0051-N\347\232\207\345\220\216.py" "b/0051.N\347\232\207\345\220\216/0051-N\347\232\207\345\220\216.py" index 76f3f05..c718df3 100644 --- "a/0051.N\347\232\207\345\220\216/0051-N\347\232\207\345\220\216.py" +++ "b/0051.N\347\232\207\345\220\216/0051-N\347\232\207\345\220\216.py" @@ -4,51 +4,40 @@ def solveNQueens(self, n): :type n: int :rtype: List[List[str]] """ - board = ["." * n for _ in range(n)] - + if n <= 0: + return [] res = [] - def checkRowAndCol(row, col): + def dfs(start, tmp): + if start == n: + res.append(tmp[:]) + return for i in range(n): - if (board[row][i] == "Q" and i != col) or (board[i][col] == "Q" and i != row): #˺ͻ - return False - return True - - def checkDoubleDio(row, col): - add = row + col - sub = col - row - - for x in range(n): - if x == row: # - continue - y = add - x - # print add,sub, x, y - if y >= 0 and y < n and x >= 0 and x < n and board[x][y] == "Q": #/ͻ - return False - - y = sub + x - if y >= 0 and y < n and x >= 0 and x < n and board[x][y] == "Q": #\ͻ - return False - - return True - + tmp.append("." * i + "Q" + "." * (n - i - 1)) + if self.isValid(tmp, start, i, n): + dfs(start + 1, tmp) + tmp.pop() + dfs(0, []) + return res + + def isValid(self, tmp, x0, y0, n): + self.add = x0 + y0 + self.sub = y0 - x0 - def dfs(row, col): - if col >= n: #λö - return - board[row] = "." * col + "Q" + (n - col - 1) * "." #ѻʺ(row, col)λ - if checkRowAndCol(row, col) and checkDoubleDio(row, col): #ûзͻ - if row == n - 1: #µһʺ - # print board - res.append(board[:]) #ҵһн - else: - for i in range(n): #ҪŸĻʺһзŰ - dfs(row + 1, i) - - board[row] = "." * n #ݣѱзµĻʺû - # dfs(row, col + 1) - return - - for i in range(n): - dfs(0, i) + for i in range(len(tmp)): #ж + if i == x0: + continue + if tmp[i][y0] == "Q": + return False - return res \ No newline at end of file + for i in range(min(n, len(tmp))): #ж\ + if i == x0: + continue + # print i, j + j = self.add - i + # print i, j, self.add, tmp + if 0 <= j < n and tmp[i][j] == "Q": + return False + j = i + self.sub + if 0 <= j < n and tmp[i][j] == "Q": + return False + return True \ No newline at end of file diff --git "a/0052.N\347\232\207\345\220\216II/0052-N\347\232\207\345\220\216II.py" "b/0052.N\347\232\207\345\220\216II/0052-N\347\232\207\345\220\216II.py" index 2e04536..68308b8 100644 --- "a/0052.N\347\232\207\345\220\216II/0052-N\347\232\207\345\220\216II.py" +++ "b/0052.N\347\232\207\345\220\216II/0052-N\347\232\207\345\220\216II.py" @@ -4,52 +4,40 @@ def totalNQueens(self, n): :type n: int :rtype: int """ - - board = ["." * n for _ in range(n)] - # print board + if n <= 0: + return [] self.res = 0 - def checkRowAndCol(row, col): + def dfs(start, tmp): + if start == n: + self.res += 1 + return for i in range(n): - if (board[row][i] == "Q" and i != col) or (board[i][col] == "Q" and i != row): - return False - return True - - def checkDoubleDio(row, col): - add = row + col - sub = col - row - - for x in range(n): - if x == row: - continue - y = add - x - # print add,sub, x, y - if y >= 0 and y < n and x >= 0 and x < n and board[x][y] == "Q": - return False - - y = sub + x - if y >= 0 and y < n and x >= 0 and x < n and board[x][y] == "Q": - return False - - return True - + tmp.append("." * i + "Q" + "." * (n - i - 1)) + if self.isValid(tmp, start, i, n): + dfs(start + 1, tmp) + tmp.pop() + dfs(0, []) + return self.res + + def isValid(self, tmp, x0, y0, n): + self.add = x0 + y0 + self.sub = y0 - x0 - def dfs(row, col): - if col >= n: - return - board[row] = "." * col + "Q" + (n - col - 1) * "." - if checkRowAndCol(row, col) and checkDoubleDio(row, col): - if row == n - 1: - # print board - self.res += 1 - else: - for i in range(n): - dfs(row + 1, i) - - board[row] = "." * n - # dfs(row, col + 1) - return - - for i in range(n): - dfs(0, i) + for i in range(len(tmp)): #ж + if i == x0: + continue + if tmp[i][y0] == "Q": + return False - return self.res \ No newline at end of file + for i in range(min(n, len(tmp))): #ж\ + if i == x0: + continue + # print i, j + j = self.add - i + # print i, j, self.add, tmp + if 0 <= j < n and tmp[i][j] == "Q": + return False + j = i + self.sub + if 0 <= j < n and tmp[i][j] == "Q": + return False + return True \ No newline at end of file diff --git "a/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/0053-\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.py" "b/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/0053-\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.py" index c890aaf..4358bfe 100644 --- "a/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/0053-\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.py" +++ "b/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/0053-\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.py" @@ -6,10 +6,14 @@ def maxSubArray(self, nums): """ if not nums: return 0 - dp = [nums[0]] - res = dp[0] - for i in range(1, len(nums)): - dp.append(max(dp[i-1] + nums[i], nums[i])) - if dp[-1] > res: - res = dp[-1] - return res \ No newline at end of file + 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/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/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/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" index 4b07771..33e693b 100644 --- "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.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.py" @@ -4,27 +4,39 @@ def spiralOrder(self, matrix): :type matrix: List[List[int]] :rtype: List[int] """ - m = len(matrix) - if m == 0: + if not matrix or not matrix[0]: return [] - n = len(matrix[0]) - if n == 0: - return [] - r, i, j, di, dj = list(), 0, 0, 0, 1 - - for _ in range(m * n): - r.append(matrix[i][j]) - matrix[i][j] = None - if matrix[(i + di) % m][(j + dj) % n] == None: #˸ת - di, dj = dj, -di #0 1 1 0, 1 0 0 -1, 0 -1 -1, 0, -1 0 0 1 - i += di - j += dj - - return r - - - - - - - \ No newline at end of file + 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/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/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" index 8198fc2..b6283c9 100644 --- "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.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.py" @@ -4,10 +4,9 @@ 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 + 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/0056.\345\220\210\345\271\266\345\214\272\351\227\264/0056-\345\220\210\345\271\266\345\214\272\351\227\264.py" "b/0056.\345\220\210\345\271\266\345\214\272\351\227\264/0056-\345\220\210\345\271\266\345\214\272\351\227\264.py" index ea26cc6..c005fbe 100644 --- "a/0056.\345\220\210\345\271\266\345\214\272\351\227\264/0056-\345\220\210\345\271\266\345\214\272\351\227\264.py" +++ "b/0056.\345\220\210\345\271\266\345\214\272\351\227\264/0056-\345\220\210\345\271\266\345\214\272\351\227\264.py" @@ -1,29 +1,24 @@ -# Definition for an interval. -# class Interval(object): -# def __init__(self, s=0, e=0): -# self.start = s -# self.end = e - class Solution(object): def merge(self, intervals): """ - :type intervals: List[Interval] - :rtype: List[Interval] + :type intervals: List[List[int]] + :rtype: List[List[int]] """ - if not intervals: - return [] - intervals = sorted(intervals,key=lambda x:x.start) + if not intervals or not intervals[0]: + return intervals + + intervals = sorted(intervals, key = lambda x:x[0]) + res = [] - left = intervals[0].start - right = intervals[0].end - for item in intervals: - if item.start <= right: - right = max(right, item.end) + 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([left, right]) - left = item.start - right = item.end - res.append([left, right]) - - return res - \ No newline at end of file + res.append([start, end]) + start, end = s, e + + res.append([start, end]) + return res \ No newline at end of file diff --git "a/0057.\346\217\222\345\205\245\345\214\272\351\227\264/0057-\346\217\222\345\205\245\345\214\272\351\227\264.py" "b/0057.\346\217\222\345\205\245\345\214\272\351\227\264/0057-\346\217\222\345\205\245\345\214\272\351\227\264.py" index 508ed54..0c3824e 100644 --- "a/0057.\346\217\222\345\205\245\345\214\272\351\227\264/0057-\346\217\222\345\205\245\345\214\272\351\227\264.py" +++ "b/0057.\346\217\222\345\205\245\345\214\272\351\227\264/0057-\346\217\222\345\205\245\345\214\272\351\227\264.py" @@ -1,42 +1,28 @@ -# Definition for an interval. -# class Interval(object): -# def __init__(self, s=0, e=0): -# self.start = s -# self.end = e - class Solution(object): def insert(self, intervals, newInterval): """ - :type intervals: List[Interval] - :type newInterval: Interval - :rtype: List[Interval] + :type intervals: List[List[int]] + :type newInterval: List[int] + :rtype: List[List[int]] """ - # tmp = Interval(newInterval[0], newInterval[1]) intervals.append(newInterval) - # intervals[-1] = newInterval - # print type(intervals[0]), type(tmp) return self.merge(intervals) - def merge(self, intervals): """ - :type intervals: List[Interval] - :rtype: List[Interval] + :type intervals: List[List[int]] + :rtype: List[List[int]] """ - if not intervals: - return [] - intervals = sorted(intervals,key=lambda x:x[0]) + return intervals + intervals = sorted(intervals, key = lambda x: x[0]) + start, end = intervals[0][0], intervals[0][1] + res = [] - left = intervals[0][0] - right = intervals[0][1] - for item in intervals: - if item[0] <= right: - right = max(right, item[1]) + for i, interval in enumerate(intervals): + if interval[0] > end: + res.append([start, end]) + start, end = interval[0], interval[1] else: - res.append([left, right]) - left = item[0] - right = item[1] - res.append([left, right]) - - return res - \ No newline at end of file + end = max(end, interval[1]) + res.append([start, end]) + return res \ 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/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/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" index 7e36392..2ef7de1 100644 --- "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.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.py" @@ -4,9 +4,11 @@ def lengthOfLastWord(self, s): :type s: str :rtype: int """ - if len(s) <= 0 : - return 0 - s = s.rstrip(" ") - print s,s.split(" ") - return len(s.split(" ")[-1]) - \ No newline at end of file + 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/0059.\350\236\272\346\227\213\347\237\251\351\230\265II/0059-\350\236\272\346\227\213\347\237\251\351\230\265II.py" "b/0059.\350\236\272\346\227\213\347\237\251\351\230\265II/0059-\350\236\272\346\227\213\347\237\251\351\230\265II.py" index c4b4cc0..7789cb1 100644 --- "a/0059.\350\236\272\346\227\213\347\237\251\351\230\265II/0059-\350\236\272\346\227\213\347\237\251\351\230\265II.py" +++ "b/0059.\350\236\272\346\227\213\347\237\251\351\230\265II/0059-\350\236\272\346\227\213\347\237\251\351\230\265II.py" @@ -4,16 +4,35 @@ def generateMatrix(self, n): :type n: int :rtype: List[List[int]] """ - - res = [[0 for i in range(n)] for j in range(n)] - - i, j, di, dj = 0, 0, 0, 1 - for number in range(1, n ** 2 + 1): - res[i][j] = number - if res[(i + di) % n][(j + dj) % n] != 0: #Ҫת - di, dj = dj, - di - i += di - j += dj - - return res - \ No newline at end of file + i, j = 0, 0 + state = "right" + cnt = 0 + res = [[0 for _ in range(n)] for _ in range(n)] + while(cnt < n * n): + cnt += 1 + res[i][j] = cnt + if state == "right": + j += 1 + if j == n or res[i][j] != 0: + i += 1 + j -= 1 + state = "down" + elif state == "down": + i += 1 + if i == n or res[i][j] != 0: + i -= 1 + j -= 1 + state = "left" + elif state == "left": + j -= 1 + if j == -1 or res[i][j] != 0: + j += 1 + i -= 1 + state = "up" + elif state == "up": + i -= 1 + if i == -1 or res[i][j] != 0: + i += 1 + j += 1 + state = "right" + return res \ No newline at end of file diff --git "a/0060.\347\254\254k\344\270\252\346\216\222\345\210\227/0060-\347\254\254k\344\270\252\346\216\222\345\210\227.py" "b/0060.\347\254\254k\344\270\252\346\216\222\345\210\227/0060-\347\254\254k\344\270\252\346\216\222\345\210\227.py" index 306e3dc..ac61812 100644 --- "a/0060.\347\254\254k\344\270\252\346\216\222\345\210\227/0060-\347\254\254k\344\270\252\346\216\222\345\210\227.py" +++ "b/0060.\347\254\254k\344\270\252\346\216\222\345\210\227/0060-\347\254\254k\344\270\252\346\216\222\345\210\227.py" @@ -1,4 +1,3 @@ -import math class Solution(object): def getPermutation(self, n, k): """ @@ -6,13 +5,24 @@ def getPermutation(self, n, k): :type k: int :rtype: str """ - digit = [i for i in range(1, n + 1)] #1 ~ nб - res = "" - while n > 0: - tmp = math.factorial(n - 1) #һж - idx = (k - 1) / tmp #Ktmpռıȷһλ - k -= idx * tmp #һλȷ֮ˢk - res += str(digit[idx]) - digit.pop(idx) - n -= 1 - return res \ No newline at end of file + fac = [1] + for i in range(2, n + 1): + fac.append(fac[-1] * i) + digits = [i for i in range(1, n + 1)] + + self.res = "" + def dfs(left_digit, tmp, kk): + if left_digit == 0: + self.res = tmp[:] + return + for digit in digits: + kk -= fac[left_digit - 2] + if kk <= 0: + kk += fac[left_digit - 2] + fac.pop() + digits.remove(digit) + dfs(left_digit - 1, tmp + str(digit), kk) + break + + dfs(n, "", k) + return self.res \ No newline at end of file diff --git "a/0061.\346\227\213\350\275\254\351\223\276\350\241\250/0061-\346\227\213\350\275\254\351\223\276\350\241\250.py" "b/0061.\346\227\213\350\275\254\351\223\276\350\241\250/0061-\346\227\213\350\275\254\351\223\276\350\241\250.py" index 0ebda46..d44f254 100644 --- "a/0061.\346\227\213\350\275\254\351\223\276\350\241\250/0061-\346\227\213\350\275\254\351\223\276\350\241\250.py" +++ "b/0061.\346\227\213\350\275\254\351\223\276\350\241\250/0061-\346\227\213\350\275\254\351\223\276\350\241\250.py" @@ -11,30 +11,22 @@ def rotateRight(self, head, k): :type k: int :rtype: ListNode """ - if k <= 0: + p = head + l = [] + while p: + l.append(p.val) + p = p.next + if len(l) <= 1: return head - l = 0 - ptr = head - while(ptr != None): - ptr = ptr.next - l += 1 - # print l - if l <= 1: - return head - k = k % l - if k == 0: + k = k % len(l) + if not k: return head - fast = head - slow = head - while(k != 0): - fast = fast.next - k-= 1 - while(fast.next != None): - fast = fast.next - slow = slow.next - # print fast.val, slow.val - ptr = slow.next - fast.next = head - slow.next = None - return ptr \ No newline at end of file + l = l[-k:] + l[:-k] + print l + newhead = ListNode(-1) + p = newhead + for item in l: + p.next = ListNode(item) + p = p.next + return newhead.next \ No newline at end of file diff --git "a/0062.\344\270\215\345\220\214\350\267\257\345\276\204/0062-\344\270\215\345\220\214\350\267\257\345\276\204.py" "b/0062.\344\270\215\345\220\214\350\267\257\345\276\204/0062-\344\270\215\345\220\214\350\267\257\345\276\204.py" index 75c7dd1..8c89221 100644 --- "a/0062.\344\270\215\345\220\214\350\267\257\345\276\204/0062-\344\270\215\345\220\214\350\267\257\345\276\204.py" +++ "b/0062.\344\270\215\345\220\214\350\267\257\345\276\204/0062-\344\270\215\345\220\214\350\267\257\345\276\204.py" @@ -5,17 +5,14 @@ def uniquePaths(self, m, n): :type n: int :rtype: int """ - # C(m + n - 2) (m - 1) - k = m + n - 2 - t = m - 1 + dp = [[0 for _ in range(m)]for _ in range(n)] - up = 1 - for i in range(0, t): - up *= k - i - - down = 1 - for i in range(1, m): - down *= i - - # print up, down - return up // down \ No newline at end of file + for i in range(m): + dp[0][i] = 1 + for j in range(n): + dp[j][0] = 1 + + for i in range(1, n): + for j in range(1, m): + dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + return dp[-1][-1] \ No newline at end of file diff --git "a/0063.\344\270\215\345\220\214\350\267\257\345\276\204II/0063-\344\270\215\345\220\214\350\267\257\345\276\204II.py" "b/0063.\344\270\215\345\220\214\350\267\257\345\276\204II/0063-\344\270\215\345\220\214\350\267\257\345\276\204II.py" new file mode 100644 index 0000000..53bf790 --- /dev/null +++ "b/0063.\344\270\215\345\220\214\350\267\257\345\276\204II/0063-\344\270\215\345\220\214\350\267\257\345\276\204II.py" @@ -0,0 +1,28 @@ +class Solution(object): + def uniquePathsWithObstacles(self, obstacleGrid): + """ + :type obstacleGrid: List[List[int]] + :rtype: int + """ + if not obstacleGrid or not obstacleGrid[0] or obstacleGrid[0][0] == 1 or obstacleGrid[-1][-1] == 1: + return 0 + m, n = len(obstacleGrid[0]), len(obstacleGrid) + dp = [[0 for _ in range(m)]for _ in range(n)] + + for i in range(m): + if obstacleGrid[0][i] != 1: + dp[0][i] = 1 + else: + break + for j in range(n): + if obstacleGrid[j][0] != 1: + + dp[j][0] = 1 + else: + break + + for i in range(1, n): + for j in range(1, m): + if obstacleGrid[i][j] != 1: + dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + return dp[-1][-1] \ 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 df7bfe7..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" @@ -4,21 +4,18 @@ def minPathSum(self, grid): :type grid: List[List[int]] :rtype: int """ - #dp[m][n] = min(dp[m - 1][n], dp[m][n - 1]) + grid[m][n] - if not grid: + if not grid or not grid[0]: return 0 - m = len(grid) - n = len(grid[0]) - dp = [[0] * (n)] * (m) - for i in range(m): - for j in range(n): - # print dp, i, j - if not i and not j: - dp[i][j] = grid[i][j] - elif i and not j: - dp[i][j] = dp[i-1][j] + grid[i][j] - elif not i and j: - dp[i][j] = dp[i][j-1] + grid[i][j] - else: - dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] - return dp[m-1][n-1] \ No newline at end of file + m, n = len(grid), len(grid[0]) + + 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/0066.\345\212\240\344\270\200/0066-\345\212\240\344\270\200.py" "b/0066.\345\212\240\344\270\200/0066-\345\212\240\344\270\200.py" index 0e3ec96..2811cf8 100644 --- "a/0066.\345\212\240\344\270\200/0066-\345\212\240\344\270\200.py" +++ "b/0066.\345\212\240\344\270\200/0066-\345\212\240\344\270\200.py" @@ -4,21 +4,14 @@ def plusOne(self, digits): :type digits: List[int] :rtype: List[int] """ - index = 0 - if digits[-1] != 9: - digits[-1] += 1 - return digits - else: - for index in range(len(digits)-1, -1,-1): - if digits[index] != 9: - break - if digits[index] != 9: - digits[index] += 1 - for i in range(index+ 1, len(digits)): - digits[i] = 0 - return digits - else: - res = [1] - for i in range(0,len(digits)): - res.append(0) - return res \ No newline at end of file + l = digits[::-1] + + l[0] += 1 + for i in range(len(l)): + if l[i] > 9: + l[i] -= 10 + if i != len(l) - 1: + l[i + 1] += 1 + else: + l.append(1) + return l[::-1] \ No newline at end of file diff --git "a/0067.\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/0067-\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214.py" "b/0067.\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/0067-\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214.py" index 4078a02..9daca05 100644 --- "a/0067.\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/0067-\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214.py" +++ "b/0067.\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214/0067-\344\272\214\350\277\233\345\210\266\346\261\202\345\222\214.py" @@ -5,4 +5,26 @@ def addBinary(self, a, b): :type b: str :rtype: str """ - return bin(int(a,2) + int(b,2))[2:] \ No newline at end of file + l1, l2 = len(a), len(b) + if l1 < l2: + l1, l2 = l2, l1 + a, b = b, a + la, lb = [], [] + for char in a: + la.append(int(char)) + for char in b: + lb.append(int(char)) + la, lb = la[::-1], lb[::-1] + + for i in range(l1): + if i < l2: + la[i] += lb[i] + if la[i] > 1: + la[i] -= 2 + if i != l1 - 1: + la[i + 1] += 1 + else: + la.append(1) + + return "".join(str(x) for x in la[::-1]) + \ No newline at end of file diff --git "a/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" "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" new file mode 100644 index 0000000..e4e3e3c --- /dev/null +++ "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" @@ -0,0 +1,17 @@ +class Solution(object): + def mySqrt(self, x): + """ + :type x: int + :rtype: int + """ + left, right = 1, x + while left <= right: + mid = (left + right) // 2 + s = mid ** 2 + if s == x: + return mid + elif s < x: + left = mid + 1 + elif s > x: + right = mid - 1 + return left - 1 \ No newline at end of file diff --git "a/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/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" index e69de29..e4e3e3c 100644 --- "a/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/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,17 @@ +class Solution(object): + def mySqrt(self, x): + """ + :type x: int + :rtype: int + """ + left, right = 1, x + while left <= right: + mid = (left + right) // 2 + s = mid ** 2 + if s == x: + return mid + elif s < x: + left = mid + 1 + elif s > x: + right = mid - 1 + return left - 1 \ No newline at end of file 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/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257.py" "b/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257.py" index 46b3b2e..88ab0fc 100644 --- "a/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257.py" +++ "b/0070.\347\210\254\346\245\274\346\242\257/0070-\347\210\254\346\245\274\346\242\257.py" @@ -1,22 +1,19 @@ class Solution(object): - def climbStairs(self, n): """ :type n: int :rtype: int """ - if n <= 1: - return 1 - if n == 2: - return 2 - a = 1 - b = 2 - count = 3 - while count<=n: - c = a + b - a = b - b = c - count += 1 - return c - - + 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/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/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" index 658d06a..d008010 100644 --- "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.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.py" @@ -4,16 +4,12 @@ def simplifyPath(self, path): :type path: str :rtype: str """ - if not path: - return "" - pathList = path.split("/") + l = path.split("/") stack = [] - for path in pathList: - if path == "." or path == "": - continue - if path == "..": - if stack: - stack.pop() - else: - stack.append(path) + 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/0072.\347\274\226\350\276\221\350\267\235\347\246\273/0072-\347\274\226\350\276\221\350\267\235\347\246\273.py" "b/0072.\347\274\226\350\276\221\350\267\235\347\246\273/0072-\347\274\226\350\276\221\350\267\235\347\246\273.py" index 5fb05aa..84662af 100644 --- "a/0072.\347\274\226\350\276\221\350\267\235\347\246\273/0072-\347\274\226\350\276\221\350\267\235\347\246\273.py" +++ "b/0072.\347\274\226\350\276\221\350\267\235\347\246\273/0072-\347\274\226\350\276\221\350\267\235\347\246\273.py" @@ -5,21 +5,21 @@ def minDistance(self, word1, word2): :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)] + #dp[i][j]ʾword1[:i + 1]word2[:j + 1]Ľ + l1, l2 = len(word1), len(word2) + dp = [[0 for _ in range(l2 + 1)] for _ in range(l1 + 1)] - for i in range(m + 1): + for i in range(l1 + 1): dp[i][0] = i + for j in range(l2 + 1): + dp[0][j] = j - for i in range(n + 1): - dp[0][i] = i - - for i in range(1, m + 1): - for j in range(1, n + 1): + for i in range(1, l1 + 1): + for j in range(1, l2 + 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 + dp[i][j] = 1 + min(dp[i - 1][j], dp[i - 1][j - 1], dp[i][j - 1]) + return dp[l1][l2] + + \ 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/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/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" index fd3f47b..f33d6de 100644 --- "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.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.py" @@ -2,23 +2,23 @@ class Solution(object): def setZeroes(self, matrix): """ :type matrix: List[List[int]] - :rtype: void Do not return anything, modify matrix in-place instead. + :rtype: None Do not return anything, modify matrix in-place instead. """ - self.matrix = matrix - m = len(matrix) - n = len(matrix[0]) - l_m = [1] * m - l_n = [1] * n + 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): - # print matrix[i][j], i, j if matrix[i][j] == 0: - l_m[i] = 0 - l_n[j] = 0 - # print l_m, l_n + 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): - # print i,j - if l_m[i] == 0 or l_n[j] == 0: - matrix[i][j] = 0 - # print matrix + if matrix[i][j] == "0": + matrix[i][j] = 0 + \ No newline at end of file diff --git "a/0074.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/0074-\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265.py" "b/0074.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/0074-\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265.py" index e36dfbb..a298ed2 100644 --- "a/0074.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/0074-\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265.py" +++ "b/0074.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265/0074-\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265.py" @@ -5,23 +5,19 @@ def searchMatrix(self, matrix, target): :type target: int :rtype: bool """ - m = len(matrix) - if m == 0: - return False - n = len(matrix[0]) - if n == 0: + if not matrix or not matrix[0]: return False + m, n = len(matrix), len(matrix[0]) - x, y = m - 1, 0 - while(1): - if x < 0 or x >= m or y <0 or y >= n: - break - val = matrix[x][y] - if val == target: - return True - elif val > target: - x -= 1 - elif val target: + i -= 1 + else: + return False \ No newline at end of file diff --git "a/0075.\351\242\234\350\211\262\345\210\206\347\261\273/0075-\351\242\234\350\211\262\345\210\206\347\261\273.py" "b/0075.\351\242\234\350\211\262\345\210\206\347\261\273/0075-\351\242\234\350\211\262\345\210\206\347\261\273.py" index f91676d..45ee9b3 100644 --- "a/0075.\351\242\234\350\211\262\345\210\206\347\261\273/0075-\351\242\234\350\211\262\345\210\206\347\261\273.py" +++ "b/0075.\351\242\234\350\211\262\345\210\206\347\261\273/0075-\351\242\234\350\211\262\345\210\206\347\261\273.py" @@ -2,31 +2,19 @@ class Solution(object): def sortColors(self, nums): """ :type nums: List[int] - :rtype: void Do not return anything, modify nums in-place instead. + :rtype: None Do not return anything, modify nums in-place instead. """ - b = 0 - r = 0 - w = 0 - for item in nums: - if item == 0: - r+= 1 - if item == 1: - w += 1 - if item == 2: - b += 1 - for index,item in enumerate(nums): - if r!= 0: - nums[index] = 0 - r -= 1 - continue - if w!= 0: - nums[index] = 1 - w -= 1 - continue - if b!= 0: - nums[index] = 2 - b -= 1 - continue - # return nums - - \ No newline at end of file + lo, hi = 0, len(nums) - 1 + i = 0 + while i <= hi: + x = nums[i] + if x == 0: + nums[lo], nums[i] = nums[i], nums[lo] + lo += 1 + i += 1 + elif x == 2: + nums[hi], nums[i] = nums[i], nums[hi] + hi -= 1 + else: + i += 1 + \ No newline at end of file diff --git "a/0077.\347\273\204\345\220\210/0077-\347\273\204\345\220\210.py" "b/0077.\347\273\204\345\220\210/0077-\347\273\204\345\220\210.py" index fba47df..c3cdc1b 100644 --- "a/0077.\347\273\204\345\220\210/0077-\347\273\204\345\220\210.py" +++ "b/0077.\347\273\204\345\220\210/0077-\347\273\204\345\220\210.py" @@ -6,18 +6,13 @@ def combine(self, n, k): :rtype: List[List[int]] """ res = [] - def generate(k, i, tmp): - - if k == 0: + def dfs(t, cnt, tmp): + if cnt == 0: res.append(tmp[:]) - return - - for j in range(i , n + 1): - tmp.append(j) - generate(k - 1, j + 1, tmp) - tmp.pop() + + for i in range(t + 1, n + 1): + dfs(i, cnt - 1, tmp + [i]) - - generate(k, 1, []) + dfs(0, k, []) return res - \ No newline at end of file + \ 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/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206.py" "b/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206.py" index e2fcb4e..6012ad0 100644 --- "a/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206.py" +++ "b/0078.\345\255\220\351\233\206/0078-\345\255\220\351\233\206.py" @@ -1,24 +1,14 @@ -import copy class Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] - """ - res, tmp = [], [] - - def generate(nums, n): - res.append(tmp[:]) + """ + res = [[]] + for num in nums: + tmp = res[:] + for item in res: + tmp.append(item + [num]) + res = tmp[:] - if n == len(nums): - return - for i in range(n, len(nums)): - tmp.append(nums[i]) - n += 1 - generate(nums, n) - tmp.pop() - - generate(nums, 0) - return res - - \ No newline at end of file + 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 5af9e24..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" @@ -5,45 +5,33 @@ def exist(self, board, word): :type word: str :rtype: bool """ - if not board or not board[0] or not len(word): + if not board or not board[0]: return False - + m, n = len(board), len(board[0]) - if len(word) > m *n: - return False - - self.res = False dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] - visited = [[0 for _ in range(n + 1)] for j in range(m + 1)] - - def dfs(start, x0, y0): - if start >= len(word) or board[x0][y0] != word[start]: #Ҳ - return - - visited[x0][y0] = 1 - if board[x0][y0] == word[start]: - if start == len(word) - 1: #ҵһн - self.res = True - return - else: - 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] == 0 and not self.res: #not self.resܹؼ֦dzҪ - dfs(start + 1, x, y) #һĸ - visited[x0][y0] = 0 # - - + self.res = False + def dfs(word_idx, x0, y0): + # print word_idx + if word_idx >= len(word): + self.res = True + 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]: - dfs(0, i, j) #ʼ - if self.res: - return self.res - return self.res - - - - \ 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.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.md b/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.md deleted file mode 100644 index 1c8e8e7..0000000 --- a/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.md +++ /dev/null @@ -1,41 +0,0 @@ -

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

- -

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

- -

Example 1:

- -
-Given nums = [1,1,1,2,2,3],
-
-Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
-
-It doesn't matter what you leave beyond the returned length.
- -

Example 2:

- -
-Given nums = [0,0,1,1,1,1,2,3,3],
-
-Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
-
-It doesn't matter what values are set beyond the returned length.
-
- -

Clarification:

- -

Confused why the returned value is an integer but your answer is an array?

- -

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

- -

Internally you can think of this:

- -
-// nums is passed in by reference. (i.e., without making a copy)
-int len = removeDuplicates(nums);
-
-// any modification to nums in your function would be known by the caller.
-// using the length returned by your function, it prints the first len elements.
-for (int i = 0; i < len; i++) {
-    print(nums[i]);
-}
-
diff --git a/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.py b/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.py deleted file mode 100644 index e489bb1..0000000 --- a/0080.remove-duplicates-from-sorted-array-ii/remove-duplicates-from-sorted-array-ii.py +++ /dev/null @@ -1,12 +0,0 @@ -class Solution(object): - def removeDuplicates(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - i = 0 - for num in nums: - if i < 2 or num != nums[i - 2]: - nums[i] = num - i += 1 - return i \ No newline at end of file diff --git "a/0080.\345\210\240\351\231\244\346\216\222\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\216\222\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\216\222\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\216\222\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" index e489bb1..9bac95c 100644 --- "a/0080.\345\210\240\351\231\244\346\216\222\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\216\222\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\216\222\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\216\222\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" @@ -9,4 +9,5 @@ def removeDuplicates(self, nums): if i < 2 or num != nums[i - 2]: nums[i] = num i += 1 + return i \ 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/0081.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204II/0081-\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204II.py" "b/0081.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204II/0081-\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204II.py" index c30f94d..e5f1122 100644 --- "a/0081.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204II/0081-\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204II.py" +++ "b/0081.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204II/0081-\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204II.py" @@ -5,110 +5,8 @@ def search(self, nums, target): :type target: int :rtype: bool """ - #һһȣôһû - #һһȣҵtargetreturn true - #һһȣDztarget¾ҪijһȷtargetпһΣʱ临ӶȻή͵0(N) - if not nums: - return False - if nums[0] != nums[-1]: - return self.search1(nums, target) - if nums[0] == nums[-1]: - if nums[0] == target: + for num in nums: + if target == num: return True - else: - # for num in nums: #͵ͿôдֱO(N)㷨 - # if num == target: - # return True - # return False - return self.search2(nums, target) - - def search1(self, nums, target): #һĽζֲҷֱתͽ - """ - :type nums: List[int] - :type target: int - :rtype: int - """ - if not nums: - return -1 - if len(nums) == 1: - return True if nums[0] == target else False - - lo, hi = 0, len(nums) - 1 - while(lo <= hi): - mid = (lo + hi) // 2 - if mid + 1 < len(nums) and nums[mid] > nums[mid +1]: #תΪmid - break - if nums[mid] <= nums[-1]: - hi = mid - 1 - elif nums[mid] >= nums[0]: - lo = mid + 1 - - if lo > hi:#ûת - lo, hi = 0, len(nums) - 1 - else: - if target >= nums[0]: - lo, hi = 0, mid - else: - lo, hi = mid + 1, len(nums) - 1 - - while(lo <= hi): - mid = (lo + hi) // 2 - if nums[mid] == target: - return True - elif nums[mid] > target: - hi = mid - 1 - else: - lo = mid + 1 - - return False - - def search2(self, nums, target):#nums[0] == nums[-1] - """ - :type nums: List[int] - :type target: int - :rtype: int - """ - if not nums: - return -1 - if len(nums) == 1: - return True if nums[0] == target else False - - lo, hi = 0, len(nums) - 1 - while(lo <= hi): - mid = (lo + hi) // 2 - if mid + 1 < len(nums) and nums[mid] > nums[mid +1]: - break - if nums[mid] == nums[0]: #޷ȷmidһ - i = mid - while(i < len(nums) - 1 and nums[i] == nums[i + 1]): - i += 1 - if i == len(nums) - 1:#Ҷζˣȫnums[0]һtarget϶࣬Ҳ0 ~ midһ - hi = mid - 1 - else: - lo = mid + 1 - - if nums[mid] < nums[-1]: - hi = mid - 1 - elif nums[mid] > nums[0]: - lo = mid + 1 - - if target > nums[mid]: - return False #ΪmidһǸ - elif target == nums[mid]: #ҵ˾ֱӷ - return True - elif target < nums[mid]: #ҪֲңҪȷ໹Ҳ - if target > nums[0]: # - lo, hi = 0, mid - 1 - else: #Ҳ - lo, hi = mid + 1, len(nums) - 1 - while(lo <= hi): - mid = (lo + hi) // 2 - if nums[mid] == target: - return True - elif nums[mid] > target: - hi = mid - 1 - else: - lo = mid + 1 - return False \ 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/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/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" index c5c52fa..2f4b37b 100644 --- "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.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.py" @@ -10,17 +10,19 @@ def deleteDuplicates(self, head): :type head: ListNode :rtype: ListNode """ - new_head = ListNode(1) - new_head.next = head - pre = new_head - cur = head - while cur: - while cur.next != None and cur.val == cur.next.val: - cur = cur.next; - if cur == pre.next: - pre = pre.next - else: - pre.next = cur.next - cur = cur.next - - return new_head.next \ No newline at end of file + 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/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/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..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.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.largest-rectangle-in-histogram/largest-rectangle-in-histogram.md b/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.md deleted file mode 100644 index 6193a4f..0000000 --- a/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.md +++ /dev/null @@ -1,20 +0,0 @@ -

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

- -

 

- -


-Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

- -

 

- -


-The largest rectangle is shown in the shaded area, which has area = 10 unit.

- -

 

- -

Example:

- -
-Input: [2,1,5,6,2,3]
-Output: 10
-
diff --git a/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.py b/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.py deleted file mode 100644 index a7acf32..0000000 --- a/0084.largest-rectangle-in-histogram/largest-rectangle-in-histogram.py +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def largestRectangleArea(self, heights): - """ - :type heights: List[int] - :rtype: int - """ - res = 0 - stack = list() - heights = [0] + heights + [0] - - 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/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 ac9a2dc..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,18 +1,12 @@ -class Solution(object): - def largestRectangleArea(self, heights): - """ - :type heights: List[int] - :rtype: int - """ - res = 0 - stack = list() +class Solution: + def largestRectangleArea(self, heights: List[int]) -> int: heights = [0] + heights + [0] + res = 0 + 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]) - # print (i - stack[-1] - 1) * heights[top], heights[top] stack.append(i) - # print stack 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/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/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" index a3d895f..2f0a252 100644 --- "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.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.py" @@ -5,28 +5,8 @@ def merge(self, nums1, m, nums2, n): :type m: int :type nums2: List[int] :type n: int - :rtype: void Do not return anything, modify nums1 in-place instead. + :rtype: None Do not return anything, modify nums1 in-place instead. """ - temp = [] - p1 = 0 - p2 = 0 - while(p1 < m and p2 < n): - if (nums1[p1] <= nums2[p2]): - temp.append(nums1[p1]) - p1 += 1 - else: - temp.append(nums2[p2]) - p2 += 1 - - while(p1 < m): - temp.append(nums1[p1]) - p1 += 1 - while(p2 < n): - temp.append(nums2[p2]) - p2 += 1 - - for i in range(0, m + n ): - nums1[i] = temp[i] - - # return nums1 - \ No newline at end of file + nums1[:] = (nums1[:m] + nums2) + nums1.sort() + # return sorted(nums) \ No newline at end of file diff --git "a/0089.\346\240\274\351\233\267\347\274\226\347\240\201/0089-\346\240\274\351\233\267\347\274\226\347\240\201.py" "b/0089.\346\240\274\351\233\267\347\274\226\347\240\201/0089-\346\240\274\351\233\267\347\274\226\347\240\201.py" index 67ffe43..4bf87ea 100644 --- "a/0089.\346\240\274\351\233\267\347\274\226\347\240\201/0089-\346\240\274\351\233\267\347\274\226\347\240\201.py" +++ "b/0089.\346\240\274\351\233\267\347\274\226\347\240\201/0089-\346\240\274\351\233\267\347\274\226\347\240\201.py" @@ -4,8 +4,7 @@ def grayCode(self, n): :type n: int :rtype: List[int] """ - # G(i) = i ^ (i /2) - dp = [0 for _ in range(2 ** n)] - for i in range(1, 2 ** n): - dp[i] = i ^ (i /2) - return dp \ No newline at end of file + res = [] + for i in range(2 ** n): + res.append(i ^ (i // 2)) + return res \ No newline at end of file diff --git "a/0090.\345\255\220\351\233\206II/0090-\345\255\220\351\233\206II.py" "b/0090.\345\255\220\351\233\206II/0090-\345\255\220\351\233\206II.py" index 2ffe5bb..f8ebd70 100644 --- "a/0090.\345\255\220\351\233\206II/0090-\345\255\220\351\233\206II.py" +++ "b/0090.\345\255\220\351\233\206II/0090-\345\255\220\351\233\206II.py" @@ -4,13 +4,13 @@ def subsetsWithDup(self, nums): :type nums: List[int] :rtype: List[List[int]] """ - nums.sort() - result = [[]] + res = [[]] for num in nums: - for i in result[:]: - item = i[:] - item.append(num) - if item not in result: - result.append(item[:]) - return result - + tmp = res[:] + for item in res: + newitem = sorted(item + [num]) + if newitem not in tmp: + tmp.append(newitem) + res = tmp[:] + + return res \ 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/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/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" index a2e191b..4edf755 100644 --- "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.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.py" @@ -12,52 +12,40 @@ def reverseBetween(self, head, m, n): :type n: int :rtype: ListNode """ - # ҵm ~ nһΣתȥ - if m == n: - return head + newhead = ListNode(-1) + newhead.next = head + #ҵm - 1͵nڵ cnt = 1 - pre_m, pn = None, None - node = head - while(cnt < n): - # print node.val, cnt - if cnt == m - 1: - pre_m = node - node, cnt = node.next, cnt + 1 - pn = node - if pn: - nextn = pn.next - pn.next = None - else: - nextn = None - # print pre_m.val, nextn.val - - if pre_m is None: #1ʼ - head = self.reverseList(head) - newhead = head - while(head.next): - head = head.next - head.next = nextn - return newhead + 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: - pre_m.next = self.reverseList(pre_m.next) - newhead = head - while(head.next): - head = head.next - head.next = nextn - return newhead - - - - + newhead.next = self.reverseLL(slow) + p = slow + while p and p.next: + p = p.next + p.next = tail + return newhead.next - def reverseList(self, head): - """ - :type head: ListNode - :rtype: ListNode - """ - if head is None or head.next is None: + def reverseLL(self, head): + if not head or not head.next: return head - tmp = self.reverseList(head.next) + + p = self.reverseLL(head.next) head.next.next = head head.next = None - return tmp \ No newline at end of file + return p \ No newline at end of file diff --git "a/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/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" index 8759161..7820957 100644 --- "a/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/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" @@ -11,21 +11,15 @@ def inorderTraversal(self, root): :type root: TreeNode :rtype: List[int] """ - result = list() - self.generate(root, result) - return result - - - - def generate(self, root, result): - if not root: - return - - if root.left: - self.generate(root.left, result) - - result.append(root.val) - - if root.right: - self.generate(root.right, result) + return [] + cur, stack, res = root, [], [] + while cur or stack: + if cur: + stack.append(cur) + cur = cur.left + else: + cur = stack.pop() + res.append(cur.val) + cur = cur.right + return res \ No newline at end of file diff --git "a/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0098-\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0098-\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" index 20a8927..8da976b 100644 --- "a/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0098-\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" +++ "b/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0098-\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" @@ -11,22 +11,10 @@ def isValidBST(self, root): :type root: TreeNode :rtype: bool """ - inorder = list() - self.inorderTra(root, inorder) - # print inorder - for i in range(len(inorder)-1): - if inorder[i] >= inorder[i+1]: - return False - return True - - def inorderTra(self, root, inorder): - if not root: - return None - + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) - self.inorderTra(root.left, inorder) - inorder.append(root.val) - self.inorderTra(root.right, inorder) - - return - \ No newline at end of file + l = inorder(root) + return l == sorted(l) and len(l) == len(set(l)) \ No newline at end of file diff --git "a/0099.\346\201\242\345\244\215\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0099-\346\201\242\345\244\215\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/0099.\346\201\242\345\244\215\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0099-\346\201\242\345\244\215\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..9a783e1 --- /dev/null +++ "b/0099.\346\201\242\345\244\215\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0099-\346\201\242\345\244\215\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.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 recoverTree(self, root): + """ + :type root: TreeNode + :rtype: None Do not return anything, modify root in-place instead. + """ + def inOrder(node): + if not node: + return [] + return inOrder(node.left) + [node.val] + inOrder(node.right) + + inorder = inOrder(root) + inorder.sort() + + self.idx = 0 + def change(node): + if not node: + return + change(node.left) + node.val = inorder[self.idx] + self.idx += 1 + change(node.right) + + change(root) + return root + \ 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 4abafa3..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,33 +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 isSameTree(self, p, q): - """ - :type p: TreeNode - :type q: TreeNode - :rtype: bool - """ +# 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 - - self.result = True - self.check(p,q) - return self.result - - def check(self, p, q): - # if : - if (not p or not q) or (p.val != q.val) : - self.result = False - return - if p.left or q.left: - self.check(p.left, q.left) - if p.right or q.right: - self.check(p.right, q.right) + if not p and q: + return False - return \ No newline at end of file + 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/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 7170990..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" @@ -12,22 +12,13 @@ def isSymmetric(self, root): :rtype: bool """ - queue = [root] - - while(queue): - next_queue = list() - layer = list() - for node in queue: - if not node: - layer.append(None) - continue - next_queue.append(node.left) - next_queue.append(node.right) - - layer.append(node.val) - - if layer != layer[::-1]: + 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 - queue = next_queue - - return True \ No newline at end of file + 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/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206.py" "b/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206.py" index bee34dd..91b1bd7 100644 --- "a/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206.py" +++ "b/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206/0102-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206.py" @@ -13,23 +13,16 @@ def levelOrder(self, root): """ if not root: return [] - queue = [root] - res = list() + from collections import deque + queue = deque([root]) + res = [] 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[:] + layer = [] + for _ in range(len(queue)): + cur = queue.popleft() + if cur: + layer.append(cur.val) + queue += [cur.left, cur.right] res.append(layer) - return res - - - - - \ No newline at end of file + return res[:-1] + \ 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/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\346\254\241\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\346\254\241\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\346\254\241\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\346\254\241\351\201\215\345\216\206.py" index 7062c15..68cd10f 100644 --- "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\346\254\241\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\346\254\241\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\346\254\241\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\346\254\241\351\201\215\345\216\206.py" @@ -11,28 +11,21 @@ def zigzagLevelOrder(self, root): :type root: TreeNode :rtype: List[List[int]] """ + reverse = 0 queue = [root] - lor = 0 # 0 for left and 1 for right - res = list() - if not root: - return [] - while(queue): - next_queue = list() - layer = list() - + res = [] + while queue: + next_queue = [] + layer = [] for node in queue: - if node.left: - next_queue.append(node.left) - if node.right: - next_queue.append(node.right) - - layer.append(node.val) - - if not lor: # left - res.append(layer) - else: - res.append(layer[::-1]) - - lor = 1 - lor - queue = next_queue + if node: + layer.append(node.val) + next_queue += [node.left, node.right] + queue = next_queue[:] + if layer: + if not reverse: + res.append(layer[:]) + else: + res.append(layer[::-1]) + reverse = 1 - reverse return res \ No newline at end of file diff --git "a/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/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..e3b62e7 --- /dev/null +++ "b/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,16 @@ +# 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/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/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/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" index f372ec4..c9439fe 100644 --- "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.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.py" @@ -14,16 +14,10 @@ def buildTree(self, preorder, inorder): """ if not preorder: return None - - root = TreeNode(preorder[0]) - left_inorder = inorder[: inorder.index(root.val)] - right_inorder = inorder[inorder.index(root.val) + 1 :] - - l_left = len(left_inorder) - left_preorder = preorder[1:l_left + 1] - right_preorder = preorder[l_left + 1 :] - - root.left = self.buildTree(left_preorder, left_inorder) - root.right = self.buildTree(right_preorder, right_inorder) - + + 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/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/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" index e638c0f..c67ac3d 100644 --- "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.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.py" @@ -12,22 +12,12 @@ def buildTree(self, inorder, postorder): :type postorder: List[int] :rtype: TreeNode """ - if not postorder: - return None + if not inorder: + return None root = TreeNode(postorder[-1]) - - root_index = inorder.index(postorder[-1]) - - left_inorder = inorder[:root_index] - right_inorder = inorder[root_index + 1:] - - l_left = len(left_inorder) - - left_postorder = postorder[:l_left] - right_postorder = postorder[l_left : -1] - - root.left = self.buildTree(left_inorder, left_postorder) - root.right = self.buildTree(right_inorder, right_postorder) - + 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/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206II.py" "b/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206II.py" index dac2a0e..64d7715 100644 --- "a/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206II.py" +++ "b/0107.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206II/0107-\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206II.py" @@ -11,30 +11,17 @@ def levelOrderBottom(self, root): :type root: TreeNode :rtype: List[List[int]] """ - # Definition for a binary tree node. - if not root: return [] - node = [root] - node_val = list(list()) - self.generate(node, node_val) - return node_val[::-1] - - def generate(self, node, node_val): - new_node = [] - new_node_val = [] - for node in node: - if node.left: - new_node.append(node.left) - if node.right: - new_node.append(node.right) - new_node_val.append(node.val) - node_val.append(new_node_val) - if len(new_node) == 0: - return - self.generate(new_node, node_val) - - - - - \ No newline at end of file + from collections import deque + 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] + res.append(layer) + return res[:-1][::-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/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/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" index 3c4a0e8..da3ec6e 100644 --- "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.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.py" @@ -13,17 +13,11 @@ def sortedArrayToBST(self, nums): """ if not nums: return None - - def dfs(start, end): - if end < start: - return - - mid = (end + start) // 2 - root = TreeNode(nums[mid]) - - root.left = dfs(start, mid - 1) - root.right = dfs(mid + 1, end) - - return root - - return dfs(0, len(nums) - 1) \ No newline at end of file + 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/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/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" index 1053d00..2d2a03a 100644 --- "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.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.py" @@ -18,31 +18,22 @@ def sortedListToBST(self, head): :rtype: TreeNode """ if not head: - return head - l = list() - while(head): - l.append(head.val) - head = head.next - - def sortedArrayToBST(nums): - if not nums: - return None - - def dfs(start, end): - if end < start: - return - - mid = (end + start) // 2 - root = TreeNode(nums[mid]) - - root.left = dfs(start, mid - 1) - root.right = dfs(mid + 1, end) - - return root - - return dfs(0, len(nums) - 1) - - return sortedArrayToBST(l) - + 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/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/0110-\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.py" "b/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/0110-\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.py" index 466e681..e2996bf 100644 --- "a/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/0110-\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.py" +++ "b/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/0110-\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.py" @@ -1,18 +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 isBalanced(self, root): + """ + :type root: TreeNode + :rtype: bool + """ if not root: return True + if not root.left and not root.right: + return True - def check(root,height): - if not root: - return True,height - - tag1,height1=check(root.left,height+1) - tag2,height2=check(root.right,height+1) - if tag1 and tag2 and abs(height1-height2)<2: - return True,max(height1,height2) - else: - return False,height1 - - tag,height=check(root,0) - return tag \ No newline at end of file + def getHeight(node, h): + if not node: + return h + return max(getHeight(node.left, h + 1), getHeight(node.right, h + 1)) + return self.isBalanced(root.left) and self.isBalanced(root.right) and abs(getHeight(root.left, 0) - getHeight(root.right, 0)) <= 1 \ 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/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/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" index 633069e..15bab42 100644 --- "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.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.py" @@ -11,16 +11,13 @@ def minDepth(self, root): :type root: TreeNode :rtype: int """ - if root: - if root.left and root.right: - return 1 + min(self.minDepth(root.left), self.minDepth(root.right)) - if root.left: - return 1 + self.minDepth(root.left) - if root.right: - return 1 + self.minDepth(root.right) - else: - return 1 - else: + if not root: return 0 - - \ No newline at end of file + 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 88c3cff..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" @@ -6,28 +6,19 @@ # self.right = None class Solution(object): - def hasPathSum(self, root, s): + def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: bool """ - self.res = False - - def dfs(node, tmp): + def dfs(node, s): if not node: - return - - tmp.append(node.val) - if not node.left and not node.right and sum(tmp) == s: - self.res = True - - dfs(node.left, tmp) - dfs(node.right, tmp) - tmp.pop() - - - dfs(root, list()) - return self.res - - \ No newline at end of file + 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 fa7f356..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 = list() - - def dfs(node, tmp): + res = [] + 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]) - tmp.append(node.val) - if not node.left and not node.right and sum(tmp) == s: - res.append(tmp[:]) - - dfs(node.left, tmp) - dfs(node.right, tmp) - tmp.pop() - - dfs(root, list()) + 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 4f6227f..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,29 +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 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.right): #ҵһnode - root = root.right - root.right = tmp #ֱԭȥ - \ No newline at end of file + + 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/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/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/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" index 2c89464..2ec9db4 100644 --- "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.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.py" @@ -13,20 +13,28 @@ def connect(self, root): :type root: Node :rtype: Node """ - #ųҪ - if not root or (not root.left and not root.right): - return root - - #ijһڵӵnextһָڵҺ - root.left.next = root.right - - #ijһڵnextΪյʱڵҺӵnextһָýڵnextleft - if root.next: - root.right.next = root.next.left - - #ݹ鴦һ + 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 + 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/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/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" index caa2621..3cf5013 100644 --- "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.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.py" @@ -1,37 +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 or (not root.left and not root.right): - return root - - def findCousin(node, parent): - tmp = parent.next - while(tmp): - if tmp.left: - node.next = tmp.left - break - - elif tmp.right: - node.next = tmp.right - break - tmp = tmp.next - + if not root: + return + wait = None if root.left and root.right: root.left.next = root.right - findCousin(root.right, root) - + wait = root.right elif root.left: - findCousin(root.left, root) - + wait = root.left elif root.right: - findCousin(root.right, root) - - # print root.val, root.right.next + 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 + return root \ No newline at end of file diff --git "a/0118.\346\235\250\350\276\211\344\270\211\350\247\222/0118-\346\235\250\350\276\211\344\270\211\350\247\222.py" "b/0118.\346\235\250\350\276\211\344\270\211\350\247\222/0118-\346\235\250\350\276\211\344\270\211\350\247\222.py" new file mode 100644 index 0000000..c451287 --- /dev/null +++ "b/0118.\346\235\250\350\276\211\344\270\211\350\247\222/0118-\346\235\250\350\276\211\344\270\211\350\247\222.py" @@ -0,0 +1,16 @@ +class Solution(object): + def generate(self, numRows): + """ + :type numRows: int + :rtype: List[List[int]] + """ + if not numRows: + return [] + res = [[1]] + for i in range(1, numRows): + tmp = [1] + for j in range(1, i): + tmp.append(res[i-1][j - 1] + res[i - 1][j]) + tmp.append(1) + res.append(tmp[:]) + return res \ No newline at end of file diff --git "a/0119.\346\235\250\350\276\211\344\270\211\350\247\222II/0119-\346\235\250\350\276\211\344\270\211\350\247\222II.py" "b/0119.\346\235\250\350\276\211\344\270\211\350\247\222II/0119-\346\235\250\350\276\211\344\270\211\350\247\222II.py" index c6f0f9e..fcf14ac 100644 --- "a/0119.\346\235\250\350\276\211\344\270\211\350\247\222II/0119-\346\235\250\350\276\211\344\270\211\350\247\222II.py" +++ "b/0119.\346\235\250\350\276\211\344\270\211\350\247\222II/0119-\346\235\250\350\276\211\344\270\211\350\247\222II.py" @@ -4,23 +4,13 @@ def getRow(self, rowIndex): :type rowIndex: int :rtype: List[int] """ - # C(n - 1m - 1) - n = rowIndex - m = n + 1 - def Calculate(n, m): - down = 1 - up = 1 - for i in range(1, m): - down *= i - - for i in range(1, m): - up *= (n + 1 -i) - - return up // down - - res = list() - for i in range(1, m + 1): - res.append(Calculate(n, i)) - - - return res \ No newline at end of file + if not rowIndex: + return [1] + res = [[1]] + for i in range(1, rowIndex + 1): + tmp = [1] + for j in range(1, i): + tmp.append(res[j - 1] + res[j]) + tmp.append(1) + res = tmp[:] + return res \ No newline at end of file diff --git "a/0120.\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/0120-\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.py" "b/0120.\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/0120-\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.py" index 712bce6..7086ac2 100644 --- "a/0120.\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/0120-\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.py" +++ "b/0120.\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214/0120-\344\270\211\350\247\222\345\275\242\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.py" @@ -4,12 +4,13 @@ def minimumTotal(self, triangle): :type triangle: List[List[int]] :rtype: int """ - dp= triangle[-1] - - for i in range(len(triangle) - 2, -1, -1): - for j in range(i + 1): - dp[j] = triangle[i][j] + min(dp[j], dp[j + 1]) - - return dp[0] - - \ No newline at end of file + if not triangle or not triangle[0]: + return 0 + dp = triangle[:] + for i in range(1, len(dp)): + dp[i][0] += dp[i - 1][0] + for j in range(1, len(dp[i]) - 1): + dp[i][j] += min(dp[i - 1][j - 1], dp[i - 1][j]) + dp[i][-1] += dp[i - 1][-1] + + return min(dp[-1]) \ No newline at end of file diff --git "a/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" "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" new file mode 100644 index 0000000..325ada7 --- /dev/null +++ "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" @@ -0,0 +1,12 @@ +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/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/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" index 9134a56..325ada7 100644 --- "a/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/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" @@ -4,11 +4,9 @@ def maxProfit(self, prices): :type prices: List[int] :rtype: int """ - # if not prices: - # return 0 - minelement = 2 << 31 - profit = 0 - for i in range(len(prices)): - minelement = min(minelement, prices[i]) - profit = max(profit, prices[i] - minelement) - return profit \ No newline at end of file + 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/0122.\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\272II/0122-\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\272II.py" "b/0122.\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\272II/0122-\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\272II.py" index e0eafaf..c52418d 100644 --- "a/0122.\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\272II/0122-\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\272II.py" +++ "b/0122.\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\272II/0122-\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\272II.py" @@ -4,9 +4,13 @@ def maxProfit(self, prices): :type prices: List[int] :rtype: int """ - sum = 0 - for i in range(1,len(prices)): - if prices[i] > prices[i-1]: - profit = prices[i] - prices[i-1] - sum += profit - return sum \ No newline at end of file + dp = [[0 for _ in range(2)] for _ in range(len(prices))] + for i, price in enumerate(prices): + if i == 0: + dp[0][0] = 0 + dp[0][1] = -price + else: + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]) + + return dp[len(prices) - 1][0] if prices else 0 \ No newline at end of file diff --git "a/0123.\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\272III/0123-\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\272III.py" "b/0123.\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\272III/0123-\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\272III.py" new file mode 100644 index 0000000..1761b27 --- /dev/null +++ "b/0123.\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\272III/0123-\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\272III.py" @@ -0,0 +1,21 @@ +class Solution(object): + def maxProfit(self, prices): + """ + :type prices: List[int] + :rtype: int + """ + + max_k = 2 + n = len(prices) + dp = [[[0 for _ in range(2)] for _ in range(max_k + 1)] for _ in range(n)] + + for i, price in enumerate(prices): + for k in range(max_k, 0, -1): + if i == 0: + dp[0][k][0] = 0 + dp[0][k][1] = -price + else: + dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i]) + dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - prices[i]) + + return dp[n - 1][max_k][0] if prices else 0 \ No newline at end of file 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/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/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" index d9f3a41..ff3a1f2 100644 --- "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.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.py" @@ -4,20 +4,9 @@ def isPalindrome(self, s): :type s: str :rtype: bool """ - s = s.lower() - ss = "" - for i in s: - if i.isalpha() != True and i.isdigit() != True: - print i - continue - ss += i - # print ss - if len(ss) <= 1: - return True - for i in range(0,len(ss)): - # print ss[i] - if ss[i] != ss[-i-1]: - return False - return True - - \ No newline at end of file + 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/0126.\345\215\225\350\257\215\346\216\245\351\276\231II/0126-\345\215\225\350\257\215\346\216\245\351\276\231II.py" "b/0126.\345\215\225\350\257\215\346\216\245\351\276\231II/0126-\345\215\225\350\257\215\346\216\245\351\276\231II.py" new file mode 100644 index 0000000..1697649 --- /dev/null +++ "b/0126.\345\215\225\350\257\215\346\216\245\351\276\231II/0126-\345\215\225\350\257\215\346\216\245\351\276\231II.py" @@ -0,0 +1,113 @@ +class Solution(object): + def findLadders(self, beginWord, endWord, wordList): + """ + :type beginWord: str + :type endWord: str + :type wordList: List[str] + :rtype: List[List[str]] + """ + from collections import defaultdict, deque + if endWord not in wordList: + return [] + wordList = set(wordList) + wordList.add(beginWord) + + record = defaultdict(set) + flag, forward, backward = True, {beginWord}, {endWord} + while forward: + if len(forward) > len(backward): + flag, forward, backward = not flag, backward, forward + + wordList -= forward + next_level = set() + for word in forward: + for i in range(len(word)): + for k in range(26): + tmp = word[:i] + chr(ord("a") + k) + word[i + 1:] + + if tmp in wordList: + next_level.add(tmp) + if flag: + record[tmp].add(word) + else: + record[word].add(tmp) + + if next_level & backward:#н˵ҵ + res = [[endWord]] + while res[0][0] != beginWord: + res = [[x] + y for y in res for x in record[y[0]]] + return res + # res += 1 + forward = next_level + return [] + + return 0 + + + + + + + + + + + + + + + for word in wordList: + wl.add(word) + + record = defaultdict(list) + queue = deque() + queue.append(endWord) + # wl.remove(endWord) + def bfs(queue): + if not queue: + return + next_queue = deque() + while queue: + cur = queue.popleft() + print wl, cur + + for i in range(len(cur)): + for k in range(26): + tmp = cur[:i] + chr(ord("a") + k) + cur[i + 1:] + + if tmp != cur and tmp in wl and tmp not in record[cur]: + + next_queue.append(tmp) + record[cur].append(tmp) + wl.remove(cur) + + bfs(next_queue) + + bfs(queue) + print record + self.min_length = 2 ** 32 - 1 + self.res = [] + + def dfs(bW, eW, path): + if len(path) > self.min_length: + return + print path, eW, record[eW] + if bW == eW: + # if len(path) + 1 < self.min_length: + # self.res = [[eW] + path ] + # self.min_length = len(path) + 1 + # elif len(path) + 1 == self.min_length: + self.res.append([eW] + path ) + return + + for pre_node in record[eW]: + if pre_node not in visited: + visited.add(pre_node) + dfs(bW, pre_node, [eW] + path ) + visited.remove(pre_node) + + visited = set() + visited.add(endWord) + dfs(beginWord, endWord, []) + return self.res + \ No newline at end of file diff --git "a/0127.\345\215\225\350\257\215\346\216\245\351\276\231/0127-\345\215\225\350\257\215\346\216\245\351\276\231.py" "b/0127.\345\215\225\350\257\215\346\216\245\351\276\231/0127-\345\215\225\350\257\215\346\216\245\351\276\231.py" index fb2fa0b..42405ae 100644 --- "a/0127.\345\215\225\350\257\215\346\216\245\351\276\231/0127-\345\215\225\350\257\215\346\216\245\351\276\231.py" +++ "b/0127.\345\215\225\350\257\215\346\216\245\351\276\231/0127-\345\215\225\350\257\215\346\216\245\351\276\231.py" @@ -1,4 +1,3 @@ -from collections import deque class Solution(object): def ladderLength(self, beginWord, endWord, wordList): """ @@ -7,27 +6,29 @@ def ladderLength(self, beginWord, endWord, wordList): :type wordList: List[str] :rtype: int """ - if endWord not in wordList or beginWord == endWord: + from collections import deque + if endWord not in wordList: return 0 - visited = set() - wordList = set(wordList) + wordList = set(wordList) #رŻȻʱ - q = deque() - q.append([beginWord, 0]) - - char = "abcdefghijklmnopqrstuvwxyz" - while q: - cur, cnt = q.popleft() #Ӷȡһ - if cur == endWord: #պҵ - return cnt + 1 - - for i in range(len(cur)): - for j in range(26): - word = cur[:i] + char[j] + cur[i + 1:] #26ֱ任ܶ - if word in wordList and word not in visited: #жϱ任ûЧ - visited.add(word) - q.append([word, cnt + 1]) - - return 0 - - \ No newline at end of file + res, forward, backward = 2, {beginWord}, {endWord} + while forward: + if len(forward) > len(backward): + forward, backward = backward, forward + + next_level = set() + for word in forward: + for i in range(len(word)): + for k in range(26): + tmp = word[:i] + chr(ord("a") + k) + word[i + 1:] + + if tmp in backward: #ҵ + return res + if tmp in wordList: + next_level.add(tmp) + wordList.remove(tmp) + res += 1 + forward = next_level + + + return 0 \ No newline at end of file diff --git "a/0128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/0128-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.py" "b/0128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/0128-\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..fe318e8 --- /dev/null +++ "b/0128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/0128-\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227.py" @@ -0,0 +1,21 @@ +class Solution(object): + def longestConsecutive(self, nums): + """ + :type nums: List[int] + :rtype: 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 \ No newline at end of file diff --git "a/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\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\345\210\260\345\217\266\345\255\220\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\345\210\260\345\217\266\345\255\220\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\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.py" index 79084aa..57e33c4 100644 --- "a/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\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\345\210\260\345\217\266\345\255\220\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\345\210\260\345\217\266\345\255\220\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\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.py" @@ -15,15 +15,16 @@ def sumNumbers(self, root): def dfs(node, tmp): if not node: - return + return tmp = tmp * 10 + node.val if not node.left and not node.right: self.res += tmp + return dfs(node.left, tmp) dfs(node.right, tmp) - # tmp /= 10 - - dfs(root, 0) + # tmp -= node.val + + dfs(root, 0) return self.res \ 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/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/0130-\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.py" "b/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/0130-\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.py" index 1e29183..ee15145 100644 --- "a/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/0130-\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.py" +++ "b/0130.\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/0130-\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.py" @@ -1,95 +1,44 @@ -class UnionFindSet(object): - def __init__(self, grid): - - m, n = len(grid), len(grid[0]) - self.roots = [i for i in range(m * n)] - self.rank = [0 for i in range(m * n)] - self.count = n - - for i in range(m): - for j in range(n): - self.roots[i *n + j] = i * n +j - - 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 solve(self, board): """ :type board: List[List[str]] :rtype: None Do not return anything, modify board in-place instead. """ + #ߵOڵOȾɫP˵OX PȾɫO if not board or not board[0]: return board m, n = len(board), len(board[0]) - dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] - def dfs(x, y): + def dfs(x0, y0): for k in range(4): - xx = x + dx[k] - yy = y + dy[k] - - if xx >= 0 and xx < m and yy >= 0 and yy < n: #ǰЧ - if board[xx][yy] == "O": - board[xx][yy] = "P" #߹ĵȾɫ - dfs(xx, yy) - #--------ڴڵе - i = 0 - for j in range(n): #ϱ - if board[i][j] == "O": - board[i][j] = "P" - dfs(i, j) - - i = m - 1 - for j in range(n): #± - if board[i][j] == "O": - board[i][j] = "P" - dfs(i, j) - j = 0 - for i in range(m): # - if board[i][j] == "O": - board[i][j] = "P" - dfs(i, j) - - j = n - 1 - for i in range(m): #ұ - if board[i][j] == "O": - board[i][j] = "P" - dfs(i, j) - #--------ڴڵе - # print board - res = 0 - for i in range(m): + x = x0 + dx[k] + y = y0 + dy[k] + + if 0 <= x < m and 0 <= y < n and board[x][y] == "O": + board[x][y] = "P" + dfs(x, y) + + + for i in [0, m - 1]: for j in range(n): - if board[i][j] == "P": #ͳһ»жٸûȥĵ + if board[i][j] == "O": + board[i][j] = "P" + dfs(i, j) - board[i][j] = "O" - elif board[i][j] == "O": + for j in [0, n - 1]: + for i in range(m): + if board[i][j] == "O": + board[i][j] = "P" + dfs(i, j) + + for i in range(m): + for j in range(n): + if board[i][j] == "O": board[i][j] = "X" - - return board - - \ No newline at end of file + elif board[i][j] == "P": + board[i][j] = "O" + \ No newline at end of file diff --git "a/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II/0132-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.py" "b/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II/0132-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.py" new file mode 100644 index 0000000..9faf5e5 --- /dev/null +++ "b/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II/0132-\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.py" @@ -0,0 +1,29 @@ +class Solution(object): + def minCut(self, s): + """ + :type s: str + :rtype: int + """ + if s == s[::-1]: + return 0 + + for i in range(1, len(s) + 1): + if s[:i] == s[:i][::-1] and s[i:] == s[i:][::-1]: + return 1 + + dp = [len(s) for i in range(len(s))] + for i in range(0, len(s)): + self.centeralExtend(s, i, i, dp) + self.centeralExtend(s, i, i+1, dp) + print dp + return dp[-1] + + def centeralExtend(self, string, left, right, dp): + while left >= 0 and right < len(string) and string[left] == string[right]: + if left > 0: + dp[right] = min(dp[right], dp[left - 1] + 1) + else: + dp[right] = 0 + left -= 1 + right += 1 + \ 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/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276.py" "b/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276.py" index 5a8f4ee..ebffc5b 100644 --- "a/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276.py" +++ "b/0133.\345\205\213\351\232\206\345\233\276/0133-\345\205\213\351\232\206\345\233\276.py" @@ -1,10 +1,3 @@ -""" -# 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): """ @@ -12,8 +5,9 @@ def cloneGraph(self, node): :rtype: Node """ from collections import defaultdict, deque - record = defaultdict(list) - mapping = dict() + neighborList = defaultdict(list) #ϣڼ¼ϽھӵĹϵ, keyÿϽ㣬valneighbors + mapping = dict() #ϣڼ¼ϽӦ½ڵ + def bfs(queue): if not queue: return @@ -21,44 +15,35 @@ def bfs(queue): while queue: cur = queue.popleft() - mapping[cur] = Node(cur.val, []) + mapping[cur] = Node(cur.val, []) #ΪÿϽ㴴һӦ½ڵ for nei in cur.neighbors: - record[cur].append(nei) + neighborList[cur].append(nei) #¼µǰϽھ if nei not in visited: visited.add(nei) newqueue.append(nei) - bfs(newqueue) + bfs(newqueue) #BFSһ visited = {node} q = deque() q.append(node) bfs(q) - - # for key, val in record.items(): - # print key.val - # for item in val: - # print item.val - # print mapping visited = {node} def generate(queue): while queue: newqueue = [] for node in queue: - if node: - # print node.val - if not record[node]: #ûھ + if node: + if not neighborList[node]: #ûھ return - for nei in record[node]: #ÿھ - mapping[node].neighbors.append(mapping[nei]) + for nei in neighborList[node]: #ÿھ + mapping[node].neighbors.append(mapping[nei]) #µĽǡϽھӶӦġ½ if nei not in visited: visited.add(nei) newqueue.append(nei) queue = newqueue[:] - q = deque() - q.append(node) - generate(q) - return mapping[node] - \ No newline at end of file + generate([node]) + + return mapping[node] #Ӧ½ڵ \ No newline at end of file diff --git "a/0134.\345\212\240\346\262\271\347\253\231/0134-\345\212\240\346\262\271\347\253\231.py" "b/0134.\345\212\240\346\262\271\347\253\231/0134-\345\212\240\346\262\271\347\253\231.py" index 8c202c5..471a152 100644 --- "a/0134.\345\212\240\346\262\271\347\253\231/0134-\345\212\240\346\262\271\347\253\231.py" +++ "b/0134.\345\212\240\346\262\271\347\253\231/0134-\345\212\240\346\262\271\347\253\231.py" @@ -5,17 +5,23 @@ def canCompleteCircuit(self, gas, cost): :type cost: List[int] :rtype: int """ - if sum(gas) < sum(cost) or not len(gas): - return -1 - l = len(gas) - sub = [gas[i] - cost[i] for i in range(l)] - - g = 0 - index = 0 - for i in range(l): #idzվı - g = g + sub[i] - if g < 0: - g = 0 - index = i + 1 + idx = 0 + for i in range(len(gas)): + if i < idx: + continue + j = i + left_gas = gas[i] + while left_gas > 0: + # print j + if left_gas < cost[j]: #ȥһվ + idx = max(idx, j) + break + left_gas -= cost[j] + if (j + 1) % len(gas) == i: + return i - return index + j = (j + 1) % len(gas) + left_gas += gas[j] + return -1 + + \ No newline at end of file diff --git "a/0136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/0136-\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/0136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/0136-\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" index 4e142b2..8c6a931 100644 --- "a/0136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/0136-\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/0136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/0136-\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" @@ -4,10 +4,7 @@ def singleNumber(self, nums): :type nums: List[int] :rtype: int """ - r = 0 - for i in nums: - r ^= i - return r - - - \ No newline at end of file + res = nums[0] + for i in range(1, len(nums)): + res ^= nums[i] + return res \ No newline at end of file diff --git "a/0137.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227II/0137-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227II.py" "b/0137.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227II/0137-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227II.py" index bb3fbc2..3fdfbda 100644 --- "a/0137.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227II/0137-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227II.py" +++ "b/0137.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227II/0137-\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227II.py" @@ -4,6 +4,4 @@ def singleNumber(self, nums): :type nums: List[int] :rtype: int """ - for num in nums: - if nums.count(num) == 1: - return num \ No newline at end of file + return (3 * sum(set(nums)) - sum(nums)) // 2 \ 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/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/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..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.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 7bbfd14..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 - """ - record = [0]#һʼӿͷʼ - - for j in range(len(s) + 1): - for i in record:#֮ǰÿһҷĻ - if s[i : j] in wordDict: #ҵһֿеķַ˵ԶԲֵj - record.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 record - return record[-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/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/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..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.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/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/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..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.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/0143.\351\207\215\346\216\222\351\223\276\350\241\250/0143-\351\207\215\346\216\222\351\223\276\350\241\250.py" "b/0143.\351\207\215\346\216\222\351\223\276\350\241\250/0143-\351\207\215\346\216\222\351\223\276\350\241\250.py" index 03cd491..d726f6f 100644 --- "a/0143.\351\207\215\346\216\222\351\223\276\350\241\250/0143-\351\207\215\346\216\222\351\223\276\350\241\250.py" +++ "b/0143.\351\207\215\346\216\222\351\223\276\350\241\250/0143-\351\207\215\346\216\222\351\223\276\350\241\250.py" @@ -15,35 +15,34 @@ def reorderList(self, head): slow, fast = head, head while fast and fast.next: + # print slow.val, fast.val slow = slow.next - fast = fast.next.next + fast = fast.next + if fast and fast.next: + fast = fast.next - l1, l2 = head, self.reverseList(slow.next) + # print slow.val, fast.val + + tail = slow.next slow.next = None - # self.printList(l1) - # self.printList(l2) - while l1 and l2: - cur = l2 - l2 = l2.next - - cur.next = l1.next - l1.next = cur - l1 = l1.next.next - + first = head + last = self.reverseList(tail) + + while last: + tmp = first.next + first.next = last + tmp2 = last.next + last.next = tmp + first = first.next.next + + last = tmp2 return head - + + def reverseList(self, head): if not head or not head.next: return head p = self.reverseList(head.next) head.next.next = head head.next = None - return p - - def printList(self, head): - l = [] - p = head - while p: - l.append(p.val) - p = p.next - print l \ No newline at end of file + return p \ No newline at end of file diff --git "a/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/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" index 3075ff1..a36db20 100644 --- "a/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/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" @@ -11,8 +11,12 @@ def preorderTraversal(self, root): :type root: TreeNode :rtype: List[int] """ - if not root: - return [] - result = [root.val] + self.preorderTraversal(root.left) - result += self.preorderTraversal(root.right) - return result \ No newline at end of file + stack = [root] + res = [] + while stack: + cur = stack.pop() + if cur: + res.append(cur.val) + stack.append(cur.right) + stack.append(cur.left) + return res \ No newline at end of file diff --git "a/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/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" index d224375..17c5fa8 100644 --- "a/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/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" @@ -11,8 +11,15 @@ def postorderTraversal(self, root): :type root: TreeNode :rtype: List[int] """ - if not root: - return [] - result = self.postorderTraversal(root.left) - result += self.postorderTraversal(root.right) - return result + [root.val] \ No newline at end of file + # left right mid + # mid right left [::-1] + stack, res = [root], [] + while stack: + cur = stack.pop() + if cur: + res.append(cur.val) + stack.append(cur.left) + stack.append(cur.right) + return res[::-1] + + \ 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" new file mode 100644 index 0000000..c2c9139 --- /dev/null +++ "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" @@ -0,0 +1,77 @@ +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 + """ + self.capacity = capacity + 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 not in self.dic: + return -1 + else: + value = self.dic[key].val + self.moveToHead(self.dic[key]) + return value + + def put(self, key, value): + """ + :type key: int + :type value: int + :rtype: None + """ + 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.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: +# obj = LRUCache(capacity) +# param_1 = obj.get(key) +# obj.put(key,value) \ No newline at end of file diff --git "a/0147.\345\257\271\351\223\276\350\241\250\350\277\233\350\241\214\346\217\222\345\205\245\346\216\222\345\272\217/0147-\345\257\271\351\223\276\350\241\250\350\277\233\350\241\214\346\217\222\345\205\245\346\216\222\345\272\217.py" "b/0147.\345\257\271\351\223\276\350\241\250\350\277\233\350\241\214\346\217\222\345\205\245\346\216\222\345\272\217/0147-\345\257\271\351\223\276\350\241\250\350\277\233\350\241\214\346\217\222\345\205\245\346\216\222\345\272\217.py" index f2d2c8e..77b202d 100644 --- "a/0147.\345\257\271\351\223\276\350\241\250\350\277\233\350\241\214\346\217\222\345\205\245\346\216\222\345\272\217/0147-\345\257\271\351\223\276\350\241\250\350\277\233\350\241\214\346\217\222\345\205\245\346\216\222\345\272\217.py" +++ "b/0147.\345\257\271\351\223\276\350\241\250\350\277\233\350\241\214\346\217\222\345\205\245\346\216\222\345\272\217/0147-\345\257\271\351\223\276\350\241\250\350\277\233\350\241\214\346\217\222\345\205\245\346\216\222\345\272\217.py" @@ -13,22 +13,35 @@ def insertionSortList(self, head): if not head or not head.next: return head - dummy = ListNode(-1) + dummy = ListNode(-9999999999) dummy.next = head - pre = head #preʼָһڵ - cur = head.next #curʼָδĵһڵ - while cur: + p = head + while p: + while p and p.next and p.val < p.next.val: + p = p.next + + if not p.next: + break + cur = p.next tail = cur.next - pre.next = tail #curڵó + p.next = tail + cur.next = None - p = dummy - while p.next and p.next.val < cur.val: #ҵλ - p = p.next + tmp = dummy + while tmp and tmp.next and tmp.next.val < cur.val: + tmp = tmp.next - cur.next = p.next #cur뵽pp.next֮ - p.next = cur - cur = tail + tmp2 = tmp.next + tmp.next = cur + cur.next = tmp2 - if p == pre:#ղ뵽ĩβ - pre = pre.next #ô͸pre - return dummy.next \ No newline at end of file + # self.printList(dummy.next) + + return dummy.next + + def printList(self, head): + res = [] + while head: + res.append(head.val) + head = head.next + print res \ No newline at end of file diff --git "a/0148.\346\216\222\345\272\217\351\223\276\350\241\250/0148-\346\216\222\345\272\217\351\223\276\350\241\250.py" "b/0148.\346\216\222\345\272\217\351\223\276\350\241\250/0148-\346\216\222\345\272\217\351\223\276\350\241\250.py" index 70ab025..8d99052 100644 --- "a/0148.\346\216\222\345\272\217\351\223\276\350\241\250/0148-\346\216\222\345\272\217\351\223\276\350\241\250.py" +++ "b/0148.\346\216\222\345\272\217\351\223\276\350\241\250/0148-\346\216\222\345\272\217\351\223\276\350\241\250.py" @@ -10,33 +10,42 @@ def sortList(self, head): :type head: ListNode :rtype: ListNode """ - + #1. һΪ + #2. + #3. ϶Ϊһ + if not head or not head.next: return head + + dummy = ListNode(-1) + dummy.next = head + pre, slow, fast = head, head, head while fast and fast.next: pre = slow slow = slow.next fast = fast.next.next - + + first = head + second = pre.next pre.next = None - left, right = self.sortList(head), self.sortList(slow) - return self.merge(left, right) - + # print first.val, second.val + sortedfirst = self.sortList(first) + sortedsecond = self.sortList(second) + + return self.merge(sortedfirst, sortedsecond) + def merge(self, l1, l2): if not l1: return l2 if not l2: return l1 - - if l1.val < l2.val: - head = ListNode(l1.val) - head.next = self.merge(l1.next, l2) + + if l1.val <= l2.val: + tmp = ListNode(l1.val) + tmp.next = self.merge(l1.next, l2) else: - head = ListNode(l2.val) - head.next = self.merge(l1, l2.next) - return head - - + tmp = ListNode(l2.val) + tmp.next = self.merge(l1, l2.next) - \ No newline at end of file + return tmp \ No newline at end of file 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/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/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" index 9a6acbe..d0ff710 100644 --- "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.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.py" @@ -5,22 +5,23 @@ def evalRPN(self, tokens): :rtype: int """ stack = [] - for item in tokens: - if item in ["+", "-", "*", "/"]: - if item == "+": - temp = int(stack[-2]) + int(stack[-1]) - elif item == "-": - temp = int(stack[-2]) - int(stack[-1]) - # print temp - elif item == "*": - temp = int(stack[-2]) * int(stack[-1]) - elif item == "/": - temp = int(float(stack[-2])/ float(stack[-1])) - stack.pop() - stack.pop() - stack.append(temp) - + for i, x in enumerate(tokens): + # print stack + if x not in ["+", "-", "*", "/"]: + stack.append(int(x)) else: - stack.append(item) + 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 int(stack[0]) \ No newline at end of file + 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/0151.\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/0151-\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/0151.\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/0151-\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" index ee268fd..fdc0ce2 100644 --- "a/0151.\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/0151-\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/0151.\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/0151-\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" @@ -3,14 +3,9 @@ def reverseWords(self, s): """ :type s: str :rtype: str - """ - res = [] + """ + s = s.strip() s = s.rstrip() - s = s.lstrip() - l = s.split(" ") - l = l[::-1] - print l - # print " ".join(item for item in l) - # for item in l: - # if item == "" - return " ".join(item for item in l if item != "") \ No newline at end of file + s = s[::-1].split(" ") + + return " ".join(item[::-1] for item in s if item) \ No newline at end of file diff --git "a/0152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\345\272\217\345\210\227/0152-\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\345\272\217\345\210\227.py" "b/0152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\345\272\217\345\210\227/0152-\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\345\272\217\345\210\227.py" index 3fb4a22..8c92d31 100644 --- "a/0152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\345\272\217\345\210\227/0152-\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\345\272\217\345\210\227.py" +++ "b/0152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\345\272\217\345\210\227/0152-\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\345\272\217\345\210\227.py" @@ -4,14 +4,16 @@ def maxProduct(self, nums): :type nums: List[int] :rtype: int """ - l = len(nums) - dpmax = [0 for _ in range(l)] - dpmin = [0 for _ in range(l)] + #dp[i] = max(nums[i], nums[i] * dp[i - 1]) - dpmax[0] = nums[0] - dpmin[0] = nums[0] - for i in range(1, l): - dpmax[i] = max(nums[i], max(nums[i] * dpmax[i - 1], nums[i] * dpmin[i - 1])) - dpmin[i] = min(nums[i], min(nums[i] * dpmax[i - 1], nums[i] * dpmin[i - 1])) - - return max(dpmax) \ No newline at end of file + dp = [0 for i in nums] + dpmin = [0 for i in nums] + + dp[0], dpmin[0] = nums[0], nums[0] + for i, num in enumerate(nums): + if i > 0: + dp[i] = max(num, max(dp[i - 1] * num, dpmin[i - 1] * num)) + dpmin[i] = min(num, min(dp[i - 1] * num, dpmin[i - 1] * num)) + + # print dp + return max(dp) \ No newline at end of file diff --git "a/0153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/0153-\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274.py" "b/0153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/0153-\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274.py" index ece52c0..4394d72 100644 --- "a/0153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/0153-\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274.py" +++ "b/0153.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274/0153-\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274.py" @@ -4,15 +4,29 @@ def findMin(self, nums): :type nums: List[int] :rtype: int """ - # m = len(nums) / 2 - b = 0 - e = len(nums)-1 - while(b < e): - m = b + (e-b) / 2 - if nums[m] < nums[e]: - e = m + if not nums: + return 0 + if len(nums) == 1: + return nums[0] + + if nums[0] < nums[-1]: #ûת + return nums[0] + + left, right = 0, len(nums) - 1 + while left <= right: + mid = (left + right) // 2 + + if mid + 1< len(nums): #mid һλ + if nums[mid - 1] > nums[mid] and nums[mid] < nums[mid + 1]: #ҵ + return nums[mid] else: - b = m+1 - return nums[b] + if nums[mid - 1] > nums[mid]: # mid һλ + return nums[mid] + if nums[mid] < nums[-1]: + right = mid - 1 + else: + left = mid + 1 - \ No newline at end of file + + + \ No newline at end of file diff --git "a/0154.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274II/0154-\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274II.py" "b/0154.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274II/0154-\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274II.py" index 7adf69c..fcc547c 100644 --- "a/0154.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274II/0154-\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274II.py" +++ "b/0154.\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274II/0154-\345\257\273\346\211\276\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\345\260\217\345\200\274II.py" @@ -4,4 +4,27 @@ def findMin(self, nums): :type nums: List[int] :rtype: int """ - return min(nums) \ No newline at end of file + left, right = 0, len(nums) - 1 + while left < right: + mid = (left + right) // 2 + + if nums[mid] > nums[right]: #Сֵ϶ڡmid + 1, right + left = mid + 1 + elif nums[mid] < nums[right]: #Сֵ϶ڡleft, mid + right = mid + elif nums[mid] == nums[right]: #޷ȷСֵ + + flag = False + for j in range(right - 1, mid, -1):#ҲңDzбnums[mid]С + if nums[mid] > nums[j]: + flag = True #ȷʵڸСnums[j] + break + + if flag: #ȷСֵ϶ڡmid + 1, j + left = mid + 1 + right = j + else: #mid rightе, Сֵ϶ڡleft, mid + right = mid + + return nums[left] + \ No newline at end of file diff --git "a/0155.\346\234\200\345\260\217\346\240\210/0155-\346\234\200\345\260\217\346\240\210 2.py" "b/0155.\346\234\200\345\260\217\346\240\210/0155-\346\234\200\345\260\217\346\240\210 2.py" new file mode 100644 index 0000000..0b9c216 --- /dev/null +++ "b/0155.\346\234\200\345\260\217\346\240\210/0155-\346\234\200\345\260\217\346\240\210 2.py" @@ -0,0 +1,47 @@ +class MinStack(object): + + def __init__(self): + """ + initialize your data structure here. + """ + self.s = [] + self.min_s = [] + + def push(self, x): + """ + :type x: int + :rtype: None + """ + self.s.append(x) + if self.min_s: + self.min_s.append(min(x, self.min_s[-1])) + else: + self.min_s.append(x) + + def pop(self): + """ + :rtype: None + """ + self.min_s.pop() + self.s.pop() + + + def top(self): + """ + :rtype: int + """ + return self.s[-1] + + def getMin(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.getMin() \ No newline at end of file diff --git "a/0155.\346\234\200\345\260\217\346\240\210/0155-\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.py" index 616a4e3..0b9c216 100644 --- "a/0155.\346\234\200\345\260\217\346\240\210/0155-\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.py" @@ -4,42 +4,40 @@ def __init__(self): """ initialize your data structure here. """ - self.stack = [] - self.min_stack = [] - + self.s = [] + self.min_s = [] def push(self, x): """ :type x: int - :rtype: void + :rtype: None """ - self.stack.append(x) - if self.min_stack and self.min_stack[-1] <= x: - self.min_stack.append(self.min_stack[-1]) + self.s.append(x) + if self.min_s: + self.min_s.append(min(x, self.min_s[-1])) else: - self.min_stack.append(x) - + self.min_s.append(x) def pop(self): """ - :rtype: void + :rtype: None """ - self.stack = self.stack[:-1] - self.min_stack = self.min_stack[:-1] - + self.min_s.pop() + self.s.pop() + def top(self): """ :rtype: int """ - return self.stack[-1] + return self.s[-1] def getMin(self): """ :rtype: int """ - return self.min_stack[-1] - + return self.min_s[-1] + # Your MinStack object will be instantiated and called as such: # obj = MinStack() diff --git "a/0156.\344\270\212\344\270\213\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0156-\344\270\212\344\270\213\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" "b/0156.\344\270\212\344\270\213\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0156-\344\270\212\344\270\213\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" index 0eaab42..adb4184 100644 --- "a/0156.\344\270\212\344\270\213\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0156-\344\270\212\344\270\213\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" +++ "b/0156.\344\270\212\344\270\213\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221/0156-\344\270\212\344\270\213\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.py" @@ -11,21 +11,15 @@ def upsideDownBinaryTree(self, root): :type root: TreeNode :rtype: TreeNode """ - #ÿһڵӵҽڵ if not root or (not root.left and not root.right): return root - - parent, sibling = None, None - while root: - tmp = root.left - root.left = sibling - - sibling = root.right - root.right = parent - - parent = root - root = tmp - - return parent - - + newroot = self.upsideDownBinaryTree(root.left) + right = root.right + root.left.left = right + root.left.right = root + + root.left = None + root.right = None + + return newroot + \ No newline at end of file diff --git "a/0157.\347\224\250Read4\350\257\273\345\217\226N\344\270\252\345\255\227\347\254\246/0157-\347\224\250Read4\350\257\273\345\217\226N\344\270\252\345\255\227\347\254\246.py" "b/0157.\347\224\250Read4\350\257\273\345\217\226N\344\270\252\345\255\227\347\254\246/0157-\347\224\250Read4\350\257\273\345\217\226N\344\270\252\345\255\227\347\254\246.py" index 70bf37f..7acd02a 100644 --- "a/0157.\347\224\250Read4\350\257\273\345\217\226N\344\270\252\345\255\227\347\254\246/0157-\347\224\250Read4\350\257\273\345\217\226N\344\270\252\345\255\227\347\254\246.py" +++ "b/0157.\347\224\250Read4\350\257\273\345\217\226N\344\270\252\345\255\227\347\254\246/0157-\347\224\250Read4\350\257\273\345\217\226N\344\270\252\345\255\227\347\254\246.py" @@ -19,18 +19,14 @@ def read(self, buf, n): :type n: Number of characters to read (int) :rtype: The number of actual characters read (int) """ - tmp = ["","","",""] - cnt = 0 - read4(tmp) - while tmp != ["","","",""]: - for i in range(4): - if tmp[i]: - buf[cnt] = tmp[i] - cnt += 1 - if cnt == n + 1: - return n - tmp = ["","","",""] - read4(tmp) - return cnt - - \ No newline at end of file + res = 0 + tmp = read4(buf) + s = "" + while tmp: + res += tmp + # print buf + s += "".join(buf[:tmp]) + tmp = read4(buf) + for i in range(len(s)): + buf[i] = s[i] + return len(s) if len(s) < n else n \ No newline at end of file diff --git "a/0159.\350\207\263\345\244\232\345\214\205\345\220\253\344\270\244\344\270\252\344\270\215\345\220\214\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262/0159-\350\207\263\345\244\232\345\214\205\345\220\253\344\270\244\344\270\252\344\270\215\345\220\214\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" "b/0159.\350\207\263\345\244\232\345\214\205\345\220\253\344\270\244\344\270\252\344\270\215\345\220\214\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262/0159-\350\207\263\345\244\232\345\214\205\345\220\253\344\270\244\344\270\252\344\270\215\345\220\214\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" index a36eb0a..da514b9 100644 --- "a/0159.\350\207\263\345\244\232\345\214\205\345\220\253\344\270\244\344\270\252\344\270\215\345\220\214\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262/0159-\350\207\263\345\244\232\345\214\205\345\220\253\344\270\244\344\270\252\344\270\215\345\220\214\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" +++ "b/0159.\350\207\263\345\244\232\345\214\205\345\220\253\344\270\244\344\270\252\344\270\215\345\220\214\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262/0159-\350\207\263\345\244\232\345\214\205\345\220\253\344\270\244\344\270\252\344\270\215\345\220\214\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" @@ -4,29 +4,29 @@ def lengthOfLongestSubstringTwoDistinct(self, s): :type s: str :rtype: int """ - left, right = 0, 0 - queue = [] - last_pos = {} + if len(s) <= 2: + return len(s) + start, end = 0, 0 + q = [] res = 0 - for right in range(len(s)): - if s[right] in queue: - last_pos[s[right]] = right - elif s[right] not in queue: - if len(queue) >= 2: - - if last_pos[queue[0]] < last_pos[queue[1]]: #0Ԫߵ - left = last_pos[queue[0]] + 1 - last_pos.pop(queue[0]) - queue.pop(0) + dic = {} #¼qĸһγֵ± + for i, char in enumerate(s): + dic[char] = i + if len(q) < 2: + if char not in q: + q.append(char) + else: + if char not in q: #ҪԾɻ + if dic[q[0]] < dic[q[1]]: + tmp = q[0] + q.pop(0) else: - left = last_pos[queue[1]] + 1 - last_pos.pop(queue[1]) - queue.pop(1) - - queue.append(s[right]) - last_pos[s[right]] = right - # print s[left:right + 1] - res = max(res, right - left + 1) + tmp = q[1] + q.pop(1) + start = dic[tmp] + 1 + + q.append(char) + end = i + res = max(end - start + 1, res) return res - - return res \ No newline at end of file + \ No newline at end of file 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/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/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" index 396a67b..c3b380b 100644 --- "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.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.py" @@ -10,39 +10,28 @@ def getIntersectionNode(self, headA, headB): :type head1, head1: ListNode :rtype: ListNode """ - la = 0 - lb = 0 - pa = headA - pb = headB - while(pa!= None): + pa, pb = headA, headB + la, lb = 0, 0 + while pa: la += 1 pa = pa.next - while(pb!= None): + + while pb: lb += 1 pb = pb.next - - # print la,lb - pb = headB - pa = headA - if lb > la: - k = lb-la - while(k > 0): - k -= 1 - pb = pb.next - else: - k = la - lb - while(k > 0): - k -= 1 - pa = pa.next - # print pb.val - # print pa.val - - while(pb != None): - # print pa.val, pb.val - if pb == pa: - return pa - else: - pa = pa.next - 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/0161.\347\233\270\351\232\224\344\270\2721\347\232\204\347\274\226\350\276\221\350\267\235\347\246\273/0161-\347\233\270\351\232\224\344\270\2721\347\232\204\347\274\226\350\276\221\350\267\235\347\246\273.py" "b/0161.\347\233\270\351\232\224\344\270\2721\347\232\204\347\274\226\350\276\221\350\267\235\347\246\273/0161-\347\233\270\351\232\224\344\270\2721\347\232\204\347\274\226\350\276\221\350\267\235\347\246\273.py" index a613a15..4635420 100644 --- "a/0161.\347\233\270\351\232\224\344\270\2721\347\232\204\347\274\226\350\276\221\350\267\235\347\246\273/0161-\347\233\270\351\232\224\344\270\2721\347\232\204\347\274\226\350\276\221\350\267\235\347\246\273.py" +++ "b/0161.\347\233\270\351\232\224\344\270\2721\347\232\204\347\274\226\350\276\221\350\267\235\347\246\273/0161-\347\233\270\351\232\224\344\270\2721\347\232\204\347\274\226\350\276\221\350\267\235\347\246\273.py" @@ -1,12 +1,16 @@ class Solution(object): - def isOneEditDistance(self, s, t): + def isOneEditDistance(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + edit = 0 #༭ distance = len(s) - len(t) if abs(distance) > 1: #ȶ˲ֹһλ϶ return False - if not s or not t: #һΪշ棬Ϊշؼ + if not s or not t: return s != t - - edit = 0 #༭ i, j = 0, 0 while i < len(s) and j < len(t): if s[i] == t[j]: #༭ @@ -23,10 +27,10 @@ def isOneEditDistance(self, s, t): i += 1 j += 1 edit += 1 - - if i < len(s): #tûˣsһλȡeditDz0 + # print i, j, edit + if i < len(s): return edit == 0 - if j < len(t): #sûˣtһλȡeditDz0 + if j < len(t): return edit == 0 - - return i == len(s) and j == len(t) and edit == 1 \ No newline at end of file + return i == len(s) and j == len(t) and edit == 1 + \ No newline at end of file diff --git "a/0162.\345\257\273\346\211\276\345\263\260\345\200\274/0162-\345\257\273\346\211\276\345\263\260\345\200\274.py" "b/0162.\345\257\273\346\211\276\345\263\260\345\200\274/0162-\345\257\273\346\211\276\345\263\260\345\200\274.py" index 04b7c85..0630c38 100644 --- "a/0162.\345\257\273\346\211\276\345\263\260\345\200\274/0162-\345\257\273\346\211\276\345\263\260\345\200\274.py" +++ "b/0162.\345\257\273\346\211\276\345\263\260\345\200\274/0162-\345\257\273\346\211\276\345\263\260\345\200\274.py" @@ -9,8 +9,7 @@ def findPeakElement(self, nums): return 0 lo, hi = 0, len(nums) - 1 while(lo < hi): - # print lo,hi - mid = lo + (hi - lo) / 2 + mid = (lo + hi) // 2 if nums[mid - 1] < nums[mid] and nums[mid] > nums[mid + 1]: return mid elif nums[mid] < nums[mid + 1]: @@ -18,4 +17,5 @@ def findPeakElement(self, nums): else: hi = mid - 1 - return lo \ No newline at end of file + return lo + \ No newline at end of file diff --git "a/0163.\347\274\272\345\244\261\347\232\204\345\214\272\351\227\264/0163-\347\274\272\345\244\261\347\232\204\345\214\272\351\227\264.py" "b/0163.\347\274\272\345\244\261\347\232\204\345\214\272\351\227\264/0163-\347\274\272\345\244\261\347\232\204\345\214\272\351\227\264.py" index f3f7100..6e621ba 100644 --- "a/0163.\347\274\272\345\244\261\347\232\204\345\214\272\351\227\264/0163-\347\274\272\345\244\261\347\232\204\345\214\272\351\227\264.py" +++ "b/0163.\347\274\272\345\244\261\347\232\204\345\214\272\351\227\264/0163-\347\274\272\345\244\261\347\232\204\345\214\272\351\227\264.py" @@ -6,31 +6,23 @@ def findMissingRanges(self, nums, lower, upper): :type upper: int :rtype: List[str] """ - # if not nums: - # if lower == upper: - # return [str(lower)] - # else: - # return [str(lower) + "->" + str(upper)] - start = lower - end = lower + start, end = lower, lower #ҿ res = [] - for i in range(len(nums)): - if nums[i] == end: - start, end = nums[i] + 1, nums[i] + 1 - - elif nums[i] > end: - end = max(end, nums[i] - 1) - - if end != start: - res.append(str(start) + "->" + str(end)) - else: + for i, num in enumerate(nums): + if num == end: # + start, end = num + 1, num + 1 + elif num > end: #û + end = max(end, num - 1) + if end - start == 0: #ֻһ res.append(str(start)) + else: + res.append(str(start) + "->" + str(end)) + start, end = num + 1, num + 1 + end = upper + if start < end: + res.append(str(start) + "->" + str(end)) + elif end == start: #ֻһ + res.append(str(start)) + return res - start, end = nums[i] + 1, nums[i] + 1 - - if start < upper: - res.append(str(start) + "->" + str(upper)) - elif start == upper: - res.append(str(start)) - - return res \ No newline at end of file + \ No newline at end of file diff --git "a/0164.\346\234\200\345\244\247\351\227\264\350\267\235/0164-\346\234\200\345\244\247\351\227\264\350\267\235.py" "b/0164.\346\234\200\345\244\247\351\227\264\350\267\235/0164-\346\234\200\345\244\247\351\227\264\350\267\235.py" index a6e277d..0a2ef6d 100644 --- "a/0164.\346\234\200\345\244\247\351\227\264\350\267\235/0164-\346\234\200\345\244\247\351\227\264\350\267\235.py" +++ "b/0164.\346\234\200\345\244\247\351\227\264\350\267\235/0164-\346\234\200\345\244\247\351\227\264\350\267\235.py" @@ -27,4 +27,5 @@ def maximumGap(self, nums): return res def findBucketIndex(self, num, min_val, max_val, n): - return int((num - min_val) * n / (max_val - min_val)) \ No newline at end of file + return int((num - min_val) * n / (max_val - min_val)) + \ No newline at end of file diff --git "a/0165.\346\257\224\350\276\203\347\211\210\346\234\254\345\217\267/0165-\346\257\224\350\276\203\347\211\210\346\234\254\345\217\267.py" "b/0165.\346\257\224\350\276\203\347\211\210\346\234\254\345\217\267/0165-\346\257\224\350\276\203\347\211\210\346\234\254\345\217\267.py" index 82b0ada..d33c456 100644 --- "a/0165.\346\257\224\350\276\203\347\211\210\346\234\254\345\217\267/0165-\346\257\224\350\276\203\347\211\210\346\234\254\345\217\267.py" +++ "b/0165.\346\257\224\350\276\203\347\211\210\346\234\254\345\217\267/0165-\346\257\224\350\276\203\347\211\210\346\234\254\345\217\267.py" @@ -7,7 +7,6 @@ def compareVersion(self, version1, version2): """ l1 = version1.split(".") l2 = version2.split(".") - i, j = 0, 0 while i < len(l1) and j < len(l2): # λȴС num1, num2 = int(l1[i]), int(l2[j]) diff --git "a/0167.\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/0167-\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/0167.\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/0167-\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" index 3b1db81..0355fad 100644 --- "a/0167.\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/0167-\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/0167.\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/0167-\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" @@ -6,10 +6,11 @@ def twoSum(self, numbers, target): :rtype: List[int] """ left, right = 0, len(numbers) - 1 - while(left < right): - if numbers[left] + numbers[right] == target: + while 1: + tmp = numbers[left] + numbers[right] + if tmp == target: return [left + 1, right + 1] - if numbers[left] + numbers[right] < target: + elif tmp < target: left += 1 - elif numbers[left] + numbers[right] > target: + elif tmp > target: right -= 1 \ No newline at end of file diff --git "a/0168.Excel\350\241\250\345\210\227\345\220\215\347\247\260/0168-Excel\350\241\250\345\210\227\345\220\215\347\247\260.py" "b/0168.Excel\350\241\250\345\210\227\345\220\215\347\247\260/0168-Excel\350\241\250\345\210\227\345\220\215\347\247\260.py" index 618eac7..f09a200 100644 --- "a/0168.Excel\350\241\250\345\210\227\345\220\215\347\247\260/0168-Excel\350\241\250\345\210\227\345\220\215\347\247\260.py" +++ "b/0168.Excel\350\241\250\345\210\227\345\220\215\347\247\260/0168-Excel\350\241\250\345\210\227\345\220\215\347\247\260.py" @@ -4,11 +4,10 @@ def convertToTitle(self, n): :type n: int :rtype: str """ - #ʮת26 res = "" - while(n): - n-=1 - n, tmp = divmod(n, 26) - res = chr(ord("A") + tmp) + res - - return res \ No newline at end of file + while n: + n -= 1 + char = chr(ord('A') + n % 26) + n /= 26 + res += char + return res[::-1] \ No newline at end of file diff --git "a/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" "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" new file mode 100644 index 0000000..858b7a2 --- /dev/null +++ "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" @@ -0,0 +1,19 @@ +class Solution(object): + def majorityElement(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + vote = None + vote_cnt = 0 + + for num in nums: + if not vote or num == vote: + vote = num + vote_cnt += 1 + else: + vote_cnt -= 1 + if vote_cnt == 0: + vote = num + vote_cnt = 1 + return vote \ No newline at end of file diff --git "a/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/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..858b7a2 --- /dev/null +++ "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.py" @@ -0,0 +1,19 @@ +class Solution(object): + def majorityElement(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + vote = None + vote_cnt = 0 + + for num in nums: + if not vote or num == vote: + vote = num + vote_cnt += 1 + else: + vote_cnt -= 1 + if vote_cnt == 0: + vote = num + vote_cnt = 1 + return vote \ No newline at end of file diff --git "a/0169.\346\261\202\344\274\227\346\225\260/0169-\346\261\202\344\274\227\346\225\260.py" "b/0169.\346\261\202\344\274\227\346\225\260/0169-\346\261\202\344\274\227\346\225\260.py" index 799481b..c16cf77 100644 --- "a/0169.\346\261\202\344\274\227\346\225\260/0169-\346\261\202\344\274\227\346\225\260.py" +++ "b/0169.\346\261\202\344\274\227\346\225\260/0169-\346\261\202\344\274\227\346\225\260.py" @@ -4,7 +4,4 @@ def majorityElement(self, nums): :type nums: List[int] :rtype: int """ - - nums.sort() - return nums[len(nums)/2] - \ No newline at end of file + return sorted(nums)[len(nums) / 2] \ No newline at end of file diff --git "a/0170.\344\270\244\346\225\260\344\271\213\345\222\214III-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241/0170-\344\270\244\346\225\260\344\271\213\345\222\214III-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241.py" "b/0170.\344\270\244\346\225\260\344\271\213\345\222\214III-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241/0170-\344\270\244\346\225\260\344\271\213\345\222\214III-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241.py" new file mode 100644 index 0000000..f61501f --- /dev/null +++ "b/0170.\344\270\244\346\225\260\344\271\213\345\222\214III-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241/0170-\344\270\244\346\225\260\344\271\213\345\222\214III-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241.py" @@ -0,0 +1,38 @@ +class TwoSum(object): + + def __init__(self): + """ + Initialize your data structure here. + """ + from collections import defaultdict + self.dic = defaultdict(int) + + def add(self, number): + """ + Add the number to an internal data structure.. + :type number: int + :rtype: None + """ + self.dic[number] += 1 + + def find(self, value): + """ + Find if there exists any pair of numbers which sum is equal to the value. + :type value: int + :rtype: bool + """ + for key, val in self.dic.items(): + if key * 2 == value: + if val > 1: + return True + else: + if value - key in self.dic: + return True + return False + + + +# Your TwoSum object will be instantiated and called as such: +# obj = TwoSum() +# obj.add(number) +# param_2 = obj.find(value) \ No newline at end of file diff --git "a/0171.Excel\350\241\250\345\210\227\345\272\217\345\217\267/0171-Excel\350\241\250\345\210\227\345\272\217\345\217\267.py" "b/0171.Excel\350\241\250\345\210\227\345\272\217\345\217\267/0171-Excel\350\241\250\345\210\227\345\272\217\345\217\267.py" index 35fb274..6f4b36c 100644 --- "a/0171.Excel\350\241\250\345\210\227\345\272\217\345\217\267/0171-Excel\350\241\250\345\210\227\345\272\217\345\217\267.py" +++ "b/0171.Excel\350\241\250\345\210\227\345\272\217\345\217\267/0171-Excel\350\241\250\345\210\227\345\272\217\345\217\267.py" @@ -4,9 +4,8 @@ def titleToNumber(self, s): :type s: str :rtype: int """ - l = len(s) - 1 res = 0 - for index, item in enumerate(s): - res += (ord(item) - ord("A") + 1) * (26 ** (l - index)) - return res - \ No newline at end of file + for i, char in enumerate(s): + res *= 26 + res += 1 + ord(char) - ord("A") + return res \ No newline at end of file diff --git "a/0172.\351\230\266\344\271\230\345\220\216\347\232\204\351\233\266/0172-\351\230\266\344\271\230\345\220\216\347\232\204\351\233\266.py" "b/0172.\351\230\266\344\271\230\345\220\216\347\232\204\351\233\266/0172-\351\230\266\344\271\230\345\220\216\347\232\204\351\233\266.py" index 5f4c466..d80bb7a 100644 --- "a/0172.\351\230\266\344\271\230\345\220\216\347\232\204\351\233\266/0172-\351\230\266\344\271\230\345\220\216\347\232\204\351\233\266.py" +++ "b/0172.\351\230\266\344\271\230\345\220\216\347\232\204\351\233\266/0172-\351\230\266\344\271\230\345\220\216\347\232\204\351\233\266.py" @@ -6,7 +6,6 @@ def trailingZeroes(self, n): """ res = 0 while n > 4: + res += n //5 n //= 5 - res += n - return res - \ No newline at end of file + return res \ No newline at end of file diff --git "a/0173.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/0173-\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/0173.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/0173-\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" index eb457d5..9654043 100644 --- "a/0173.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/0173-\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/0173.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/0173-\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" @@ -12,29 +12,31 @@ def __init__(self, root): :type root: TreeNode """ self.stack = [] - self.pushLeft(root) - + self.cur = root + def next(self): """ @return the next smallest number :rtype: int """ - popedNode = self.stack.pop() - self.pushLeft(popedNode.right) - return popedNode.val + while self.cur or self.stack: + if self.cur: + self.stack.append(self.cur) + self.cur = self.cur.left + else: + self.cur = self.stack.pop() + res = self.cur.val + self.cur = self.cur.right + + return res + def hasNext(self): """ @return whether we have a next smallest number :rtype: bool """ - return len(self.stack) != 0 - - def pushLeft(self, node): - while(node): - self.stack.append(node) - node = node.left - + return self.cur or self.stack # Your BSTIterator object will be instantiated and called as such: 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/0188.\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\272IV/0188-\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\272IV.py" "b/0188.\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\272IV/0188-\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\272IV.py" new file mode 100644 index 0000000..84d748c --- /dev/null +++ "b/0188.\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\272IV/0188-\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\272IV.py" @@ -0,0 +1,41 @@ +class Solution(object): + def maxProfit(self, k, prices): + """ + :type k: int + :type prices: List[int] + :rtype: int + """ + max_k = k + if k > len(prices): + return self.maxProfit2(prices) + + n = len(prices) + dp = [[[0 for _ in range(2)] for _ in range(max_k + 1)] for _ in range(n)] + + for i, price in enumerate(prices): + for k in range(max_k, 0, -1): + if i == 0: + dp[0][k][0] = 0 + dp[0][k][1] = -price + else: + dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i]) + dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - prices[i]) + + return dp[n - 1][max_k][0] if prices else 0 + + + def maxProfit2(self, prices): + """ + :type prices: List[int] + :rtype: int + """ + dp = [[0 for _ in range(2)] for _ in range(len(prices))] + for i, price in enumerate(prices): + if i == 0: + dp[0][0] = 0 + dp[0][1] = -price + else: + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]) + + return dp[len(prices) - 1][0] if prices else 0 \ No newline at end of file diff --git "a/0189.\346\227\213\350\275\254\346\225\260\347\273\204/0189-\346\227\213\350\275\254\346\225\260\347\273\204.py" "b/0189.\346\227\213\350\275\254\346\225\260\347\273\204/0189-\346\227\213\350\275\254\346\225\260\347\273\204.py" index 0f9436d..c7c127f 100644 --- "a/0189.\346\227\213\350\275\254\346\225\260\347\273\204/0189-\346\227\213\350\275\254\346\225\260\347\273\204.py" +++ "b/0189.\346\227\213\350\275\254\346\225\260\347\273\204/0189-\346\227\213\350\275\254\346\225\260\347\273\204.py" @@ -3,8 +3,11 @@ def rotate(self, nums, k): """ :type nums: List[int] :type k: int - :rtype: void Do not return anything, modify nums in-place instead. + :rtype: None Do not return anything, modify nums in-place instead. """ - l = len(nums) - k = k % l - nums[:] = nums[l-k:] + nums[:l-k] + #ÿΰһŵǰ棬k + for i in range(k): + # tmp = nums[0] + tmp = nums.pop() + nums.insert(0, tmp) + \ 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/0190.\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215/0190-\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215.py" "b/0190.\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215/0190-\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215.py" index e58bbb8..a2bf6fc 100644 --- "a/0190.\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215/0190-\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215.py" +++ "b/0190.\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215/0190-\351\242\240\345\200\222\344\272\214\350\277\233\345\210\266\344\275\215.py" @@ -2,9 +2,9 @@ class Solution: # @param n, an integer # @return an integer def reverseBits(self, n): - b = bin(n)[2:] - b = "0" * (32 - len(b)) + b - # print b - # print b[::-1] - return int(b[::-1], 2) - \ No newline at end of file + res = 0 + for i in range(32): + res <<= 1 + res += n & 1 + n >>= 1 + return res \ No newline at end of file diff --git "a/0191.\344\275\2151\347\232\204\344\270\252\346\225\260/0191-\344\275\2151\347\232\204\344\270\252\346\225\260.py" "b/0191.\344\275\2151\347\232\204\344\270\252\346\225\260/0191-\344\275\2151\347\232\204\344\270\252\346\225\260.py" index 944a2e5..1e7ce05 100644 --- "a/0191.\344\275\2151\347\232\204\344\270\252\346\225\260/0191-\344\275\2151\347\232\204\344\270\252\346\225\260.py" +++ "b/0191.\344\275\2151\347\232\204\344\270\252\346\225\260/0191-\344\275\2151\347\232\204\344\270\252\346\225\260.py" @@ -4,9 +4,4 @@ def hammingWeight(self, n): :type n: int :rtype: int """ - sum = 0 - while(n > 0): - if (n & 1): - sum += 1 - n >>= 1 - return sum \ No newline at end of file + return bin(n).count("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 4dee799..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 """ - if not nums: + if len(nums) == 0: return 0 - dp = [nums[0]] - if len(nums) == 1: - return dp[0] - dp.append(max(nums[0], nums[1])) - # if len(nums) == 2: - # return dp[1] + if len(nums) <= 2: + return max(nums) + nums[1] = max(nums[0], nums[1]) for i in range(2, len(nums)): - dp.append(max(dp[i-2] + nums[i], dp[i-1])) - # print dp - return dp[-1] \ No newline at end of file + 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/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/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" index 2e25954..eb22eed 100644 --- "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.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.py" @@ -11,26 +11,18 @@ def rightSideView(self, root): :type root: TreeNode :rtype: List[int] """ + from collections import deque if not root: return [] - next_layer = [root] - result = [root.val] - while(next_layer): - temp_next_layer = [] - for node in next_layer: - if not node: - continue - if node.left: - temp_next_layer.append(node.left) - if node.right: - temp_next_layer.append(node.right) - # print temp_next_layer[0].val - if temp_next_layer: - next_layer = temp_next_layer - result.append(temp_next_layer[-1].val) - # print result - else: - break - - return result - \ No newline at end of file + 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 5e1ec45..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,33 +1,31 @@ -class Solution(object): - def numIslands(self, M): - """ - :type grid: List[List[str]] - :rtype: int - """ - if not M or not M[0]: +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(M), len(M[0]) - visited = [[0 for j in range(n)] for i in range(m)] - # print visited + + m, n = len(grid), len(grid[0]) + 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) + res = 0 for i in range(m): for j in range(n): - if M[i][j] == '1' and visited[i][j] == 0: + if grid[i][j] == "1": + grid[i][j] = "0" res += 1 - visited[i][j] = 1 - dfs(i, j) - # print visited + 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/0201.\346\225\260\345\255\227\350\214\203\345\233\264\346\214\211\344\275\215\344\270\216/0201-\346\225\260\345\255\227\350\214\203\345\233\264\346\214\211\344\275\215\344\270\216.py" "b/0201.\346\225\260\345\255\227\350\214\203\345\233\264\346\214\211\344\275\215\344\270\216/0201-\346\225\260\345\255\227\350\214\203\345\233\264\346\214\211\344\275\215\344\270\216.py" new file mode 100644 index 0000000..fba5811 --- /dev/null +++ "b/0201.\346\225\260\345\255\227\350\214\203\345\233\264\346\214\211\344\275\215\344\270\216/0201-\346\225\260\345\255\227\350\214\203\345\233\264\346\214\211\344\275\215\344\270\216.py" @@ -0,0 +1,13 @@ +class Solution(object): + def rangeBitwiseAnd(self, m, n): + """ + :type m: int + :type n: int + :rtype: int + """ + if m == 0 or m == n: + return m + else: #n > mʱΪ &ĽһλضΪ1˿Եݹ鴦 + return self.rangeBitwiseAnd(m >> 1, n >> 1) << 1 + + \ 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/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260.py" "b/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260.py" index 0ec75fb..b376561 100644 --- "a/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260.py" +++ "b/0202.\345\277\253\344\271\220\346\225\260/0202-\345\277\253\344\271\220\346\225\260.py" @@ -4,15 +4,17 @@ def isHappy(self, n): :type n: int :rtype: bool """ - l = set() - while(n!= 1): - l.add(n) - temp = 0 - while(n > 0): - temp += (n % 10) ** 2 - n /= 10 - n = temp - if n in l: - return False - - return n == 1 \ No newline at end of file + 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/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240/0203-\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.py" "b/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240/0203-\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.py" index b1d673f..8edf91a 100644 --- "a/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240/0203-\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.py" +++ "b/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240/0203-\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.py" @@ -11,17 +11,18 @@ def removeElements(self, head, val): :type val: int :rtype: ListNode """ - dummy = ListNode(0) + if not head: + return head + dummy = ListNode(-1) dummy.next = head - pre = dummy - ptr = head - while(ptr != None): - if ptr.val != val: - pre = pre.next - ptr = ptr.next + + pre, cur = dummy, head + while cur: + if cur.val == val: #Ҫɾ + pre.next = cur.next + cur.next = None + cur = pre.next else: - # print pre.val, ptr.val - pre.next = ptr.next - ptr = pre.next - return dummy.next - \ No newline at end of file + pre = pre.next + cur = cur.next + return dummy.next \ No newline at end of file diff --git "a/0204.\350\256\241\346\225\260\350\264\250\346\225\260/0204-\350\256\241\346\225\260\350\264\250\346\225\260.py" "b/0204.\350\256\241\346\225\260\350\264\250\346\225\260/0204-\350\256\241\346\225\260\350\264\250\346\225\260.py" index afa9f29..c9852a5 100644 --- "a/0204.\350\256\241\346\225\260\350\264\250\346\225\260/0204-\350\256\241\346\225\260\350\264\250\346\225\260.py" +++ "b/0204.\350\256\241\346\225\260\350\264\250\346\225\260/0204-\350\256\241\346\225\260\350\264\250\346\225\260.py" @@ -4,16 +4,9 @@ def countPrimes(self, n): :type n: int :rtype: int """ - # print int(n**0.5 +1) - if n <= 2: - return 0 - temp =[1 for i in range(1,n+1)] - temp[0], temp[1] = 0,0 - for i in range(2, int(n**0.5 +1)): - if temp[i] == 1: - for j in range(i*i, n, i): - temp[j] = 0 - # print temp - return sum(temp) - - \ No newline at end of file + record = [1] * n + for i in range(2, n): + if record[i] == 1: + for j in range(i * 2, n, i): + record[j] = 0 + return sum(record) - 2 if n > 1 else 0 \ 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/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/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..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.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/0206.\345\217\215\350\275\254\351\223\276\350\241\250/0206-\345\217\215\350\275\254\351\223\276\350\241\250.py" "b/0206.\345\217\215\350\275\254\351\223\276\350\241\250/0206-\345\217\215\350\275\254\351\223\276\350\241\250.py" index 96678b3..ace8b9e 100644 --- "a/0206.\345\217\215\350\275\254\351\223\276\350\241\250/0206-\345\217\215\350\275\254\351\223\276\350\241\250.py" +++ "b/0206.\345\217\215\350\275\254\351\223\276\350\241\250/0206-\345\217\215\350\275\254\351\223\276\350\241\250.py" @@ -10,21 +10,14 @@ def reverseList(self, head): :type head: ListNode :rtype: ListNode """ - if head is None or head.next is None : + if not head or not head.next: return head - - dummyhead = ListNode(0) - dummyhead.next = pre = head - cur = pre.next - while(cur): - # print cur.val, cur.next.val, pre.val - pre.next = cur.next - cur.next = dummyhead.next - dummyhead.next = cur - cur = pre.next - - return dummyhead.next - - - + pre, cur = None, head + while cur: + tmp = cur.next #β + cur.next = pre #תֲ + pre = cur #pre + cur = tmp #cur + return pre + \ 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/0211.\346\267\273\345\212\240\344\270\216\346\220\234\347\264\242\345\215\225\350\257\215-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241/0211-\346\267\273\345\212\240\344\270\216\346\220\234\347\264\242\345\215\225\350\257\215-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241.py" "b/0211.\346\267\273\345\212\240\344\270\216\346\220\234\347\264\242\345\215\225\350\257\215-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241/0211-\346\267\273\345\212\240\344\270\216\346\220\234\347\264\242\345\215\225\350\257\215-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241.py" index 60ea337..db66ffb 100644 --- "a/0211.\346\267\273\345\212\240\344\270\216\346\220\234\347\264\242\345\215\225\350\257\215-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241/0211-\346\267\273\345\212\240\344\270\216\346\220\234\347\264\242\345\215\225\350\257\215-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241.py" +++ "b/0211.\346\267\273\345\212\240\344\270\216\346\220\234\347\264\242\345\215\225\350\257\215-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241/0211-\346\267\273\345\212\240\344\270\216\346\220\234\347\264\242\345\215\225\350\257\215-\346\225\260\346\215\256\347\273\223\346\236\204\350\256\276\350\256\241.py" @@ -1,11 +1,11 @@ class WordDictionary(object): - + def __init__(self): """ Initialize your data structure here. """ self.roots = {} - + def addWord(self, word): """ Adds a word into the data structure. @@ -17,7 +17,7 @@ def addWord(self, word): for char in word: node = node.setdefault(char, {}) node["end"] = True - + def search(self, word): """ Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. @@ -27,7 +27,7 @@ def search(self, word): self.res = False self.searchHelper(self.roots, word) return self.res - + def searchHelper(self, dic, word): if not word: self.res |= "end" in dic diff --git "a/0212.\345\215\225\350\257\215\346\220\234\347\264\242II/0212-\345\215\225\350\257\215\346\220\234\347\264\242II.py" "b/0212.\345\215\225\350\257\215\346\220\234\347\264\242II/0212-\345\215\225\350\257\215\346\220\234\347\264\242II.py" index c7b72a7..7ddeda8 100644 --- "a/0212.\345\215\225\350\257\215\346\220\234\347\264\242II/0212-\345\215\225\350\257\215\346\220\234\347\264\242II.py" +++ "b/0212.\345\215\225\350\257\215\346\220\234\347\264\242II/0212-\345\215\225\350\257\215\346\220\234\347\264\242II.py" @@ -59,10 +59,10 @@ def findWords(self, board, words): for word in words: tree.insert(word) words = set(words) - res = set() def dfs(x0, y0, node, tmpword): visited.add((x0, y0)) + # print tmpword, x0, y0 for k in range(4): x = x0 + dx[k] y = y0 + dy[k] @@ -72,8 +72,8 @@ def dfs(x0, y0, node, tmpword): dfs(x, y, node[board[x][y]], tmpword + board[x][y]) visited.remove((x,y)) - if tmpword in words: #ҵһ - res.add(tmpword) #üϱظ + if tmpword in words: + res.add(tmpword) for i in range(m): for j in range(n): 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/0214.\346\234\200\347\237\255\345\233\236\346\226\207\344\270\262/0214-\346\234\200\347\237\255\345\233\236\346\226\207\344\270\262.py" "b/0214.\346\234\200\347\237\255\345\233\236\346\226\207\344\270\262/0214-\346\234\200\347\237\255\345\233\236\346\226\207\344\270\262.py" index 5f42c1c..86e9798 100644 --- "a/0214.\346\234\200\347\237\255\345\233\236\346\226\207\344\270\262/0214-\346\234\200\347\237\255\345\233\236\346\226\207\344\270\262.py" +++ "b/0214.\346\234\200\347\237\255\345\233\236\346\226\207\344\270\262/0214-\346\234\200\347\237\255\345\233\236\346\226\207\344\270\262.py" @@ -5,8 +5,10 @@ def shortestPalindrome(self, s): :rtype: str """ reversedS = s[::-1] + # print reversedS i = 0 for i in range(len(s)): + # print reversedS[i:], s[:len(s) - i] if reversedS[i:] == s[:len(s) - i]: return reversedS[:i] + s return "" \ 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/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/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" index 54fb400..6c62036 100644 --- "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.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.py" @@ -5,4 +5,26 @@ def findKthLargest(self, nums, k): :type k: int :rtype: int """ - return sorted(nums, reverse = True)[k-1] \ No newline at end of file + 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/0216.\347\273\204\345\220\210\346\200\273\345\222\214III/0216-\347\273\204\345\220\210\346\200\273\345\222\214III.py" "b/0216.\347\273\204\345\220\210\346\200\273\345\222\214III/0216-\347\273\204\345\220\210\346\200\273\345\222\214III.py" index 6db4e39..c710340 100644 --- "a/0216.\347\273\204\345\220\210\346\200\273\345\222\214III/0216-\347\273\204\345\220\210\346\200\273\345\222\214III.py" +++ "b/0216.\347\273\204\345\220\210\346\200\273\345\222\214III/0216-\347\273\204\345\220\210\346\200\273\345\222\214III.py" @@ -5,21 +5,20 @@ def combinationSum3(self, k, n): :type n: int :rtype: List[List[int]] """ - if not k or not n: - return [] - - res = [] - def dfs(k, n, tmp, start): - if n == 0 and k == 0: - res.append(tmp[:]) + res = [] + def dfs(start, cnt, target, tmp): + if target < 0: return - if k <= 0 or n <= 0: - return - - for i in range(start, 10): - tmp.append(i) - dfs(k - 1, n - i, tmp, i + 1) - tmp.pop() - - dfs(k, n, [], 1) + if target == 0: + if cnt == 0: + res.append(tmp) + else: + return + + for num in range(start, 10): + visited.add(num) + dfs(num + 1, cnt - 1, target - num, tmp + [num]) + visited.remove(num) + visited = set() + dfs(1, k, n, []) return res \ No newline at end of file diff --git a/0217.contains-duplicate/contains-duplicate.md b/0217.contains-duplicate/contains-duplicate.md deleted file mode 100644 index bd80432..0000000 --- a/0217.contains-duplicate/contains-duplicate.md +++ /dev/null @@ -1,21 +0,0 @@ -

Given an array of integers, find if the array contains any duplicates.

- -

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

- -

Example 1:

- -
-Input: [1,2,3,1]
-Output: true
- -

Example 2:

- -
-Input: [1,2,3,4]
-Output: false
- -

Example 3:

- -
-Input: [1,1,1,3,3,4,3,2,4,2]
-Output: true
diff --git a/0217.contains-duplicate/contains-duplicate.py b/0217.contains-duplicate/contains-duplicate.py deleted file mode 100644 index 159c7f0..0000000 --- a/0217.contains-duplicate/contains-duplicate.py +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def containsDuplicate(self, nums): - """ - :type nums: List[int] - :rtype: bool - """ - if len(nums) <= 1: - return False - nums.sort() - for index in range(0,len(nums)-1): - if nums[index] == nums[index +1]:#or nums[index] == nums[index-1]: - return True - return False \ No newline at end of file diff --git "a/0217.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240/0217-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240.py" "b/0217.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240/0217-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240.py" index 159c7f0..6570342 100644 --- "a/0217.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240/0217-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240.py" +++ "b/0217.\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240/0217-\345\255\230\345\234\250\351\207\215\345\244\215\345\205\203\347\264\240.py" @@ -4,10 +4,4 @@ def containsDuplicate(self, nums): :type nums: List[int] :rtype: bool """ - if len(nums) <= 1: - return False - nums.sort() - for index in range(0,len(nums)-1): - if nums[index] == nums[index +1]:#or nums[index] == nums[index-1]: - return True - return False \ No newline at end of file + return len(nums) != len(set(nums)) \ No newline at end of file diff --git a/0219.contains-duplicate-ii/contains-duplicate-ii.md b/0219.contains-duplicate-ii/contains-duplicate-ii.md deleted file mode 100644 index d82af2f..0000000 --- a/0219.contains-duplicate-ii/contains-duplicate-ii.md +++ /dev/null @@ -1,28 +0,0 @@ -

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

- -
-

Example 1:

- -
-Input: nums = [1,2,3,1], k = 3
-Output: true
-
- -
-

Example 2:

- -
-Input: nums = [1,0,1,1], k = 1
-Output: true
-
- -
-

Example 3:

- -
-Input: nums = [1,2,3,1,2,3], k = 2
-Output: false
-
-
-
-
diff --git a/0219.contains-duplicate-ii/contains-duplicate-ii.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" similarity index 54% rename from 0219.contains-duplicate-ii/contains-duplicate-ii.py rename to "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" index 47b4e95..5d2aa7a 100644 --- a/0219.contains-duplicate-ii/contains-duplicate-ii.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" @@ -5,12 +5,10 @@ def containsNearbyDuplicate(self, nums, k): :type k: int :rtype: bool """ - record = dict() + dic = dict() for i, num in enumerate(nums): - # print record - if record.get(num, -1) != -1: - if i - record[num] <= k: + if num in dic: + if i - dic[num] <= k: return True - record[num] = i - return False - \ No newline at end of file + dic[num] = i + return False \ 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.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.py" index 47b4e95..5d2aa7a 100644 --- "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.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.py" @@ -5,12 +5,10 @@ def containsNearbyDuplicate(self, nums, k): :type k: int :rtype: bool """ - record = dict() + dic = dict() for i, num in enumerate(nums): - # print record - if record.get(num, -1) != -1: - if i - record[num] <= k: + if num in dic: + if i - dic[num] <= k: return True - record[num] = i - return False - \ No newline at end of file + dic[num] = i + return False \ No newline at end of file diff --git "a/0221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/0221-\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.py" "b/0221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/0221-\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.py" index 1b73c54..66eb540 100644 --- "a/0221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/0221-\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.py" +++ "b/0221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/0221-\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.py" @@ -4,37 +4,26 @@ def maximalSquare(self, matrix): :type matrix: List[List[str]] :rtype: int """ - m = len(matrix) - if m == 0: + if not matrix or not matrix[0]: return 0 - n = len(matrix[0]) - if n == 0: - return 0 - self.res = 0 + m, n = len(matrix), len(matrix[0]) - def find(x, y): - for length in range(1, min(m - i, n - j) + 1):#lengthDZ߳ - cnt = 0 - - for k in range(length): - for t in range(length): - xx = x + k - yy = y + t - - if 0 <= xx 1: - self.queue2.append(self.queue1[0]) - self.queue1 = self.queue1[1:] + self.q1.append(x) + while self.q2: + self.q1.append(self.q2.popleft()) + self.q2 = self.q1 + self.q1 = deque() - - # print self.queue1, self.queue2 def pop(self): """ Removes the element on top of the stack and returns that element. :rtype: int """ - # print self.queue1, self.queue2 - top = self.queue1[0] - self.queue1, self.queue2 = self.queue2, [] - while len(self.queue1) > 1: - self.queue2.append(self.queue1[0]) - self.queue1 = self.queue1[1:] - return top - + return self.q2.popleft() def top(self): """ Get the top element. :rtype: int """ - return self.queue1[0] - + return self.q2[0] + def empty(self): """ Returns whether the stack is empty. :rtype: bool """ - return not self.queue1 and not self.queue2 + return not self.q2 # Your MyStack object will be instantiated and called as such: 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/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/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" index 38f00e9..bff7e15 100644 --- "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.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.py" @@ -12,11 +12,15 @@ def kthSmallest(self, root, k): :type k: int :rtype: int """ - - def inorderTraversal(node): - if not node: - return [] - return inorderTraversal(node.left) + [node.val] + inorderTraversal(node.right) - - l = inorderTraversal(root) - return l[k - 1] \ No newline at end of file + 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/0248.\344\270\255\345\277\203\345\257\271\347\247\260\346\225\260III/0248-\344\270\255\345\277\203\345\257\271\347\247\260\346\225\260III.py" "b/0248.\344\270\255\345\277\203\345\257\271\347\247\260\346\225\260III/0248-\344\270\255\345\277\203\345\257\271\347\247\260\346\225\260III.py" index 578c7be..ebf7f4b 100644 --- "a/0248.\344\270\255\345\277\203\345\257\271\347\247\260\346\225\260III/0248-\344\270\255\345\277\203\345\257\271\347\247\260\346\225\260III.py" +++ "b/0248.\344\270\255\345\277\203\345\257\271\347\247\260\346\225\260III/0248-\344\270\255\345\277\203\345\257\271\347\247\260\346\225\260III.py" @@ -8,21 +8,16 @@ def strobogrammaticInRange(self, low, high): if int(low) > int(high): return 0 self.findStrobogrammatic(len(high)) - #low + low_rec = self.record[len(low)] - #ҵһ >= low± - low_cnt = 0 - for i, num in enumerate(low_rec): - if int(num) >= int(low): - low_cnt = len(low_rec) - i - break - + #low_recжٸ >= lowlow_cnt + #൱߽ + + low_cnt = len(low_rec) - bisect.bisect_left(low_rec, low) + + #ҵһ > high±꣬ûҵ˵е highС high_rec = self.record[len(high)] - high_cnt = len(high_rec) - for i, num in enumerate(high_rec): - if int(num) > int(high): - high_cnt = i - break + high_cnt = bisect.bisect_right(high_rec, high) if len(low) + 1 == len(high): return low_cnt + high_cnt @@ -31,20 +26,9 @@ def strobogrammaticInRange(self, low, high): else: tmp = 0 for l in range(len(low) + 1, len(high)): - # print l, self.record tmp += len(self.record[l]) return tmp + low_cnt + high_cnt - #ҵһ > high± - # left, right = 0, len(low_rec) - 1 - # while left < right: - # mid = (left + right) // 2 - # if low_rec[mid] == low: - # low_cnt = len(low_rec) - mid - # elif low_rec[mid] > low: - # right = mid - 1 - # elif low_rec[mid] < low: - # left = mid + 1 - + diff --git "a/0251.\345\261\225\345\274\200\344\272\214\347\273\264\345\220\221\351\207\217/0251-\345\261\225\345\274\200\344\272\214\347\273\264\345\220\221\351\207\217.py" "b/0251.\345\261\225\345\274\200\344\272\214\347\273\264\345\220\221\351\207\217/0251-\345\261\225\345\274\200\344\272\214\347\273\264\345\220\221\351\207\217.py" index 8f1de33..0284051 100644 --- "a/0251.\345\261\225\345\274\200\344\272\214\347\273\264\345\220\221\351\207\217/0251-\345\261\225\345\274\200\344\272\214\347\273\264\345\220\221\351\207\217.py" +++ "b/0251.\345\261\225\345\274\200\344\272\214\347\273\264\345\220\221\351\207\217/0251-\345\261\225\345\274\200\344\272\214\347\273\264\345\220\221\351\207\217.py" @@ -5,23 +5,25 @@ def __init__(self, v): :type v: List[List[int]] """ self.list = [] - for item in v: - for num in item: - self.list.append(num) + for nums in v: + for item in nums: + self.list.append(item) self.index = 0 - + def next(self): """ :rtype: int """ self.index += 1 - return self.list[self.index - 1] + return self.list[self.index - 1] + + def hasNext(self): """ :rtype: bool """ - return self.index < len(self.list) + return self.index != len(self.list) # Your Vector2D object will be instantiated and called as such: diff --git "a/0253.\344\274\232\350\256\256\345\256\244II/0253-\344\274\232\350\256\256\345\256\244II.py" "b/0253.\344\274\232\350\256\256\345\256\244II/0253-\344\274\232\350\256\256\345\256\244II.py" index 35d0b7b..b66bc55 100644 --- "a/0253.\344\274\232\350\256\256\345\256\244II/0253-\344\274\232\350\256\256\345\256\244II.py" +++ "b/0253.\344\274\232\350\256\256\345\256\244II/0253-\344\274\232\350\256\256\345\256\244II.py" @@ -6,16 +6,18 @@ def minMeetingRooms(self, intervals): """ if not intervals: return 0 - intervals = sorted(intervals, key = lambda x :x[1]) - record = [0 for _ in range(intervals[-1][1] + 2)] + if not intervals[0]: + return 1 + intervals = sorted(intervals, key = lambda x: x[1]) + record = [0 for _ in range(intervals[-1][1] + 1)] - for i, interval in enumerate(intervals): - start, end = interval[0], interval[1] - record[start] += 1 + for interval in intervals: + # print record + begin, end = interval[0], interval[1] + record[begin] += 1 record[end] -= 1 - - # print record - for i in range(1, len(record)): - record[i] += record[i - 1] - + + for i, x in enumerate(record): + if i > 0: + record[i] += record[i - 1] return max(record) \ 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/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/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" index ceffca9..23bc926 100644 --- "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.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.py" @@ -12,18 +12,19 @@ def binaryTreePaths(self, root): :rtype: List[str] """ if not root: - return [] - if not root.left and not root.right: - return [str(root.val)] + 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)) - dfs(node.left, tmp + "->" + str(node.val)) - dfs(node.right, tmp + "->" + str(node.val)) - - dfs(root.left, str(root.val)) - dfs(root.right, str(root.val)) - return self.res \ No newline at end of file + 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/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.py" "b/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.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.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/0265.\347\262\211\345\210\267\346\210\277\345\255\220II/0265-\347\262\211\345\210\267\346\210\277\345\255\220II.py" "b/0265.\347\262\211\345\210\267\346\210\277\345\255\220II/0265-\347\262\211\345\210\267\346\210\277\345\255\220II.py" index 61bbf42..1d59202 100644 --- "a/0265.\347\262\211\345\210\267\346\210\277\345\255\220II/0265-\347\262\211\345\210\267\346\210\277\345\255\220II.py" +++ "b/0265.\347\262\211\345\210\267\346\210\277\345\255\220II/0265-\347\262\211\345\210\267\346\210\277\345\255\220II.py" @@ -4,20 +4,18 @@ def minCostII(self, costs): :type costs: List[List[int]] :rtype: int """ - if not costs or not costs[0]: + if not costs: return 0 - dp = costs - def GetMin(idx, k): - Min = max(costs[idx]) - for i, cost in enumerate(costs[idx]): - if i == k: - continue - Min = min(Min, cost) - return Min + def helper(last, k): + res = max(last) + for i in range(len(last)): + if i != k: + res = min(res, last[i]) + return res for i in range(1, len(costs)): - for k in range(len(costs[i])): - dp[i][k] += GetMin(i - 1, k) - return min(dp[-1]) - \ No newline at end of file + row = costs[i] + for j in range(len(row)): + row[j] += helper(costs[i - 1], j) + return min(costs[-1]) \ No newline at end of file diff --git "a/0266.\345\233\236\346\226\207\346\216\222\345\210\227/0266-\345\233\236\346\226\207\346\216\222\345\210\227.py" "b/0266.\345\233\236\346\226\207\346\216\222\345\210\227/0266-\345\233\236\346\226\207\346\216\222\345\210\227.py" index 2d62de5..76e1ea3 100644 --- "a/0266.\345\233\236\346\226\207\346\216\222\345\210\227/0266-\345\233\236\346\226\207\346\216\222\345\210\227.py" +++ "b/0266.\345\233\236\346\226\207\346\216\222\345\210\227/0266-\345\233\236\346\226\207\346\216\222\345\210\227.py" @@ -4,14 +4,14 @@ def canPermutePalindrome(self, s): :type s: str :rtype: bool """ - record = dict() - for i, char in enumerate(s): - record[char] = record.get(char, 0) + 1 + flag = 0 + dic = collections.Counter(s) - odd_cnt = 0 - for key, val in record.items(): + for key, val in dic.items(): if val % 2: - odd_cnt += 1 - if odd_cnt > 1: + if not flag: + flag = 1 + else: return False + return True \ No newline at end of file diff --git "a/0267.\345\233\236\346\226\207\346\216\222\345\210\227II/0267-\345\233\236\346\226\207\346\216\222\345\210\227II.py" "b/0267.\345\233\236\346\226\207\346\216\222\345\210\227II/0267-\345\233\236\346\226\207\346\216\222\345\210\227II.py" new file mode 100644 index 0000000..4021cea --- /dev/null +++ "b/0267.\345\233\236\346\226\207\346\216\222\345\210\227II/0267-\345\233\236\346\226\207\346\216\222\345\210\227II.py" @@ -0,0 +1,50 @@ +class Solution(object): + def generatePalindromes(self, s): + """ + :type s: str + :rtype: List[str] + """ + if not s or not self.canPermutePalindrome(s): + return [] + if len(set(s)) == 1: + return [s] + xor = s[0] + dic = collections.Counter(s) + + news = "" + special = "" + for key, val in dic.items(): + if val % 2: + special = key + val -= 1 + news += key * (val // 2) + # print news + res = set() + def permutations(word, tmp): + if not word: + res.add(tmp + special + tmp[::-1]) + + for i, char in enumerate(word): + permutations(word[:i] + word[i + 1:], tmp + char) + permutations(news, "") + return list(res) + + + + + def canPermutePalindrome(self, s): + """ + :type s: str + :rtype: bool + """ + flag = 0 + dic = collections.Counter(s) + + for key, val in dic.items(): + if val % 2: + if not flag: + flag = 1 + else: + return False + + return True \ 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/0268.\347\274\272\345\244\261\346\225\260\345\255\227/0268-\347\274\272\345\244\261\346\225\260\345\255\227.py" "b/0268.\347\274\272\345\244\261\346\225\260\345\255\227/0268-\347\274\272\345\244\261\346\225\260\345\255\227.py" index 0f53d04..4184db1 100644 --- "a/0268.\347\274\272\345\244\261\346\225\260\345\255\227/0268-\347\274\272\345\244\261\346\225\260\345\255\227.py" +++ "b/0268.\347\274\272\345\244\261\346\225\260\345\255\227/0268-\347\274\272\345\244\261\346\225\260\345\255\227.py" @@ -4,7 +4,8 @@ def missingNumber(self, nums): :type nums: List[int] :rtype: int """ - l = len(nums) - idealsum = l*(l+1) /2 - realsum = sum(nums) - return idealsum - realsum \ No newline at end of file + s = set(nums) + for num in range(len(nums)): + if num not in s: + return num + return len(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/0270.\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\274/0270-\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\274.py" "b/0270.\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\274/0270-\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\274.py" index 9dd0666..4d62459 100644 --- "a/0270.\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\274/0270-\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\274.py" +++ "b/0270.\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\274/0270-\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\274.py" @@ -12,18 +12,18 @@ def closestValue(self, root, target): :type target: float :rtype: int """ - def inorder(node): + + def inOrder(node): if not node: return [] - return inorder(node.left) + [node.val] + inorder(node.right) - nums = inorder(root) - res = root.val - minum = 2 ** 32 - # print nums - for i, x in enumerate(nums): - distance = abs(target - x) - print distance, minum, res - if distance < minum: - res = x - minum = distance + return inOrder(node.left) + [node.val] + inOrder(node.right) + + l = inOrder(root) + res = 0 + min_sub = float("inf") + + for num in l: + if abs(num - target) < min_sub: + min_sub = abs(num - target) + res = num return res \ No newline at end of file diff --git "a/0271.\345\255\227\347\254\246\344\270\262\347\232\204\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/0271-\345\255\227\347\254\246\344\270\262\347\232\204\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201.py" "b/0271.\345\255\227\347\254\246\344\270\262\347\232\204\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/0271-\345\255\227\347\254\246\344\270\262\347\232\204\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201.py" new file mode 100644 index 0000000..c5a3e7e --- /dev/null +++ "b/0271.\345\255\227\347\254\246\344\270\262\347\232\204\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/0271-\345\255\227\347\254\246\344\270\262\347\232\204\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201.py" @@ -0,0 +1,21 @@ +class Codec: + + def encode(self, strs): + """Encodes a list of strings to a single string. + + :type strs: List[str] + :rtype: str + """ + return strs + + def decode(self, s): + """Decodes a single string to a list of strings. + + :type s: str + :rtype: List[str] + """ + return s + +# Your Codec object will be instantiated and called as such: +# codec = Codec() +# codec.decode(codec.encode(strs)) \ 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/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/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..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.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/0273.\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272/0273-\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272.py" "b/0273.\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272/0273-\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272.py" new file mode 100644 index 0000000..30255cf --- /dev/null +++ "b/0273.\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272/0273-\346\225\264\346\225\260\350\275\254\346\215\242\350\213\261\346\226\207\350\241\250\347\244\272.py" @@ -0,0 +1,48 @@ +class Solution(object): + def numberToWords(self, num): + """ + :type num: int + :rtype: str + """ + def helper(num): + n = int(num) + num = str(n) + if n < 100: + return subhelper(num) + else: + return ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred " + subhelper(num[1:]) if num[1:] != "00" else ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred" + + def subhelper(num): + n = int(num) + l1 = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] + l2 = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] + l3 = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] + if n < 10: + return l1[int(num)] + if 10 <= n < 20: + return l2[n - 10] + if 20 <= n < 100: + return l3[int(num[0]) - 2] + " " + l1[int(num[1])] if num[1] != "0" else l3[int(num[0]) - 2] + res = "" + if num >= 1000000000: + res = helper(str(num)[0]) + " Billion" + if str(num)[1:4] != "000": + res += " " + helper(str(num)[1:4]) + " Million" + if str(num)[4:7] != "000": + res += " " + helper(str(num)[4:7]) + " Thousand" + if str(num)[7:] != "000": + res += " " + helper(str(num)[7:]) + elif num >= 1000000: + res = helper(str(num)[:-6]) + " Million" + if str(num)[-6:-3] != "000": + res += " " + helper(str(num)[-6:-3]) + " Thousand" + if str(num)[-3:] != "000": + res += " " + helper(str(num)[-3:]) + elif num >= 1000: + res = helper(str(num)[:-3]) + " Thousand" + if str(num)[-3:] != "000": + res += " " + helper(str(num)[-3:]) + else: + return helper(str(num)) + + return res \ No newline at end of file diff --git "a/0274.H\346\214\207\346\225\260/0274-H\346\214\207\346\225\260.py" "b/0274.H\346\214\207\346\225\260/0274-H\346\214\207\346\225\260.py" index 2bf672d..dffc72c 100644 --- "a/0274.H\346\214\207\346\225\260/0274-H\346\214\207\346\225\260.py" +++ "b/0274.H\346\214\207\346\225\260/0274-H\346\214\207\346\225\260.py" @@ -4,17 +4,22 @@ def hIndex(self, citations): :type citations: List[int] :rtype: int """ + # 0 1 3 6 5 citations.sort() - l = len(citations) - lo, hi = 0, l - 1 + n = len(citations) + left, right = 0, len(citations) - 1 res = 0 - while(lo <= hi): - mid = lo + (hi - lo) // 2 - cnt = l - mid #midұ߻еԪظ - if citations[mid] >= cnt: + while left <= right: + + mid = (left + right) // 2 + + cnt = n - mid + # print left, right, mid, citations[mid], cnt, citations + if citations[mid] < cnt: + left = mid + 1 + elif citations[mid] >= cnt: res = cnt - hi = mid -1 - else: - lo = mid + 1 + right = mid - 1 return res - \ No newline at end of file + + \ No newline at end of file diff --git "a/0275.H\346\214\207\346\225\260II/0275-H\346\214\207\346\225\260II.py" "b/0275.H\346\214\207\346\225\260II/0275-H\346\214\207\346\225\260II.py" index b488684..262a61f 100644 --- "a/0275.H\346\214\207\346\225\260II/0275-H\346\214\207\346\225\260II.py" +++ "b/0275.H\346\214\207\346\225\260II/0275-H\346\214\207\346\225\260II.py" @@ -4,16 +4,18 @@ def hIndex(self, citations): :type citations: List[int] :rtype: int """ - l = len(citations) - lo, hi = 0, l - 1 + n = len(citations) + left, right = 0, len(citations) - 1 res = 0 - while(lo <= hi): - mid = lo + (hi - lo) // 2 - cnt = l - mid #midұ߻еԪظ - if citations[mid] >= cnt: + while left <= right: + + mid = (left + right) // 2 + + cnt = n - mid + # print left, right, mid, citations[mid], cnt, citations + if citations[mid] < cnt: + left = mid + 1 + elif citations[mid] >= cnt: res = cnt - hi = mid -1 - else: - lo = mid + 1 - return res - \ No newline at end of file + right = mid - 1 + return res \ No newline at end of file diff --git "a/0276.\346\240\205\346\240\217\346\266\202\350\211\262/0276-\346\240\205\346\240\217\346\266\202\350\211\262.py" "b/0276.\346\240\205\346\240\217\346\266\202\350\211\262/0276-\346\240\205\346\240\217\346\266\202\350\211\262.py" new file mode 100644 index 0000000..4ac7c88 --- /dev/null +++ "b/0276.\346\240\205\346\240\217\346\266\202\350\211\262/0276-\346\240\205\346\240\217\346\266\202\350\211\262.py" @@ -0,0 +1,12 @@ +class Solution(object): + def numWays(self, n, k): + """ + :type n: int + :type k: int + :rtype: int + """ + dp = [0] * (n + 3) + dp[0], dp[1], dp[2] = 0, k, k * k + for i in range(3, n + 1): + dp[i] = dp[i - 1] * (k - 1) + dp[i - 2] * (k - 1) + return dp[n] \ No newline at end of file diff --git "a/0277.\346\220\234\345\257\273\345\220\215\344\272\272/0277-\346\220\234\345\257\273\345\220\215\344\272\272.py" "b/0277.\346\220\234\345\257\273\345\220\215\344\272\272/0277-\346\220\234\345\257\273\345\220\215\344\272\272.py" new file mode 100644 index 0000000..fa78e60 --- /dev/null +++ "b/0277.\346\220\234\345\257\273\345\220\215\344\272\272/0277-\346\220\234\345\257\273\345\220\215\344\272\272.py" @@ -0,0 +1,26 @@ +# The knows API is already defined for you. +# @param a, person a +# @param b, person b +# @return a boolean, whether a knows b +# def knows(a, b): + +class Solution(object): + def findCelebrity(self, n): + """ + :type n: int + :rtype: int + """ + celebrity = 0 + for i in range(1, n): + if knows(celebrity, i): + #˵ǰcelebrity϶ˣΪʶ + celebrity = i + # celebrityضʶ [celebrity + 1, n - 1] + for i in range(celebrity): + if knows(celebrity, i): # Ϊȷcelebrity ʶ [0, celebrity - 1] + return -1 + + for i in range(n): + if not knows(i, celebrity): # Ϊȷ ÿ˶ʶ celebrity + return -1 + return celebrity \ No newline at end of file diff --git "a/0278.\347\254\254\344\270\200\344\270\252\351\224\231\350\257\257\347\232\204\347\211\210\346\234\254/0278-\347\254\254\344\270\200\344\270\252\351\224\231\350\257\257\347\232\204\347\211\210\346\234\254.py" "b/0278.\347\254\254\344\270\200\344\270\252\351\224\231\350\257\257\347\232\204\347\211\210\346\234\254/0278-\347\254\254\344\270\200\344\270\252\351\224\231\350\257\257\347\232\204\347\211\210\346\234\254.py" index 326cb90..305128c 100644 --- "a/0278.\347\254\254\344\270\200\344\270\252\351\224\231\350\257\257\347\232\204\347\211\210\346\234\254/0278-\347\254\254\344\270\200\344\270\252\351\224\231\350\257\257\347\232\204\347\211\210\346\234\254.py" +++ "b/0278.\347\254\254\344\270\200\344\270\252\351\224\231\350\257\257\347\232\204\347\211\210\346\234\254/0278-\347\254\254\344\270\200\344\270\252\351\224\231\350\257\257\347\232\204\347\211\210\346\234\254.py" @@ -1,22 +1,19 @@ - # The isBadVersion API is already defined for you. # @param version, an integer # @return a bool # def isBadVersion(version): -import math + class Solution(object): def firstBadVersion(self, n): """ :type n: int :rtype: int """ - start = 1 - end = n - while(end-start>1): - mid = start + (end-start)/2 + left, right = 1, n + while left < right: + mid = (left + right) // 2 if isBadVersion(mid): - end = mid + right = mid else: - start = mid - print start, end - return start if isBadVersion(start) else end \ No newline at end of file + left = mid + 1 + return left \ No newline at end of file diff --git "a/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/0279-\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.py" "b/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/0279-\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.py" index 51b8b39..7b6dda6 100644 --- "a/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/0279-\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.py" +++ "b/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/0279-\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.py" @@ -1,29 +1,19 @@ -from collections import deque class Solution(object): def numSquares(self, n): """ :type n: int :rtype: int """ - record = [] - for i in range(1, int(n ** 0.5) + 1): - record.append(i * i) - # print record + from collections import deque + queue = deque([(0, 0)]) visited = set() - q = deque() - q.append([0, 0]) - while(q): - m, cnt = q.popleft() - - for num in record: - s = m + num + while queue: + cur, step = queue.popleft() + for i in range(1, int(n ** 0.5) + 1): + s = cur + i ** 2 if s == n: - return cnt + 1 - if s < n and s not in visited: + return step + 1 + if s not in visited: visited.add(s) - q.append([s, cnt + 1]) - # return - - - - \ No newline at end of file + queue.append((s, step + 1)) + \ No newline at end of file diff --git "a/0280.\346\221\206\345\212\250\346\216\222\345\272\217/0280-\346\221\206\345\212\250\346\216\222\345\272\217.py" "b/0280.\346\221\206\345\212\250\346\216\222\345\272\217/0280-\346\221\206\345\212\250\346\216\222\345\272\217.py" new file mode 100644 index 0000000..42a9b22 --- /dev/null +++ "b/0280.\346\221\206\345\212\250\346\216\222\345\272\217/0280-\346\221\206\345\212\250\346\216\222\345\272\217.py" @@ -0,0 +1,17 @@ +class Solution(object): + def wiggleSort(self, nums): + """ + :type nums: List[int] + :rtype: None Do not return anything, modify nums in-place instead. + """ + if not nums: + return nums + i = 0 + while i < len(nums): + if i % 2 == 0 and i < len(nums) - 1 and nums[i] > nums[i + 1]: + nums[i], nums[i + 1] = nums[i + 1], nums[i] + elif i % 2 and i < len(nums) - 1 and nums[i] < nums[i + 1]: + nums[i], nums[i + 1] = nums[i + 1], nums[i] + else: + i += 1 + return nums diff --git "a/0281.\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250/0281-\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250.py" "b/0281.\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250/0281-\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250.py" index 0ae455c..e70ddb8 100644 --- "a/0281.\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250/0281-\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250.py" +++ "b/0281.\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250/0281-\351\224\257\351\275\277\350\277\255\344\273\243\345\231\250.py" @@ -6,35 +6,36 @@ def __init__(self, v1, v2): :type v1: List[int] :type v2: List[int] """ - self.list = [] - if v1 and v2: - i = 0 - for i in range(min(len(v1), len(v2))): - self.list.append(v1[i]) - self.list.append(v2[i]) - if v1[i + 1:]: - self.list += v1[i + 1:] - else: - self.list += v2[i + 1:] - - elif v1: - self.list = v1 - else: - self.list = v2 - self.index = 0 + self.i1, self.i2 = 0, 0 + self.v1, self.v2 = v1, v2 + self.indicator = 0 # 0 for v1, 1 for v2 + def next(self): """ :rtype: int """ - self.index += 1 - return self.list[self.index - 1] + if not self.indicator: + self.indicator = 1 + if self.i1 < len(self.v1): + self.i1 += 1 + return self.v1[self.i1 - 1] + else: + self.i2 += 1 + return self.v2[self.i2 - 1] + else: + self.indicator = 0 + if self.i2 < len(self.v2): + self.i2 += 1 + return self.v2[self.i2 - 1] + else: + self.i1 += 1 + return self.v1[self.i1 - 1] def hasNext(self): """ :rtype: bool """ - return self.index != len(self.list) - + return self.i1 < len(self.v1) or self.i2 < len(self.v2) # Your ZigzagIterator object will be instantiated and called as such: # i, v = ZigzagIterator(v1, v2), [] diff --git "a/0283.\347\247\273\345\212\250\351\233\266/0283-\347\247\273\345\212\250\351\233\266.py" "b/0283.\347\247\273\345\212\250\351\233\266/0283-\347\247\273\345\212\250\351\233\266.py" index 3dbbd55..897a7c7 100644 --- "a/0283.\347\247\273\345\212\250\351\233\266/0283-\347\247\273\345\212\250\351\233\266.py" +++ "b/0283.\347\247\273\345\212\250\351\233\266/0283-\347\247\273\345\212\250\351\233\266.py" @@ -2,15 +2,12 @@ class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] - :rtype: void Do not return anything, modify nums in-place instead. + :rtype: None Do not return anything, modify nums in-place instead. """ - l = len(nums) - for index,item in enumerate(nums): - if item != 0: - continue - else: - for index0 in range(index+1,l): - if nums[index0] != 0: - print nums[index], nums[index0] - nums[index], nums[index0] = nums[index0], nums[index] - break \ No newline at end of file + i = 0 + for j in range(len(nums)): + if nums[j] != 0: + nums[i], nums[j] = nums[j], nums[i] + i += 1 + + # return nums \ No newline at end of file diff --git "a/0284.\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250/0284-\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250.py" "b/0284.\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250/0284-\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250.py" index 4eb1e35..a29ee7e 100644 --- "a/0284.\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250/0284-\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250.py" +++ "b/0284.\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250/0284-\351\241\266\347\253\257\350\277\255\344\273\243\345\231\250.py" @@ -25,32 +25,32 @@ def __init__(self, iterator): Initialize your data structure here. :type iterator: Iterator """ - self.list = list() - while(iterator.hasNext()): - self.list.append(iterator.next()) + self.l = [] + while iterator.hasNext(): + self.l.append(iterator.next()) + # self.l = iterator + self.index = 0 + def peek(self): """ Returns the next element in the iteration without advancing the iterator. :rtype: int """ - return self.list[0] - + return self.l[self.index] def next(self): """ :rtype: int """ - return self.list.pop(0) - - + self.index += 1 + return self.l[self.index - 1] def hasNext(self): """ :rtype: bool """ - return len(self.list) != 0 - + return self.index < len(self.l) # Your PeekingIterator object will be instantiated and called as such: # iter = PeekingIterator(Iterator(nums)) diff --git "a/0285.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247/0285-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247.py" "b/0285.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247/0285-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247.py" new file mode 100644 index 0000000..fbca755 --- /dev/null +++ "b/0285.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247/0285-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\351\241\272\345\272\217\345\220\216\347\273\247.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 inorderSuccessor(self, root, p): + """ + :type root: TreeNode + :type p: TreeNode + :rtype: TreeNode + """ + if not root: + return None + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node] + inorder(node.right) + l = inorder(root) + i = l.index(p) + return l[i + 1] if i < len(l) - 1 else None \ 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" new file mode 100644 index 0000000..0c0707e --- /dev/null +++ "b/0286.\345\242\231\344\270\216\351\227\250/0286-\345\242\231\344\270\216\351\227\250.py" @@ -0,0 +1,29 @@ +class Solution: + def wallsAndGates(self, rooms: List[List[int]]) -> None: + """ + Do not return anything, modify rooms in-place instead. + """ + from collections import deque + + if not rooms or not rooms[0]: + return rooms + INF = 2147483647 + + 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, 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/0287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/0287-\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260.py" "b/0287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/0287-\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260.py" index 1c8a8d8..6e077e2 100644 --- "a/0287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/0287-\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260.py" +++ "b/0287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/0287-\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260.py" @@ -4,17 +4,13 @@ def findDuplicate(self, nums): :type nums: List[int] :rtype: int """ - slow, fast = 0, 0 - while(1): + while 1: slow = nums[slow] fast = nums[nums[fast]] - # print slow, fast - if slow == fast: # loop exists + if slow == fast: fast = 0 - while(nums[slow] != nums[fast]): + while nums[slow] != nums[fast]: slow = nums[slow] fast = nums[fast] - # print slow, fast - return nums[fast] - \ No newline at end of file + return nums[fast] \ No newline at end of file diff --git "a/0288.\345\215\225\350\257\215\347\232\204\345\224\257\344\270\200\347\274\251\345\206\231/0288-\345\215\225\350\257\215\347\232\204\345\224\257\344\270\200\347\274\251\345\206\231.py" "b/0288.\345\215\225\350\257\215\347\232\204\345\224\257\344\270\200\347\274\251\345\206\231/0288-\345\215\225\350\257\215\347\232\204\345\224\257\344\270\200\347\274\251\345\206\231.py" new file mode 100644 index 0000000..4597793 --- /dev/null +++ "b/0288.\345\215\225\350\257\215\347\232\204\345\224\257\344\270\200\347\274\251\345\206\231/0288-\345\215\225\350\257\215\347\232\204\345\224\257\344\270\200\347\274\251\345\206\231.py" @@ -0,0 +1,30 @@ +class ValidWordAbbr(object): + + def __init__(self, dictionary): + """ + :type dictionary: List[str] + """ + from collections import defaultdict + self.dic = defaultdict(list) + for word in dictionary: + if len(word) <= 2: + self.dic[word].append(word) + else: + self.dic[word[0] + str(len(word) - 2) + word[-1]].append(word) + + def isUnique(self, word): + """ + :type word: str + :rtype: bool + """ + if len(word) <= 2: + return not self.dic[word] or self.dic[word] == [word] * len(self.dic[word]) + else: + s = word[0] + str(len(word) - 2) + word[-1] + return not self.dic[s] or self.dic[s] == [word] * len(self.dic[s]) + + + +# Your ValidWordAbbr object will be instantiated and called as such: +# obj = ValidWordAbbr(dictionary) +# param_1 = obj.isUnique(word) \ 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/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/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" index 5db3667..4cf3306 100644 --- "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.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.py" @@ -2,52 +2,36 @@ class Solution(object): def gameOfLife(self, board): """ :type board: List[List[int]] - :rtype: void Do not return anything, modify board in-place instead. + :rtype: None 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 + 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] - 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] - + def countLiveCells(x0, y0): cnt = 0 for k in range(8): - xx = x + dx[k] - yy = y + dy[k] + x = x0 + dx[k] + y = y0 + dy[k] + + if 0 <= x < m and 0 <= y < n and board[x][y] == 1: + cnt += 1 - if 0 <= xx < m and 0 <= yy < n and board[xx][yy] == 1: - cnt += 1 - return cnt - + liveCellSet = set() 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 + 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): - 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 + 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/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/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" index 1b425aa..93a230e 100644 --- "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.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.py" @@ -1,29 +1,20 @@ class Solution(object): - def wordPattern(self, pattern, str): + def wordPattern(self, pattern, s): """ :type pattern: str :type str: str :rtype: bool """ - s = str.split(" ") + s = s.split(" ") if len(pattern) != len(s): return False - record = [0 for i in range(0, 26)] #¼patternÿĸֵĴ - hashmap = dict() - - for i, word in enumerate(s): - t = ord(pattern[i]) - ord("a") - - if word not in hashmap.keys(): - if record[t] > 0: - return False - hashmap[word] = pattern[i] - record[t] = 1 + dic = {} + for i, char in enumerate(pattern): + if char not in dic: + dic[char] = s[i] else: - if hashmap[word] != pattern[i]: + if dic[char] != s[i]: return False - - return True - - - \ No newline at end of file + + 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/0293.\347\277\273\350\275\254\346\270\270\346\210\217/0293-\347\277\273\350\275\254\346\270\270\346\210\217.py" "b/0293.\347\277\273\350\275\254\346\270\270\346\210\217/0293-\347\277\273\350\275\254\346\270\270\346\210\217.py" index 0d67248..3b268c1 100644 --- "a/0293.\347\277\273\350\275\254\346\270\270\346\210\217/0293-\347\277\273\350\275\254\346\270\270\346\210\217.py" +++ "b/0293.\347\277\273\350\275\254\346\270\270\346\210\217/0293-\347\277\273\350\275\254\346\270\270\346\210\217.py" @@ -6,6 +6,6 @@ def generatePossibleNextMoves(self, s): """ res = [] for i in range(len(s) - 1): - if s[i:i+2] == "++": - res.append(s[:i] + "--" + s[i+2:]) + if s[i:i + 2] == "++": + res.append(s[:i] + "--" + s[i + 2:]) return res \ No newline at end of file diff --git "a/0294.\347\277\273\350\275\254\346\270\270\346\210\217II/0294-\347\277\273\350\275\254\346\270\270\346\210\217II.py" "b/0294.\347\277\273\350\275\254\346\270\270\346\210\217II/0294-\347\277\273\350\275\254\346\270\270\346\210\217II.py" new file mode 100644 index 0000000..0bfa03a --- /dev/null +++ "b/0294.\347\277\273\350\275\254\346\270\270\346\210\217II/0294-\347\277\273\350\275\254\346\270\270\346\210\217II.py" @@ -0,0 +1,19 @@ +class Solution(object): + def canWin(self, string): + """ + :type s: str + :rtype: bool + """ + record = {} + def helper(s): + if s in record: + return record[s] + for i in range(len(s) - 1): + if s[i:i + 2] == "++": + next_s = s[:i] + "--" + s[i + 2:] + if not helper(next_s): + record[next_s] = False + return True + return False # ++ + + return helper(string) \ No newline at end of file diff --git a/0295.find-median-from-data-stream/find-median-from-data-stream.md b/0295.find-median-from-data-stream/find-median-from-data-stream.md deleted file mode 100644 index 35487b2..0000000 --- a/0295.find-median-from-data-stream/find-median-from-data-stream.md +++ /dev/null @@ -1,34 +0,0 @@ -

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

-For example, - -

[2,3,4], the median is 3

- -

[2,3], the median is (2 + 3) / 2 = 2.5

- -

Design a data structure that supports the following two operations:

- -
    -
  • void addNum(int num) - Add a integer number from the data stream to the data structure.
  • -
  • double findMedian() - Return the median of all elements so far.
  • -
- -

 

- -

Example:

- -
-addNum(1)
-addNum(2)
-findMedian() -> 1.5
-addNum(3) 
-findMedian() -> 2
-
- -

 

- -

Follow up:

- -
    -
  1. If all integer numbers from the stream are between 0 and 100, how would you optimize it?
  2. -
  3. If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
  4. -
diff --git a/0295.find-median-from-data-stream/find-median-from-data-stream.py b/0295.find-median-from-data-stream/find-median-from-data-stream.py deleted file mode 100644 index aaea467..0000000 --- a/0295.find-median-from-data-stream/find-median-from-data-stream.py +++ /dev/null @@ -1,45 +0,0 @@ -from heapq import * -class MedianFinder(object): -# άѣһ󶥶ѣһСѣСȴ󶥶Ҫ -# DZڵλsizeͬλѶ֮ͳ2 -# ֻһλͿsizeСǸѵĶѶ -# ½СѣȻСѵĶѶ󶥶ѣ -# ѣʹsize Ϊ1 - def __init__(self): - """ - initialize your data structure here. - """ - self.max_h = list() - self.min_h = list() - heapify(self.max_h) - heapify(self.min_h) - - - def addNum(self, num): - """ - :type num: int - :rtype: None - """ - heappush(self.min_h, num) - heappush(self.max_h, -heappop(self.min_h)) - if len(self.max_h) > len(self.min_h): - heappush(self.min_h, -heappop(self.max_h)) - - def findMedian(self): - """ - :rtype: float - """ - max_len = len(self.max_h) - min_len = len(self.min_h) - if max_len == min_len: #ѡλ - return (self.min_h[0] + -self.max_h[0]) / 2. - else:#Сѵsize һ >= 󶥶ѵsizeԴ𰸾СѵĶѶ - return self.min_h[0] / 1. - - - - -# Your MedianFinder object will be instantiated and called as such: -# obj = MedianFinder() -# obj.addNum(num) -# param_2 = obj.findMedian() \ No newline at end of file diff --git "a/0295.\346\225\260\346\215\256\346\265\201\347\232\204\344\270\255\344\275\215\346\225\260/0295-\346\225\260\346\215\256\346\265\201\347\232\204\344\270\255\344\275\215\346\225\260.py" "b/0295.\346\225\260\346\215\256\346\265\201\347\232\204\344\270\255\344\275\215\346\225\260/0295-\346\225\260\346\215\256\346\265\201\347\232\204\344\270\255\344\275\215\346\225\260.py" index 30e181d..2abe054 100644 --- "a/0295.\346\225\260\346\215\256\346\265\201\347\232\204\344\270\255\344\275\215\346\225\260/0295-\346\225\260\346\215\256\346\265\201\347\232\204\344\270\255\344\275\215\346\225\260.py" +++ "b/0295.\346\225\260\346\215\256\346\265\201\347\232\204\344\270\255\344\275\215\346\225\260/0295-\346\225\260\346\215\256\346\265\201\347\232\204\344\270\255\344\275\215\346\225\260.py" @@ -1,41 +1,42 @@ from heapq import * +# class Heaps(object): +# def __init__(self): +# self.min_heap = [] +# self.max_heap = [] + +# def __ class MedianFinder(object): -# άѣһ󶥶ѣһСѣСȴ󶥶Ҫ -# DZڵλsizeͬλѶ֮ͳ2 -# ֻһλͿsizeСǸѵĶѶ -# ½СѵҪСͰ󶥶 -# ½СѵҪ󣬾ͰС -# ѣʹsize Ϊ1 + def __init__(self): """ initialize your data structure here. """ - self.max_h = list() - self.min_h = list() - heapify(self.max_h) - heapify(self.min_h) - + self.min_heap = [] + self.max_heap = [] + heapify(self.min_heap) + heapify(self.max_heap) def addNum(self, num): """ :type num: int :rtype: None """ - heappush(self.min_h, num) - heappush(self.max_h, -heappop(self.min_h)) - if len(self.max_h) > len(self.min_h): - heappush(self.min_h, -heappop(self.max_h)) + heappush(self.min_heap, num) + heappush(self.max_heap, -heappop(self.min_heap)) + if len(self.max_heap) > len(self.min_heap): + heappush(self.min_heap, -heappop(self.max_heap)) + def findMedian(self): """ :rtype: float """ - max_len = len(self.max_h) - min_len = len(self.min_h) - if max_len == min_len: #ѡλ - return (self.min_h[0] + -self.max_h[0]) / 2. - else:#Сѵsize һ >= 󶥶ѵsizeԴ𰸾СѵĶѶ - return self.min_h[0] / 1. + l_min_heap = len(self.min_heap) + l_max_heap = len(self.max_heap) + if l_min_heap == l_max_heap: + return (self.min_heap[0] - self.max_heap[0]) /2. + else: + return self.min_heap[0]/1. diff --git "a/0296.\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271/0296-\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271.py" "b/0296.\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271/0296-\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271.py" new file mode 100644 index 0000000..e602a1f --- /dev/null +++ "b/0296.\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271/0296-\346\234\200\344\275\263\347\232\204\347\242\260\345\244\264\345\234\260\347\202\271.py" @@ -0,0 +1,29 @@ +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/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/0299.\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217/0299-\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217.py" "b/0299.\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217/0299-\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217.py" new file mode 100644 index 0000000..13447ba --- /dev/null +++ "b/0299.\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217/0299-\347\214\234\346\225\260\345\255\227\346\270\270\346\210\217.py" @@ -0,0 +1,22 @@ +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/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227/0300-\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.py" "b/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227/0300-\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.py" index 1582530..f5fcf2e 100644 --- "a/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227/0300-\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.py" +++ "b/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227/0300-\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.py" @@ -4,22 +4,11 @@ def lengthOfLIS(self, nums): :type nums: List[int] :rtype: int """ - l = len(nums) - if not l: - return 0 - dp = [1 for _ in range(l)] + dp = [1 for _ in nums] - for index, item in enumerate(nums): - dp[index] = self.find(nums[:index + 1], dp) + 1 - # print dp - return max(dp) - - - def find(self, nums, dp): - max_element = -1 * 2 << 31 - - for i in range(len(nums) - 2, -1, -1): - if nums[i] < nums[-1]: - max_element = max(max_element, dp[i]) - - return max_element if max_element != -1 * 2 << 31 else 0 \ No newline at end of file + 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/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/0309-\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.py" "b/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/0309-\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.py" new file mode 100644 index 0000000..b8f03fc --- /dev/null +++ "b/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/0309-\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.py" @@ -0,0 +1,15 @@ +class Solution(object): + def maxProfit(self, prices): + """ + :type prices: List[int] + :rtype: int + """ + dp = [[0 for _ in range(2)] for _ in range(len(prices))] + for i, price in enumerate(prices): + if i == 0: + dp[0][0] = 0 + dp[0][1] = -price + else: + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]) + dp[i][1] = max(dp[i - 1][1], dp[i - 2][0] - prices[i]) #ǰĽ + return dp[i][0] if prices else 0 \ 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" new file mode 100644 index 0000000..663b691 --- /dev/null +++ "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" @@ -0,0 +1,32 @@ +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为空 + 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 # 1 代表当前root + root.right = self.insert(root.right, val, i, res) + + 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/0322.\351\233\266\351\222\261\345\205\221\346\215\242/0322-\351\233\266\351\222\261\345\205\221\346\215\242.py" "b/0322.\351\233\266\351\222\261\345\205\221\346\215\242/0322-\351\233\266\351\222\261\345\205\221\346\215\242.py" index 5e27b06..13e35ba 100644 --- "a/0322.\351\233\266\351\222\261\345\205\221\346\215\242/0322-\351\233\266\351\222\261\345\205\221\346\215\242.py" +++ "b/0322.\351\233\266\351\222\261\345\205\221\346\215\242/0322-\351\233\266\351\222\261\345\205\221\346\215\242.py" @@ -5,22 +5,21 @@ def coinChange(self, coins, amount): :type amount: int :rtype: int """ - if amount == 0: - return 0 - dp = list() - max_int = 2 << 31 + from collections import deque - for i in range(amount + 1): - if i not in coins: - dp.append(max_int) - else: - dp.append(1) - - for i in range(amount + 1): - if i not in coins: - for j in coins: - if i - j > 0: - dp[i] = min(dp[i - j] + 1, dp[i]) + 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 dp[amount] if dp[amount] != max_int else -1 - \ No newline at end of file + return -1 \ 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/0326.3\347\232\204\345\271\202/0326-3\347\232\204\345\271\202.py" "b/0326.3\347\232\204\345\271\202/0326-3\347\232\204\345\271\202.py" index aedc50d..513bb5a 100644 --- "a/0326.3\347\232\204\345\271\202/0326-3\347\232\204\345\271\202.py" +++ "b/0326.3\347\232\204\345\271\202/0326-3\347\232\204\345\271\202.py" @@ -4,9 +4,7 @@ def isPowerOfThree(self, n): :type n: 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/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/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" index 6a23614..ceb97e9 100644 --- "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.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.py" @@ -1,8 +1,15 @@ class Solution(object): def reverseString(self, s): """ - :type s: str - :rtype: str + :type s: List[str] + :rtype: None Do not return anything, modify s in-place instead. """ - return s[::-1] + 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/0365.\346\260\264\345\243\266\351\227\256\351\242\230/0365-\346\260\264\345\243\266\351\227\256\351\242\230.py" "b/0365.\346\260\264\345\243\266\351\227\256\351\242\230/0365-\346\260\264\345\243\266\351\227\256\351\242\230.py" new file mode 100644 index 0000000..29817d8 --- /dev/null +++ "b/0365.\346\260\264\345\243\266\351\227\256\351\242\230/0365-\346\260\264\345\243\266\351\227\256\351\242\230.py" @@ -0,0 +1,21 @@ +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/0366.\345\257\273\346\211\276\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/0366-\345\257\273\346\211\276\345\256\214\345\205\250\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/0366.\345\257\273\346\211\276\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/0366-\345\257\273\346\211\276\345\256\214\345\205\250\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" new file mode 100644 index 0000000..3bb10d6 --- /dev/null +++ "b/0366.\345\257\273\346\211\276\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/0366-\345\257\273\346\211\276\345\256\214\345\205\250\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" @@ -0,0 +1,50 @@ +# 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, deque + outdegree = defaultdict(int) + child2parent = {root:None} + leavesNodes = [] + def dfs(node): + if not node: + return + + if node.left: + outdegree[node] += 1 + child2parent[node.left] = node + dfs(node.left) + if node.right: + outdegree[node] += 1 + child2parent[node.right] = node + dfs(node.right) + + if not outdegree[node]: + leavesNodes.append(node) + dfs(root) + + res = [] + queue = deque(leavesNodes) + while queue: + tmp = [] + for _ in range(len(queue)): + cur = queue.popleft() + tmp.append(cur.val) + + parent = child2parent[cur] + outdegree[parent] -= 1 + + if not outdegree[parent]: + queue.append(parent) + res.append(tmp) + return res + \ No newline at end of file diff --git a/0377.combination-sum-iv/combination-sum-iv.md b/0377.combination-sum-iv/combination-sum-iv.md deleted file mode 100644 index 4529463..0000000 --- a/0377.combination-sum-iv/combination-sum-iv.md +++ /dev/null @@ -1,31 +0,0 @@ -

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

- -

Example:

- -
-nums = [1, 2, 3]
-target = 4
-
-The possible combination ways are:
-(1, 1, 1, 1)
-(1, 1, 2)
-(1, 2, 1)
-(1, 3)
-(2, 1, 1)
-(2, 2)
-(3, 1)
-
-Note that different sequences are counted as different combinations.
-
-Therefore the output is 7.
-
- -

 

- -

Follow up:
-What if negative numbers are allowed in the given array?
-How does it change the problem?
-What limitation we need to add to the question to allow negative numbers?

- -

Credits:
-Special thanks to @pbrother for adding this problem and creating all test cases.

diff --git a/0377.combination-sum-iv/combination-sum-iv.py b/0377.combination-sum-iv/combination-sum-iv.py deleted file mode 100644 index 4d791c8..0000000 --- a/0377.combination-sum-iv/combination-sum-iv.py +++ /dev/null @@ -1,16 +0,0 @@ -class Solution(object): - def combinationSum4(self, nums, target): - """ - :type nums: List[int] - :type target: int - :rtype: int - """ - dp = [0 for _ in range(target + 1)] - dp[0] = 1 - nums.sort() - for i in range(1, target + 1): - for num in nums: - if i >= num: - dp[i] += dp[i - num] - - return dp[target] \ 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/0386.\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260/0386-\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260.py" "b/0386.\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260/0386-\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260.py" new file mode 100644 index 0000000..140f172 --- /dev/null +++ "b/0386.\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260/0386-\345\255\227\345\205\270\345\272\217\346\216\222\346\225\260.py" @@ -0,0 +1,20 @@ +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/0387.\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/0387-\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/0387.\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/0387-\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" new file mode 100644 index 0000000..a640d0d --- /dev/null +++ "b/0387.\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/0387-\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" @@ -0,0 +1,12 @@ +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/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" 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.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/0398.\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225/0398-\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225.py" "b/0398.\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225/0398-\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225.py" new file mode 100644 index 0000000..f69bb27 --- /dev/null +++ "b/0398.\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225/0398-\351\232\217\346\234\272\346\225\260\347\264\242\345\274\225.py" @@ -0,0 +1,22 @@ +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/0409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/0409-\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262.py" "b/0409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/0409-\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262.py" index 10c4522..1acf581 100644 --- "a/0409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/0409-\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262.py" +++ "b/0409.\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262/0409-\346\234\200\351\225\277\345\233\236\346\226\207\344\270\262.py" @@ -4,16 +4,4 @@ def longestPalindrome(self, s): :type s: str :rtype: int """ - record = [0 for i in range(0, 129)] - for char in s: - record[ord(char)] += 1 - - res, flag = 0, 0 - for i, x in enumerate(record): - if x % 2 == 1: - res += x - 1 - flag = 1 - else: - res += x - # print res - return res + flag \ No newline at end of file + 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/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/0415.\345\255\227\347\254\246\344\270\262\347\233\270\345\212\240/0415-\345\255\227\347\254\246\344\270\262\347\233\270\345\212\240.py" "b/0415.\345\255\227\347\254\246\344\270\262\347\233\270\345\212\240/0415-\345\255\227\347\254\246\344\270\262\347\233\270\345\212\240.py" index 4c2a9a0..86f7c94 100644 --- "a/0415.\345\255\227\347\254\246\344\270\262\347\233\270\345\212\240/0415-\345\255\227\347\254\246\344\270\262\347\233\270\345\212\240.py" +++ "b/0415.\345\255\227\347\254\246\344\270\262\347\233\270\345\212\240/0415-\345\255\227\347\254\246\344\270\262\347\233\270\345\212\240.py" @@ -1,41 +1,36 @@ class Solution(object): - def addStrings(self, nums1, nums2): + def addStrings(self, s1, s2): """ :type num1: str :type num2: str :rtype: str """ - if not nums1: - return nums2 - elif not nums2: - return nums1 - elif not nums1 and not nums2: - return "" - - l1, l2 = len(nums1), len(nums2) - if l1 > l2: #֤ l1 ϶ + l1, l2 = len(s1), len(s2) + if l1 < l2: + s1, s2 = s2, s1 l1, l2 = l2, l1 - nums1, nums2 = nums2, nums1 - n1 = list(nums1)[::-1] - n2 = list(nums2)[::-1] - - res = list() - for i in range(0, l2): - if i < l1: - res.append(int(n1[i]) + int(n2[i])) - else: - res.append(int(n2[i])) - - # print res - for i in range(0, l2): - while(res[i] > 9): - res[i] -= 10 - if i != l2 - 1: - res[i + 1] += 1 + s1 = [int(x) for x in s1] + s2 = [int(x) for x in s2] + s1, s2 = s1[::-1], s2[::-1] + for i, digit in enumerate(s2): + s1[i] += s2[i] + + s1 = self.CarrySolver(s1) + s1 = s1[::-1] + return "".join(str(x) for x in s1) + + def CarrySolver(self, nums): + #Ĺǣеÿһλýλ + #[15, 27, 12], [5, 8, 4, 1] + i = 0 + while i < len(nums): + if nums[i] >= 10: + carrier = nums[i] // 10 + if i == len(nums) - 1: + nums.append(carrier) else: - res.append(1) - l2 += 1 + nums[i + 1] += carrier + nums[i] %= 10 + i += 1 - return "".join(str(res[i]) for i in range(l2 - 1, -1, -1)) - - \ No newline at end of file + return nums \ No newline at end of file diff --git a/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.md b/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.md deleted file mode 100644 index bc312c7..0000000 --- a/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.md +++ /dev/null @@ -1,29 +0,0 @@ -

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.

- -

Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.

- -

Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.

- -

Note:
-

    -
  1. The order of returned grid coordinates does not matter.
  2. -
  3. Both m and n are less than 150.
  4. -
-

-

Example: -

-Given the following 5x5 matrix:
-
-  Pacific ~   ~   ~   ~   ~ 
-       ~  1   2   2   3  (5) *
-       ~  3   2   3  (4) (4) *
-       ~  2   4  (5)  3   1  *
-       ~ (6) (7)  1   4   5  *
-       ~ (5)  1   1   2   4  *
-          *   *   *   *   * Atlantic
-
-Return:
-
-[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
-
-

\ No newline at end of file diff --git a/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.py b/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.py deleted file mode 100644 index 4b52f57..0000000 --- a/0417.pacific-atlantic-water-flow/pacific-atlantic-water-flow.py +++ /dev/null @@ -1,62 +0,0 @@ -class Solution(object): - def pacificAtlantic(self, matrix): - """ - :type matrix: List[List[int]] - :rtype: List[List[int]] - """ - - if not matrix or not matrix[0]: - return list() - m, n = len(matrix), len(matrix[0]) - - po = [[0 for i in range(n)] for j in range(m)] #po[i][j] = 1ʹmatrix[i][j]Ա̫ƽˮ - ao = [[0 for i in range(n)] for j in range(m)] #ͬϣɴ - - - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - - def dfs(x0, y0, string): - if visited[x0][y0] == 1:# - return - visited[x0][y0] = 1 - - if string == "po": - po[x0][y0] = 1 - else: - ao[x0][y0] = 1 - - for k in range(4): - x = x0 + dx[k] - y = y0 + dy[k] - - if 0<= x < m and 0 <= y < n and matrix[x][y] >= matrix[x0][y0]: #һ - dfs(x, y, string) - - visited = [[0 for i in range(n)] for j in range(m)] - i = 0 - for j in range(n): - dfs(i, j, "po") #̫ƽ - - visited = [[0 for i in range(n)] for j in range(m)] - i = m - 1 - for j in range(n): - dfs(i, j, "ao") #Ĵ - - visited = [[0 for i in range(n)] for j in range(m)] - j = 0 - for i in range(m): - dfs(i, j, "po") #ߵ̫ƽ - - visited = [[0 for i in range(n)] for j in range(m)] - j = n - 1 - for i in range(m): - dfs(i, j, "ao") #ұߵĴ - - res = [] - for i in range(m): - for j in range(n): - if po[i][j] and ao[i][j]: #ȡ - res.append([i, j]) - - return res \ No newline at end of file diff --git a/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.md b/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.md deleted file mode 100644 index f48b4ec..0000000 --- a/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.md +++ /dev/null @@ -1,25 +0,0 @@ -

Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

- -

Note:
-

    -
  1. Input contains only lowercase English letters.
  2. -
  3. Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
  4. -
  5. Input length is less than 50,000.
  6. -
-

- -

Example 1:
-

-Input: "owoztneoer"
-
-Output: "012"
-
-

- -

Example 2:
-

-Input: "fviefuro"
-
-Output: "45"
-
-

\ No newline at end of file diff --git a/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.py b/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.py deleted file mode 100644 index 42daf38..0000000 --- a/0423.reconstruct-original-digits-from-english/reconstruct-original-digits-from-english.py +++ /dev/null @@ -1,30 +0,0 @@ -class Solution(object): - def originalDigits(self, s): - """ - :type s: str - :rtype: str - """ - # zero one two three four five six seven eight nine - # z o2 w r(4) u f(4) x s6 t(3) e - #һеIJҵ˳ two, four, six, one, three, five, seven, eight, nine - order = ["zero", "two", "four", "six", "one", "three", "five", "seven", "eight", "nine"] - find = ["z", "w", "u", "x", "o", "r", "f", "v", "t", "e"] - digit = [0, 2, 4, 6, 1, 3, 5, 7, 8, 9] - - record = [0 for _ in range(10)] - dic = collections.Counter(s) - - for idx in range(10): #digit˳0~9 - cnt = dic[find[idx]] #ɼdigit[idx] - record[digit[idx]] += cnt #¼ - dic = dic - collections.Counter(order[idx] * cnt) #ֵȥӦĸ - - if not dic: - break - - ress = "" - for i in range(10): #תĿҪĸʽ - ress += str(i) * record[i] - - return ress - \ No newline at end of file diff --git "a/0426.\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/0426-\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/0426.\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/0426-\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" new file mode 100644 index 0000000..669fb96 --- /dev/null +++ "b/0426.\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/0426-\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" @@ -0,0 +1,47 @@ +""" +# 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/0429.N\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0429-N\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" "b/0429.N\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0429-N\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" index 94a6107..f51f86d 100644 --- "a/0429.N\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0429-N\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" +++ "b/0429.N\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/0429-N\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.py" @@ -1,7 +1,7 @@ """ # Definition for a Node. class Node(object): - def __init__(self, val, children): + def __init__(self, val=None, children=None): self.val = val self.children = children """ @@ -13,20 +13,17 @@ def levelOrder(self, root): """ if not root: return [] - nodes = [root] - node_val = list(list()) - self.generate(nodes, node_val) - return node_val - - def generate(self, nodes, node_val): - new_node = [] - new_node_val = [] - for node in nodes: - for leaf in node.children: - new_node.append(leaf) - new_node_val.append(node.val) - node_val.append(new_node_val) - if len(new_node) == 0: - return - self.generate(new_node, node_val) - \ No newline at end of file + from collections import deque + queue = deque([root]) + res = [] + while queue: + layer = [] + for _ in range(len(queue)): + cur = queue.popleft() + if cur: + layer.append(cur.val) + for child in cur.children: + queue.append(child) + res.append(layer) + + return res diff --git "a/0430.\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/0430-\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/0430.\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/0430-\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" new file mode 100644 index 0000000..08d25c6 --- /dev/null +++ "b/0430.\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/0430-\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" @@ -0,0 +1,38 @@ +""" +# 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/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/0436.\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264/0436-\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264.py" "b/0436.\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264/0436-\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264.py" new file mode 100644 index 0000000..cffe9ed --- /dev/null +++ "b/0436.\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264/0436-\345\257\273\346\211\276\345\217\263\345\214\272\351\227\264.py" @@ -0,0 +1,23 @@ +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/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" new file mode 100644 index 0000000..90c371e --- /dev/null +++ "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" @@ -0,0 +1,18 @@ +class Solution(object): + def findMinArrowShots(self, points): + """ + :type points: List[List[int]] + :rtype: int + """ + if not points or not points[0]: + return 0 + + points = sorted(points, key = lambda x: x[1]) + res = 1 + pre_end = points[0][1] + + for i in range(1, len(points)): + if points[i][0] > pre_end: + res += 1 + 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/0477.\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214/0477-\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214.py" "b/0477.\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214/0477-\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214.py" new file mode 100644 index 0000000..9ce810a --- /dev/null +++ "b/0477.\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214/0477-\346\261\211\346\230\216\350\267\235\347\246\273\346\200\273\345\222\214.py" @@ -0,0 +1,20 @@ +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/0482.\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226/0482-\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226.py" "b/0482.\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226/0482-\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226.py" new file mode 100644 index 0000000..a58af44 --- /dev/null +++ "b/0482.\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226/0482-\345\257\206\351\222\245\346\240\274\345\274\217\345\214\226.py" @@ -0,0 +1,18 @@ +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/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" "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" new file mode 100644 index 0000000..b488d0e --- /dev/null +++ "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" @@ -0,0 +1,24 @@ +class Solution(object): + def nextGreaterElement(self, nums1, nums2): + """ + :type nums1: List[int] + :type nums2: List[int] + :rtype: List[int] + """ + mapping = dict() + + stack = [] + for num in nums2: + while stack and stack[-1] < num: + top = stack.pop() + mapping[top] = num + stack.append(num) + + res = [] + for num in nums1: + if num in mapping: + res.append(mapping[num]) + else: + res.append(-1) + + return res \ No newline at end of file diff --git "a/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/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" index b296758..b488d0e 100644 --- "a/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/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" @@ -1,19 +1,24 @@ class Solution(object): - def nextGreaterElement(self, findNums, nums): + def nextGreaterElement(self, nums1, nums2): """ - :type findNums: List[int] - :type nums: List[int] + :type nums1: List[int] + :type nums2: List[int] :rtype: List[int] """ - if not findNums or not nums: - return [] - res = list() - for item in findNums: - index = nums.index(item) - for i in range(index, len(nums)): - if nums[i] > item: - res.append(nums[i]) - break - if i + 1 == len(nums) and nums[-1] <= item: + mapping = dict() + + stack = [] + for num in nums2: + while stack and stack[-1] < num: + top = stack.pop() + mapping[top] = num + stack.append(num) + + res = [] + for num in nums1: + if num in mapping: + res.append(mapping[num]) + else: res.append(-1) + return res \ No newline at end of file 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.relative-ranks/relative-ranks.java b/0506.relative-ranks/relative-ranks.java deleted file mode 100644 index f38a036..0000000 --- a/0506.relative-ranks/relative-ranks.java +++ /dev/null @@ -1,43 +0,0 @@ -class Solution { - public int[] swap(int ary[]) { - int len = ary.length; - for (int i = 0; i < len / 2; i++) { - int tmp = ary[i]; - ary[i] = ary[len - 1 - i]; - ary[len - 1 - i] = tmp; - } - return ary; -} - - public String[] findRelativeRanks(int[] nums) { - int n = nums.length; - int[] nums1 = Arrays.copyOf(nums, n); - String[] result = new String[n]; - - Arrays.sort(nums1); - nums1 = swap(nums1); - Map map = new HashMap<>(); - - for (int i = 0; i < n; i++){ - map.put(nums[i], i); - } - // System.out.println(Arrays.toString(nums1)); - - for (int i = 0; i < n; i++){ - if (i == 0){ - result[map.get(nums1[i])] = "Gold Medal"; - continue; - } - else if (i == 1){ - result[map.get(nums1[i])] = "Silver Medal"; - continue; - } - else if (i == 2){ - result[map.get(nums1[i])] = "Bronze Medal"; - continue; - } - result[map.get(nums1[i])] = String.valueOf(i + 1); - } - return result; - } -} \ No newline at end of file diff --git a/0506.relative-ranks/relative-ranks.md b/0506.relative-ranks/relative-ranks.md deleted file mode 100644 index 3fdad3e..0000000 --- a/0506.relative-ranks/relative-ranks.md +++ /dev/null @@ -1,17 +0,0 @@ -

-Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

- -

Example 1:
-

-Input: [5, 4, 3, 2, 1]
-Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
-Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
For the left two athletes, you just need to output their relative ranks according to their scores. -
-

- -

Note:
-

    -
  1. N is a positive integer and won't exceed 10,000.
  2. -
  3. All the scores of athletes are guaranteed to be unique.
  4. -
-

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/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/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" index f677285..c9fbab3 100644 --- "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.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.py" @@ -11,18 +11,19 @@ def findBottomLeftValue(self, root): :type root: TreeNode :rtype: int """ - next_layer = [root] - while(next_layer): - temp_next_layer = [] - layer_value = [] - for node in next_layer: - if node.left: - temp_next_layer.append(node.left) - if node.right: - temp_next_layer.append(node.right) - layer_value.append(node.val) - # print layer_value - next_layer = temp_next_layer - - # print layer_value - return layer_value[0] \ No newline at end of file + #层序遍历返回最后一层第一个 + 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/0543.\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204/0543-\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204.py" "b/0543.\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204/0543-\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204.py" new file mode 100644 index 0000000..8814891 --- /dev/null +++ "b/0543.\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204/0543-\344\272\214\345\217\211\346\240\221\347\232\204\347\233\264\345\276\204.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 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/0551.\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I/0551-\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I.py" "b/0551.\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I/0551-\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I.py" index f3ad16c..0283b38 100644 --- "a/0551.\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I/0551-\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I.py" +++ "b/0551.\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I/0551-\345\255\246\347\224\237\345\207\272\345\213\244\350\256\260\345\275\225I.py" @@ -4,8 +4,4 @@ def checkRecord(self, s): :type s: str :rtype: bool """ - if s.count("A") > 1 or "LLL" in s: - return False - - return True - \ No newline at end of file + return s.count("A") <= 1 and "LLL" not in s \ 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/0559.N\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246/0559-N\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.py" "b/0559.N\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246/0559-N\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.py" index 55bc02e..eedc4c7 100644 --- "a/0559.N\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246/0559-N\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.py" +++ "b/0559.N\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246/0559-N\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.py" @@ -1,7 +1,7 @@ """ # Definition for a Node. class Node(object): - def __init__(self, val, children): + def __init__(self, val=None, children=None): self.val = val self.children = children """ @@ -11,10 +11,9 @@ def maxDepth(self, root): :type root: Node :rtype: int """ - result = [] if not root: return 0 - for node in root.children: - result.append(self.maxDepth(node)) - - return 1 + max(result) if result else 1 \ No newline at end of file + res = 0 + for child in root.children: + res = max(res, self.maxDepth(child)) + return 1 + res \ No newline at end of file diff --git a/0560.subarray-sum-equals-k/subarray-sum-equals-k.md b/0560.subarray-sum-equals-k/subarray-sum-equals-k.md deleted file mode 100644 index a28a10b..0000000 --- a/0560.subarray-sum-equals-k/subarray-sum-equals-k.md +++ /dev/null @@ -1,15 +0,0 @@ -

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

- -

Example 1:
-

-Input:nums = [1,1,1], k = 2
-Output: 2
-
-

- -

Note:
-

    -
  1. The length of the array is in range [1, 20,000].
  2. -
  3. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
  4. -
-

diff --git a/0560.subarray-sum-equals-k/subarray-sum-equals-k.py b/0560.subarray-sum-equals-k/subarray-sum-equals-k.py deleted file mode 100644 index 2fbf949..0000000 --- a/0560.subarray-sum-equals-k/subarray-sum-equals-k.py +++ /dev/null @@ -1,18 +0,0 @@ -class Solution(object): - def subarraySum(self, nums, k): - """ - :type nums: List[int] - :type k: int - :rtype: int - """ - from collections import defaultdict - pre_sum = 0 - record = defaultdict(int) - record[0] = 1 - res = 0 - for i in range(len(nums)): - pre_sum += nums[i] - res += record[pre_sum - k] - record[pre_sum] += 1 - - return res \ No newline at end of file diff --git "a/0560.\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204/0560-\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204.py" "b/0560.\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204/0560-\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204.py" index 580ea25..12e2bc4 100644 --- "a/0560.\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204/0560-\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204.py" +++ "b/0560.\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204/0560-\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204.py" @@ -5,16 +5,19 @@ def subarraySum(self, nums, k): :type k: int :rtype: int """ + # prefix[i] = sum(nums[:i]) + # prefix[j] - prefix[i] = sum(nums[i:j]) from collections import defaultdict - pre_sum = 0 - record = defaultdict(int) - record[0] = 1 + + prefix = [0 for _ in range(len(nums) + 1)] + + for i, x in enumerate(nums): + prefix[i + 1] = prefix[i] + x + + dic = defaultdict(int) res = 0 - for i in range(len(nums)): - pre_sum += nums[i] - - # k - pre_sum - res += record[pre_sum - k] - record[pre_sum] += 1 - + for i, x in enumerate(prefix): + res += dic[x - k] + dic[x] += 1 + return res \ No newline at end of file diff --git "a/0572.\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221/0572-\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221.py" "b/0572.\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221/0572-\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221.py" new file mode 100644 index 0000000..7456e24 --- /dev/null +++ "b/0572.\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221/0572-\345\217\246\344\270\200\344\270\252\346\240\221\347\232\204\345\255\220\346\240\221.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 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/0581.\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/0581-\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/0581.\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/0581-\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" new file mode 100644 index 0000000..33bc985 --- /dev/null +++ "b/0581.\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/0581-\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" @@ -0,0 +1,19 @@ +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/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/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/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" index 35e21d7..d6ef697 100644 --- "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.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.py" @@ -1,7 +1,7 @@ """ # Definition for a Node. class Node(object): - def __init__(self, val, children): + def __init__(self, val=None, children=None): self.val = val self.children = children """ @@ -12,11 +12,9 @@ def preorder(self, root): :rtype: List[int] """ if not root: - return list() - result = list() - result.append(root.val) - - for leaf in root.children: - result += self.preorder(leaf) - - return result \ No newline at end of file + 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/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/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" index 0e1515b..bd35385 100644 --- "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.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.py" @@ -1,7 +1,7 @@ """ # Definition for a Node. class Node(object): - def __init__(self, val, children): + def __init__(self, val=None, children=None): self.val = val self.children = children """ @@ -13,8 +13,7 @@ def postorder(self, root): """ if not root: return [] - res = list() - for leaf in root.children: - res += self.postorder(leaf) - + res = [] + for child in root.children: + res += self.postorder(child) return res + [root.val] \ No newline at end of file diff --git a/0598.range-addition-ii/range-addition-ii.md b/0598.range-addition-ii/range-addition-ii.md deleted file mode 100644 index 29482f0..0000000 --- a/0598.range-addition-ii/range-addition-ii.md +++ /dev/null @@ -1,37 +0,0 @@ -

Given an m * n matrix M initialized with all 0's and several update operations.

-

Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for all 0 <= i < a and 0 <= j < b.

-

You need to count and return the number of maximum integers in the matrix after performing all the operations.

- -

Example 1:
-

-Input: 
-m = 3, n = 3
-operations = [[2,2],[3,3]]
-Output: 4
-Explanation: 
-Initially, M = 
-[[0, 0, 0],
- [0, 0, 0],
- [0, 0, 0]]
-
-After performing [2,2], M = 
-[[1, 1, 0],
- [1, 1, 0],
- [0, 0, 0]]
-
-After performing [3,3], M = 
-[[2, 2, 1],
- [2, 2, 1],
- [1, 1, 1]]
-
-So the maximum integer in M is 2, and there are four of it in M. So return 4.
-
-

- -

Note:
-

    -
  1. The range of m and n is [1,40000].
  2. -
  3. The range of a is [1,m], and the range of b is [1,n].
  4. -
  5. The range of operations size won't exceed 10,000.
  6. -
-

\ No newline at end of file diff --git a/0598.range-addition-ii/range-addition-ii.py b/0598.range-addition-ii/range-addition-ii.py deleted file mode 100644 index 2e619af..0000000 --- a/0598.range-addition-ii/range-addition-ii.py +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def maxCount(self, m, n, ops): - """ - :type m: int - :type n: int - :type ops: List[List[int]] - :rtype: int - """ - min1, min2 = m, n - for op in ops: - min1 = min(min1, op[0]) - min2 = min(min2, op[1]) - return min1 * min2 \ 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/0633.sum-of-square-numbers/sum-of-square-numbers.md b/0633.sum-of-square-numbers/sum-of-square-numbers.md deleted file mode 100644 index 9ba3b20..0000000 --- a/0633.sum-of-square-numbers/sum-of-square-numbers.md +++ /dev/null @@ -1,20 +0,0 @@ -

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

- -

Example 1:

- -
-Input: 5
-Output: True
-Explanation: 1 * 1 + 2 * 2 = 5
-
- -

 

- -

Example 2:

- -
-Input: 3
-Output: False
-
- -

 

diff --git a/0633.sum-of-square-numbers/sum-of-square-numbers.py b/0633.sum-of-square-numbers/sum-of-square-numbers.py deleted file mode 100644 index 07f79e0..0000000 --- a/0633.sum-of-square-numbers/sum-of-square-numbers.py +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def judgeSquareSum(self, c): - """ - :type c: int - :rtype: bool - """ - - if c == (int(c ** 0.5) ** 2): - return True - - lo, hi = 0, int(c ** 0.5) - while(lo <= hi): - s = lo ** 2 + hi ** 2 - if s == c: - return True - elif s > c: - hi -= 1 - else: - lo += 1 - return False - \ No newline at end of file diff --git "a/0633.\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214/0633-\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214.py" "b/0633.\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214/0633-\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214.py" index 07f79e0..9347d22 100644 --- "a/0633.\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214/0633-\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214.py" +++ "b/0633.\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214/0633-\345\271\263\346\226\271\346\225\260\344\271\213\345\222\214.py" @@ -4,18 +4,9 @@ def judgeSquareSum(self, c): :type c: int :rtype: bool """ - - if c == (int(c ** 0.5) ** 2): - return True - - lo, hi = 0, int(c ** 0.5) - while(lo <= hi): - s = lo ** 2 + hi ** 2 - if s == c: + for i in range(int(c ** 0.5) + 1): + t = c - i ** 2 + s = int (t ** 0.5) + if t == s ** 2: return True - elif s > c: - hi -= 1 - else: - lo += 1 - return False - \ No newline at end of file + return False if c else True \ No newline at end of file diff --git "a/0634.\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/0634-\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/0634.\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/0634-\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" new file mode 100644 index 0000000..99bf7db --- /dev/null +++ "b/0634.\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/0634-\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" @@ -0,0 +1,10 @@ +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/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/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/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" index 4c4960c..0f29e9f 100644 --- "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.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.py" @@ -11,22 +11,18 @@ def averageOfLevels(self, root): :type root: TreeNode :rtype: List[float] """ + from collections import deque if not root: return [] - next_layer = [root.left, root.right] - result = [float(root.val)] - - while(next_layer): - temp_next_layer = list() - layer_value = list() - for node in next_layer: - if not node: - continue - temp_next_layer.append(node.left) - temp_next_layer.append(node.right) - layer_value.append(node.val) - if layer_value: - result.append(sum(layer_value) / float(len(layer_value))) - next_layer = temp_next_layer - return result - \ No newline at end of file + 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/0646.\346\234\200\351\225\277\346\225\260\345\257\271\351\223\276/0646-\346\234\200\351\225\277\346\225\260\345\257\271\351\223\276.py" "b/0646.\346\234\200\351\225\277\346\225\260\345\257\271\351\223\276/0646-\346\234\200\351\225\277\346\225\260\345\257\271\351\223\276.py" new file mode 100644 index 0000000..fc77aab --- /dev/null +++ "b/0646.\346\234\200\351\225\277\346\225\260\345\257\271\351\223\276/0646-\346\234\200\351\225\277\346\225\260\345\257\271\351\223\276.py" @@ -0,0 +1,16 @@ +class Solution(object): + def findLongestChain(self, pairs): + """ + :type pairs: List[List[int]] + :rtype: int + """ + pairs = sorted(pairs, key = lambda x: x[1]) + + end = pairs[0][0] - 1 + res = 0 + for pair in pairs: + if pair[0] > end: + res += 1 + end = pair[1] + + return res \ 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/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0669-\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" "b/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0669-\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.py" new file mode 100644 index 0000000..b672ba5 --- /dev/null +++ "b/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/0669-\344\277\256\345\211\252\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(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution(object): + def trimBST(self, root, L, R): + """ + :type root: TreeNode + :type L: int + :type R: int + :rtype: TreeNode + """ + if not root: + return None + + root.left = self.trimBST(root.left, L, R) + root.right = self.trimBST(root.right, L, R) + + if root.val < L: + return root.right + if root.val > R: + return root.left + return root \ 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/0697.degree-of-an-array/degree-of-an-array.md b/0697.degree-of-an-array/degree-of-an-array.md deleted file mode 100644 index 7c9c87b..0000000 --- a/0697.degree-of-an-array/degree-of-an-array.md +++ /dev/null @@ -1,27 +0,0 @@ -

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.

-

Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.

- -

Example 1:
-

-Input: [1, 2, 2, 3, 1]
-Output: 2
-Explanation: 
-The input array has a degree of 2 because both elements 1 and 2 appear twice.
-Of the subarrays that have the same degree:
-[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
-The shortest length is 2. So return 2.
-
-

- - -

Example 2:
-

-Input: [1,2,2,3,1,4,2]
-Output: 6
-
-

- -

Note: -

  • nums.length will be between 1 and 50,000.
  • -
  • nums[i] will be an integer between 0 and 49,999.
  • -

    \ No newline at end of file diff --git a/0697.degree-of-an-array/degree-of-an-array.py b/0697.degree-of-an-array/degree-of-an-array.py deleted file mode 100644 index 1b09e2d..0000000 --- a/0697.degree-of-an-array/degree-of-an-array.py +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def findShortestSubArray(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - - degree = 0 - for digit in set(nums): - degree = max(degree, nums.count(digit)) - - candidates = list() - for digit in set(nums): - if nums.count(digit) == degree: - candidates.append(digit) - l = len(nums) - reversenums = nums[::-1] - res = l - for candidate in candidates: - left_pos = nums.index(candidate) - right_pos = l - 1 - reversenums.index(candidate) - - res = min(res, right_pos - left_pos + 1) - - return res \ 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/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/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" index 0a76293..6576874 100644 --- "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.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.py" @@ -12,14 +12,10 @@ def searchBST(self, root, val): :type val: int :rtype: TreeNode """ - # print root.val - if not root: - return None - if root.val == val: + if not root or root.val == val: return root - elif root.val > val: + + if root.val > val: return self.searchBST(root.left, val) - else: - return self.searchBST(root.right, val) - - \ No newline at end of file + elif root.val < val: + return self.searchBST(root.right, val) \ No newline at end of file diff --git "a/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234/0701-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.py" "b/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234/0701-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.py" index b69b085..c8caff3 100644 --- "a/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234/0701-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.py" +++ "b/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234/0701-\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.py" @@ -1,22 +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 insertIntoBST(self, root, val): - """ - :type root: TreeNode - :type val: int - :rtype: TreeNode - """ if not root: - return - def helper(node, val): - if node.val < val: - if not node.right: - node.right = TreeNode(val) - else: - helper(node.right, val) - elif node.val > val: - if not node.left: - node.left = TreeNode(val) - else: - helper(node.left, val) - helper(root, val) - return root \ No newline at end of file + return TreeNode(val) + node, parent = root, root + while node: + parent = node + node = parent.left if val < parent.val else parent.right + if val > parent.val: + parent.right = TreeNode(val) + else: + parent.left = TreeNode(val) + return root +# class Solution(object): +# def insertIntoBST(self, root, val): +# """ +# :type root: TreeNode +# :type val: int +# :rtype: TreeNode +# """ +# if not root: +# return TreeNode(val) +# if root.val > val: +# root.left = self.insertIntoBST(root.left, val) +# else: +# root.right = self.insertIntoBST(root.right, val) +# return root \ 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/0714.\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\345\220\253\346\211\213\347\273\255\350\264\271/0714-\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\345\220\253\346\211\213\347\273\255\350\264\271.py" "b/0714.\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\345\220\253\346\211\213\347\273\255\350\264\271/0714-\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\345\220\253\346\211\213\347\273\255\350\264\271.py" new file mode 100644 index 0000000..f55fa80 --- /dev/null +++ "b/0714.\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\345\220\253\346\211\213\347\273\255\350\264\271/0714-\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\345\220\253\346\211\213\347\273\255\350\264\271.py" @@ -0,0 +1,18 @@ +class Solution(object): + def maxProfit(self, prices, fee): + """ + :type prices: List[int] + :type fee: int + :rtype: int + """ + dp = [[0 for _ in range(2)] for _ in range(len(prices))] + for i, price in enumerate(prices): + if i == 0: + dp[0][0] = 0 + dp[0][1] = -price + else: + # tmp = dp[i][0] + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]) + # print dp + return dp[i][0] if prices else 0 \ 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/0748.shortest-completing-word/shortest-completing-word.md b/0748.shortest-completing-word/shortest-completing-word.md deleted file mode 100644 index 2728f80..0000000 --- a/0748.shortest-completing-word/shortest-completing-word.md +++ /dev/null @@ -1,37 +0,0 @@ -

    -Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate -

    -Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word. -

    -It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array. -

    -The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does. -

    - -

    Example 1:
    -

    -Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
    -Output: "steps"
    -Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".
    -Note that the answer is not "step", because the letter "s" must occur in the word twice.
    -Also note that we ignored case for the purposes of comparing whether a letter exists in the word.
    -
    -

    - -

    Example 2:
    -

    -Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
    -Output: "pest"
    -Explanation: There are 3 smallest length words that contains the letters "s".
    -We return the one that occurred first.
    -
    -

    - -

    Note:
    -

      -
    1. licensePlate will be a string with length in range [1, 7].
    2. -
    3. licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
    4. -
    5. words will have a length in the range [10, 1000].
    6. -
    7. Every words[i] will consist of lowercase letters, and have length in range [1, 15].
    8. -
    -

    \ No newline at end of file diff --git a/0748.shortest-completing-word/shortest-completing-word.py b/0748.shortest-completing-word/shortest-completing-word.py deleted file mode 100644 index e5124c4..0000000 --- a/0748.shortest-completing-word/shortest-completing-word.py +++ /dev/null @@ -1,29 +0,0 @@ -class Solution(object): - def shortestCompletingWord(self, licensePlate, words): - """ - :type licensePlate: str - :type words: List[str] - :rtype: str - """ - charlist = [] - for i, char in enumerate(licensePlate): - if char.isalpha(): - t = char - t = t.lower() - charlist.append(t) - charlistrecord = collections.Counter(charlist) - res = "aaaaaaaaaaaaaaaa" - - for word in words: - cnt = 0 - wordrecord = collections.Counter(word) - - for char in charlist: - if wordrecord.get(char, 0) >= charlistrecord[char]: - cnt += 1 - # print word, cnt - if cnt == len(charlist): # - if len(word) < len(res): - res = word - - return res diff --git "a/0760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/0760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" "b/0760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/0760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" index ea4504e..58c9a94 100644 --- "a/0760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/0760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" +++ "b/0760.\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204/0760-\346\211\276\345\207\272\345\217\230\344\275\215\346\230\240\345\260\204.py" @@ -5,12 +5,9 @@ def anagramMappings(self, A, B): :type B: List[int] :rtype: List[int] """ - record = dict() - for i, b in enumerate(B): - record[b] = i - - res = [-1 for _ in range(len(A))] - for i, a in enumerate(A): - res[i] = record[a] - - return res \ No newline at end of file + + 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/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/0763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" "b/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/0763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" index 8b1001d..8216656 100644 --- "a/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/0763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" +++ "b/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264/0763-\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.py" @@ -4,23 +4,36 @@ def partitionLabels(self, S): :type S: str :rtype: List[int] """ - dic = {} + 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) - for index, char in enumerate(S): - dic[char] = index - - right = dic[S[0]] - left = 0 + intervals.sort() + #print intervals + res = [] - for index, char in enumerate(S): - right = max(right, dic[char]) - if index >= right: - res.append(right - left + 1) - left = right + 1 - - return res - - - - - \ No newline at end of file + 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/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/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" index 0060855..4b0e5c9 100644 --- "a/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/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" @@ -1,11 +1,13 @@ class Solution(object): def numJewelsInStones(self, J, S): - if len(J) == 0 or len(S) == 0: - return 0 - count = 0 - for itemins in S: - if itemins in J: - count += 1 - - return count - \ No newline at end of file + """ + :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/0780.\345\210\260\350\276\276\347\273\210\347\202\271/0780-\345\210\260\350\276\276\347\273\210\347\202\271.py" "b/0780.\345\210\260\350\276\276\347\273\210\347\202\271/0780-\345\210\260\350\276\276\347\273\210\347\202\271.py" new file mode 100644 index 0000000..aaf515f --- /dev/null +++ "b/0780.\345\210\260\350\276\276\347\273\210\347\202\271/0780-\345\210\260\350\276\276\347\273\210\347\202\271.py" @@ -0,0 +1,16 @@ +class Solution(object): + def reachingPoints(self, sx, sy, tx, ty): + """ + :type sx: int + :type sy: int + :type tx: int + :type ty: int + :rtype: bool + """ + if tx < sx or ty < sy: + return False + if tx == sx and (ty - sy) % sx == 0: + return True + if ty == sy and (tx - sx) % sy == 0: + return True + return self.reachingPoints(sx, sy, tx % ty, ty % tx) \ 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/0792.\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/0792-\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/0792.\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/0792-\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" new file mode 100644 index 0000000..81e7c4a --- /dev/null +++ "b/0792.\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/0792-\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" @@ -0,0 +1,31 @@ +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/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/0809.\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227/0809-\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227.py" "b/0809.\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227/0809-\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227.py" new file mode 100644 index 0000000..59c336c --- /dev/null +++ "b/0809.\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227/0809-\346\203\205\346\204\237\344\270\260\345\257\214\347\232\204\346\226\207\345\255\227.py" @@ -0,0 +1,38 @@ +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/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/0814.\344\272\214\345\217\211\346\240\221\345\211\252\346\236\235/0814-\344\272\214\345\217\211\346\240\221\345\211\252\346\236\235.py" "b/0814.\344\272\214\345\217\211\346\240\221\345\211\252\346\236\235/0814-\344\272\214\345\217\211\346\240\221\345\211\252\346\236\235.py" index 2cefde6..c33fe84 100644 --- "a/0814.\344\272\214\345\217\211\346\240\221\345\211\252\346\236\235/0814-\344\272\214\345\217\211\346\240\221\345\211\252\346\236\235.py" +++ "b/0814.\344\272\214\345\217\211\346\240\221\345\211\252\346\236\235/0814-\344\272\214\345\217\211\346\240\221\345\211\252\346\236\235.py" @@ -12,12 +12,9 @@ def pruneTree(self, root): :rtype: TreeNode """ if not root: - return None - # print root.val, self.generate(root.left), self.generate(root.right) + return None root.left = self.pruneTree(root.left) root.right = self.pruneTree(root.right) - if root.left == None and root.right == None and (root.val == 0): - return None - return root - - + if not root.left and not root.right and not root.val: + root = None + return root \ No newline at end of file diff --git "a/0820.\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201/0820-\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201.py" "b/0820.\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201/0820-\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201.py" new file mode 100644 index 0000000..618a6c9 --- /dev/null +++ "b/0820.\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201/0820-\345\215\225\350\257\215\347\232\204\345\216\213\347\274\251\347\274\226\347\240\201.py" @@ -0,0 +1,36 @@ +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/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/0836.\347\237\251\345\275\242\351\207\215\345\217\240/0836-\347\237\251\345\275\242\351\207\215\345\217\240.py" "b/0836.\347\237\251\345\275\242\351\207\215\345\217\240/0836-\347\237\251\345\275\242\351\207\215\345\217\240.py" new file mode 100644 index 0000000..a13282d --- /dev/null +++ "b/0836.\347\237\251\345\275\242\351\207\215\345\217\240/0836-\347\237\251\345\275\242\351\207\215\345\217\240.py" @@ -0,0 +1,10 @@ +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/0845.\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211/0845-\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/0845.\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211/0845-\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" new file mode 100644 index 0000000..970933d --- /dev/null +++ "b/0845.\346\225\260\347\273\204\344\270\255\347\232\204\346\234\200\351\225\277\345\261\261\350\204\211/0845-\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" @@ -0,0 +1,24 @@ +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/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/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\237\245\346\211\276\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\237\245\346\211\276\346\240\221.py" "b/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\237\245\346\211\276\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\237\245\346\211\276\346\240\221.py" index 568c9c2..ccc0483 100644 --- "a/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\237\245\346\211\276\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\237\245\346\211\276\346\240\221.py" +++ "b/0897.\351\200\222\345\242\236\351\241\272\345\272\217\346\237\245\346\211\276\346\240\221/0897-\351\200\222\345\242\236\351\241\272\345\272\217\346\237\245\346\211\276\346\240\221.py" @@ -11,26 +11,24 @@ def increasingBST(self, root): :type root: TreeNode :rtype: TreeNode """ - preorder = list() - - def pre_order(root): - if not root: - return - pre_order(root.left) - preorder.append(root.val) - pre_order(root.right) - - pre_order(root) - dummy = TreeNode(0) - for i, node in enumerate(preorder): + if not root: + return root + new_root = TreeNode(-1) - temp = TreeNode(node) - temp.left = None - temp.right = None - if i == 0: - dummy.right = temp - cur = temp + cur, stack = root, [] + parent = None + while cur or stack: + if cur: + stack.append(cur) + cur = cur.left else: - cur.right = temp - cur = temp - return dummy.right \ No newline at end of file + cur = stack.pop() + cur.left = None + if not parent: + parent = cur + new_root.right = parent + else: + parent.right = cur + parent = cur + cur = cur.right + return new_root.right diff --git "a/0905.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204/0905-\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.py" "b/0905.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204/0905-\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.py" index 626a868..43e1110 100644 --- "a/0905.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204/0905-\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.py" +++ "b/0905.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204/0905-\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.py" @@ -4,11 +4,4 @@ def sortArrayByParity(self, A): :type A: List[int] :rtype: List[int] """ - odd, even = [], [] - for i in A: - if i % 2: - odd.append(i) - else: - even.append(i) - - return even + odd \ No newline at end of file + return sorted(A, key = lambda x:x % 2) \ No newline at end of file diff --git a/0912.sort-an-array/sort-an-array.md b/0912.sort-an-array/sort-an-array.md deleted file mode 100644 index 4e352dc..0000000 --- a/0912.sort-an-array/sort-an-array.md +++ /dev/null @@ -1,29 +0,0 @@ -

    Given an array of integers nums, sort the array in ascending order.

    - -

     

    - -
      -
    - -

    Example 1:

    - -
    -Input: [5,2,3,1]
    -Output: [1,2,3,5]
    -
    - -

    Example 2:

    - -
    -Input: [5,1,1,2,0,0]
    -Output: [0,0,1,1,2,5]
    -
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= A.length <= 10000
    2. -
    3. -50000 <= A[i] <= 50000
    4. -
    diff --git a/0912.sort-an-array/sort-an-array.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" similarity index 78% rename from 0912.sort-an-array/sort-an-array.py rename to "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" index ea57e4d..3e624db 100644 --- a/0912.sort-an-array/sort-an-array.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" @@ -4,4 +4,5 @@ def sortArray(self, nums): :type nums: List[int] :rtype: List[int] """ - return sorted(nums) \ No newline at end of file + return sorted(nums) + \ No newline at end of file diff --git "a/0914.\345\215\241\347\211\214\345\210\206\347\273\204/0914-\345\215\241\347\211\214\345\210\206\347\273\204.py" "b/0914.\345\215\241\347\211\214\345\210\206\347\273\204/0914-\345\215\241\347\211\214\345\210\206\347\273\204.py" index 224b036..609c27e 100644 --- "a/0914.\345\215\241\347\211\214\345\210\206\347\273\204/0914-\345\215\241\347\211\214\345\210\206\347\273\204.py" +++ "b/0914.\345\215\241\347\211\214\345\210\206\347\273\204/0914-\345\215\241\347\211\214\345\210\206\347\273\204.py" @@ -1,36 +1,11 @@ class Solution(object): - def hasGroupsSizeX( self,deck): + def hasGroupsSizeX(self, deck): """ :type deck: List[int] :rtype: bool """ - if len(deck) <=0: - return False - deck.sort() - print deck - a = deck[0] - count = deck.count(a) - if count < 2: - return False - for j in range(2, count +1): - if len(deck) % j != 0: - continue - print "invalid length",j,len(deck) - for i in range(0, len(deck),j): - temp = deck[i] - flag = 0 - print i,temp - for k in range(i,i+j): - if deck[k] != temp: - flag = 1 - print "not the same",deck[k],temp - if flag == 1: - break - if flag == 1: - continue - return True - return False - - - - \ No newline at end of file + 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/0921.\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/0921-\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/0921.\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/0921-\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" index f20e3b0..94e321e 100644 --- "a/0921.\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/0921-\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/0921.\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/0921-\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" @@ -1,19 +1,14 @@ -# -*- coding: utf-8 -*- class Solution(object): def minAddToMakeValid(self, S): """ :type S: str :rtype: int """ - dic = {"(":"", ")" : "("} - res = len(S) - temp = [None] - for item in S: - # print item - if temp[-1] != dic[item.encode('utf-8')]: - temp.append(item) + stack = [] + dic = {"(":")", "{":"}", "[":"]"} + for ch in S: + if stack and stack[-1] in dic and dic[stack[-1]] == ch: + stack.pop() else: - temp = temp[:-1] - return len(temp) -1 - - \ No newline at end of file + stack.append(ch) + return len(stack) \ 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/0946.\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227/0946-\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227.py" "b/0946.\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227/0946-\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227.py" index 35dd4b0..ca0caa0 100644 --- "a/0946.\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227/0946-\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227.py" +++ "b/0946.\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227/0946-\351\252\214\350\257\201\346\240\210\345\272\217\345\210\227.py" @@ -5,51 +5,12 @@ def validateStackSequences(self, pushed, popped): :type popped: List[int] :rtype: bool """ - - stack = [] - i = 0 - for item in pushed: - stack.append(item) - while(stack and popped[i] == stack[-1]): - stack.pop() - i += 1 - return stack == [] - - - - - - - - - - - - - - - - - - - - - - - - l = len(pushed) - - stack = list() - - - for i in range(0, l): - stack.append(pushed[i]) - while(stack and stack[-1] == popped[0]): - stack = stack[:-1] - popped = popped[1:] - - return stack == [] - - - - \ No newline at end of file + 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/0951.\347\277\273\350\275\254\347\255\211\344\273\267\344\272\214\345\217\211\346\240\221/0951-\347\277\273\350\275\254\347\255\211\344\273\267\344\272\214\345\217\211\346\240\221.py" "b/0951.\347\277\273\350\275\254\347\255\211\344\273\267\344\272\214\345\217\211\346\240\221/0951-\347\277\273\350\275\254\347\255\211\344\273\267\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..de188d5 --- /dev/null +++ "b/0951.\347\277\273\350\275\254\347\255\211\344\273\267\344\272\214\345\217\211\346\240\221/0951-\347\277\273\350\275\254\347\255\211\344\273\267\344\272\214\345\217\211\346\240\221.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 flipEquiv(self, root1, root2): + """ + :type root1: TreeNode + :type root2: TreeNode + :rtype: bool + """ + if not root1: + return root2 is None + if not root2: + return root1 is None + if root1.val != root2.val: + return False + return (self.flipEquiv(root1.left, root2.left) and self.flipEquiv(root1.right, root2.right)) or (self.flipEquiv(root1.left, root2.right) and self.flipEquiv(root1.right, root2.left)) \ No newline at end of file diff --git "a/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/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" index be1cad8..b4a817e 100644 --- "a/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/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" @@ -11,19 +11,8 @@ def isUnivalTree(self, root): :type root: TreeNode :rtype: bool """ - - self.value = root.val - self.result = True - self.generate(root) - return self.result - - def generate(self, root): - if root.val != self.value: - self.result = False - return if not root: - return - if root.left: - self.generate(root.left) - if root.right: - self.generate(root.right) \ No newline at end of file + return True + left = not root.left or (self.isUnivalTree(root.left) and root.val == root.left.val) + right = not root.right or (self.isUnivalTree(root.right) and root.val == root.right.val) + return left and right \ 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/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.md b/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.md deleted file mode 100644 index 0fe953e..0000000 --- a/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.md +++ /dev/null @@ -1,24 +0,0 @@ -

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.

    - -

     

    - -
    -

    Example 1:

    - -
    -Input: A = [4,5,0,-2,-3,1], K = 5
    -Output: 7
    -Explanation: There are 7 subarrays with a sum divisible by K = 5:
    -[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
    -
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= A.length <= 30000
    2. -
    3. -10000 <= A[i] <= 10000
    4. -
    5. 2 <= K <= 10000
    6. -
    -
    \ No newline at end of file diff --git a/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.py b/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.py deleted file mode 100644 index 8ba5d0b..0000000 --- a/0974.subarray-sums-divisible-by-k/subarray-sums-divisible-by-k.py +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def subarraysDivByK(self, A, K): - """ - :type A: List[int] - :type K: int - :rtype: int - """ - s = [0 for i in range(len(A) + 1)] #sǰ׺ͣs[i]ʾsum(A[:i]) - kcnt = [0 for i in range(K)] #k[i]sжٸԪ mod k Ϊi - for i in range(len(A)): - s[i + 1] = s[i] + A[i] - for item in s: - kcnt[item % K] += 1 - print s, kcnt - #ĿĺܱKĺ;ͿԱʾΪǰ׺͵IJ - # sum(A[i:j + 1]) = s[j + 1] - s[i] - #IJܱK˵ mod kõĽͬ - #ֻҪжٶ mod k ͬͿԵõ - # [4,5,0,-2,-3,1] 5 - # s = [0, 4, 9, 9, 7, 4, 5] - # k = [2, 0, 1, 0, 4] sԪصΪ0051ԪصΪ27ĸԪصΪ44994 - # ڱ֤ͬ£ȡԵõһ𰸡Ӵ𰸾 C22 + C12 + C42 = 1 + 0 + 6 = 7 - return sum(x * (x - 1) // 2 for x in kcnt) - \ No newline at end of file diff --git "a/0979.\345\234\250\344\272\214\345\217\211\346\240\221\344\270\255\345\210\206\351\205\215\347\241\254\345\270\201/0979-\345\234\250\344\272\214\345\217\211\346\240\221\344\270\255\345\210\206\351\205\215\347\241\254\345\270\201.py" "b/0979.\345\234\250\344\272\214\345\217\211\346\240\221\344\270\255\345\210\206\351\205\215\347\241\254\345\270\201/0979-\345\234\250\344\272\214\345\217\211\346\240\221\344\270\255\345\210\206\351\205\215\347\241\254\345\270\201.py" index f7ee90d..0ea3ac7 100644 --- "a/0979.\345\234\250\344\272\214\345\217\211\346\240\221\344\270\255\345\210\206\351\205\215\347\241\254\345\270\201/0979-\345\234\250\344\272\214\345\217\211\346\240\221\344\270\255\345\210\206\351\205\215\347\241\254\345\270\201.py" +++ "b/0979.\345\234\250\344\272\214\345\217\211\346\240\221\344\270\255\345\210\206\351\205\215\347\241\254\345\270\201/0979-\345\234\250\344\272\214\345\217\211\346\240\221\344\270\255\345\210\206\351\205\215\347\241\254\345\270\201.py" @@ -12,20 +12,13 @@ def distributeCoins(self, root): :rtype: int """ self.res = 0 - def dfs(node): if not node: - return - - if node.left: - dfs(node.left) - node.val += node.left.val - 1 - if node.right: - dfs(node.right) - node.val += node.right.val - 1 - - self.res += abs(node.val - 1) - - + return 0 + l = dfs(node.left) + r = dfs(node.right) + + self.res += abs(l) + abs(r) + return l + r + node.val - 1 dfs(root) return self.res \ 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/0994.\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220/0994-\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220.py" "b/0994.\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220/0994-\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220.py" index 195a6d3..5d9ce8b 100644 --- "a/0994.\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220/0994-\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220.py" +++ "b/0994.\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220/0994-\350\205\220\347\203\202\347\232\204\346\251\230\345\255\220.py" @@ -4,36 +4,40 @@ 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] - rotlist = list() - for i in range(len(grid)): - for j in range(len(grid[0])): + + queue = deque() + for i in range(m): + for j in range(n): if grid[i][j] == 2: - rotlist.append([i, j]) - minute = 0 - while(rotlist): - newrotlist = list() - for rotnode in rotlist: - x0 = rotnode[0] - y0 = rotnode[1] - + 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 < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == 1: + + if 0 <= x < m and 0 <= y < n and grid[x][y] == 1: grid[x][y] = 2 - newrotlist.append([x,y]) - if not newrotlist: + queue.append((x, y)) + if not queue: break - - rotlist = newrotlist[:] - minute += 1 - - for row in grid: - for i in row: - if i == 1:#ʵ + res += 1 + for i in range(m): + for j in range(n): + if grid[i][j] == 1: return -1 - return minute - \ No newline at end of file + return res + + + \ No newline at end of file diff --git "a/1008.\345\205\210\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/1008-\345\205\210\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" "b/1008.\345\205\210\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/1008-\345\205\210\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" index 0f52525..73d41dc 100644 --- "a/1008.\345\205\210\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/1008-\345\205\210\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" +++ "b/1008.\345\205\210\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/1008-\345\205\210\345\272\217\351\201\215\345\216\206\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.py" @@ -11,30 +11,12 @@ def bstFromPreorder(self, preorder): :type preorder: List[int] :rtype: TreeNode """ - inorder = sorted(preorder) - - return self.buildTree(preorder, inorder) - - - - def buildTree(self, preorder, inorder): # leetcode 105 - """ - :type preorder: List[int] - :type inorder: List[int] - :rtype: TreeNode - """ if not preorder: - return None - - root = TreeNode(preorder[0]) - left_inorder = inorder[: inorder.index(root.val)] - right_inorder = inorder[inorder.index(root.val) + 1 :] - - l_left = len(left_inorder) - left_preorder = preorder[1:l_left + 1] - right_preorder = preorder[l_left + 1 :] - - root.left = self.buildTree(left_preorder, left_inorder) - root.right = self.buildTree(right_preorder, right_inorder) - + return None + inorder = sorted(preorder) + + idx = inorder.index(preorder[0]) + root = TreeNode(preorder[0]) + root.left = self.bstFromPreorder(preorder[1:idx + 1]) + root.right = self.bstFromPreorder(preorder[idx + 1:]) return root \ 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/1013.\345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\345\222\214\347\233\270\347\255\211\347\232\204\344\270\211\344\270\252\351\203\250\345\210\206/1013-\345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\345\222\214\347\233\270\347\255\211\347\232\204\344\270\211\344\270\252\351\203\250\345\210\206.py" "b/1013.\345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\345\222\214\347\233\270\347\255\211\347\232\204\344\270\211\344\270\252\351\203\250\345\210\206/1013-\345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\345\222\214\347\233\270\347\255\211\347\232\204\344\270\211\344\270\252\351\203\250\345\210\206.py" index de497d7..295c1c0 100644 --- "a/1013.\345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\345\222\214\347\233\270\347\255\211\347\232\204\344\270\211\344\270\252\351\203\250\345\210\206/1013-\345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\345\222\214\347\233\270\347\255\211\347\232\204\344\270\211\344\270\252\351\203\250\345\210\206.py" +++ "b/1013.\345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\345\222\214\347\233\270\347\255\211\347\232\204\344\270\211\344\270\252\351\203\250\345\210\206/1013-\345\260\206\346\225\260\347\273\204\345\210\206\346\210\220\345\222\214\347\233\270\347\255\211\347\232\204\344\270\211\344\270\252\351\203\250\345\210\206.py" @@ -13,4 +13,4 @@ def canThreePartsEqualSum(self, A): if target == snow: snow = 0 cnt += 1 - return cnt == 3 \ No newline at end of file + return cnt >= 3 diff --git "a/1014.\346\234\200\344\275\263\350\247\202\345\205\211\347\273\204\345\220\210/1014-\346\234\200\344\275\263\350\247\202\345\205\211\347\273\204\345\220\210.py" "b/1014.\346\234\200\344\275\263\350\247\202\345\205\211\347\273\204\345\220\210/1014-\346\234\200\344\275\263\350\247\202\345\205\211\347\273\204\345\220\210.py" index 98c66fa..7c69fc9 100644 --- "a/1014.\346\234\200\344\275\263\350\247\202\345\205\211\347\273\204\345\220\210/1014-\346\234\200\344\275\263\350\247\202\345\205\211\347\273\204\345\220\210.py" +++ "b/1014.\346\234\200\344\275\263\350\247\202\345\205\211\347\273\204\345\220\210/1014-\346\234\200\344\275\263\350\247\202\345\205\211\347\273\204\345\220\210.py" @@ -5,12 +5,9 @@ def maxScoreSightseeingPair(self, A): :rtype: int """ res = 0 - mmax = A[0] - mmax_index = 0 + pre_max = A[0] + 0 #ʼֵ for j in range(1, len(A)): - res = max(res, mmax + A[j] + mmax_index - j) - if A[j] + j > mmax + mmax_index: - mmax = A[j] - mmax_index = j + res = max(res, pre_max + A[j] - j) #жܷˢres + pre_max = max(pre_max, A[j] + j) #жܷˢpre_max õA[i] + i return res \ 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/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/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.md b/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.md deleted file mode 100644 index 2c17648..0000000 --- a/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.md +++ /dev/null @@ -1,21 +0,0 @@ -

    Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray.

    - -

    Return the largest sum of the given array after partitioning.

    - -

     

    - -

    Example 1:

    - -
    -Input: A = [1,15,7,9,2,5,10], K = 3
    -Output: 84
    -Explanation: A becomes [15,15,15,9,10,10,10]
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= K <= A.length <= 500
    2. -
    3. 0 <= A[i] <= 10^6
    4. -
    \ No newline at end of file diff --git a/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.py b/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.py deleted file mode 100644 index 7562da0..0000000 --- a/1043.partition-array-for-maximum-sum/partition-array-for-maximum-sum.py +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def maxSumAfterPartitioning(self, A, K): - """ - :type A: List[int] - :type K: int - :rtype: int - """ - dp = [0 for _ in range(len(A))] - - for i, x in enumerate(A): #ɨÿ - subarray_max = x - for j in range(1, K + 1): # J ǰijȣA[i] A[i - (j - 1): i + 1] - if i - (j - 1) >= 0:#ǰⳤΪ J 飬 - subarray_max = max(subarray_max, A[i - (j - 1)]) #ȷsubarray_maxǴֵ - #ôдsubarray_max = max(A[i - (j - 1): i + 1]]ҲԹǺ - - if i - j < 0: # A[i]֮ǰǡj - 1Ԫأһ˳ΪJ飬൱ڵǰԱʾΪA[:j] - dp[i] = max(dp[i], subarray_max * j) - else: - dp[i] = max(dp[i], dp[i - j] + subarray_max * j) - - return dp[-1] \ No newline at end of file diff --git a/1046.last-stone-weight/last-stone-weight.md b/1046.last-stone-weight/last-stone-weight.md deleted file mode 100644 index 39320e0..0000000 --- a/1046.last-stone-weight/last-stone-weight.md +++ /dev/null @@ -1,32 +0,0 @@ -

    We have a collection of rocks, each rock has a positive integer weight.

    - -

    Each turn, we choose the two heaviest rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is:

    - -
      -
    • If x == y, both stones are totally destroyed;
    • -
    • If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.
    • -
    - -

    At the end, there is at most 1 stone left.  Return the weight of this stone (or 0 if there are no stones left.)

    - -

     

    - -

    Example 1:

    - -
    -Input: [2,7,4,1,8,1]
    -Output: 1
    -Explanation: 
    -We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
    -we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
    -we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
    -we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= stones.length <= 30
    2. -
    3. 1 <= stones[i] <= 1000
    4. -
    \ No newline at end of file diff --git a/1046.last-stone-weight/last-stone-weight.py b/1046.last-stone-weight/last-stone-weight.py deleted file mode 100644 index 0c2f7e6..0000000 --- a/1046.last-stone-weight/last-stone-weight.py +++ /dev/null @@ -1,14 +0,0 @@ -class Solution(object): - def lastStoneWeight(self, stones): - """ - :type stones: List[int] - :rtype: int - """ - while( len(stones) > 1): - stones.sort() - x, y = stones[-2], stones[-1] - if x == y: - stones = stones[:-2] - else: - stones = stones[:-2] + [y - x] - return 0 if len(stones) == 0 else stones[0] \ 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/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.md b/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.md deleted file mode 100644 index a80de4e..0000000 --- a/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.md +++ /dev/null @@ -1,25 +0,0 @@ -

    Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.

    - -

    We repeatedly make duplicate removals on S until we no longer can.

    - -

    Return the final string after all such duplicate removals have been made.  It is guaranteed the answer is unique.

    - -

     

    - -

    Example 1:

    - -
    -Input: "abbaca"
    -Output: "ca"
    -Explanation: 
    -For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
    -
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= S.length <= 20000
    2. -
    3. S consists only of English lowercase letters.
    4. -
    \ No newline at end of file diff --git a/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.py b/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.py deleted file mode 100644 index 07fe785..0000000 --- a/1047.remove-all-adjacent-duplicates-in-string/remove-all-adjacent-duplicates-in-string.py +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def removeDuplicates(self, S): - """ - :type S: str - :rtype: str - """ - flag = 1 - while 1: - original_l = len(S) - i = 0 - while i < len(S) - 1: - if i >= 0 and S[i] == S[i + 1]: - S = S[:i] + S[i + 2:] - i -= 2 - i += 1 - if original_l == len(S): - return S \ No newline at end of file diff --git a/1048.longest-string-chain/longest-string-chain.md b/1048.longest-string-chain/longest-string-chain.md deleted file mode 100644 index 87983da..0000000 --- a/1048.longest-string-chain/longest-string-chain.md +++ /dev/null @@ -1,31 +0,0 @@ -

    Given a list of words, each word consists of English lowercase letters.

    - -

    Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac".

    - -

    A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.

    - -

    Return the longest possible length of a word chain with words chosen from the given list of words.

    - -

     

    - -

    Example 1:

    - -
    -Input: ["a","b","ba","bca","bda","bdca"]
    -Output: 4
    -Explanation: one of the longest word chain is "a","ba","bda","bdca".
    -
    - -

     

    - -

    Note:

    - -
      -
    1. 1 <= words.length <= 1000
    2. -
    3. 1 <= words[i].length <= 16
    4. -
    5. words[i] only consists of English lowercase letters.
    6. -
    - -
    -

     

    -
    \ No newline at end of file diff --git a/1048.longest-string-chain/longest-string-chain.py b/1048.longest-string-chain/longest-string-chain.py deleted file mode 100644 index aeaeb03..0000000 --- a/1048.longest-string-chain/longest-string-chain.py +++ /dev/null @@ -1,41 +0,0 @@ -class Solution(object): - def longestStrChain(self, words): - """ - :type words: List[str] - :rtype: int - """ - from collections import Counter - words = sorted(words, key = lambda x: len(x)) - # dic = [Counter(word) for word in words] - # words = list(set(words)) - # print len(words), words - if len(words) == 1000: - return 1 - def cover(str1, str2): - # print str1, str2 - tmp = "" - for i, char in enumerate(str2): - tmp = str2[:i] + str2[i + 1:] - if tmp == str1: - return True - return False - - dp = [1 for _ in range(len(words))] - for i, word in enumerate(words): - if i >= 1 and words[i] == words[i - 1]: - continue - for j in range(i + 1, len(words)): - # print word, words[j] - if len(words[j]) - 1 > len(word): - break - - # print word, words[j] - if len(words[j]) - 1 == len(word) and cover(word, words[j]): - # print word, words[j], i, j - dp[j] = max(dp[j], dp[i] + 1) - # print dp[i], dp[j] - # print dp - return max(dp) - - - \ 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/1055.\345\275\242\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1055-\345\275\242\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" "b/1055.\345\275\242\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1055-\345\275\242\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" new file mode 100644 index 0000000..6562a70 --- /dev/null +++ "b/1055.\345\275\242\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/1055-\345\275\242\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" @@ -0,0 +1,25 @@ +class Solution(object): + def shortestWay(self, source, target): + """ + :type source: str + :type target: str + :rtype: int + """ + s = set(source) + t = set(target) + for ch in t: + if ch not in s: + return -1 + + i, j = 0, 0 + res = 0 + while j < len(target): + i = 0 + while i < len(source) and j < len(target): + if source[i] == target[j]: + i += 1 + j += 1 + else: + i += 1 + res += 1 + return res \ No newline at end of file diff --git "a/1071.\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220/1071-\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220.py" "b/1071.\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220/1071-\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220.py" new file mode 100644 index 0000000..12efd30 --- /dev/null +++ "b/1071.\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220/1071-\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220.py" @@ -0,0 +1,15 @@ +class Solution(object): + def gcdOfStrings(self, str1, str2): + """ + :type str1: str + :type str2: str + :rtype: str + """ + if len(str1) < len(str2): + str1, str2 = str2, str1 + + for i in range(len(str2), 0, -1): + + if len(str1) % i == 0 and str2[:i] * (len(str1) / i) == str1 and len(str2) % i == 0 and str2[:i] * (len(str2) / i) == str2: + return str2[:i] + return "" \ No newline at end of file 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 0: + geshu += i + + find = False + for i, num in enumerate(count): + s += num * i #ܺ + if mean_cnt < num: #countֵ± + mean = i + mean_cnt = num + + cnt += num #Ŀǰ˶ٸ + if cnt > total_cnt // 2 and find == False: + if total_cnt % 2: #λ϶һ + median = i + find = True + else: + if cnt - num == total_cnt // 2: #λͬ + for j in range(i - 1, -1, -1): #ǰһ + if count[j] > 0: + median = (i + j) /2.0 + find = True + break + else:#λͬ + median = i + find = True + + return [min_element, max_element, 1.0 * s /geshu, median, mean ] + + + \ No newline at end of file diff --git "a/1094.\346\213\274\350\275\246/1094-\346\213\274\350\275\246.py" "b/1094.\346\213\274\350\275\246/1094-\346\213\274\350\275\246.py" new file mode 100644 index 0000000..83d63f5 --- /dev/null +++ "b/1094.\346\213\274\350\275\246/1094-\346\213\274\350\275\246.py" @@ -0,0 +1,20 @@ +class Solution(object): + def carPooling(self, trips, capacity): + """ + :type trips: List[List[int]] + :type capacity: int + :rtype: bool + """ + time = [0 for _ in range(1005)] + + for num, start, end in trips: + time[start] += num + time[end] -= num + + for i, x in enumerate(time): + time[i] += time[i - 1] + if time[i] > capacity: + return False + + return True + \ No newline at end of file diff --git "a/1095.\345\261\261\350\204\211\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\347\233\256\346\240\207\345\200\274/1095-\345\261\261\350\204\211\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\347\233\256\346\240\207\345\200\274.py" "b/1095.\345\261\261\350\204\211\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\347\233\256\346\240\207\345\200\274/1095-\345\261\261\350\204\211\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\347\233\256\346\240\207\345\200\274.py" new file mode 100644 index 0000000..9968a55 --- /dev/null +++ "b/1095.\345\261\261\350\204\211\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\347\233\256\346\240\207\345\200\274/1095-\345\261\261\350\204\211\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\347\233\256\346\240\207\345\200\274.py" @@ -0,0 +1,52 @@ +class Solution(object): + def findInMountainArray(self, target, mountain_arr): + """ + :type target: integer + :type mountain_arr: MountainArray + :rtype: integer + """ + # õ鳤 + n = mountain_arr.length() + #ɽ + left, right = 0, n - 1 + while 1: + mid = (left + right) // 2 + val = mountain_arr.get(mid) + lval, rval = mountain_arr.get(mid - 1), mountain_arr.get(mid + 1) + + if val > lval and val > rval: + peak = val + peak_idx = mid + break + if val < rval: + left = mid + 1 + else: + right = mid + if target == peak: + return mid + + # + left, right = 0, peak_idx - 1 + while left <= right: + mid = (left + right) // 2 + val = mountain_arr.get(mid) + if val == target: + return mid + elif val > target: + right = mid - 1 + else: + left = mid + 1 + + #Ҳ + left, right = peak_idx + 1, n - 1 + while left <= right: + mid = (left + right) // 2 + val = mountain_arr.get(mid) + if val == target: + return mid + elif val > target: + left = mid + 1 + else: + right = mid - 1 + + return -1 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/1103.\345\210\206\347\263\226\346\236\234II/1103-\345\210\206\347\263\226\346\236\234II.py" "b/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..c988b09 --- /dev/null +++ "b/1103.\345\210\206\347\263\226\346\236\234II/1103-\345\210\206\347\263\226\346\236\234II.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/1104.\344\272\214\345\217\211\346\240\221\345\257\273\350\267\257/1104-\344\272\214\345\217\211\346\240\221\345\257\273\350\267\257.py" "b/1104.\344\272\214\345\217\211\346\240\221\345\257\273\350\267\257/1104-\344\272\214\345\217\211\346\240\221\345\257\273\350\267\257.py" new file mode 100644 index 0000000..69e81d2 --- /dev/null +++ "b/1104.\344\272\214\345\217\211\346\240\221\345\257\273\350\267\257/1104-\344\272\214\345\217\211\346\240\221\345\257\273\350\267\257.py" @@ -0,0 +1,12 @@ +class Solution(object): + def pathInZigZagTree(self, label): + """ + :type label: int + :rtype: List[int] + """ + res = [] + while label > 1: + res.append(label) + label >>= 1 + label = label ^(1 << (label.bit_length() - 1)) - 1 + return [1] + res[::-1] 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/1111.\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246/1111-\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246.py" "b/1111.\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246/1111-\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246.py" new file mode 100644 index 0000000..026e2ec --- /dev/null +++ "b/1111.\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246/1111-\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246.py" @@ -0,0 +1,17 @@ +class Solution(object): + def maxDepthAfterSplit(self, seq): + """ + :type seq: str + :rtype: List[int] + """ + par_cnt = 0 + res = [0 for _ in seq] + + for i, par in enumerate(seq): + if par == "(": + par_cnt += 1 + res[i] = par_cnt % 2 + else: + res[i] = par_cnt % 2 + par_cnt -= 1 + 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/5037.\345\271\263\350\241\214\350\257\276\347\250\213/5037-\345\271\263\350\241\214\350\257\276\347\250\213.py" "b/1136.\345\271\263\350\241\214\350\257\276\347\250\213/1136-\345\271\263\350\241\214\350\257\276\347\250\213.py" similarity index 53% rename from "5037.\345\271\263\350\241\214\350\257\276\347\250\213/5037-\345\271\263\350\241\214\350\257\276\347\250\213.py" rename to "1136.\345\271\263\350\241\214\350\257\276\347\250\213/1136-\345\271\263\350\241\214\350\257\276\347\250\213.py" index 175126c..8ea4616 100644 --- "a/5037.\345\271\263\350\241\214\350\257\276\347\250\213/5037-\345\271\263\350\241\214\350\257\276\347\250\213.py" +++ "b/1136.\345\271\263\350\241\214\350\257\276\347\250\213/1136-\345\271\263\350\241\214\350\257\276\347\250\213.py" @@ -7,30 +7,27 @@ def minimumSemesters(self, N, relations): """ indegree = [0 for i in range(N + 1)] adj = [set() for _ in range(N + 1)] - for pre, cur in relations: - indegree[cur] += 1 - adj[pre].add(cur) + for pre, cur in relations: + indegree[cur] += 1 #统计入度 + adj[pre].add(cur) #统计邻居节点 from collections import deque queue = deque() for i, x in enumerate(indegree): - if x == 0 and i > 0: + if x == 0 and i > 0: #将入度为0的节点入队 queue.append(i) - print queue - cnt, res = 0, 0 - out = 0 + + semester_cnt= 0 + finished_course = 0 while queue: next_queue = deque() - cnt += 1 + semester_cnt += 1 #新的学期来了 for cur in queue: - out += 1 + finished_course += 1 #又一门课学完了 for neighbor in adj[cur]: indegree[neighbor] -= 1 - + if indegree[neighbor] == 0: - next_queue.append(neighbor) + next_queue.append(neighbor) #下个学期可以学neighbor这门课了 queue = next_queue - return cnt if out == N else -1 - - - \ No newline at end of file + return semester_cnt if finished_course == N else -1 #如果所有的课都学完了,那么finished_course == N \ 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/1160.\346\213\274\345\206\231\345\215\225\350\257\215/1160-\346\213\274\345\206\231\345\215\225\350\257\215.py" "b/1160.\346\213\274\345\206\231\345\215\225\350\257\215/1160-\346\213\274\345\206\231\345\215\225\350\257\215.py" new file mode 100644 index 0000000..910aacb --- /dev/null +++ "b/1160.\346\213\274\345\206\231\345\215\225\350\257\215/1160-\346\213\274\345\206\231\345\215\225\350\257\215.py" @@ -0,0 +1,23 @@ +class Solution(object): + def countCharacters(self, words, chars): + """ + :type words: List[str] + :type chars: str + :rtype: int + """ + res = 0 + from collections import Counter + + dic_a = Counter(chars) + + for word in words: + dic_w = Counter(word) + flag = 1 + for key, val in dic_w.items(): + if key not in dic_a or dic_a[key] < val: + flag = 0 + break + if flag: + res += len(word) + + return res \ 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/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/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..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.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/1162.\345\234\260\345\233\276\345\210\206\346\236\220/1162-\345\234\260\345\233\276\345\210\206\346\236\220.py" "b/1162.\345\234\260\345\233\276\345\210\206\346\236\220/1162-\345\234\260\345\233\276\345\210\206\346\236\220.py" new file mode 100644 index 0000000..b1dbb0c --- /dev/null +++ "b/1162.\345\234\260\345\233\276\345\210\206\346\236\220/1162-\345\234\260\345\233\276\345\210\206\346\236\220.py" @@ -0,0 +1,38 @@ +class Solution(object): + def maxDistance(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + from collections import deque + m, n = len(grid), len(grid[0]) + land = [] + for i in range(m): + for j in range(n): + if grid[i][j] == 1: + land.append((i, j)) + + if not land or len(land) == m * n: + return -1 + + res = 0 + dx = [1, -1, 0, 0] + dy = [0, 0, 1, -1] + + queue = deque(land) + visited = set(land) + while queue: + for _ in range(len(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] == 0 and (x, y) not in visited: + queue.append((x, y)) + visited.add((x, y)) + res += 1 + return res - 1 + + \ 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/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/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..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.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/1166.\350\256\276\350\256\241\346\226\207\344\273\266\347\263\273\347\273\237/1166-\350\256\276\350\256\241\346\226\207\344\273\266\347\263\273\347\273\237.py" "b/1166.\350\256\276\350\256\241\346\226\207\344\273\266\347\263\273\347\273\237/1166-\350\256\276\350\256\241\346\226\207\344\273\266\347\263\273\347\273\237.py" new file mode 100644 index 0000000..4f1466d --- /dev/null +++ "b/1166.\350\256\276\350\256\241\346\226\207\344\273\266\347\263\273\347\273\237/1166-\350\256\276\350\256\241\346\226\207\344\273\266\347\263\273\347\273\237.py" @@ -0,0 +1,38 @@ +class FileSystem(object): + + def __init__(self): + self.dic = dict() + + def create(self, path, value): + """ + :type path: str + :type value: int + :rtype: bool + """ + if path in self.dic: + return False + parent = path.split("/") + parent_path = "" + for i in range(1, len(parent) - 1): + # print parent, i + parent_path += "/" + parent[i] + # print parent_path + if parent_path not in self.dic: + return False + self.dic[path] = value + return True + + + def get(self, path): + """ + :type path: str + :rtype: int + """ + return self.dic[path] if path in self.dic else -1 + + + +# Your FileSystem object will be instantiated and called as such: +# obj = FileSystem() +# param_1 = obj.create(path,value) +# param_2 = obj.get(path) \ No newline at end of file diff --git "a/1167.\350\277\236\346\216\245\346\243\222\346\235\220\347\232\204\346\234\200\344\275\216\350\264\271\347\224\250/1167-\350\277\236\346\216\245\346\243\222\346\235\220\347\232\204\346\234\200\344\275\216\350\264\271\347\224\250.py" "b/1167.\350\277\236\346\216\245\346\243\222\346\235\220\347\232\204\346\234\200\344\275\216\350\264\271\347\224\250/1167-\350\277\236\346\216\245\346\243\222\346\235\220\347\232\204\346\234\200\344\275\216\350\264\271\347\224\250.py" new file mode 100644 index 0000000..7d861bf --- /dev/null +++ "b/1167.\350\277\236\346\216\245\346\243\222\346\235\220\347\232\204\346\234\200\344\275\216\350\264\271\347\224\250/1167-\350\277\236\346\216\245\346\243\222\346\235\220\347\232\204\346\234\200\344\275\216\350\264\271\347\224\250.py" @@ -0,0 +1,15 @@ +class Solution(object): + def connectSticks(self, sticks): + """ + :type sticks: List[int] + :rtype: int + """ + from heapq import * + heapify(sticks) + res = 0 + while len(sticks) > 1: + s1 = heappop(sticks) + s2 = heappop(sticks) + res += s1 + s2 + heappush(sticks, s1 + s2) + return res \ No newline at end of file diff --git "a/5036.\346\234\200\344\275\216\346\210\220\346\234\254\350\201\224\351\200\232\346\211\200\346\234\211\345\237\216\345\270\202/5036-\346\234\200\344\275\216\346\210\220\346\234\254\350\201\224\351\200\232\346\211\200\346\234\211\345\237\216\345\270\202.py" "b/1168.\346\260\264\350\265\204\346\272\220\345\210\206\351\205\215\344\274\230\345\214\226/1168-\346\260\264\350\265\204\346\272\220\345\210\206\351\205\215\344\274\230\345\214\226.py" similarity index 67% rename from "5036.\346\234\200\344\275\216\346\210\220\346\234\254\350\201\224\351\200\232\346\211\200\346\234\211\345\237\216\345\270\202/5036-\346\234\200\344\275\216\346\210\220\346\234\254\350\201\224\351\200\232\346\211\200\346\234\211\345\237\216\345\270\202.py" rename to "1168.\346\260\264\350\265\204\346\272\220\345\210\206\351\205\215\344\274\230\345\214\226/1168-\346\260\264\350\265\204\346\272\220\345\210\206\351\205\215\344\274\230\345\214\226.py" index dabb4c7..5e4b3ac 100644 --- "a/5036.\346\234\200\344\275\216\346\210\220\346\234\254\350\201\224\351\200\232\346\211\200\346\234\211\345\237\216\345\270\202/5036-\346\234\200\344\275\216\346\210\220\346\234\254\350\201\224\351\200\232\346\211\200\346\234\211\345\237\216\345\270\202.py" +++ "b/1168.\346\260\264\350\265\204\346\272\220\345\210\206\351\205\215\344\274\230\345\214\226/1168-\346\260\264\350\265\204\346\272\220\345\210\206\351\205\215\344\274\230\345\214\226.py" @@ -1,11 +1,11 @@ + 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]: @@ -14,7 +14,7 @@ def find(self, member): for root in tmp: self.roots[root] = member return member - + def union(self, p, q): parentP = self.find(p) parentQ = self.find(q) @@ -27,35 +27,34 @@ def union(self, p, q): self.roots[parentQ] = parentP self.rank[parentP] -= 1 self.count -= 1 - + class Solution(object): - def minimumCost(self, N, connections): + def minCostToSupplyWater(self, n, wells, pipes): """ - :type N: int - :type conections: List[List[int]] + :type n: int + :type wells: List[int] + :type pipes: List[List[int]] :rtype: int """ - from collections import deque from heapq import * - ufs = UnionFindSet(N) - queue = [] + for i, well in enumerate(wells): + pipes.append([0, i + 1, well]) - for start, end, cost in connections: - ufs.union(start, end) + queue = [] + ufs = UnionFindSet(n) + for start, end, cost in pipes: queue.append([cost, start, end]) - if ufs.count != 1: - return -1 #޷һ - #Ա֤һ԰гһ - ufs2 = UnionFindSet(N) - heapify(queue) + heapify(queue) res = 0 - while ufs2.count > 1: + while ufs.count > 0: + cost, start, end = heappop(queue) - if ufs2.find(start) == ufs2.find(end): + # print ufs.roots, cost, start, end + if ufs.find(start) == ufs.find(end): continue - ufs2.union(start, end) res += cost + ufs.union(end, start) return res - + \ No newline at end of file diff --git "a/1169.\346\237\245\350\257\242\346\227\240\346\225\210\344\272\244\346\230\223/1169-\346\237\245\350\257\242\346\227\240\346\225\210\344\272\244\346\230\223.py" "b/1169.\346\237\245\350\257\242\346\227\240\346\225\210\344\272\244\346\230\223/1169-\346\237\245\350\257\242\346\227\240\346\225\210\344\272\244\346\230\223.py" new file mode 100644 index 0000000..e48c69f --- /dev/null +++ "b/1169.\346\237\245\350\257\242\346\227\240\346\225\210\344\272\244\346\230\223/1169-\346\237\245\350\257\242\346\227\240\346\225\210\344\272\244\346\230\223.py" @@ -0,0 +1,32 @@ +class Solution(object): + def invalidTransactions(self, transactions): + """ + :type transactions: List[str] + :rtype: List[str] + """ + recordByName = collections.defaultdict(list) + for trans in transactions: + name, time, amount, city = trans.split(",") + recordByName[name].append([name, int(time), int(amount), city]) + + def convert(l): + return l[0] + "," + str(l[1]) + "," + str(l[2]) + "," + l[3] + + res = set() + for name, rec in recordByName.items(): + curRec = sorted(rec, key = lambda x:x[1]) + + for i in range(len(curRec)): + if curRec[i][2] > 1000: + res.add(convert(curRec[i])) + for j in range(i + 1, len(curRec)): + + if abs(curRec[j][1] - curRec[i][1]) > 60: + break + if curRec[j][3] != curRec[i][3]: + res.add(convert(curRec[i])) + res.add(convert(curRec[j])) + return res + + + \ 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" new file mode 100644 index 0000000..1d151bc --- /dev/null +++ "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" @@ -0,0 +1,28 @@ +class Solution(object): + def numSmallerByFrequency(self, queries, words): + """ + :type queries: List[str] + :type words: List[str] + :rtype: List[int] + """ + + def func(word): + for char in "abcdefghijklmnopqrstuvwxyz": + if char in word: + return word.count(char) + return 0 + + 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/1171.\344\273\216\351\223\276\350\241\250\344\270\255\345\210\240\345\216\273\346\200\273\345\222\214\345\200\274\344\270\272\351\233\266\347\232\204\350\277\236\347\273\255\350\212\202\347\202\271/1171-\344\273\216\351\223\276\350\241\250\344\270\255\345\210\240\345\216\273\346\200\273\345\222\214\345\200\274\344\270\272\351\233\266\347\232\204\350\277\236\347\273\255\350\212\202\347\202\271.py" "b/1171.\344\273\216\351\223\276\350\241\250\344\270\255\345\210\240\345\216\273\346\200\273\345\222\214\345\200\274\344\270\272\351\233\266\347\232\204\350\277\236\347\273\255\350\212\202\347\202\271/1171-\344\273\216\351\223\276\350\241\250\344\270\255\345\210\240\345\216\273\346\200\273\345\222\214\345\200\274\344\270\272\351\233\266\347\232\204\350\277\236\347\273\255\350\212\202\347\202\271.py" new file mode 100644 index 0000000..9df319f --- /dev/null +++ "b/1171.\344\273\216\351\223\276\350\241\250\344\270\255\345\210\240\345\216\273\346\200\273\345\222\214\345\200\274\344\270\272\351\233\266\347\232\204\350\277\236\347\273\255\350\212\202\347\202\271/1171-\344\273\216\351\223\276\350\241\250\344\270\255\345\210\240\345\216\273\346\200\273\345\222\214\345\200\274\344\270\272\351\233\266\347\232\204\350\277\236\347\273\255\350\212\202\347\202\271.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 removeZeroSumSublists(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + dummy = ListNode(-1) + dummy.next = head + + record = {0:dummy} + pre_sum = 0 + + while head: + pre_sum += head.val + if pre_sum in record: + record[pre_sum].next = head.next + else: + record[pre_sum] = head + head = head.next + return dummy.next \ No newline at end of file diff --git "a/1172.\351\244\220\347\233\230\346\240\210/1172-\351\244\220\347\233\230\346\240\210.py" "b/1172.\351\244\220\347\233\230\346\240\210/1172-\351\244\220\347\233\230\346\240\210.py" new file mode 100644 index 0000000..f2f14d7 --- /dev/null +++ "b/1172.\351\244\220\347\233\230\346\240\210/1172-\351\244\220\347\233\230\346\240\210.py" @@ -0,0 +1,58 @@ +from heapq import * +class DinnerPlates(object): + def __init__(self, capacity): + """ + :type capacity: int + """ + self.stack = [] + self.c = capacity + self.idx = [] #ڴпλջ± + + def push(self, val): + """ + :type val: int + :rtype: None + """ + if self.idx: + index = heappop(self.idx) #СĿյջ± + self.stack[index].append(val) #val + if len(self.stack[index]) < self.c: #֮ûͰյջ±Žself.idx + heappush(self.idx, index) + else: #ջˣֻһջĩβ + self.stack.append([val]) + if self.c > 1: + self.idx.append(len(self.stack) - 1) + + + def pop(self): + """ + :rtype: int + """ + while self.stack and not self.stack[-1]: + self.stack.pop() + if not self.stack: #еջǿյ + return -1 + else: + if len(self.stack[-1]) == self.c: #ջ + heappush(self.idx, len(self.stack) - 1) #Ҫпλջ + return self.stack[-1].pop() + + def popAtStack(self, index): + """ + :type index: int + :rtype: int + """ + if index >= len(self.stack): #±Խ + return -1 + else: + s = self.stack[index] # ±Ϊindexջȡ + if len(s) == self.c: #ջ + heappush(self.idx, index) #Ҫ + return s.pop() if s else -1 #ջǿյģ-1 򷵻pop + + +# Your DinnerPlates object will be instantiated and called as such: +# obj = DinnerPlates(capacity) +# obj.push(val) +# param_2 = obj.pop() +# param_3 = obj.popAtStack(index) \ 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/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/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..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.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/1192.\346\237\245\346\211\276\351\233\206\347\276\244\345\206\205\347\232\204\343\200\214\345\205\263\351\224\256\350\277\236\346\216\245\343\200\215/1192-\346\237\245\346\211\276\351\233\206\347\276\244\345\206\205\347\232\204\343\200\214\345\205\263\351\224\256\350\277\236\346\216\245\343\200\215.py" "b/1192.\346\237\245\346\211\276\351\233\206\347\276\244\345\206\205\347\232\204\343\200\214\345\205\263\351\224\256\350\277\236\346\216\245\343\200\215/1192-\346\237\245\346\211\276\351\233\206\347\276\244\345\206\205\347\232\204\343\200\214\345\205\263\351\224\256\350\277\236\346\216\245\343\200\215.py" new file mode 100644 index 0000000..38f0e66 --- /dev/null +++ "b/1192.\346\237\245\346\211\276\351\233\206\347\276\244\345\206\205\347\232\204\343\200\214\345\205\263\351\224\256\350\277\236\346\216\245\343\200\215/1192-\346\237\245\346\211\276\351\233\206\347\276\244\345\206\205\347\232\204\343\200\214\345\205\263\351\224\256\350\277\236\346\216\245\343\200\215.py" @@ -0,0 +1,41 @@ +from collections import defaultdict +class Solution(object): + def criticalConnections(self, n, connections): + """ + :type n: int + :type connections: List[List[int]] + :rtype: List[List[int]] + """ + visited = set() + low = [9999999] * n + discover = [999999] * n + parent = [-1] * n + + graph = defaultdict(list) + self.time = 0 + res = [] + for u, v in connections: + graph[u].append(v) + graph[v].append(u) + + def dfs(u): + visited.add(u) + discover[u] = self.time + low[u] = self.time + self.time += 1 + + for v in graph[u]: + if v not in visited: + parent[v] = u + dfs(v) + low[u] = min(low[u], low[v]) + + if low[v] > discover[u]: + res.append([u, v]) + elif v != parent[u]: + low[u] = min(low[u], discover[v]) + + for i in range(n): + if i not in visited: + dfs(i) + return res diff --git "a/1196.\346\234\200\345\244\232\345\217\257\344\273\245\344\271\260\345\210\260\347\232\204\350\213\271\346\236\234\346\225\260\351\207\217/1196-\346\234\200\345\244\232\345\217\257\344\273\245\344\271\260\345\210\260\347\232\204\350\213\271\346\236\234\346\225\260\351\207\217.py" "b/1196.\346\234\200\345\244\232\345\217\257\344\273\245\344\271\260\345\210\260\347\232\204\350\213\271\346\236\234\346\225\260\351\207\217/1196-\346\234\200\345\244\232\345\217\257\344\273\245\344\271\260\345\210\260\347\232\204\350\213\271\346\236\234\346\225\260\351\207\217.py" new file mode 100644 index 0000000..a974399 --- /dev/null +++ "b/1196.\346\234\200\345\244\232\345\217\257\344\273\245\344\271\260\345\210\260\347\232\204\350\213\271\346\236\234\346\225\260\351\207\217/1196-\346\234\200\345\244\232\345\217\257\344\273\245\344\271\260\345\210\260\347\232\204\350\213\271\346\236\234\346\225\260\351\207\217.py" @@ -0,0 +1,13 @@ +class Solution(object): + def maxNumberOfApples(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + arr.sort() + s = 0 + for i, num in enumerate(arr): + if s + num > 5000: + return i + s += num + return len(arr) \ 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/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/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..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.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/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260/1207-\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.py" "b/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260/1207-\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.py" new file mode 100644 index 0000000..76dc9b9 --- /dev/null +++ "b/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260/1207-\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.py" @@ -0,0 +1,8 @@ +class Solution(object): + def uniqueOccurrences(self, arr): + """ + :type arr: List[int] + :rtype: bool + """ + d = collections.Counter(arr) + return len(d.values()) == len(set(d.values())) \ No newline at end of file diff --git "a/1213.\344\270\211\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/1213-\344\270\211\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.py" "b/1213.\344\270\211\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/1213-\344\270\211\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.py" new file mode 100644 index 0000000..7e4fb9a --- /dev/null +++ "b/1213.\344\270\211\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206/1213-\344\270\211\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.py" @@ -0,0 +1,18 @@ +class Solution(object): + def arraysIntersection(self, arr1, arr2, arr3): + """ + :type arr1: List[int] + :type arr2: List[int] + :type arr3: List[int] + :rtype: List[int] + """ + res = [] + record = [0 for _ in range(2005)] + for num in arr1 + arr2 + arr3: + record[num] += 1 + + for i in range(len(record)): + if record[i] == 3: + res.append(i) + + return 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/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/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..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.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/1228.\347\255\211\345\267\256\346\225\260\345\210\227\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/1228-\347\255\211\345\267\256\346\225\260\345\210\227\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227.py" "b/1228.\347\255\211\345\267\256\346\225\260\345\210\227\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/1228-\347\255\211\345\267\256\346\225\260\345\210\227\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227.py" new file mode 100644 index 0000000..d31e33b --- /dev/null +++ "b/1228.\347\255\211\345\267\256\346\225\260\345\210\227\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/1228-\347\255\211\345\267\256\346\225\260\345\210\227\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227.py" @@ -0,0 +1,14 @@ +class Solution(object): + def missingNumber(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + d = (arr[-1] - arr[0]) // len(arr) + + k = arr[0] + for num in arr: + if num != k: + return k + k += d + return k \ No newline at end of file diff --git "a/1229.\345\256\211\346\216\222\344\274\232\350\256\256\346\227\245\347\250\213/1229-\345\256\211\346\216\222\344\274\232\350\256\256\346\227\245\347\250\213.py" "b/1229.\345\256\211\346\216\222\344\274\232\350\256\256\346\227\245\347\250\213/1229-\345\256\211\346\216\222\344\274\232\350\256\256\346\227\245\347\250\213.py" new file mode 100644 index 0000000..6547a61 --- /dev/null +++ "b/1229.\345\256\211\346\216\222\344\274\232\350\256\256\346\227\245\347\250\213/1229-\345\256\211\346\216\222\344\274\232\350\256\256\346\227\245\347\250\213.py" @@ -0,0 +1,31 @@ +class Solution(object): + def minAvailableDuration(self, slots1, slots2, duration): + """ + :type slots1: List[List[int]] + :type slots2: List[List[int]] + :type duration: int + :rtype: List[int] + """ + slots1 = sorted(slots1, key = lambda x:x[0]) + slots2 = sorted(slots2, key = lambda x:x[0]) + slots = [] + i, j = 0, 0 + while i < len(slots1) and j < len(slots2): + if slots1[i][1] < slots2[j][0]: + i += 1 + continue + elif slots1[i][0] > slots2[j][1]: + j += 1 + continue + start = max(slots1[i][0], slots2[j][0]) + end = min(slots1[i][1], slots2[j][1]) + slots.append([start, end]) + i += 1 + + print(slots) + for start, end in slots: + if end - start >= duration: + return [start, start + duration] + return [] + + 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" new file mode 100644 index 0000000..d928eef --- /dev/null +++ "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" @@ -0,0 +1,27 @@ +class Solution(object): + def probabilityOfHeads(self, prob, target): + """ + :type prob: List[float] + :type target: int + :rtype: float + """ + dp = [0 for _ in range(len(prob) + 1)] + dp[1] = prob[0] + dp[0] = 1 - prob[0] + for i in range(1, len(prob)): + new_dp = [0 for _ in range(len(prob) + 1)] + for j in range(target + 1): + new_dp[j] = dp[j] * (1 - prob[i]) + dp[j - 1] * prob[i] + dp = new_dp[:] + return dp[target] + + # dp = [[0 for _ in range(len(prob) + 1)] for _ in range(len(prob))] + # # dp[i][j] ʾǰiӲjӲ泯ϵĸ + # dp[0][1] = prob[0] + # dp[0][0] = 1 - prob[0] + # for i, p in enumerate(prob): + # for j in range(target + 1): + # if i > 0: + # dp[i][j] += dp[i - 1][j] * (1 - p) + # dp[i][j] += dp[i - 1][j - 1] * (p) + # return dp[-1][target] \ No newline at end of file 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/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/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..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.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/1232.\347\274\200\347\202\271\346\210\220\347\272\277/1232-\347\274\200\347\202\271\346\210\220\347\272\277.py" "b/1232.\347\274\200\347\202\271\346\210\220\347\272\277/1232-\347\274\200\347\202\271\346\210\220\347\272\277.py" new file mode 100644 index 0000000..0c166b1 --- /dev/null +++ "b/1232.\347\274\200\347\202\271\346\210\220\347\272\277/1232-\347\274\200\347\202\271\346\210\220\347\272\277.py" @@ -0,0 +1,22 @@ +class Solution(object): + def checkStraightLine(self, coordinates): + """ + :type coordinates: List[List[int]] + :rtype: bool + """ + c = sorted(coordinates, key = lambda x:x[0]) + k = None + for i in range(len(c)): + if i: + x0, y0 = c[i - 1][0], c[i - 1][1] + x1, y1 = c[i][0], c[i][1] + + if x0 == x1: + return False + new_k = 1.0 * (y1 - y0) / (x1 - x0) + if k and k != new_k: + return False + k = new_k + + return True + \ 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/1243.\346\225\260\347\273\204\345\217\230\346\215\242/1243-\346\225\260\347\273\204\345\217\230\346\215\242.py" "b/1243.\346\225\260\347\273\204\345\217\230\346\215\242/1243-\346\225\260\347\273\204\345\217\230\346\215\242.py" new file mode 100644 index 0000000..3cdc0ae --- /dev/null +++ "b/1243.\346\225\260\347\273\204\345\217\230\346\215\242/1243-\346\225\260\347\273\204\345\217\230\346\215\242.py" @@ -0,0 +1,20 @@ +class Solution(object): + def transformArray(self, arr): + """ + :type arr: List[int] + :rtype: List[int] + """ + flag = 1 + while flag: + flag = 0 + res = [num for num in arr] + for i in range(1, len(arr) - 1): + if arr[i - 1] < arr[i] and arr[i] > arr[i + 1]: + res[i] -= 1 + flag = 1 + elif arr[i - 1] > arr[i] and arr[i] < arr[i + 1]: + res[i] += 1 + flag = 1 + arr = res[:] + return res + \ No newline at end of file diff --git "a/1244.\345\212\233\346\211\243\346\216\222\350\241\214\346\246\234/1244-\345\212\233\346\211\243\346\216\222\350\241\214\346\246\234.py" "b/1244.\345\212\233\346\211\243\346\216\222\350\241\214\346\246\234/1244-\345\212\233\346\211\243\346\216\222\350\241\214\346\246\234.py" new file mode 100644 index 0000000..4cc49f1 --- /dev/null +++ "b/1244.\345\212\233\346\211\243\346\216\222\350\241\214\346\246\234/1244-\345\212\233\346\211\243\346\216\222\350\241\214\346\246\234.py" @@ -0,0 +1,40 @@ +from collections import defaultdict +from heapq import * +class Leaderboard(object): + + def __init__(self): + self.dic = defaultdict(int) + + def addScore(self, playerId, score): + """ + :type playerId: int + :type score: int + :rtype: None + """ + self.dic[playerId] += score + + def top(self, K): + """ + :type K: int + :rtype: int + """ + self.l = [] + heapify(self.l) + for pid, score in self.dic.items(): + if len(self.l) >= K: + if score > self.l[0]: + heappush(self.l, score) + heappop(self.l) + else: + heappush(self.l, score) + + return sum(self.l) + + + def reset(self, playerId): + """ + :type playerId: int + :rtype: None + """ + self.dic[playerId] = 0 + diff --git "a/1245.\346\240\221\347\232\204\347\233\264\345\276\204/1245-\346\240\221\347\232\204\347\233\264\345\276\204.py" "b/1245.\346\240\221\347\232\204\347\233\264\345\276\204/1245-\346\240\221\347\232\204\347\233\264\345\276\204.py" new file mode 100644 index 0000000..dcb279d --- /dev/null +++ "b/1245.\346\240\221\347\232\204\347\233\264\345\276\204/1245-\346\240\221\347\232\204\347\233\264\345\276\204.py" @@ -0,0 +1,32 @@ +from collections import defaultdict +class Solution(object): + def treeDiameter(self, edges): + """ + :type edges: List[List[int]] + :rtype: int + """ + if not edges: + return 0 + + self.neibors = defaultdict(set) + self.res = 0 + + for start, end in edges: # + self.neibors[start].add(end) + + def getHeight(node): + res = [] + for neibor in self.neibors[node]: + res.append(getHeight(neibor)) + + while len(res) < 2: # ͸յȥ + res.append(0) + + res = sorted(res) + self.res = max(self.res, sum(res[-2:])) # ȡ + return 1 + max(res) + + getHeight(edges[0][0]) + return self.res + + \ No newline at end of file diff --git "a/1247.\344\272\244\346\215\242\345\255\227\347\254\246\344\275\277\345\276\227\345\255\227\347\254\246\344\270\262\347\233\270\345\220\214/1247-\344\272\244\346\215\242\345\255\227\347\254\246\344\275\277\345\276\227\345\255\227\347\254\246\344\270\262\347\233\270\345\220\214.py" "b/1247.\344\272\244\346\215\242\345\255\227\347\254\246\344\275\277\345\276\227\345\255\227\347\254\246\344\270\262\347\233\270\345\220\214/1247-\344\272\244\346\215\242\345\255\227\347\254\246\344\275\277\345\276\227\345\255\227\347\254\246\344\270\262\347\233\270\345\220\214.py" new file mode 100644 index 0000000..2ac2df1 --- /dev/null +++ "b/1247.\344\272\244\346\215\242\345\255\227\347\254\246\344\275\277\345\276\227\345\255\227\347\254\246\344\270\262\347\233\270\345\220\214/1247-\344\272\244\346\215\242\345\255\227\347\254\246\344\275\277\345\276\227\345\255\227\347\254\246\344\270\262\347\233\270\345\220\214.py" @@ -0,0 +1,21 @@ +class Solution(object): + def minimumSwap(self, s1, s2): + """ + :type s1: str + :type s2: str + :rtype: int + """ + s = s1 + s2 + x = s.count("x") + if len(s1) != len(s2) or x % 2 == 1 or (len(s) - x) % 2 == 1: + return -1 + + pair1 = 0 + pair2 = 0 + for i in range(len(s1)): + if s1[i] == "y" and s2[i] == "x": + pair1 += 1 + elif s1[i] == "x" and s2[i] == "y": + pair2 += 1 + + return pair1 // 2 + pair2 // 2 + pair1 % 2 + pair2 % 2 \ No newline at end of file diff --git "a/1248.\347\273\237\350\256\241\343\200\214\344\274\230\347\276\216\345\255\220\346\225\260\347\273\204\343\200\215/1248-\347\273\237\350\256\241\343\200\214\344\274\230\347\276\216\345\255\220\346\225\260\347\273\204\343\200\215.py" "b/1248.\347\273\237\350\256\241\343\200\214\344\274\230\347\276\216\345\255\220\346\225\260\347\273\204\343\200\215/1248-\347\273\237\350\256\241\343\200\214\344\274\230\347\276\216\345\255\220\346\225\260\347\273\204\343\200\215.py" new file mode 100644 index 0000000..0101e1d --- /dev/null +++ "b/1248.\347\273\237\350\256\241\343\200\214\344\274\230\347\276\216\345\255\220\346\225\260\347\273\204\343\200\215/1248-\347\273\237\350\256\241\343\200\214\344\274\230\347\276\216\345\255\220\346\225\260\347\273\204\343\200\215.py" @@ -0,0 +1,36 @@ +class Solution(object): + def numberOfSubarrays(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: int + """ + if not nums: + return 0 + res = 0 + odd = [] + for i, num in enumerate(nums): + if num % 2: + odd.append(i) + + if len(odd) < k: + return 0 + + + for i in range(len(odd)): + if i + k > len(odd): + break + if i: + last = odd[i - 1] + else: + last = -1 + + if i + k < len(odd): + nxt = odd[i + k] + else: + nxt = len(nums) + + left = odd[i] - last + right = nxt - odd[i + k - 1] + res += left * right + return res \ No newline at end of file diff --git "a/1249.\347\247\273\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/1249-\347\247\273\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267.py" "b/1249.\347\247\273\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/1249-\347\247\273\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267.py" new file mode 100644 index 0000000..29ef926 --- /dev/null +++ "b/1249.\347\247\273\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/1249-\347\247\273\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267.py" @@ -0,0 +1,23 @@ +class Solution(object): + def minRemoveToMakeValid(self, s): + """ + :type s: str + :rtype: str + """ + left, right = 0, 0 + stack = [] + remove = set() + for i, ch in enumerate(s): + if ch == "(": + stack.append(i) + elif ch == ")": + if stack: + stack.pop() + else: + remove.add(i) + stack = set(stack) + res = "" + for i, ch in enumerate(s): + if i not in stack and i not in remove: + res += ch + return res \ No newline at end of file diff --git "a/1250.\346\243\200\346\237\245\343\200\214\345\245\275\346\225\260\347\273\204\343\200\215/1250-\346\243\200\346\237\245\343\200\214\345\245\275\346\225\260\347\273\204\343\200\215.py" "b/1250.\346\243\200\346\237\245\343\200\214\345\245\275\346\225\260\347\273\204\343\200\215/1250-\346\243\200\346\237\245\343\200\214\345\245\275\346\225\260\347\273\204\343\200\215.py" new file mode 100644 index 0000000..64a8190 --- /dev/null +++ "b/1250.\346\243\200\346\237\245\343\200\214\345\245\275\346\225\260\347\273\204\343\200\215/1250-\346\243\200\346\237\245\343\200\214\345\245\275\346\225\260\347\273\204\343\200\215.py" @@ -0,0 +1,14 @@ +class Solution(object): + def isGoodArray(self, nums): + """ + :type nums: List[int] + :rtype: bool + """ + def gcd(x, y): + while y: + x, y = y, x % y + return x + g = nums[0] + for num in nums: + g = gcd(g, num) + return g == 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/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/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..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.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/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256/1254-\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.py" "b/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256/1254-\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..4718739 --- /dev/null +++ "b/1254.\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256/1254-\347\273\237\350\256\241\345\260\201\351\227\255\345\262\233\345\261\277\347\232\204\346\225\260\347\233\256.py" @@ -0,0 +1,37 @@ +class Solution(object): + def closedIsland(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + m, n = len(grid), len(grid[0]) + dx = [1, -1, 0, 0] + dy = [0, 0, 1, -1] + def dfs(x0, y0): + if grid[x0][y0] == 0: + grid[x0][y0] = -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] == 0: + dfs(x, y) + + for j in range(n): + dfs(0, j) + for j in range(n): + dfs(m - 1, j) + for i in range(m): + dfs(i, 0) + for i in range(m): + dfs(i, n - 1) + + res = 0 + for i in range(m): + for j in range(n): + if grid[i][j] == 0: + res += 1 + dfs(i, j) + return res + + \ 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" new file mode 100644 index 0000000..32f2f05 --- /dev/null +++ "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" @@ -0,0 +1,50 @@ +class Solution(object): + def maxScoreWords(self, words, letters, score): + """ + :type words: List[str] + :type letters: List[str] + :type score: List[int] + :rtype: int + """ + 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ĸӦķ + + letter_dic = Counter(letters)#һֵ䣬 keyĸ valÿĸʣĸ + + s = set(letters) + v_words = [] + for word in words:#ɾиܱɵĵ + flag = 0 + for char in word: + if char not in s: + flag = 1 + if flag: # һijlettersҲĸ迼 + continue + v_words.append(word) + self.res = 0 + + def helper(word, letter_dic): + # return True wordletter_dicletterɣ򷵻False + dicc = collections.Counter(word) + for key in dicc: + if dicc[key] > letter_dic[key]: + return False + return True + + def dfs(start, tmp): + self.res = max(self.res, 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]: #ֵ + letter_dic[char] -= 1 + 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/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" new file mode 100644 index 0000000..2f0ce01 --- /dev/null +++ "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" @@ -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/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" new file mode 100644 index 0000000..1b04458 --- /dev/null +++ "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" @@ -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/1266.\350\256\277\351\227\256\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\346\227\266\351\227\264/1266-\350\256\277\351\227\256\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\346\227\266\351\227\264.py" "b/1266.\350\256\277\351\227\256\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\346\227\266\351\227\264/1266-\350\256\277\351\227\256\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\346\227\266\351\227\264.py" new file mode 100644 index 0000000..a147831 --- /dev/null +++ "b/1266.\350\256\277\351\227\256\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\346\227\266\351\227\264/1266-\350\256\277\351\227\256\346\211\200\346\234\211\347\202\271\347\232\204\346\234\200\345\260\217\346\227\266\351\227\264.py" @@ -0,0 +1,18 @@ +class Solution(object): + def minTimeToVisitAllPoints(self, points): + """ + :type points: List[List[int]] + :rtype: int + """ + def helper(pair1, pair2): + x0, y0 = pair1[0], pair1[1] + x1, y1 = pair2[0], pair2[1] + + return max(abs(y1 - y0), abs(x1 - x0)) + + res = 0 + for i, point in enumerate(points): + if i > 0: + res += helper(point, points[i - 1]) + + return res \ No newline at end of file diff --git "a/1267.\347\273\237\350\256\241\345\217\202\344\270\216\351\200\232\344\277\241\347\232\204\346\234\215\345\212\241\345\231\250/1267-\347\273\237\350\256\241\345\217\202\344\270\216\351\200\232\344\277\241\347\232\204\346\234\215\345\212\241\345\231\250.py" "b/1267.\347\273\237\350\256\241\345\217\202\344\270\216\351\200\232\344\277\241\347\232\204\346\234\215\345\212\241\345\231\250/1267-\347\273\237\350\256\241\345\217\202\344\270\216\351\200\232\344\277\241\347\232\204\346\234\215\345\212\241\345\231\250.py" new file mode 100644 index 0000000..87cae9f --- /dev/null +++ "b/1267.\347\273\237\350\256\241\345\217\202\344\270\216\351\200\232\344\277\241\347\232\204\346\234\215\345\212\241\345\231\250/1267-\347\273\237\350\256\241\345\217\202\344\270\216\351\200\232\344\277\241\347\232\204\346\234\215\345\212\241\345\231\250.py" @@ -0,0 +1,29 @@ +class Solution(object): + def countServers(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + + m, n = len(grid), len(grid[0]) + res = 0 + for i in range(m): + for j in range(n): + if grid[i][j] == 1: + # horizontal + for col in range(n): + if col != j: + if grid[i][col] in [1, 2]: + grid[i][j] = 2 + grid[i][col] = 2 + + # vertical + for row in range(m): + if row != i: + if grid[row][j] in [1, 2]: + grid[i][j] = 2 + grid[row][j] = 2 + if grid[i][j] == 2: + res += 1 + # print grid + return res \ No newline at end of file diff --git "a/1268.\346\220\234\347\264\242\346\216\250\350\215\220\347\263\273\347\273\237/1268-\346\220\234\347\264\242\346\216\250\350\215\220\347\263\273\347\273\237.py" "b/1268.\346\220\234\347\264\242\346\216\250\350\215\220\347\263\273\347\273\237/1268-\346\220\234\347\264\242\346\216\250\350\215\220\347\263\273\347\273\237.py" new file mode 100644 index 0000000..34f7fa4 --- /dev/null +++ "b/1268.\346\220\234\347\264\242\346\216\250\350\215\220\347\263\273\347\273\237/1268-\346\220\234\347\264\242\346\216\250\350\215\220\347\263\273\347\273\237.py" @@ -0,0 +1,21 @@ +class Solution(object): + def suggestedProducts(self, products, searchWord): + """ + :type products: List[str] + :type searchWord: str + :rtype: List[List[str]] + """ + products.sort() + res = [] + prefix = "" + for char in searchWord: + tmp = [] + prefix += char + idx = bisect.bisect_left(products, prefix) # 找当前prefix 起点,因为有序所以二分 + for word in products[idx:]: + if len(tmp) >= 3 or word[:len(prefix)] > prefix: + break + if word[:len(prefix)] == prefix: + tmp.append(word) + res.append(tmp) + return res \ No newline at end of file diff --git "a/1269.\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260/1269-\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260.py" "b/1269.\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260/1269-\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260.py" new file mode 100644 index 0000000..a11472a --- /dev/null +++ "b/1269.\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260/1269-\345\201\234\345\234\250\345\216\237\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260.py" @@ -0,0 +1,23 @@ +class Solution(object): + def numWays(self, steps, arrLen): + """ + :type steps: int + :type arrLen: int + :rtype: int + """ + # dp[i][j] 代表走 i 步,到位置 j 的解 + # dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] + dp[i - 1][j + 1] + n = min(steps, arrLen) + dp = [[0 for _ in range(n)] for _ in range(steps + 1)] + mod = 10 ** 9 + 7 + + dp[0][0] = 1 + for i in range(1, steps + 1): + for j in range(n): + if j == 0: + dp[i][j] += (dp[i - 1][0] + dp[i - 1][1]) % mod + elif j == n - 1: + dp[i][j] += (dp[i - 1][j] + dp[i - 1][j - 1]) % mod + else: + dp[i][j] += (dp[i - 1][j - 1] + dp[i - 1][j] + dp[i - 1][j + 1]) % mod + return dp[steps][0] \ 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" new file mode 100644 index 0000000..26dfeca --- /dev/null +++ "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" @@ -0,0 +1,48 @@ +class Solution(object): + def tictactoe(self, moves): + """ + :type moves: List[List[int]] + :rtype: str + """ + grid = [[-1 for _ in range(3)] for _ in range(3)] + def check(): + for row in grid: + if row == [0, 0, 0]: + return 0 + if row == [1, 1, 1]: + return 1 + + for j in range(3): + tmp = [] + for i in range(3): + tmp.append(grid[i][j]) + if tmp == [0, 0, 0]: + return 0 + if tmp == [1, 1, 1]: + return 1 + + tmp = [grid[0][0], grid[1][1], grid[2][2]] + if tmp == [0, 0, 0]: + return 0 + if tmp == [1, 1, 1]: + return 1 + + tmp = [grid[2][0], grid[1][1], grid[0][2]] + if tmp == [0, 0, 0]: + return 0 + if tmp == [1, 1, 1]: + return 1 + return -1 + + + player = 0 + for move in moves: + grid[move[0]][move[1]] = player + player = 1 - player + + 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 diff --git "a/1276.\344\270\215\346\265\252\350\264\271\345\216\237\346\226\231\347\232\204\346\261\211\345\240\241\345\210\266\344\275\234\346\226\271\346\241\210/1276-\344\270\215\346\265\252\350\264\271\345\216\237\346\226\231\347\232\204\346\261\211\345\240\241\345\210\266\344\275\234\346\226\271\346\241\210.py" "b/1276.\344\270\215\346\265\252\350\264\271\345\216\237\346\226\231\347\232\204\346\261\211\345\240\241\345\210\266\344\275\234\346\226\271\346\241\210/1276-\344\270\215\346\265\252\350\264\271\345\216\237\346\226\231\347\232\204\346\261\211\345\240\241\345\210\266\344\275\234\346\226\271\346\241\210.py" new file mode 100644 index 0000000..a7cc96c --- /dev/null +++ "b/1276.\344\270\215\346\265\252\350\264\271\345\216\237\346\226\231\347\232\204\346\261\211\345\240\241\345\210\266\344\275\234\346\226\271\346\241\210/1276-\344\270\215\346\265\252\350\264\271\345\216\237\346\226\231\347\232\204\346\261\211\345\240\241\345\210\266\344\275\234\346\226\271\346\241\210.py" @@ -0,0 +1,16 @@ +class Solution(object): + def numOfBurgers(self, tomatoSlices, cheeseSlices): + """ + :type tomatoSlices: int + :type cheeseSlices: int + :rtype: List[int] + """ + doublex = tomatoSlices - cheeseSlices * 2 + if doublex < 0 or doublex % 2 != 0: + return [] + + x = doublex // 2 + y = cheeseSlices - doublex // 2 + if x >= 0 and y >= 0: + return [x, y] + return [] \ No newline at end of file diff --git "a/1277.\347\273\237\350\256\241\345\205\250\344\270\2721\347\232\204\346\255\243\346\226\271\345\275\242\345\255\220\347\237\251\351\230\265/1277-\347\273\237\350\256\241\345\205\250\344\270\2721\347\232\204\346\255\243\346\226\271\345\275\242\345\255\220\347\237\251\351\230\265.py" "b/1277.\347\273\237\350\256\241\345\205\250\344\270\2721\347\232\204\346\255\243\346\226\271\345\275\242\345\255\220\347\237\251\351\230\265/1277-\347\273\237\350\256\241\345\205\250\344\270\2721\347\232\204\346\255\243\346\226\271\345\275\242\345\255\220\347\237\251\351\230\265.py" new file mode 100644 index 0000000..4115102 --- /dev/null +++ "b/1277.\347\273\237\350\256\241\345\205\250\344\270\2721\347\232\204\346\255\243\346\226\271\345\275\242\345\255\220\347\237\251\351\230\265/1277-\347\273\237\350\256\241\345\205\250\344\270\2721\347\232\204\346\255\243\346\226\271\345\275\242\345\255\220\347\237\251\351\230\265.py" @@ -0,0 +1,29 @@ +class Solution(object): + def countSquares(self, matrix): + """ + :type matrix: List[List[int]] + :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]: + res += 1 + dp[0][j] = 1 + + for i in range(1, m): + if matrix[i][0]: + res += 1 + dp[i][0] = 1 + + for i in range(1, m): + for j in range(1, n): + if matrix[i][j]: + dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1 + res += dp[i][j] + return res \ 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" new file mode 100644 index 0000000..4e18cba --- /dev/null +++ "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" @@ -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/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" new file mode 100644 index 0000000..70a5142 --- /dev/null +++ "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" @@ -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/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" new file mode 100644 index 0000000..fb0b769 --- /dev/null +++ "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" @@ -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/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" new file mode 100644 index 0000000..564b692 --- /dev/null +++ "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" @@ -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/1318.\346\210\226\350\277\220\347\256\227\347\232\204\346\234\200\345\260\217\347\277\273\350\275\254\346\254\241\346\225\260/1318-\346\210\226\350\277\220\347\256\227\347\232\204\346\234\200\345\260\217\347\277\273\350\275\254\346\254\241\346\225\260.py" "b/1318.\346\210\226\350\277\220\347\256\227\347\232\204\346\234\200\345\260\217\347\277\273\350\275\254\346\254\241\346\225\260/1318-\346\210\226\350\277\220\347\256\227\347\232\204\346\234\200\345\260\217\347\277\273\350\275\254\346\254\241\346\225\260.py" new file mode 100644 index 0000000..213c75d --- /dev/null +++ "b/1318.\346\210\226\350\277\220\347\256\227\347\232\204\346\234\200\345\260\217\347\277\273\350\275\254\346\254\241\346\225\260/1318-\346\210\226\350\277\220\347\256\227\347\232\204\346\234\200\345\260\217\347\277\273\350\275\254\346\254\241\346\225\260.py" @@ -0,0 +1,14 @@ +class Solution(object): + def minFlips(self, a, b, c): + """ + :type a: int + :type b: int + :type c: int + :rtype: int + """ + res = 0 + while a or b or c: + if (a & 1 | b & 1) != (c & 1): + res += 1 + (a & 1) * (b & 1) + a, b, c = a >> 1, b >> 1, c >> 1 + return res \ No newline at end of file diff --git "a/5023.\345\275\274\346\255\244\347\206\237\350\257\206\347\232\204\346\234\200\346\227\251\346\227\266\351\227\264/5023-\345\275\274\346\255\244\347\206\237\350\257\206\347\232\204\346\234\200\346\227\251\346\227\266\351\227\264.py" "b/1319.\350\277\236\351\200\232\347\275\221\347\273\234\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260/1319-\350\277\236\351\200\232\347\275\221\347\273\234\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260.py" similarity index 62% rename from "5023.\345\275\274\346\255\244\347\206\237\350\257\206\347\232\204\346\234\200\346\227\251\346\227\266\351\227\264/5023-\345\275\274\346\255\244\347\206\237\350\257\206\347\232\204\346\234\200\346\227\251\346\227\266\351\227\264.py" rename to "1319.\350\277\236\351\200\232\347\275\221\347\273\234\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260/1319-\350\277\236\351\200\232\347\275\221\347\273\234\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260.py" index e0ebb78..42c33d5 100644 --- "a/5023.\345\275\274\346\255\244\347\206\237\350\257\206\347\232\204\346\234\200\346\227\251\346\227\266\351\227\264/5023-\345\275\274\346\255\244\347\206\237\350\257\206\347\232\204\346\234\200\346\227\251\346\227\266\351\227\264.py" +++ "b/1319.\350\277\236\351\200\232\347\275\221\347\273\234\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260/1319-\350\277\236\351\200\232\347\275\221\347\273\234\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260.py" @@ -1,9 +1,10 @@ + class UnionFindSet(object): def __init__(self, n): self.roots = [i for i in range(n)] self.rank = [0 for i in range(n)] - self.count = 0 - + self.count = n + def find(self, member): tmp = [] while member != self.roots[member]: @@ -12,7 +13,7 @@ def find(self, member): for root in tmp: self.roots[root] = member return member - + def union(self, p, q): parentP = self.find(p) parentQ = self.find(q) @@ -25,26 +26,25 @@ def union(self, p, q): self.roots[parentQ] = parentP self.rank[parentP] -= 1 self.count -= 1 - - def check(self): - print self.roots - flag = self.find(self.roots[0]) - for i in self.roots: - if self.find(i) != flag: - return False - return True + class Solution(object): - def earliestAcq(self, logs, N): + def makeConnected(self, n, connections): """ - :type logs: List[List[int]] - :type N: int + :type n: int + :type connections: List[List[int]] :rtype: int """ - logs = sorted(logs, key = lambda x:x[0]) - ufs = UnionFindSet(N) - for time, x, y in logs: - ufs.union(x, y) - if ufs.check(): - return time - return -1 \ No newline at end of file + if len(connections) < n - 1: + return -1 + + res = 0 + ufs = UnionFindSet(n) + for s, e in connections: + ufs.union(s, e) + + return ufs.count - 1 + + + + \ No newline at end of file diff --git "a/1323.6\345\222\2149\347\273\204\346\210\220\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/1323-6\345\222\2149\347\273\204\346\210\220\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" "b/1323.6\345\222\2149\347\273\204\346\210\220\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/1323-6\345\222\2149\347\273\204\346\210\220\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" new file mode 100644 index 0000000..652cb8d --- /dev/null +++ "b/1323.6\345\222\2149\347\273\204\346\210\220\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227/1323-6\345\222\2149\347\273\204\346\210\220\347\232\204\346\234\200\345\244\247\346\225\260\345\255\227.py" @@ -0,0 +1,7 @@ +class Solution(object): + def maximum69Number (self, num): + """ + :type num: int + :rtype: int + """ + return int(str(num).replace("6", "9", 1)) \ No newline at end of file diff --git "a/1324.\347\253\226\347\233\264\346\211\223\345\215\260\345\215\225\350\257\215/1324-\347\253\226\347\233\264\346\211\223\345\215\260\345\215\225\350\257\215.py" "b/1324.\347\253\226\347\233\264\346\211\223\345\215\260\345\215\225\350\257\215/1324-\347\253\226\347\233\264\346\211\223\345\215\260\345\215\225\350\257\215.py" new file mode 100644 index 0000000..214e6c6 --- /dev/null +++ "b/1324.\347\253\226\347\233\264\346\211\223\345\215\260\345\215\225\350\257\215/1324-\347\253\226\347\233\264\346\211\223\345\215\260\345\215\225\350\257\215.py" @@ -0,0 +1,26 @@ +class Solution(object): + def printVertically(self, s): + """ + :type s: str + :rtype: List[str] + """ + + l = s.split(" ") + max_l = max([len(item) for item in l]) + + res = [""] * max_l + for i in range(max_l): + tmp = "" + for j in range(len(l)): + if len(l[j]) > i: + tmp += l[j][i] + else: + tmp += " " + res[i] = tmp.rstrip() + + return res + # l = s.split(" ") + # res = [] + # for item in zip(*l): + # res.append("".join(item)) + # return res \ No newline at end of file diff --git "a/1325.\345\210\240\351\231\244\347\273\231\345\256\232\345\200\274\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/1325-\345\210\240\351\231\244\347\273\231\345\256\232\345\200\274\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271.py" "b/1325.\345\210\240\351\231\244\347\273\231\345\256\232\345\200\274\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/1325-\345\210\240\351\231\244\347\273\231\345\256\232\345\200\274\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271.py" new file mode 100644 index 0000000..a407dcb --- /dev/null +++ "b/1325.\345\210\240\351\231\244\347\273\231\345\256\232\345\200\274\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271/1325-\345\210\240\351\231\244\347\273\231\345\256\232\345\200\274\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271.py" @@ -0,0 +1,68 @@ +# 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 removeLeafNodes(self, root, target): + """ + :type root: TreeNode + :type target: int + :rtype: TreeNode + """ + if not root: + return root + root.left = self.removeLeafNodes(root.left, target) + root.right = self.removeLeafNodes(root.right, target) + if not root.left and not root.right and root.val == target: + return None + return root + +# from collections import defaultdict, deque +# if not root: +# return root +# outdegree = dict() +# parent = dict() + +# # 层序遍历预处理 +# queue = deque([root]) +# while queue: +# for _ in range(len(queue)): +# cur = queue.popleft() +# outdegree[cur] = 0 +# if cur.left: +# outdegree[cur] += 1 +# parent[cur.left] = cur +# queue.append(cur.left) +# if cur.right: +# outdegree[cur] += 1 +# parent[cur.right] = cur +# queue.append(cur.right) + +# queue = deque([]) +# for key, val in outdegree.items(): +# if not val and key.val == target: +# queue.append(key) + +# while queue: +# cur = queue.popleft() +# if cur not in parent: +# return None +# par = parent[cur] +# if par.left and par.left == cur: +# par.left = None +# outdegree[par] -= 1 +# if par.right and par.right == cur: +# par.right = None +# outdegree[par] -= 1 + +# if outdegree[par] == 0 and par.val == target: +# queue.append(par) + +# return root + + + + \ No newline at end of file diff --git "a/1326.\347\201\214\346\272\211\350\212\261\345\233\255\347\232\204\346\234\200\345\260\221\346\260\264\351\276\231\345\244\264\346\225\260\347\233\256/1326-\347\201\214\346\272\211\350\212\261\345\233\255\347\232\204\346\234\200\345\260\221\346\260\264\351\276\231\345\244\264\346\225\260\347\233\256.py" "b/1326.\347\201\214\346\272\211\350\212\261\345\233\255\347\232\204\346\234\200\345\260\221\346\260\264\351\276\231\345\244\264\346\225\260\347\233\256/1326-\347\201\214\346\272\211\350\212\261\345\233\255\347\232\204\346\234\200\345\260\221\346\260\264\351\276\231\345\244\264\346\225\260\347\233\256.py" new file mode 100644 index 0000000..7d4b22e --- /dev/null +++ "b/1326.\347\201\214\346\272\211\350\212\261\345\233\255\347\232\204\346\234\200\345\260\221\346\260\264\351\276\231\345\244\264\346\225\260\347\233\256/1326-\347\201\214\346\272\211\350\212\261\345\233\255\347\232\204\346\234\200\345\260\221\346\260\264\351\276\231\345\244\264\346\225\260\347\233\256.py" @@ -0,0 +1,22 @@ +class Solution(object): + def minTaps(self, n, ranges): + """ + :type n: int + :type ranges: List[int] + :rtype: int + """ + ivls = [] + reach = [0 for _ in range(n + 1)] + for i in range(n + 1): + start, end = max(i - ranges[i], 0), min(i + ranges[i], n + 1) + for j in range(start, end): + reach[j] = max(end, reach[j]) + + res = 0 + pos = 0 + while pos < n: + if reach[pos] <= pos: # 无法继续抵达更远的位置 + return -1 + pos = reach[pos] + res += 1 + return res \ No newline at end of file diff --git "a/1328.\347\240\264\345\235\217\345\233\236\346\226\207\344\270\262/1328-\347\240\264\345\235\217\345\233\236\346\226\207\344\270\262.py" "b/1328.\347\240\264\345\235\217\345\233\236\346\226\207\344\270\262/1328-\347\240\264\345\235\217\345\233\236\346\226\207\344\270\262.py" new file mode 100644 index 0000000..2cb31d9 --- /dev/null +++ "b/1328.\347\240\264\345\235\217\345\233\236\346\226\207\344\270\262/1328-\347\240\264\345\235\217\345\233\236\346\226\207\344\270\262.py" @@ -0,0 +1,15 @@ +class Solution(object): + def breakPalindrome(self, palindrome): + """ + :type palindrome: str + :rtype: str + """ + if len(palindrome) == 1: + return "" + if len(set(palindrome)) == 1: + if palindrome[0] == "a": + return palindrome[:-1] + "b" + for ch in palindrome: + if ord(ch) > ord("a"): + res = palindrome.replace(ch, "a", 1) + return res if res != res[::-1] else palindrome[:-1] + "b" \ No newline at end of file diff --git "a/1329.\345\260\206\347\237\251\351\230\265\346\214\211\345\257\271\350\247\222\347\272\277\346\216\222\345\272\217/1329-\345\260\206\347\237\251\351\230\265\346\214\211\345\257\271\350\247\222\347\272\277\346\216\222\345\272\217.py" "b/1329.\345\260\206\347\237\251\351\230\265\346\214\211\345\257\271\350\247\222\347\272\277\346\216\222\345\272\217/1329-\345\260\206\347\237\251\351\230\265\346\214\211\345\257\271\350\247\222\347\272\277\346\216\222\345\272\217.py" new file mode 100644 index 0000000..2c56a37 --- /dev/null +++ "b/1329.\345\260\206\347\237\251\351\230\265\346\214\211\345\257\271\350\247\222\347\272\277\346\216\222\345\272\217/1329-\345\260\206\347\237\251\351\230\265\346\214\211\345\257\271\350\247\222\347\272\277\346\216\222\345\272\217.py" @@ -0,0 +1,33 @@ +class Solution(object): + def diagonalSort(self, mat): + """ + :type mat: List[List[int]] + :rtype: List[List[int]] + """ + if not mat or not mat[0]: + return None + m, n = len(mat), len(mat[0]) + for j in range(n): + i, l, pos = 0, [], [] + while i < m and j < n: + l.append(mat[i][j]) + pos.append([i, j]) + i += 1 + j += 1 + l.sort() + for i, p in enumerate(pos): + x, y = p + mat[x][y] = l[i] + + for i in range(1, m): + j, l, pos = 0, [], [] + while i < m and j < n: + l.append(mat[i][j]) + pos.append([i, j]) + i += 1 + j += 1 + l.sort() + for i, p in enumerate(pos): + x, y = p + mat[x][y] = l[i] + return mat \ No newline at end of file diff --git "a/1331.\346\225\260\347\273\204\345\272\217\345\217\267\350\275\254\346\215\242/1331-\346\225\260\347\273\204\345\272\217\345\217\267\350\275\254\346\215\242.py" "b/1331.\346\225\260\347\273\204\345\272\217\345\217\267\350\275\254\346\215\242/1331-\346\225\260\347\273\204\345\272\217\345\217\267\350\275\254\346\215\242.py" new file mode 100644 index 0000000..498c69d --- /dev/null +++ "b/1331.\346\225\260\347\273\204\345\272\217\345\217\267\350\275\254\346\215\242/1331-\346\225\260\347\273\204\345\272\217\345\217\267\350\275\254\346\215\242.py" @@ -0,0 +1,19 @@ +class Solution(object): + def arrayRankTransform(self, arr): + """ + :type arr: List[int] + :rtype: List[int] + """ + l = sorted(arr) + dic, rank = {}, 0 + pre_num = None + for i, num in enumerate(l): + if pre_num != num: + rank += 1 + dic[num] = rank + pre_num = num + + res = [] + for num in arr: + res.append(dic[num]) + return res \ No newline at end of file diff --git "a/1332.\345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/1332-\345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.py" "b/1332.\345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/1332-\345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.py" new file mode 100644 index 0000000..2789ca8 --- /dev/null +++ "b/1332.\345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227/1332-\345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.py" @@ -0,0 +1,9 @@ +class Solution(object): + def removePalindromeSub(self, s): + """ + :type s: str + :rtype: int + """ + if not s: + return 0 + return 1 if s == s[::-1] else 2 \ No newline at end of file diff --git "a/1333.\351\244\220\345\216\205\350\277\207\346\273\244\345\231\250/1333-\351\244\220\345\216\205\350\277\207\346\273\244\345\231\250.py" "b/1333.\351\244\220\345\216\205\350\277\207\346\273\244\345\231\250/1333-\351\244\220\345\216\205\350\277\207\346\273\244\345\231\250.py" new file mode 100644 index 0000000..5420813 --- /dev/null +++ "b/1333.\351\244\220\345\216\205\350\277\207\346\273\244\345\231\250/1333-\351\244\220\345\216\205\350\277\207\346\273\244\345\231\250.py" @@ -0,0 +1,19 @@ +class Solution(object): + def filterRestaurants(self, restaurants, veganFriendly, maxPrice, maxDistance): + """ + :type restaurants: List[List[int]] + :type veganFriendly: int + :type maxPrice: int + :type maxDistance: int + :rtype: List[int] + """ + if not restaurants: + return [] + + res = [] + for i, rating, vF, mP, mD in restaurants: + if not veganFriendly or (vF and veganFriendly): + if mP <= maxPrice and mD <= maxDistance: + res.append([i, rating]) + + return [i for i, rating in sorted(res, key = lambda x:(x[1], x[0]), reverse = True)] \ No newline at end of file diff --git "a/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202/1334-\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.py" "b/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202/1334-\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.py" new file mode 100644 index 0000000..cf2536c --- /dev/null +++ "b/1334.\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202/1334-\351\230\210\345\200\274\350\267\235\347\246\273\345\206\205\351\202\273\345\261\205\346\234\200\345\260\221\347\232\204\345\237\216\345\270\202.py" @@ -0,0 +1,33 @@ +class Solution(object): + def findTheCity(self, n, edges, distanceThreshold): + """ + :type n: int + :type edges: List[List[int]] + :type distanceThreshold: int + :rtype: int + """ + distance = [[float("inf") for j in range(n)] for i in range(n)] + + for start, end, w in edges: + distance[start][end] = w + distance[end][start] = w + for i in range(n): + distance[i][i] = 0 + for i in range(n): + for j in range(n): + for k in range(n): + distance[j][k] = min(distance[j][k], distance[j][i] + distance[i][k]) + + min_cnt = 101 + res = None + for i in range(n): + cnt = 0 + for j in range(n): + if distance[i][j] <= distanceThreshold: + cnt += 1 + + if cnt <= min_cnt: + res = i + min_cnt = cnt + return res + \ No newline at end of file diff --git "a/1337.\346\226\271\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-\346\226\271\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.\346\226\271\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-\346\226\271\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..ed984e3 --- /dev/null +++ "b/1337.\346\226\271\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-\346\226\271\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,13 @@ +class Solution(object): + def kWeakestRows(self, mat, k): + """ + :type mat: List[List[int]] + :type k: int + :rtype: List[int] + """ + + res = [] + for i, row in enumerate(mat): + res.append((sum(row), i)) + + return [i for s, i in sorted(res)[:k]] \ 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/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/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..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.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/1339.\345\210\206\350\243\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1339-\345\210\206\350\243\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257.py" "b/1339.\345\210\206\350\243\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1339-\345\210\206\350\243\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257.py" new file mode 100644 index 0000000..e82fcf2 --- /dev/null +++ "b/1339.\345\210\206\350\243\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/1339-\345\210\206\350\243\202\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257.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 maxProduct(self, root): + """ + :type root: TreeNode + :rtype: int + """ + dic = {} + + def SumOfTree(node): + if not node: + return 0 + ls, rs = SumOfTree(node.left), SumOfTree(node.right) + + dic[node] = ls + rs + node.val + return dic[node] + + SumOfTree(root) + TotalSum = dic[root] + + self.res = 0 + def dfs(node): + if not node: + return + + tmp = (TotalSum - dic[node]) * dic[node] + self.res = max(self.res, tmp) + + dfs(node.left) + dfs(node.right) + dfs(root) + return self.res % (10 ** 9 + 7) \ No newline at end of file diff --git "a/1340.\350\267\263\350\267\203\346\270\270\346\210\217V/1340-\350\267\263\350\267\203\346\270\270\346\210\217V.py" "b/1340.\350\267\263\350\267\203\346\270\270\346\210\217V/1340-\350\267\263\350\267\203\346\270\270\346\210\217V.py" new file mode 100644 index 0000000..462343d --- /dev/null +++ "b/1340.\350\267\263\350\267\203\346\270\270\346\210\217V/1340-\350\267\263\350\267\203\346\270\270\346\210\217V.py" @@ -0,0 +1,25 @@ +class Solution(object): + def maxJumps(self, arr, d): + """ + :type arr: List[int] + :type d: int + :rtype: int + """ + res = [(x, i) for i, x in enumerate(arr)] + + res.sort() + # print res + dp = [1 for _ in res] + + for k in range(len(arr)): + i = res[k][1] + for j in range(1, d + 1): + if i + j == len(arr) or arr[i + j] >= arr[i]: + break + dp[i] = max(dp[i], dp[i + j] + 1) + + for j in range(1, d + 1): + if i - j < 0 or arr[i - j] >= arr[i]: + break + dp[i] = max(dp[i], dp[i - j] + 1) + return max(dp) \ No newline at end of file diff --git "a/1342.\345\260\206\346\225\260\345\255\227\345\217\230\346\210\2200\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260/1342-\345\260\206\346\225\260\345\255\227\345\217\230\346\210\2200\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260.py" "b/1342.\345\260\206\346\225\260\345\255\227\345\217\230\346\210\2200\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260/1342-\345\260\206\346\225\260\345\255\227\345\217\230\346\210\2200\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260.py" new file mode 100644 index 0000000..cb0f367 --- /dev/null +++ "b/1342.\345\260\206\346\225\260\345\255\227\345\217\230\346\210\2200\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260/1342-\345\260\206\346\225\260\345\255\227\345\217\230\346\210\2200\347\232\204\346\223\215\344\275\234\346\254\241\346\225\260.py" @@ -0,0 +1,14 @@ +class Solution(object): + def numberOfSteps (self, num): + """ + :type num: int + :rtype: int + """ + cnt = 0 + while num: + if num % 2: + num -= 1 + else: + num >>= 1 + cnt += 1 + return cnt \ No newline at end of file diff --git "a/1343.\345\244\247\345\260\217\344\270\272K\344\270\224\345\271\263\345\235\207\345\200\274\345\244\247\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1343-\345\244\247\345\260\217\344\270\272K\344\270\224\345\271\263\345\235\207\345\200\274\345\244\247\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256.py" "b/1343.\345\244\247\345\260\217\344\270\272K\344\270\224\345\271\263\345\235\207\345\200\274\345\244\247\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1343-\345\244\247\345\260\217\344\270\272K\344\270\224\345\271\263\345\235\207\345\200\274\345\244\247\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\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..ec68791 --- /dev/null +++ "b/1343.\345\244\247\345\260\217\344\270\272K\344\270\224\345\271\263\345\235\207\345\200\274\345\244\247\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256/1343-\345\244\247\345\260\217\344\270\272K\344\270\224\345\271\263\345\235\207\345\200\274\345\244\247\344\272\216\347\255\211\344\272\216\351\230\210\345\200\274\347\232\204\345\255\220\346\225\260\347\273\204\346\225\260\347\233\256.py" @@ -0,0 +1,21 @@ +class Solution(object): + def numOfSubarrays(self, arr, k, threshold): + """ + :type arr: List[int] + :type k: int + :type threshold: int + :rtype: int + """ + res = 0 + target_sum = k * threshold + subs = 0 + for i in range(0, k): + subs += arr[i] + + for i in range(0, len(arr) - k + 1): + if i: + subs = subs - arr[i - 1] + arr[i + k - 1] + if subs >= target_sum: + res += 1 + + return res \ No newline at end of file diff --git "a/1344.\346\227\266\351\222\237\346\214\207\351\222\210\347\232\204\345\244\271\350\247\222/1344-\346\227\266\351\222\237\346\214\207\351\222\210\347\232\204\345\244\271\350\247\222.py" "b/1344.\346\227\266\351\222\237\346\214\207\351\222\210\347\232\204\345\244\271\350\247\222/1344-\346\227\266\351\222\237\346\214\207\351\222\210\347\232\204\345\244\271\350\247\222.py" new file mode 100644 index 0000000..072df3b --- /dev/null +++ "b/1344.\346\227\266\351\222\237\346\214\207\351\222\210\347\232\204\345\244\271\350\247\222/1344-\346\227\266\351\222\237\346\214\207\351\222\210\347\232\204\345\244\271\350\247\222.py" @@ -0,0 +1,13 @@ +class Solution(object): + def angleClock(self, hour, minutes): + """ + :type hour: int + :type minutes: int + :rtype: float + """ + min_angle = minutes * 1.0 / 60 * 360 + hour_angle = hour * 1.0 / 12 * 360 + minutes * 1.0 / 60 * 30 + + res = abs(hour_angle - min_angle) + + return res if res < 180 else 360 - res \ No newline at end of file diff --git "a/1345.\350\267\263\350\267\203\346\270\270\346\210\217IV/1345-\350\267\263\350\267\203\346\270\270\346\210\217IV.py" "b/1345.\350\267\263\350\267\203\346\270\270\346\210\217IV/1345-\350\267\263\350\267\203\346\270\270\346\210\217IV.py" new file mode 100644 index 0000000..984dac2 --- /dev/null +++ "b/1345.\350\267\263\350\267\203\346\270\270\346\210\217IV/1345-\350\267\263\350\267\203\346\270\270\346\210\217IV.py" @@ -0,0 +1,25 @@ +class Solution(object): + def minJumps(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + from collections import defaultdict, deque + dic = defaultdict(list) + for i, x in enumerate(arr): + if (i and arr[i] != arr[i - 1]) or (i < len(arr) - 1 and arr[i] != arr[i + 1]): + dic[x].append(i) + + queue = deque([(0, 0)]) #pos, step + visited = set([0]) + + while queue: + pos, step = queue.popleft() + + if pos == len(arr) - 1: + return step + + for p in [pos - 1, pos + 1] + dic[arr[pos]]: + if 0 <= p < len(arr) and p not in visited: + queue.append((p, step + 1)) + visited.add(p) diff --git "a/1346.\346\243\200\346\237\245\346\225\264\346\225\260\345\217\212\345\205\266\344\270\244\345\200\215\346\225\260\346\230\257\345\220\246\345\255\230\345\234\250/1346-\346\243\200\346\237\245\346\225\264\346\225\260\345\217\212\345\205\266\344\270\244\345\200\215\346\225\260\346\230\257\345\220\246\345\255\230\345\234\250.py" "b/1346.\346\243\200\346\237\245\346\225\264\346\225\260\345\217\212\345\205\266\344\270\244\345\200\215\346\225\260\346\230\257\345\220\246\345\255\230\345\234\250/1346-\346\243\200\346\237\245\346\225\264\346\225\260\345\217\212\345\205\266\344\270\244\345\200\215\346\225\260\346\230\257\345\220\246\345\255\230\345\234\250.py" new file mode 100644 index 0000000..519caeb --- /dev/null +++ "b/1346.\346\243\200\346\237\245\346\225\264\346\225\260\345\217\212\345\205\266\344\270\244\345\200\215\346\225\260\346\230\257\345\220\246\345\255\230\345\234\250/1346-\346\243\200\346\237\245\346\225\264\346\225\260\345\217\212\345\205\266\344\270\244\345\200\215\346\225\260\346\230\257\345\220\246\345\255\230\345\234\250.py" @@ -0,0 +1,18 @@ +class Solution(object): + def checkIfExist(self, arr): + """ + :type arr: List[int] + :rtype: bool + """ + if arr.count(0) > 1: + return True + + arr = set(arr) - set([0]) + + for x in arr: + if x * 2 in arr: + return True + + return False + + \ No newline at end of file diff --git "a/1347.\345\210\266\351\200\240\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\347\232\204\346\234\200\345\260\217\346\255\245\351\252\244\346\225\260/1347-\345\210\266\351\200\240\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\347\232\204\346\234\200\345\260\217\346\255\245\351\252\244\346\225\260.py" "b/1347.\345\210\266\351\200\240\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\347\232\204\346\234\200\345\260\217\346\255\245\351\252\244\346\225\260/1347-\345\210\266\351\200\240\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\347\232\204\346\234\200\345\260\217\346\255\245\351\252\244\346\225\260.py" new file mode 100644 index 0000000..2ae18ea --- /dev/null +++ "b/1347.\345\210\266\351\200\240\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\347\232\204\346\234\200\345\260\217\346\255\245\351\252\244\346\225\260/1347-\345\210\266\351\200\240\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215\347\232\204\346\234\200\345\260\217\346\255\245\351\252\244\346\225\260.py" @@ -0,0 +1,17 @@ +class Solution(object): + def minSteps(self, s, t): + """ + :type s: str + :type t: str + :rtype: int + """ + n = len(t) + from collections import Counter + dic1 = Counter(s) + dic2 = Counter(t) + + valid = 0 + for char, fre in dic2.items(): + valid += min(dic1[char], fre) + + return n - valid \ 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/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/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..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.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/1352.\346\234\200\345\220\216K\344\270\252\346\225\260\347\232\204\344\271\230\347\247\257/1352-\346\234\200\345\220\216K\344\270\252\346\225\260\347\232\204\344\271\230\347\247\257.py" "b/1352.\346\234\200\345\220\216K\344\270\252\346\225\260\347\232\204\344\271\230\347\247\257/1352-\346\234\200\345\220\216K\344\270\252\346\225\260\347\232\204\344\271\230\347\247\257.py" new file mode 100644 index 0000000..e30f7ca --- /dev/null +++ "b/1352.\346\234\200\345\220\216K\344\270\252\346\225\260\347\232\204\344\271\230\347\247\257/1352-\346\234\200\345\220\216K\344\270\252\346\225\260\347\232\204\344\271\230\347\247\257.py" @@ -0,0 +1,23 @@ +class ProductOfNumbers(object): + + def __init__(self): + self.prefix = [1] + + def add(self, num): + """ + :type num: int + :rtype: None + """ + if num: + self.prefix.append(self.prefix[-1] * num) + else: + self.prefix = [1] + + def getProduct(self, k): + """ + :type k: int + :rtype: int + """ + if k >= len(self.prefix): + return 0 + return self.prefix[-1] / self.prefix[-k - 1] \ No newline at end of file diff --git "a/1353.\346\234\200\345\244\232\345\217\257\344\273\245\345\217\202\345\212\240\347\232\204\344\274\232\350\256\256\346\225\260\347\233\256/1353-\346\234\200\345\244\232\345\217\257\344\273\245\345\217\202\345\212\240\347\232\204\344\274\232\350\256\256\346\225\260\347\233\256.py" "b/1353.\346\234\200\345\244\232\345\217\257\344\273\245\345\217\202\345\212\240\347\232\204\344\274\232\350\256\256\346\225\260\347\233\256/1353-\346\234\200\345\244\232\345\217\257\344\273\245\345\217\202\345\212\240\347\232\204\344\274\232\350\256\256\346\225\260\347\233\256.py" new file mode 100644 index 0000000..cfc526c --- /dev/null +++ "b/1353.\346\234\200\345\244\232\345\217\257\344\273\245\345\217\202\345\212\240\347\232\204\344\274\232\350\256\256\346\225\260\347\233\256/1353-\346\234\200\345\244\232\345\217\257\344\273\245\345\217\202\345\212\240\347\232\204\344\274\232\350\256\256\346\225\260\347\233\256.py" @@ -0,0 +1,33 @@ +class Solution(object): + def maxEvents(self, events): + """ + :type events: List[List[int]] + :rtype: int + """ + from heapq import * + if not events: + return 0 + # 排序并反转,反转是为了可以快速pop + events = sorted(events, key = lambda x:(x[0], x[1]))[::-1] + + queue = [] + res = 0 + for day in range(1, 10 ** 5 + 1): + # 把所有结束日期在当前日期之前的event都pop掉 + while queue and queue[0] < day: + heappop(queue) + + # 把所有开始日期大于等于当前日期的event都push进队列 + while events and events[-1][0] <= day: + last = events.pop() + heappush(queue, last[1]) + + if queue: + # 如果当前日期有可以去的event,就去这一个 + heappop(queue) + res += 1 + if not queue and not events: + # 如果所有event都参加完了 + break + + return res \ No newline at end of file diff --git "a/1354.\345\244\232\346\254\241\346\261\202\345\222\214\346\236\204\351\200\240\347\233\256\346\240\207\346\225\260\347\273\204/1354-\345\244\232\346\254\241\346\261\202\345\222\214\346\236\204\351\200\240\347\233\256\346\240\207\346\225\260\347\273\204.py" "b/1354.\345\244\232\346\254\241\346\261\202\345\222\214\346\236\204\351\200\240\347\233\256\346\240\207\346\225\260\347\273\204/1354-\345\244\232\346\254\241\346\261\202\345\222\214\346\236\204\351\200\240\347\233\256\346\240\207\346\225\260\347\273\204.py" new file mode 100644 index 0000000..ebf5cb0 --- /dev/null +++ "b/1354.\345\244\232\346\254\241\346\261\202\345\222\214\346\236\204\351\200\240\347\233\256\346\240\207\346\225\260\347\273\204/1354-\345\244\232\346\254\241\346\261\202\345\222\214\346\236\204\351\200\240\347\233\256\346\240\207\346\225\260\347\273\204.py" @@ -0,0 +1,29 @@ +class Solution(object): + def isPossible(self, target): + """ + :type target: List[int] + :rtype: bool + """ + from heapq import * + if len(target) == 1: + return target[0] == 1 + + s = sum(target) + target = [-item for item in target] + heapify(target) + + while s > len(target): + # 找当前最大的数和第二大的数 + m = -heappop(target) + s_m = -target[0] + + # 更新 m 并更新 s + diff = s - m + if not diff: + break + new_m = m - (max(1, (m - s_m) / diff) * diff) + s = s - m + new_m + + heappush(target, -new_m) + + return not any([num != -1 for num in target]) \ 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/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/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..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.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/1375.\347\201\257\346\263\241\345\274\200\345\205\263III/1375-\347\201\257\346\263\241\345\274\200\345\205\263III.py" "b/1375.\347\201\257\346\263\241\345\274\200\345\205\263III/1375-\347\201\257\346\263\241\345\274\200\345\205\263III.py" new file mode 100644 index 0000000..124e9e3 --- /dev/null +++ "b/1375.\347\201\257\346\263\241\345\274\200\345\205\263III/1375-\347\201\257\346\263\241\345\274\200\345\205\263III.py" @@ -0,0 +1,13 @@ +class Solution(object): + def numTimesAllBlue(self, light): + """ + :type light: List[int] + :rtype: int + """ + res = 0 + maxx = 0 + for i in range(len(light)): + maxx = max(light[i] - 1, maxx) + if i == maxx: + res += 1 + return res \ No newline at end of file diff --git "a/1376.\351\200\232\347\237\245\346\211\200\346\234\211\345\221\230\345\267\245\346\211\200\351\234\200\347\232\204\346\227\266\351\227\264/1376-\351\200\232\347\237\245\346\211\200\346\234\211\345\221\230\345\267\245\346\211\200\351\234\200\347\232\204\346\227\266\351\227\264.py" "b/1376.\351\200\232\347\237\245\346\211\200\346\234\211\345\221\230\345\267\245\346\211\200\351\234\200\347\232\204\346\227\266\351\227\264/1376-\351\200\232\347\237\245\346\211\200\346\234\211\345\221\230\345\267\245\346\211\200\351\234\200\347\232\204\346\227\266\351\227\264.py" new file mode 100644 index 0000000..96b9996 --- /dev/null +++ "b/1376.\351\200\232\347\237\245\346\211\200\346\234\211\345\221\230\345\267\245\346\211\200\351\234\200\347\232\204\346\227\266\351\227\264/1376-\351\200\232\347\237\245\346\211\200\346\234\211\345\221\230\345\267\245\346\211\200\351\234\200\347\232\204\346\227\266\351\227\264.py" @@ -0,0 +1,29 @@ +class Solution(object): + def numOfMinutes(self, n, headID, manager, informTime): + """ + :type n: int + :type headID: int + :type manager: List[int] + :type informTime: List[int] + :rtype: int + """ + from collections import defaultdict, deque + layer = defaultdict(set) + + for i in range(n): + layer[manager[i]].add(i) # key is the boss, val is the sub + + self.res = 0 + + def dfs(hid, time): + if hid not in layer: + self.res = max(self.res, time) + return + + for sub in layer[hid]: + dfs(sub, time + informTime[hid]) + + dfs(headID, 0) + return self.res + + \ No newline at end of file diff --git "a/1377.T\347\247\222\345\220\216\351\235\222\350\233\231\347\232\204\344\275\215\347\275\256/1377-T\347\247\222\345\220\216\351\235\222\350\233\231\347\232\204\344\275\215\347\275\256.py" "b/1377.T\347\247\222\345\220\216\351\235\222\350\233\231\347\232\204\344\275\215\347\275\256/1377-T\347\247\222\345\220\216\351\235\222\350\233\231\347\232\204\344\275\215\347\275\256.py" new file mode 100644 index 0000000..149bc23 --- /dev/null +++ "b/1377.T\347\247\222\345\220\216\351\235\222\350\233\231\347\232\204\344\275\215\347\275\256/1377-T\347\247\222\345\220\216\351\235\222\350\233\231\347\232\204\344\275\215\347\275\256.py" @@ -0,0 +1,42 @@ +class Solution(object): + def frogPosition(self, n, edges, t, target): + """ + :type n: int + :type edges: List[List[int]] + :type t: int + :type target: int + :rtype: float + """ + from collections import deque, defaultdict + + # 1. 建图 + adj = defaultdict(set) + for s, e in edges: + adj[s].add(e) + adj[e].add(s) + + # 2. 初始化 DP 数组 + # dp[k][node] 代表在第 k 秒,蛤处于 node 的概率 + # dp[k][node] += dp[k - 1][parent] * prbability(parent -> node) + + dp = [[0 for _ in range(n + 1)] for _ in range(t + 1)] + dp[0][1] = 1 + + for time in range(1, t + 1): + for par in range(1, n + 1): + if dp[time - 1][par]: + if not adj[par]: + # 如果无处可去,则停留在原地 + dp[time][par] = dp[time - 1][par] + else: + # 能跳就跳 + for node in adj[par]: + dp[time][node] += dp[time - 1][par] * 1.0 / len(adj[par]) + + # 跳完就把用过的边删掉 + for node in adj[par]: + adj[node].remove(par) + adj[par] = set() + + return dp[t][target] + \ 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" new file mode 100644 index 0000000..8c9b498 --- /dev/null +++ "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" @@ -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/1380.\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1380-\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" "b/1380.\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1380-\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" new file mode 100644 index 0000000..29235f9 --- /dev/null +++ "b/1380.\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260/1380-\347\237\251\351\230\265\344\270\255\347\232\204\345\271\270\350\277\220\346\225\260.py" @@ -0,0 +1,15 @@ +class Solution(object): + def luckyNumbers (self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + row, col = set(), set() + + for r in matrix: + row.add(min(r)) + + for c in zip(*matrix): + col.add(max(c)) + + return list(row & col) \ No newline at end of file diff --git "a/1381.\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210/1381-\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210.py" "b/1381.\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210/1381-\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210.py" new file mode 100644 index 0000000..98e67dd --- /dev/null +++ "b/1381.\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210/1381-\350\256\276\350\256\241\344\270\200\344\270\252\346\224\257\346\214\201\345\242\236\351\207\217\346\223\215\344\275\234\347\232\204\346\240\210.py" @@ -0,0 +1,38 @@ +class CustomStack(object): + + def __init__(self, maxSize): + """ + :type maxSize: int + """ + self.stack = [] + self.maxSize = maxSize + def push(self, x): + """ + :type x: int + :rtype: None + """ + if len(self.stack) < self.maxSize: + self.stack.append(x) + + def pop(self): + """ + :rtype: int + """ + return self.stack.pop() if self.stack else -1 + + + def increment(self, k, val): + """ + :type k: int + :type val: int + :rtype: None + """ + for i in range(min(k, len(self.stack))): + self.stack[i] += val + + +# Your CustomStack object will be instantiated and called as such: +# obj = CustomStack(maxSize) +# obj.push(x) +# param_2 = obj.pop() +# obj.increment(k,val) \ No newline at end of file diff --git "a/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241/1382-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.py" "b/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241/1382-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.py" new file mode 100644 index 0000000..d49e963 --- /dev/null +++ "b/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241/1382-\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.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 balanceBST(self, root): + """ + :type root: TreeNode + :rtype: TreeNode + """ + def inorder(node): + if not node: + return [] + return inorder(node.left) + [node.val] + inorder(node.right) + + l = inorder(root) + + def generator(l): + if not l: + return None + idx = len(l) // 2 + root = TreeNode(l[idx]) + root.left = generator(l[:idx]) + root.right = generator(l[idx + 1:]) + return root + + return generator(l) \ No newline at end of file diff --git "a/1383.\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274/1383-\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274.py" "b/1383.\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274/1383-\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274.py" new file mode 100644 index 0000000..8fc30fe --- /dev/null +++ "b/1383.\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274/1383-\346\234\200\345\244\247\347\232\204\345\233\242\351\230\237\350\241\250\347\216\260\345\200\274.py" @@ -0,0 +1,29 @@ +class Solution(object): + def maxPerformance(self, n, speed, efficiency, k): + """ + :type n: int + :type speed: List[int] + :type efficiency: List[int] + :type k: int + :rtype: int + """ + from heapq import * + combine = [(speed[i], efficiency[i]) for i in range(n)] + combine = sorted(combine, key = lambda x: - x[1]) + res = 0 + MOD = 10 ** 9 + 7 + min_heap = [] + speed_sum = 0 + for i in range(n): + s, e = combine[i] + if len(min_heap) < k: + heappush(min_heap, s) + speed_sum += s + else: + if min_heap and min_heap[0] < s: + speed_sum = speed_sum - min_heap[0] + s + heappush(min_heap, s) + heappop(min_heap) + + res = max(res, speed_sum * e) + return res % MOD \ 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/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/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..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.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/1390.\345\233\233\345\233\240\346\225\260/1390-\345\233\233\345\233\240\346\225\260.py" "b/1390.\345\233\233\345\233\240\346\225\260/1390-\345\233\233\345\233\240\346\225\260.py" new file mode 100644 index 0000000..3044a40 --- /dev/null +++ "b/1390.\345\233\233\345\233\240\346\225\260/1390-\345\233\233\345\233\240\346\225\260.py" @@ -0,0 +1,19 @@ +class Solution(object): + def sumFourDivisors(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + def factor(n): + l = [] + for i in range(1, int(n ** 0.5) + 1): + if n % i == 0: + l.append(i) + if n / i != i: + l.append(n / i) + if len(l) > 4: + break + + return sum(l) if len(l) == 4 else 0 + + return sum(map(factor, nums)) \ No newline at end of file diff --git "a/1391.\346\243\200\346\237\245\347\275\221\346\240\274\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\346\234\211\346\225\210\350\267\257\345\276\204/1391-\346\243\200\346\237\245\347\275\221\346\240\274\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\346\234\211\346\225\210\350\267\257\345\276\204.py" "b/1391.\346\243\200\346\237\245\347\275\221\346\240\274\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\346\234\211\346\225\210\350\267\257\345\276\204/1391-\346\243\200\346\237\245\347\275\221\346\240\274\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\346\234\211\346\225\210\350\267\257\345\276\204.py" new file mode 100644 index 0000000..47fd706 --- /dev/null +++ "b/1391.\346\243\200\346\237\245\347\275\221\346\240\274\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\346\234\211\346\225\210\350\267\257\345\276\204/1391-\346\243\200\346\237\245\347\275\221\346\240\274\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\346\234\211\346\225\210\350\267\257\345\276\204.py" @@ -0,0 +1,33 @@ +class Solution(object): + def hasValidPath(self, grid): + """ + :type grid: List[List[int]] + :rtype: bool + """ + m, n = len(grid), len(grid[0]) + dir = {} + dir[1] = [0, 1, 0, 1] + dir[2] = [1, 0, 1, 0] + dir[3] = [0, 0, 1, 1] + dir[4] = [0, 1, 1, 0] + dir[5] = [1, 0, 0, 1] + dir[6] = [1, 1, 0, 0] + + dx = [-1, 0, 1, 0] + dy = [0, 1, 0, -1] + queue = [(0, 0)] + visited = set(queue) + + cor = {0:2, 1:3, 3:1, 2:0} + while queue: + x0, y0 = queue.pop() + if [x0, y0] == [m - 1, n - 1]: + return True + 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 dir[grid[x0][y0]][k] & dir[grid[x][y]][cor[k]]: + visited.add((x, y)) + queue.append((x, y)) + return False + diff --git "a/1392.\346\234\200\351\225\277\345\277\253\344\271\220\345\211\215\347\274\200/1392-\346\234\200\351\225\277\345\277\253\344\271\220\345\211\215\347\274\200.py" "b/1392.\346\234\200\351\225\277\345\277\253\344\271\220\345\211\215\347\274\200/1392-\346\234\200\351\225\277\345\277\253\344\271\220\345\211\215\347\274\200.py" new file mode 100644 index 0000000..418bd1e --- /dev/null +++ "b/1392.\346\234\200\351\225\277\345\277\253\344\271\220\345\211\215\347\274\200/1392-\346\234\200\351\225\277\345\277\253\344\271\220\345\211\215\347\274\200.py" @@ -0,0 +1,20 @@ +class Solution(object): + def longestPrefix(self, s): + """ + :type s: str + :rtype: str + """ + base = 131 + mod = 10 ** 9 + 7 + res = "" + prefix, suffix = 0, 0 + multiple = 1 + for i in range(len(s) - 1): + prefix = (prefix * base + ord(s[i])) % mod + suffix = (ord(s[-(i + 1)]) * multiple + suffix) % mod + if prefix == suffix: + res = s[:i + 1] + + multiple = multiple * base % mod + + return res \ No newline at end of file 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" new file mode 100644 index 0000000..a54cba9 --- /dev/null +++ "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" @@ -0,0 +1,48 @@ +class UndergroundSystem(object): + + def __init__(self): + self.check_in_history = dict() # key is id + stationName, val is t + self.StationTraverlTime = dict() + def checkIn(self, id, stationName, t): + """ + :type id: int + :type stationName: str + :type t: int + :rtype: None + """ + self.check_in_history[str(id)] = [stationName, t] + + + def checkOut(self, id, stationName, t): + """ + :type id: int + :type stationName: str + :type t: int + :rtype: None + """ + 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): + """ + :type startStation: str + :type endStation: str + :rtype: float + """ + key = startStation + "#" + endStation + return self.StationTraverlTime[key][0] * 1.0 / self.StationTraverlTime[key][1] + + +# Your UndergroundSystem object will be instantiated and called as such: +# obj = UndergroundSystem() +# obj.checkIn(id,stationName,t) +# obj.checkOut(id,stationName,t) +# param_3 = obj.getAverageTime(startStation,endStation) \ 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" new file mode 100644 index 0000000..edec649 --- /dev/null +++ "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" @@ -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/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" new file mode 100644 index 0000000..d0936a2 --- /dev/null +++ "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" @@ -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/1427.\345\255\227\347\254\246\344\270\262\347\232\204\345\267\246\345\217\263\347\247\273/1427-\345\255\227\347\254\246\344\270\262\347\232\204\345\267\246\345\217\263\347\247\273.py" "b/1427.\345\255\227\347\254\246\344\270\262\347\232\204\345\267\246\345\217\263\347\247\273/1427-\345\255\227\347\254\246\344\270\262\347\232\204\345\267\246\345\217\263\347\247\273.py" new file mode 100644 index 0000000..0fe9c1f --- /dev/null +++ "b/1427.\345\255\227\347\254\246\344\270\262\347\232\204\345\267\246\345\217\263\347\247\273/1427-\345\255\227\347\254\246\344\270\262\347\232\204\345\267\246\345\217\263\347\247\273.py" @@ -0,0 +1,14 @@ +class Solution(object): + def stringShift(self, s, shift): + """ + :type s: str + :type shift: List[List[int]] + :rtype: str + """ + for d, amount in shift: + if d == 0: + s = s[amount:] + s[:amount] + else: + s = s[-amount:] + s[:-amount] + + return s \ No newline at end of file diff --git "a/1431.\346\213\245\346\234\211\346\234\200\345\244\232\347\263\226\346\236\234\347\232\204\345\255\251\345\255\220/1431-\346\213\245\346\234\211\346\234\200\345\244\232\347\263\226\346\236\234\347\232\204\345\255\251\345\255\220.py" "b/1431.\346\213\245\346\234\211\346\234\200\345\244\232\347\263\226\346\236\234\347\232\204\345\255\251\345\255\220/1431-\346\213\245\346\234\211\346\234\200\345\244\232\347\263\226\346\236\234\347\232\204\345\255\251\345\255\220.py" new file mode 100644 index 0000000..2b4f78b --- /dev/null +++ "b/1431.\346\213\245\346\234\211\346\234\200\345\244\232\347\263\226\346\236\234\347\232\204\345\255\251\345\255\220/1431-\346\213\245\346\234\211\346\234\200\345\244\232\347\263\226\346\236\234\347\232\204\345\255\251\345\255\220.py" @@ -0,0 +1,9 @@ +class Solution(object): + def kidsWithCandies(self, candies, extraCandies): + """ + :type candies: List[int] + :type extraCandies: int + :rtype: List[bool] + """ + maxCandies = max(candies) + return [curCandies + extraCandies >= maxCandies for curCandies in candies] \ No newline at end of file diff --git "a/1432.\346\224\271\345\217\230\344\270\200\344\270\252\346\225\264\346\225\260\350\203\275\345\276\227\345\210\260\347\232\204\346\234\200\345\244\247\345\267\256\345\200\274/1432-\346\224\271\345\217\230\344\270\200\344\270\252\346\225\264\346\225\260\350\203\275\345\276\227\345\210\260\347\232\204\346\234\200\345\244\247\345\267\256\345\200\274.py" "b/1432.\346\224\271\345\217\230\344\270\200\344\270\252\346\225\264\346\225\260\350\203\275\345\276\227\345\210\260\347\232\204\346\234\200\345\244\247\345\267\256\345\200\274/1432-\346\224\271\345\217\230\344\270\200\344\270\252\346\225\264\346\225\260\350\203\275\345\276\227\345\210\260\347\232\204\346\234\200\345\244\247\345\267\256\345\200\274.py" new file mode 100644 index 0000000..0007901 --- /dev/null +++ "b/1432.\346\224\271\345\217\230\344\270\200\344\270\252\346\225\264\346\225\260\350\203\275\345\276\227\345\210\260\347\232\204\346\234\200\345\244\247\345\267\256\345\200\274/1432-\346\224\271\345\217\230\344\270\200\344\270\252\346\225\264\346\225\260\350\203\275\345\276\227\345\210\260\347\232\204\346\234\200\345\244\247\345\267\256\345\200\274.py" @@ -0,0 +1,16 @@ +class Solution(object): + def maxDiff(self, num): + """ + :type num: int + :rtype: int + """ + maxValues, minValues = float("-inf"), float("inf") + s = str(num) + for x in range(0, 10): + for y in range(0, 10): + val = s.replace(str(x), str(y)) + if val[0] != '0' and int(val) != 0: + maxValues = max(maxValues, int(val)) + minValues = min(minValues, int(val)) + + return maxValues - minValues \ No newline at end of file diff --git "a/1433.\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\217\257\344\273\245\346\211\223\347\240\264\345\217\246\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262/1433-\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\217\257\344\273\245\346\211\223\347\240\264\345\217\246\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262.py" "b/1433.\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\217\257\344\273\245\346\211\223\347\240\264\345\217\246\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262/1433-\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\217\257\344\273\245\346\211\223\347\240\264\345\217\246\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262.py" new file mode 100644 index 0000000..371d23a --- /dev/null +++ "b/1433.\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\217\257\344\273\245\346\211\223\347\240\264\345\217\246\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262/1433-\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\217\257\344\273\245\346\211\223\347\240\264\345\217\246\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,11 @@ +class Solution(object): + def checkIfCanBreak(self, s1, s2): + """ + :type s1: str + :type s2: str + :rtype: bool + """ + s1 = sorted(s1) + s2 = sorted(s2) + + return all(s1[i] >= s2[i] for i in range(len(s1))) or all(s2[i] >= s1[i] for i in range(len(s1))) \ 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/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/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..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.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/1437.\346\230\257\345\220\246\346\211\200\346\234\2111\351\203\275\350\207\263\345\260\221\347\233\270\351\232\224k\344\270\252\345\205\203\347\264\240/1437-\346\230\257\345\220\246\346\211\200\346\234\2111\351\203\275\350\207\263\345\260\221\347\233\270\351\232\224k\344\270\252\345\205\203\347\264\240.py" "b/1437.\346\230\257\345\220\246\346\211\200\346\234\2111\351\203\275\350\207\263\345\260\221\347\233\270\351\232\224k\344\270\252\345\205\203\347\264\240/1437-\346\230\257\345\220\246\346\211\200\346\234\2111\351\203\275\350\207\263\345\260\221\347\233\270\351\232\224k\344\270\252\345\205\203\347\264\240.py" new file mode 100644 index 0000000..c0bec61 --- /dev/null +++ "b/1437.\346\230\257\345\220\246\346\211\200\346\234\2111\351\203\275\350\207\263\345\260\221\347\233\270\351\232\224k\344\270\252\345\205\203\347\264\240/1437-\346\230\257\345\220\246\346\211\200\346\234\2111\351\203\275\350\207\263\345\260\221\347\233\270\351\232\224k\344\270\252\345\205\203\347\264\240.py" @@ -0,0 +1,15 @@ +class Solution(object): + def kLengthApart(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: bool + """ + pre = None + for i, num in enumerate(nums): + if num == 1: + if pre != None: + if i - pre - 1 < k: + return False + pre = i + return True \ No newline at end of file diff --git "a/1438.\347\273\235\345\257\271\345\267\256\344\270\215\350\266\205\350\277\207\351\231\220\345\210\266\347\232\204\346\234\200\351\225\277\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204/1438-\347\273\235\345\257\271\345\267\256\344\270\215\350\266\205\350\277\207\351\231\220\345\210\266\347\232\204\346\234\200\351\225\277\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204.py" "b/1438.\347\273\235\345\257\271\345\267\256\344\270\215\350\266\205\350\277\207\351\231\220\345\210\266\347\232\204\346\234\200\351\225\277\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204/1438-\347\273\235\345\257\271\345\267\256\344\270\215\350\266\205\350\277\207\351\231\220\345\210\266\347\232\204\346\234\200\351\225\277\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204.py" new file mode 100644 index 0000000..bb97650 --- /dev/null +++ "b/1438.\347\273\235\345\257\271\345\267\256\344\270\215\350\266\205\350\277\207\351\231\220\345\210\266\347\232\204\346\234\200\351\225\277\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204/1438-\347\273\235\345\257\271\345\267\256\344\270\215\350\266\205\350\277\207\351\231\220\345\210\266\347\232\204\346\234\200\351\225\277\350\277\236\347\273\255\345\255\220\346\225\260\347\273\204.py" @@ -0,0 +1,25 @@ +class Solution(object): + def longestSubarray(self, nums, limit): + """ + :type nums: List[int] + :type limit: int + :rtype: int + """ + if not nums: + return 0 + from heapq import * + res = 1 + minHeap = [] + maxHeap = [] + pre = 0 + for i in range(0, len(nums)): + heappush(minHeap, (nums[i], i)) + heappush(maxHeap, (-nums[i], i)) + while -minHeap[0][0] - maxHeap[0][0] > limit: + while maxHeap and maxHeap[0][1] <= pre: + heappop(maxHeap) + while minHeap and minHeap[0][1] <= pre: + heappop(minHeap) + pre += 1 + res = max(res, i - pre + 1) + return res \ 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/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/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..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.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/1442.\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256/1442-\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256.py" "b/1442.\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256/1442-\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..f0a5ac8 --- /dev/null +++ "b/1442.\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256/1442-\345\275\242\346\210\220\344\270\244\344\270\252\345\274\202\346\210\226\347\233\270\347\255\211\346\225\260\347\273\204\347\232\204\344\270\211\345\205\203\347\273\204\346\225\260\347\233\256.py" @@ -0,0 +1,15 @@ +class Solution(object): + def countTriplets(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + res = 0 + for i in range(len(arr)): + preSum = arr[i] + for j in range(i + 1, len(arr)): + preSum ^= arr[j] + if not preSum: + res += j - i + + return res \ No newline at end of file diff --git "a/1443.\346\224\266\351\233\206\346\240\221\344\270\212\346\211\200\346\234\211\350\213\271\346\236\234\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264/1443-\346\224\266\351\233\206\346\240\221\344\270\212\346\211\200\346\234\211\350\213\271\346\236\234\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264.py" "b/1443.\346\224\266\351\233\206\346\240\221\344\270\212\346\211\200\346\234\211\350\213\271\346\236\234\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264/1443-\346\224\266\351\233\206\346\240\221\344\270\212\346\211\200\346\234\211\350\213\271\346\236\234\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264.py" new file mode 100644 index 0000000..a3080e5 --- /dev/null +++ "b/1443.\346\224\266\351\233\206\346\240\221\344\270\212\346\211\200\346\234\211\350\213\271\346\236\234\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264/1443-\346\224\266\351\233\206\346\240\221\344\270\212\346\211\200\346\234\211\350\213\271\346\236\234\347\232\204\346\234\200\345\260\221\346\227\266\351\227\264.py" @@ -0,0 +1,31 @@ +class Solution(object): + def minTime(self, n, edges, hasApple): + """ + :type n: int + :type edges: List[List[int]] + :type hasApple: List[bool] + :rtype: int + """ + from collections import defaultdict + dic = defaultdict(set) + mustVisitNodes = set() + + for src, des in edges: + dic[src].add(des) + dic[des].add(src) + + def findMustVisitNodesDFS(node, path, visited): + path.append(node) + + if hasApple[node]: + for n in path: + mustVisitNodes.add(n) + + for child in dic[node]: + if child not in visited: + visited.add(child) + findMustVisitNodesDFS(child, path + [node], visited) + + findMustVisitNodesDFS(0, [], set([0])) + + return max(0, 2 * (len(mustVisitNodes) - 1)) \ No newline at end of file diff --git "a/1446.\350\277\236\347\273\255\345\255\227\347\254\246/1446-\350\277\236\347\273\255\345\255\227\347\254\246.py" "b/1446.\350\277\236\347\273\255\345\255\227\347\254\246/1446-\350\277\236\347\273\255\345\255\227\347\254\246.py" new file mode 100644 index 0000000..8b3d99a --- /dev/null +++ "b/1446.\350\277\236\347\273\255\345\255\227\347\254\246/1446-\350\277\236\347\273\255\345\255\227\347\254\246.py" @@ -0,0 +1,18 @@ +class Solution(object): + def maxPower(self, s): + """ + :type s: str + :rtype: int + """ + res = 1 + pre = s[0] + tmp = 1 + for i, ch in enumerate(s): + if i: + if ch == pre: + tmp += 1 + else: + pre = ch + tmp = 1 + res = max(res, tmp) + 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" new file mode 100644 index 0000000..1c1ba4f --- /dev/null +++ "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" @@ -0,0 +1,11 @@ +class Solution: + def simplifiedFractions(self, n: int) -> List[str]: + import math + res = [] + + for down in range(1, n + 1): + for up in range(1, down): + if math.gcd(up, down) == 1: + res.append(str(up) + "/" + str(down)) + + return res \ No newline at end of file diff --git "a/1448.\347\273\237\350\256\241\344\272\214\345\217\211\346\240\221\344\270\255\345\245\275\350\212\202\347\202\271\347\232\204\346\225\260\347\233\256/1448-\347\273\237\350\256\241\344\272\214\345\217\211\346\240\221\344\270\255\345\245\275\350\212\202\347\202\271\347\232\204\346\225\260\347\233\256.py" "b/1448.\347\273\237\350\256\241\344\272\214\345\217\211\346\240\221\344\270\255\345\245\275\350\212\202\347\202\271\347\232\204\346\225\260\347\233\256/1448-\347\273\237\350\256\241\344\272\214\345\217\211\346\240\221\344\270\255\345\245\275\350\212\202\347\202\271\347\232\204\346\225\260\347\233\256.py" new file mode 100644 index 0000000..544ddb1 --- /dev/null +++ "b/1448.\347\273\237\350\256\241\344\272\214\345\217\211\346\240\221\344\270\255\345\245\275\350\212\202\347\202\271\347\232\204\346\225\260\347\233\256/1448-\347\273\237\350\256\241\344\272\214\345\217\211\346\240\221\344\270\255\345\245\275\350\212\202\347\202\271\347\232\204\346\225\260\347\233\256.py" @@ -0,0 +1,27 @@ +# 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 goodNodes(self, root): + """ + :type root: TreeNode + :rtype: int + """ + self.res = 0 + + def dfs(node, maxVal): + if not node: + return + + if node.val >= maxVal: + self.res += 1 + + maxVal = max(maxVal, node.val) + dfs(node.left, maxVal) + dfs(node.right, maxVal) + + dfs(root, root.val) + return self.res \ No newline at end of file diff --git "a/1450.\345\234\250\346\227\242\345\256\232\346\227\266\351\227\264\345\201\232\344\275\234\344\270\232\347\232\204\345\255\246\347\224\237\344\272\272\346\225\260/1450-\345\234\250\346\227\242\345\256\232\346\227\266\351\227\264\345\201\232\344\275\234\344\270\232\347\232\204\345\255\246\347\224\237\344\272\272\346\225\260.py" "b/1450.\345\234\250\346\227\242\345\256\232\346\227\266\351\227\264\345\201\232\344\275\234\344\270\232\347\232\204\345\255\246\347\224\237\344\272\272\346\225\260/1450-\345\234\250\346\227\242\345\256\232\346\227\266\351\227\264\345\201\232\344\275\234\344\270\232\347\232\204\345\255\246\347\224\237\344\272\272\346\225\260.py" new file mode 100644 index 0000000..0006c92 --- /dev/null +++ "b/1450.\345\234\250\346\227\242\345\256\232\346\227\266\351\227\264\345\201\232\344\275\234\344\270\232\347\232\204\345\255\246\347\224\237\344\272\272\346\225\260/1450-\345\234\250\346\227\242\345\256\232\346\227\266\351\227\264\345\201\232\344\275\234\344\270\232\347\232\204\345\255\246\347\224\237\344\272\272\346\225\260.py" @@ -0,0 +1,9 @@ +class Solution(object): + def busyStudent(self, startTime, endTime, queryTime): + """ + :type startTime: List[int] + :type endTime: List[int] + :type queryTime: int + :rtype: int + """ + return sum([1 for i in range(len(startTime)) if startTime[i] <= queryTime <= endTime[i]]) \ No newline at end of file diff --git "a/1451.\351\207\215\346\226\260\346\216\222\345\210\227\345\217\245\345\255\220\344\270\255\347\232\204\345\215\225\350\257\215/1451-\351\207\215\346\226\260\346\216\222\345\210\227\345\217\245\345\255\220\344\270\255\347\232\204\345\215\225\350\257\215.py" "b/1451.\351\207\215\346\226\260\346\216\222\345\210\227\345\217\245\345\255\220\344\270\255\347\232\204\345\215\225\350\257\215/1451-\351\207\215\346\226\260\346\216\222\345\210\227\345\217\245\345\255\220\344\270\255\347\232\204\345\215\225\350\257\215.py" new file mode 100644 index 0000000..4ac663c --- /dev/null +++ "b/1451.\351\207\215\346\226\260\346\216\222\345\210\227\345\217\245\345\255\220\344\270\255\347\232\204\345\215\225\350\257\215/1451-\351\207\215\346\226\260\346\216\222\345\210\227\345\217\245\345\255\220\344\270\255\347\232\204\345\215\225\350\257\215.py" @@ -0,0 +1,7 @@ +class Solution(object): + def arrangeWords(self, text): + """ + :type text: str + :rtype: str + """ + return " ".join(sorted(text.split(), key = lambda word: len(word))).capitalize() \ No newline at end of file diff --git "a/1452.\346\224\266\350\227\217\346\270\205\345\215\225/1452-\346\224\266\350\227\217\346\270\205\345\215\225.py" "b/1452.\346\224\266\350\227\217\346\270\205\345\215\225/1452-\346\224\266\350\227\217\346\270\205\345\215\225.py" new file mode 100644 index 0000000..996f758 --- /dev/null +++ "b/1452.\346\224\266\350\227\217\346\270\205\345\215\225/1452-\346\224\266\350\227\217\346\270\205\345\215\225.py" @@ -0,0 +1,8 @@ +class Solution(object): + def peopleIndexes(self, favoriteCompanies): + """ + :type favoriteCompanies: List[List[str]] + :rtype: List[int] + """ + s = [set(l) for l in favoriteCompanies] + return [i for i, s1 in enumerate(s) if not any(s1 < s2 for s2 in s)] \ No newline at end of file diff --git "a/1455.\346\243\200\346\237\245\345\215\225\350\257\215\346\230\257\345\220\246\344\270\272\345\217\245\344\270\255\345\205\266\344\273\226\345\215\225\350\257\215\347\232\204\345\211\215\347\274\200/1455-\346\243\200\346\237\245\345\215\225\350\257\215\346\230\257\345\220\246\344\270\272\345\217\245\344\270\255\345\205\266\344\273\226\345\215\225\350\257\215\347\232\204\345\211\215\347\274\200.py" "b/1455.\346\243\200\346\237\245\345\215\225\350\257\215\346\230\257\345\220\246\344\270\272\345\217\245\344\270\255\345\205\266\344\273\226\345\215\225\350\257\215\347\232\204\345\211\215\347\274\200/1455-\346\243\200\346\237\245\345\215\225\350\257\215\346\230\257\345\220\246\344\270\272\345\217\245\344\270\255\345\205\266\344\273\226\345\215\225\350\257\215\347\232\204\345\211\215\347\274\200.py" new file mode 100644 index 0000000..3b87ee0 --- /dev/null +++ "b/1455.\346\243\200\346\237\245\345\215\225\350\257\215\346\230\257\345\220\246\344\270\272\345\217\245\344\270\255\345\205\266\344\273\226\345\215\225\350\257\215\347\232\204\345\211\215\347\274\200/1455-\346\243\200\346\237\245\345\215\225\350\257\215\346\230\257\345\220\246\344\270\272\345\217\245\344\270\255\345\205\266\344\273\226\345\215\225\350\257\215\347\232\204\345\211\215\347\274\200.py" @@ -0,0 +1,13 @@ +class Solution(object): + def isPrefixOfWord(self, sentence, searchWord): + """ + :type sentence: str + :type searchWord: str + :rtype: int + """ + words = sentence.split() + for i, word in enumerate(words): + if word.startswith(searchWord): + return i + 1 + + return -1 \ No newline at end of file diff --git "a/1456.\345\256\232\351\225\277\345\255\220\344\270\262\344\270\255\345\205\203\351\237\263\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/1456-\345\256\232\351\225\277\345\255\220\344\270\262\344\270\255\345\205\203\351\237\263\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256.py" "b/1456.\345\256\232\351\225\277\345\255\220\344\270\262\344\270\255\345\205\203\351\237\263\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/1456-\345\256\232\351\225\277\345\255\220\344\270\262\344\270\255\345\205\203\351\237\263\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256.py" new file mode 100644 index 0000000..809e4c2 --- /dev/null +++ "b/1456.\345\256\232\351\225\277\345\255\220\344\270\262\344\270\255\345\205\203\351\237\263\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256/1456-\345\256\232\351\225\277\345\255\220\344\270\262\344\270\255\345\205\203\351\237\263\347\232\204\346\234\200\345\244\247\346\225\260\347\233\256.py" @@ -0,0 +1,21 @@ +class Solution(object): + def maxVowels(self, s, k): + """ + :type s: str + :type k: int + :rtype: int + """ + vowels = "aeiou" + + start = 0 + res = 0 + tmp = 0 + for end in range(len(s)): + if s[end] in vowels: + tmp += 1 + while end - start + 1 > k: + if s[start] in vowels: + tmp -= 1 + start += 1 + res = max(res, tmp) + return res \ No newline at end of file diff --git "a/1457.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\344\274\252\345\233\236\346\226\207\350\267\257\345\276\204/1457-\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\344\274\252\345\233\236\346\226\207\350\267\257\345\276\204.py" "b/1457.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\344\274\252\345\233\236\346\226\207\350\267\257\345\276\204/1457-\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\344\274\252\345\233\236\346\226\207\350\267\257\345\276\204.py" new file mode 100644 index 0000000..6add82a --- /dev/null +++ "b/1457.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\344\274\252\345\233\236\346\226\207\350\267\257\345\276\204/1457-\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\344\274\252\345\233\236\346\226\207\350\267\257\345\276\204.py" @@ -0,0 +1,45 @@ +# 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 pseudoPalindromicPaths (self, root): + """ + :type root: TreeNode + :rtype: int + """ + self.res = 0 + + def dfs(node, path): + if not node: + return + + path = path + [node.val] + if not node.left and not node.right: + if self.hasPalindromic(path): + self.res += 1 + + dfs(node.left, path) + dfs(node.right, path) + + dfs(root, []) + return self.res + + def hasPalindromic(self, nums): + from collections import Counter + + dic = Counter(nums) + + oddCnt = 0 + for val in dic.values(): + if val % 2: + oddCnt += 1 + + return oddCnt <= 1 + + + + + diff --git "a/1460.\351\200\232\350\277\207\347\277\273\350\275\254\345\255\220\346\225\260\347\273\204\344\275\277\344\270\244\344\270\252\346\225\260\347\273\204\347\233\270\347\255\211/1460-\351\200\232\350\277\207\347\277\273\350\275\254\345\255\220\346\225\260\347\273\204\344\275\277\344\270\244\344\270\252\346\225\260\347\273\204\347\233\270\347\255\211.py" "b/1460.\351\200\232\350\277\207\347\277\273\350\275\254\345\255\220\346\225\260\347\273\204\344\275\277\344\270\244\344\270\252\346\225\260\347\273\204\347\233\270\347\255\211/1460-\351\200\232\350\277\207\347\277\273\350\275\254\345\255\220\346\225\260\347\273\204\344\275\277\344\270\244\344\270\252\346\225\260\347\273\204\347\233\270\347\255\211.py" new file mode 100644 index 0000000..9a6ca8e --- /dev/null +++ "b/1460.\351\200\232\350\277\207\347\277\273\350\275\254\345\255\220\346\225\260\347\273\204\344\275\277\344\270\244\344\270\252\346\225\260\347\273\204\347\233\270\347\255\211/1460-\351\200\232\350\277\207\347\277\273\350\275\254\345\255\220\346\225\260\347\273\204\344\275\277\344\270\244\344\270\252\346\225\260\347\273\204\347\233\270\347\255\211.py" @@ -0,0 +1,9 @@ +class Solution(object): + def canBeEqual(self, target, arr): + """ + :type target: List[int] + :type arr: List[int] + :rtype: bool + """ + from collections import Counter + return Counter(target) == Counter(arr) \ No newline at end of file diff --git "a/1461.\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\214\205\345\220\253\346\211\200\346\234\211\351\225\277\345\272\246\344\270\272K\347\232\204\344\272\214\350\277\233\345\210\266\345\255\220\344\270\262/1461-\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\214\205\345\220\253\346\211\200\346\234\211\351\225\277\345\272\246\344\270\272K\347\232\204\344\272\214\350\277\233\345\210\266\345\255\220\344\270\262.py" "b/1461.\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\214\205\345\220\253\346\211\200\346\234\211\351\225\277\345\272\246\344\270\272K\347\232\204\344\272\214\350\277\233\345\210\266\345\255\220\344\270\262/1461-\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\214\205\345\220\253\346\211\200\346\234\211\351\225\277\345\272\246\344\270\272K\347\232\204\344\272\214\350\277\233\345\210\266\345\255\220\344\270\262.py" new file mode 100644 index 0000000..cff29ca --- /dev/null +++ "b/1461.\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\214\205\345\220\253\346\211\200\346\234\211\351\225\277\345\272\246\344\270\272K\347\232\204\344\272\214\350\277\233\345\210\266\345\255\220\344\270\262/1461-\346\243\200\346\237\245\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\346\230\257\345\220\246\345\214\205\345\220\253\346\211\200\346\234\211\351\225\277\345\272\246\344\270\272K\347\232\204\344\272\214\350\277\233\345\210\266\345\255\220\344\270\262.py" @@ -0,0 +1,15 @@ +class Solution(object): + def hasAllCodes(self, s, k): + """ + :type s: str + :type k: int + :rtype: bool + """ + numSet = set() + for i in range(len(s) - k + 1): + numSet.add(int(s[i:i + k], 2)) + + for num in range(2 ** k): + if num not in numSet: + return False + return True \ No newline at end of file diff --git "a/1462.\350\257\276\347\250\213\345\256\211\346\216\222IV/1462-\350\257\276\347\250\213\345\256\211\346\216\222IV.py" "b/1462.\350\257\276\347\250\213\345\256\211\346\216\222IV/1462-\350\257\276\347\250\213\345\256\211\346\216\222IV.py" new file mode 100644 index 0000000..9d6376b --- /dev/null +++ "b/1462.\350\257\276\347\250\213\345\256\211\346\216\222IV/1462-\350\257\276\347\250\213\345\256\211\346\216\222IV.py" @@ -0,0 +1,37 @@ +class Solution(object): + def checkIfPrerequisite(self, n, prerequisites, queries): + """ + :type n: int + :type prerequisites: List[List[int]] + :type queries: List[List[int]] + :rtype: List[bool] + """ + from collections import defaultdict + from heapq import * + pre = defaultdict(set) + children = defaultdict(set) + inDegree = defaultdict(int) + + for src, dec in prerequisites: + inDegree[dec] += 1 + children[src].add(dec) + + queue = [] + for i in range(n): + if inDegree[i] == 0: + heappush(queue, i) + + while queue: + cur = heappop(queue) + + for child in children[cur]: + pre[child] = pre[cur] | set([cur]) | pre[child] + + inDegree[child] -= 1 + if inDegree[child] == 0: + heappush(queue, child) + + res = [] + for src, dec in queries: + res.append(src in pre[dec]) + return res \ 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/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/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..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.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/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/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..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.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/1471.\346\225\260\347\273\204\344\270\255\347\232\204k\344\270\252\346\234\200\345\274\272\345\200\274/1471-\346\225\260\347\273\204\344\270\255\347\232\204k\344\270\252\346\234\200\345\274\272\345\200\274.py" "b/1471.\346\225\260\347\273\204\344\270\255\347\232\204k\344\270\252\346\234\200\345\274\272\345\200\274/1471-\346\225\260\347\273\204\344\270\255\347\232\204k\344\270\252\346\234\200\345\274\272\345\200\274.py" new file mode 100644 index 0000000..88c70e3 --- /dev/null +++ "b/1471.\346\225\260\347\273\204\344\270\255\347\232\204k\344\270\252\346\234\200\345\274\272\345\200\274/1471-\346\225\260\347\273\204\344\270\255\347\232\204k\344\270\252\346\234\200\345\274\272\345\200\274.py" @@ -0,0 +1,10 @@ +class Solution(object): + def getStrongest(self, arr, k): + """ + :type arr: List[int] + :type k: int + :rtype: List[int] + """ + arr.sort() + m = arr[(len(arr) - 1) // 2] + return sorted(arr, key = lambda x: abs(x - m))[-k:] \ 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/1491.\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274/1491-\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274.py" "b/1491.\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274/1491-\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274.py" new file mode 100644 index 0000000..0aeb3b1 --- /dev/null +++ "b/1491.\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274/1491-\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274.py" @@ -0,0 +1,7 @@ +class Solution(object): + def average(self, salary): + """ + :type salary: List[int] + :rtype: float + """ + return (sum(salary) - max(salary) - min(salary)) * 1.0 / (len(salary) - 2) \ No newline at end of file diff --git "a/1492.n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220/1492-n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220.py" "b/1492.n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220/1492-n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220.py" new file mode 100644 index 0000000..5f7b65d --- /dev/null +++ "b/1492.n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220/1492-n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220.py" @@ -0,0 +1,21 @@ +class Solution(object): + def kthFactor(self, n, k): + """ + :type n: int + :type k: int + :rtype: int + """ + l = [] + for i in range(1, int(n ** 0.5) + 1): + if n % i == 0: + l.append(i) + total = len(l) * 2 if l[-1] ** 2 != n else len(l) * 2 - 1 + if total < k: + return -1 + + if k - 1 < len(l): + return l[k - 1] + else: + idx = (len(l) - 1) * 2 - k + return n // l[idx] if total % 2 else n // l[idx + 2] + \ No newline at end of file diff --git "a/1493.\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204/1493-\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204.py" "b/1493.\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204/1493-\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204.py" new file mode 100644 index 0000000..c41b8a5 --- /dev/null +++ "b/1493.\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204/1493-\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204.py" @@ -0,0 +1,21 @@ +class Solution(object): + def longestSubarray(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if sum(nums) == len(nums): + return len(nums) - 1 + zeroCnt = 0 + left = 0 + res = 0 + for right in range(len(nums)): + if nums[right] == 0: + zeroCnt += 1 + while zeroCnt > 1: + if nums[left] == 0: + zeroCnt -= 1 + left += 1 + res = max(res, right - left + 1 - zeroCnt) + return res + \ No newline at end of file diff --git "a/1494.\345\271\266\350\241\214\350\257\276\347\250\213II/1494-\345\271\266\350\241\214\350\257\276\347\250\213II.py" "b/1494.\345\271\266\350\241\214\350\257\276\347\250\213II/1494-\345\271\266\350\241\214\350\257\276\347\250\213II.py" new file mode 100644 index 0000000..f8a7f64 --- /dev/null +++ "b/1494.\345\271\266\350\241\214\350\257\276\347\250\213II/1494-\345\271\266\350\241\214\350\257\276\347\250\213II.py" @@ -0,0 +1,49 @@ +class Solution(object): + def minNumberOfSemesters(self, n, dependencies, k): + """ + :type n: int + :type dependencies: List[List[int]] + :type k: int + :rtype: int + """ + from collections import defaultdict + inDegree = defaultdict(int) + outDegree = defaultdict(int) + children = defaultdict(set) + + for src, dec in dependencies: # 建图 + inDegree[dec] += 1 + outDegree[src] += 1 + children[src].add(dec) + + queue = [] + for i in range(1, n + 1): + if inDegree[i] == 0: # 入度为0(没有先修课了)的课入队 + heappush(queue, (-outDegree[i], i, -1)) # 出度越大(以这门课作为先修课的课越多),优先级越高 + + semesterCnt = 0 + while queue: + semesterCnt += 1 + nextSemesterCourses = [] # 存放这个学期不能上的课 + courseCnt = 0 + while courseCnt < k and queue: # 每个学期最多上 k 门课 + priority, node, preFinishedSemester = heappop(queue) + + if preFinishedSemester >= semesterCnt: # 当前学期不能上这门课 + nextSemesterCourses.append((priority, node, preFinishedSemester)) + continue + + for child in children[node]: # 这门课可以学,学它,然后处理孩子课的入度 + inDegree[child] -= 1 + + if inDegree[child] == 0: # 孩子课的先修课全上完了 + heappush(queue, (-outDegree[child], child, semesterCnt)) + courseCnt += 1 + + for item in nextSemesterCourses: # 把之前存起来的本学期不能上的课再重新入队 + heappush(queue, item) + + return semesterCnt + + + \ No newline at end of file diff --git "a/1496.\345\210\244\346\226\255\350\267\257\345\276\204\346\230\257\345\220\246\347\233\270\344\272\244/1496-\345\210\244\346\226\255\350\267\257\345\276\204\346\230\257\345\220\246\347\233\270\344\272\244.py" "b/1496.\345\210\244\346\226\255\350\267\257\345\276\204\346\230\257\345\220\246\347\233\270\344\272\244/1496-\345\210\244\346\226\255\350\267\257\345\276\204\346\230\257\345\220\246\347\233\270\344\272\244.py" new file mode 100644 index 0000000..cc00d2f --- /dev/null +++ "b/1496.\345\210\244\346\226\255\350\267\257\345\276\204\346\230\257\345\220\246\347\233\270\344\272\244/1496-\345\210\244\346\226\255\350\267\257\345\276\204\346\230\257\345\220\246\347\233\270\344\272\244.py" @@ -0,0 +1,22 @@ +class Solution(object): + def isPathCrossing(self, path): + """ + :type path: str + :rtype: bool + """ + s = set([(0, 0)]) + x, y = 0, 0 + for d in path: + if d == "N": + y += 1 + elif d == "S": + y -= 1 + elif d == "E": + x += 1 + elif d == "W": + x -= 1 + if (x, y) in s: + return True + s.add((x, y)) + return False + \ No newline at end of file diff --git "a/1497.\346\243\200\346\237\245\346\225\260\347\273\204\345\257\271\346\230\257\345\220\246\345\217\257\344\273\245\350\242\253k\346\225\264\351\231\244/1497-\346\243\200\346\237\245\346\225\260\347\273\204\345\257\271\346\230\257\345\220\246\345\217\257\344\273\245\350\242\253k\346\225\264\351\231\244.py" "b/1497.\346\243\200\346\237\245\346\225\260\347\273\204\345\257\271\346\230\257\345\220\246\345\217\257\344\273\245\350\242\253k\346\225\264\351\231\244/1497-\346\243\200\346\237\245\346\225\260\347\273\204\345\257\271\346\230\257\345\220\246\345\217\257\344\273\245\350\242\253k\346\225\264\351\231\244.py" new file mode 100644 index 0000000..b85ec4b --- /dev/null +++ "b/1497.\346\243\200\346\237\245\346\225\260\347\273\204\345\257\271\346\230\257\345\220\246\345\217\257\344\273\245\350\242\253k\346\225\264\351\231\244/1497-\346\243\200\346\237\245\346\225\260\347\273\204\345\257\271\346\230\257\345\220\246\345\217\257\344\273\245\350\242\253k\346\225\264\351\231\244.py" @@ -0,0 +1,20 @@ +class Solution(object): + def canArrange(self, arr, k): + """ + :type arr: List[int] + :type k: int + :rtype: bool + """ + from collections import defaultdict + dic = defaultdict(int) + for num in arr: + mod = num % k + if mod: + dic[mod] += 1 + for key in dic.keys(): + if key * 2 != k and dic[key] != dic[k - key]: + return False + if key * 2 == k and dic[key] % 2 != 0: + return False + return True + \ No newline at end of file diff --git "a/1498.\346\273\241\350\266\263\346\235\241\344\273\266\347\232\204\345\255\220\345\272\217\345\210\227\346\225\260\347\233\256/1498-\346\273\241\350\266\263\346\235\241\344\273\266\347\232\204\345\255\220\345\272\217\345\210\227\346\225\260\347\233\256.py" "b/1498.\346\273\241\350\266\263\346\235\241\344\273\266\347\232\204\345\255\220\345\272\217\345\210\227\346\225\260\347\233\256/1498-\346\273\241\350\266\263\346\235\241\344\273\266\347\232\204\345\255\220\345\272\217\345\210\227\346\225\260\347\233\256.py" new file mode 100644 index 0000000..692db36 --- /dev/null +++ "b/1498.\346\273\241\350\266\263\346\235\241\344\273\266\347\232\204\345\255\220\345\272\217\345\210\227\346\225\260\347\233\256/1498-\346\273\241\350\266\263\346\235\241\344\273\266\347\232\204\345\255\220\345\272\217\345\210\227\346\225\260\347\233\256.py" @@ -0,0 +1,20 @@ +class Solution(object): + def numSubseq(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: int + """ + nums.sort() + left, right = 0, len(nums) - 1 + res = 0 + MOD = 10 ** 9 + 7 + while left <= right: + s = nums[left] + nums[right] + l = right - left + if s <= target: + res = (res + (1 << l)) % MOD + left += 1 + else: + right -= 1 + return res \ No newline at end of file diff --git "a/1499.\346\273\241\350\266\263\344\270\215\347\255\211\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274/1499-\346\273\241\350\266\263\344\270\215\347\255\211\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/1499.\346\273\241\350\266\263\344\270\215\347\255\211\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274/1499-\346\273\241\350\266\263\344\270\215\347\255\211\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..bb26882 --- /dev/null +++ "b/1499.\346\273\241\350\266\263\344\270\215\347\255\211\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274/1499-\346\273\241\350\266\263\344\270\215\347\255\211\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,20 @@ +class Solution(object): + def findMaxValueOfEquation(self, points, k): + """ + :type points: List[List[int]] + :type k: int + :rtype: int + """ + from collections import deque + queue = deque([(points[0][0], points[0][0] - points[0][1])]) + res = float("-inf") + for yi, yj in points[1:]: + while queue and queue[0][0] < yi - k: + queue.popleft() + if queue: + res = max(res, -queue[0][1] + yi + yj) + sub = yi - yj + while queue and queue[-1][1] > sub: + queue.pop() + queue.append((yi, sub)) + 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/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/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/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/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/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/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/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/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/5008.\344\270\215\345\212\250\347\202\271/5008-\344\270\215\345\212\250\347\202\271.py" "b/5008.\344\270\215\345\212\250\347\202\271/5008-\344\270\215\345\212\250\347\202\271.py" deleted file mode 100644 index 3d497e6..0000000 --- "a/5008.\344\270\215\345\212\250\347\202\271/5008-\344\270\215\345\212\250\347\202\271.py" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution(object): - def fixedPoint(self, A): - """ - :type A: List[int] - :rtype: int - """ - for i, x in enumerate(A): - if i == x: - return i - return -1 - \ No newline at end of file diff --git "a/5009.\346\240\241\345\233\255\350\207\252\350\241\214\350\275\246\345\210\206\351\205\215II/5009-\346\240\241\345\233\255\350\207\252\350\241\214\350\275\246\345\210\206\351\205\215II.py" "b/5009.\346\240\241\345\233\255\350\207\252\350\241\214\350\275\246\345\210\206\351\205\215II/5009-\346\240\241\345\233\255\350\207\252\350\241\214\350\275\246\345\210\206\351\205\215II.py" deleted file mode 100644 index 8e33d41..0000000 --- "a/5009.\346\240\241\345\233\255\350\207\252\350\241\214\350\275\246\345\210\206\351\205\215II/5009-\346\240\241\345\233\255\350\207\252\350\241\214\350\275\246\345\210\206\351\205\215II.py" +++ /dev/null @@ -1,63 +0,0 @@ -class Solution(object): - def assignBikes(self, workers, bikes): - """ - :type workers: List[List[int]] - :type bikes: List[List[int]] - :rtype: int - """ - if workers[-1] == [7,0] and bikes[-1] == [9, 999]: - return 7992 - if workers[-1] == [8,0] and bikes[-1] == [8, 999]: - return 8991 - if workers[-1] == [8,0] and bikes[-1] == [9, 999]: - return 8991 - if workers[-1] == [9,0] and bikes[-1] == [9, 999]: - return 9990 - if workers[-1] == [955,855] and bikes[-1] == [577, 936]: - return 3320 - if workers[-1] == [941,916] and bikes[-1] == [374, 822]: - return 1902 - ans = [-1] * len(workers) - record = dict() - n, m = len(workers), len(bikes) - dp = [[0 for _ in range(m)] for _ in range(n)] - - - for w, worker in enumerate(workers): - xw, yw = worker[0], worker[1] - - for b, bike in enumerate(bikes): - xb, yb = bike[0], bike[1] - distance = abs(xb - xw) + abs(yb - yw) - dp[w][b] = distance - # print record - - - def findRes(wid, tmp): - if wid >= n: - return - for bid in range(m): - if usedbike[bid] == 1: - continue - - usedbike[bid] = 1 - tmp += dp[wid][bid] - - if wid >= n - 1:# - self.res = min(self.res, tmp) - - if tmp > self.res:#֦ - usedbike[bid] = 0 - tmp -= dp[wid][bid] - continue - - findRes(wid + 1, tmp) - - usedbike[bid] = 0 - tmp -= dp[wid][bid] - - - self.res = 999999 - usedbike = [0 for _ in range(m)] - findRes(0, 0) - return self.res \ No newline at end of file diff --git "a/5013.\345\255\227\347\254\246\344\270\262\347\232\204\347\264\242\345\274\225\345\257\271/5013-\345\255\227\347\254\246\344\270\262\347\232\204\347\264\242\345\274\225\345\257\271.py" "b/5013.\345\255\227\347\254\246\344\270\262\347\232\204\347\264\242\345\274\225\345\257\271/5013-\345\255\227\347\254\246\344\270\262\347\232\204\347\264\242\345\274\225\345\257\271.py" deleted file mode 100644 index fe6b7ed..0000000 --- "a/5013.\345\255\227\347\254\246\344\270\262\347\232\204\347\264\242\345\274\225\345\257\271/5013-\345\255\227\347\254\246\344\270\262\347\232\204\347\264\242\345\274\225\345\257\271.py" +++ /dev/null @@ -1,34 +0,0 @@ -class Solution(object): - def indexPairs(self, text, words): - """ - :type text: str - :type words: List[str] - :rtype: List[List[int]] - """ - res = [] - record = {} - for word in words: - if record.get(len(word), -1) == -1: - record[len(word)] = [word] - else: - record[len(word)].append(word) - # min_l, max_l = min(record.keys()), min(record) - l = len(text) - for i in range(l): - for j in range(i + 1, l + 1): - # print j - if record.get(j - i, -1) == -1: - continue - - tmp = text[i:j] - # print tmp - for word in record[j - i]: - if word == tmp: - res.append([i, j - 1]) - # flag = 1 - break - - return res - - - \ No newline at end of file diff --git "a/5021.\345\260\217\344\272\216K\347\232\204\344\270\244\346\225\260\344\271\213\345\222\214/5021-\345\260\217\344\272\216K\347\232\204\344\270\244\346\225\260\344\271\213\345\222\214.py" "b/5021.\345\260\217\344\272\216K\347\232\204\344\270\244\346\225\260\344\271\213\345\222\214/5021-\345\260\217\344\272\216K\347\232\204\344\270\244\346\225\260\344\271\213\345\222\214.py" deleted file mode 100644 index aefb992..0000000 --- "a/5021.\345\260\217\344\272\216K\347\232\204\344\270\244\346\225\260\344\271\213\345\222\214/5021-\345\260\217\344\272\216K\347\232\204\344\270\244\346\225\260\344\271\213\345\222\214.py" +++ /dev/null @@ -1,23 +0,0 @@ -class Solution(object): - def twoSumLessThanK(self, A, K): - """ - :type A: List[int] - :type K: int - :rtype: int - """ - if len(A) < 2: - return -1 - tmp = [] - for i in range(len(A)): - for j in range(i + 1, len(A)): - tmp.append(A[i] + A[j]) - - tmp.sort() - if tmp[0] > K: - return -1 - # print tmp - res = tmp[0] - for item in tmp: - if item < K and abs(item - K) < abs(res - K): - res = item - return res \ No newline at end of file diff --git "a/5022.\351\225\277\345\272\246\344\270\272K\347\232\204\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\345\255\220\344\270\262/5022-\351\225\277\345\272\246\344\270\272K\347\232\204\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\345\255\220\344\270\262.py" "b/5022.\351\225\277\345\272\246\344\270\272K\347\232\204\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\345\255\220\344\270\262/5022-\351\225\277\345\272\246\344\270\272K\347\232\204\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\345\255\220\344\270\262.py" deleted file mode 100644 index b7138d2..0000000 --- "a/5022.\351\225\277\345\272\246\344\270\272K\347\232\204\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\345\255\220\344\270\262/5022-\351\225\277\345\272\246\344\270\272K\347\232\204\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\345\255\220\344\270\262.py" +++ /dev/null @@ -1,28 +0,0 @@ -class Solution(object): - def numKLenSubstrNoRepeats(self, S, K): - """ - :type S: str - :type K: int - :rtype: int - """ - if len(S) < K: - return 0 - res = 0 - for i in range(len(S) - K + 1): - # for j in range(i + K, len(S)): - tmp = S[i:i + K] - if self.check(tmp): - res += 1 - return res - - - def check(self, string): - # record = {} - from collections import Counter - record = Counter(string) - # print record - for key, val in record.items(): - if val > 1: - return False - # print string - return True \ No newline at end of file diff --git "a/5028.\351\230\277\345\247\206\346\226\257\347\211\271\346\234\227\346\225\260/5028-\351\230\277\345\247\206\346\226\257\347\211\271\346\234\227\346\225\260.py" "b/5028.\351\230\277\345\247\206\346\226\257\347\211\271\346\234\227\346\225\260/5028-\351\230\277\345\247\206\346\226\257\347\211\271\346\234\227\346\225\260.py" deleted file mode 100644 index 513bcf2..0000000 --- "a/5028.\351\230\277\345\247\206\346\226\257\347\211\271\346\234\227\346\225\260/5028-\351\230\277\345\247\206\346\226\257\347\211\271\346\234\227\346\225\260.py" +++ /dev/null @@ -1,13 +0,0 @@ -class Solution(object): - def isArmstrong(self, N): - """ - :type N: int - :rtype: bool - """ - k = len(str(N)) - n = N - s = 0 - while N: - N, tmp = divmod(N, 10) - s += tmp ** k - return s == n \ No newline at end of file diff --git "a/5034.\346\234\200\345\244\247\345\224\257\344\270\200\346\225\260/5034-\346\234\200\345\244\247\345\224\257\344\270\200\346\225\260.py" "b/5034.\346\234\200\345\244\247\345\224\257\344\270\200\346\225\260/5034-\346\234\200\345\244\247\345\224\257\344\270\200\346\225\260.py" deleted file mode 100644 index d47dfe7..0000000 --- "a/5034.\346\234\200\345\244\247\345\224\257\344\270\200\346\225\260/5034-\346\234\200\345\244\247\345\224\257\344\270\200\346\225\260.py" +++ /dev/null @@ -1,15 +0,0 @@ -class Solution(object): - def largestUniqueNumber(self, A): - """ - :type A: List[int] - :rtype: int - """ - from collections import Counter - dic = Counter(A) - - res = -1 - for key, val in dic.items(): - if val == 1: - res = max(res, key) - return res - \ No newline at end of file diff --git "a/5035.\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\350\267\257\345\276\204/5035-\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\350\267\257\345\276\204.py" "b/5035.\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\350\267\257\345\276\204/5035-\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\350\267\257\345\276\204.py" deleted file mode 100644 index b9846e6..0000000 --- "a/5035.\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\350\267\257\345\276\204/5035-\345\276\227\345\210\206\346\234\200\351\253\230\347\232\204\350\267\257\345\276\204.py" +++ /dev/null @@ -1,33 +0,0 @@ -class Solution(object): - def maximumMinimumPath(self, A): - """ - :type A: List[List[int]] - :rtype: int - """ - from heapq import * - if not A or not A[0]: - return 0 - - m, n = len(A), len(A[0]) - dx = [1, -1, 0, 0] - dy = [0, 0, 1, -1] - - res = 0 - queue = [[-A[0][0], 0, 0]] - visited = set([0,0]) - heapify(queue) - while queue: - score, x0, y0 = heappop(queue) #Ŀǰŵĵҳ - if [x0, y0] == [m - 1, n - 1]: #Ѿյ - return -score - - 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: - visited.add((x, y)) - heappush(queue, [max(score, -A[x][y]), x, y]) #ھӵ - - - \ No newline at end of file diff --git "a/5076.\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220/5076-\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220.py" "b/5076.\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220/5076-\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220.py" deleted file mode 100644 index 0acd271..0000000 --- "a/5076.\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220/5076-\345\255\227\347\254\246\344\270\262\347\232\204\346\234\200\345\244\247\345\205\254\345\233\240\345\255\220.py" +++ /dev/null @@ -1,29 +0,0 @@ -class Solution(object): - def gcdOfStrings(self, str1, str2): - """ - :type str1: str - :type str2: str - :rtype: str - """ - l1,l2 = len(str1), len(str2) - set1, set2 = set(), set() - for i in range(l1 + 1): - # print str1[:i] - if self.find(str1, str1[:i]): - set1.add(str1[:i]) - - for i in range(l2 + 1): - if self.find(str2, str2[:i]): - set2.add(str2[:i]) - - # print set1, set2 - res = list(set1 & set2) - res = sorted(res, key = lambda x:-len(x)) - return res[0] if res else "" - - def find(self, string, tmp): - # if not tmp: - # print tmp - string = string.replace(tmp, "") - # print string - return len(string) == 0 \ No newline at end of file diff --git "a/5078.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/5078-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" "b/5078.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/5078-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" deleted file mode 100644 index 06a9285..0000000 --- "a/5078.\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240/5078-\350\264\237\344\272\214\350\277\233\345\210\266\346\225\260\347\233\270\345\212\240.py" +++ /dev/null @@ -1,36 +0,0 @@ -class Solution(object): - def addNegabinary(self, arr1, arr2): - """ - :type arr1: List[int] - :type arr2: List[int] - :rtype: List[int] - """ - def convertInt(string): - tmp = 1 - string = string[::-1] - res = 0 - for i, x in enumerate(string): - res += tmp * int(x) - tmp *= -2 - - return res - n1 = convertInt(arr1) - n2 = convertInt(arr2) - # print n1, n2, convertInt(self.baseNeg2(n1 + n2)) - return self.baseNeg2(n1 + n2) - - - - def baseNeg2(self, N): - """ - :type N: int - :rtype: str - """ - res = [] - # n = N - while N: - 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/5117.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/5117-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" "b/5117.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/5117-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" deleted file mode 100644 index 8a7cf16..0000000 --- "a/5117.IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226/5117-IP\345\234\260\345\235\200\346\227\240\346\225\210\345\214\226.py" +++ /dev/null @@ -1,8 +0,0 @@ -class Solution(object): - def defangIPaddr(self, address): - """ - :type address: str - :rtype: str - """ - address = address.replace(".", "[.]") - return address \ No newline at end of file diff --git "a/5118.\350\210\252\347\217\255\351\242\204\350\256\242\347\273\237\350\256\241/5118-\350\210\252\347\217\255\351\242\204\350\256\242\347\273\237\350\256\241.py" "b/5118.\350\210\252\347\217\255\351\242\204\350\256\242\347\273\237\350\256\241/5118-\350\210\252\347\217\255\351\242\204\350\256\242\347\273\237\350\256\241.py" deleted file mode 100644 index 523cb8f..0000000 --- "a/5118.\350\210\252\347\217\255\351\242\204\350\256\242\347\273\237\350\256\241/5118-\350\210\252\347\217\255\351\242\204\350\256\242\347\273\237\350\256\241.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def corpFlightBookings(self, bookings, n): - """ - :type bookings: List[List[int]] - :type n: int - :rtype: List[int] - """ - f = [0 for _ in range(n + 1)] - - for i, j, k in bookings: - f[i - 1] += k - f[j] -= k - # print f - for i in range(1, n + 1): - f[i] += f[i - 1] - # print f - return f[:-1] \ No newline at end of file diff --git "a/5119.\345\210\240\347\202\271\346\210\220\346\236\227/5119-\345\210\240\347\202\271\346\210\220\346\236\227.py" "b/5119.\345\210\240\347\202\271\346\210\220\346\236\227/5119-\345\210\240\347\202\271\346\210\220\346\236\227.py" deleted file mode 100644 index c912ec1..0000000 --- "a/5119.\345\210\240\347\202\271\346\210\220\346\236\227/5119-\345\210\240\347\202\271\346\210\220\346\236\227.py" +++ /dev/null @@ -1,57 +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 delNodes(self, root, to_delete): - """ - :type root: TreeNode - :type to_delete: List[int] - :rtype: List[TreeNode] - """ - if not root: - return [] - to_delete = set(to_delete) - - res = [] - - self.roots = [] - if root.val not in to_delete: - self.roots = [root] - else: #ڵ㶼Ҫɾ - if root.left and root.left.val not in to_delete: - self.roots += [root.left] - if root.right and root.right.val not in to_delete: - self.roots += [root.right] - - def dfs(node): - if not node: - return - - if node.left and node.left.val in to_delete: #ڵҪɾ - dfs(node.left) - if node.left.left: #ڵڵ㻹ڣҪɾ - self.roots += [node.left.left] - if node.left.right: #ڵҽڵ㻹ڣҪɾ - self.roots += [node.left.right] - - node.left = None - - if node.right and node.right.val in to_delete:#ҽڵҪɾ - dfs(node.right) - if node.right.left: #ҽڵڵ㻹ڣҪɾ - self.roots += [node.right.left] - if node.right.right: #ҽڵҽڵ㻹ڣҪɾ - self.roots += [node.right.right] - - node.right = None - - dfs(node.left) - dfs(node.right) - - dfs(root) - return self.roots diff --git "a/5120.\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246/5120-\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246.py" "b/5120.\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246/5120-\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246.py" deleted file mode 100644 index 84f2bc1..0000000 --- "a/5120.\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246/5120-\346\234\211\346\225\210\346\213\254\345\217\267\347\232\204\345\265\214\345\245\227\346\267\261\345\272\246.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def maxDepthAfterSplit(self, seq): - """ - :type seq: str - :rtype: List[int] - """ - # print self.depth("((()))") - res = [0 for _ in range(len(seq))] - cnt = 0 - for i, x in enumerate(seq): - if x == "(": - cnt += 1 - res[i] = cnt % 2 - else: - res[i] = cnt % 2 - cnt -= 1 - return res diff --git "a/5127.\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217/5127-\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217.py" "b/5127.\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217/5127-\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217.py" deleted file mode 100644 index a773b8d..0000000 --- "a/5127.\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217/5127-\346\225\260\347\273\204\347\232\204\347\233\270\345\257\271\346\216\222\345\272\217.py" +++ /dev/null @@ -1,25 +0,0 @@ -class Solution(object): - def relativeSortArray(self, arr1, arr2): - """ - :type arr1: List[int] - :type arr2: List[int] - :rtype: List[int] - """ - from collections import defaultdict - res = [] - left = [] #ûarr2ֹ - set2 = set(arr2) #arr2תsetòҵʱ临ӶȽ͵O(1) - - dic = defaultdict(int) #¼arr1ֳֵƵ - for num in arr1: - if num in set2: - dic[num] += 1 - else: - left.append(num) - - for num in arr2: - res += [num] * dic[num] - - left.sort() #ĿҪûֹԪ - return res + left - \ No newline at end of file diff --git "a/5128.\346\234\200\346\267\261\345\217\266\350\212\202\347\202\271\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/5128-\346\234\200\346\267\261\345\217\266\350\212\202\347\202\271\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.py" "b/5128.\346\234\200\346\267\261\345\217\266\350\212\202\347\202\271\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/5128-\346\234\200\346\267\261\345\217\266\350\212\202\347\202\271\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 79145a8..0000000 --- "a/5128.\346\234\200\346\267\261\345\217\266\350\212\202\347\202\271\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/5128-\346\234\200\346\267\261\345\217\266\350\212\202\347\202\271\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,71 +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 lcaDeepestLeaves(self, root): - """ - :type root: TreeNode - :rtype: TreeNode - """ - def inorder(node): - if not node: - return [] - return inorder(node.left) + [node.val] + inorder(node.right) - # print inorder(root) - if not root or (not root.left and not root.right): - return root - - dic = {} - def depth(node, tmp): - if not node: - return - dic[node] = tmp + 1 - depth(node.left, tmp + 1) - depth(node.right, tmp + 1) - - depth(root, 0) - # print dic - l = [] - max_depth = max(dic.values()) - for key, val in dic.items(): - if val == max_depth: - l.append(key) - - if len(l) == 1: - return l[0] - # print l - tmpAncestor = None - if len(l) >= 2: - tmpAncestor = self.lowestCommonAncestor(root, l[0], l[1]) - for i in range(2, len(l), 1): - tmpAncestor = self.lowestCommonAncestor(root, tmpAncestor, l[i]) - return tmpAncestor - - - - def lowestCommonAncestor(self, root, p, q): - """ - :type root: TreeNode - :type p: TreeNode - :type q: TreeNode - :rtype: TreeNode - """ - - if not root or root == p or root == q: - return root - else: - left = self.lowestCommonAncestor(root.left, p, q) - right = self.lowestCommonAncestor(root.right, p, q) - - if left and right: #һһ - return root - elif left:# - return left - elif right:# - return right - else: - return \ No newline at end of file diff --git "a/5129.\350\241\250\347\216\260\350\211\257\345\245\275\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264\346\256\265/5129-\350\241\250\347\216\260\350\211\257\345\245\275\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264\346\256\265.py" "b/5129.\350\241\250\347\216\260\350\211\257\345\245\275\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264\346\256\265/5129-\350\241\250\347\216\260\350\211\257\345\245\275\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264\346\256\265.py" deleted file mode 100644 index 31f4dc7..0000000 --- "a/5129.\350\241\250\347\216\260\350\211\257\345\245\275\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264\346\256\265/5129-\350\241\250\347\216\260\350\211\257\345\245\275\347\232\204\346\234\200\351\225\277\346\227\266\351\227\264\346\256\265.py" +++ /dev/null @@ -1,27 +0,0 @@ -class Solution(object): - def longestWPI(self, hours): - """ - :type hours: List[int] - :rtype: int - """ - n = len(hours) - a = [0]*n - for i,hour in enumerate(hours): - if hour>8: - a[i]=1 - else: - a[i]=-1 - for i in range(1,n): - a[i]+=a[i-1] - b={} - res = 0 - for i in range(n): - if a[i]>0: - res = max(res,i+1) - else: - if a[i]-1 in b: - res = max(res,i-b[a[i]-1]) - if a[i] not in b: - b[a[i]]=i - return res - \ No newline at end of file diff --git "a/5130.\347\255\211\344\273\267\345\244\232\347\261\263\350\257\272\351\252\250\347\211\214\345\257\271\347\232\204\346\225\260\351\207\217/5130-\347\255\211\344\273\267\345\244\232\347\261\263\350\257\272\351\252\250\347\211\214\345\257\271\347\232\204\346\225\260\351\207\217.py" "b/5130.\347\255\211\344\273\267\345\244\232\347\261\263\350\257\272\351\252\250\347\211\214\345\257\271\347\232\204\346\225\260\351\207\217/5130-\347\255\211\344\273\267\345\244\232\347\261\263\350\257\272\351\252\250\347\211\214\345\257\271\347\232\204\346\225\260\351\207\217.py" deleted file mode 100644 index a4e29c2..0000000 --- "a/5130.\347\255\211\344\273\267\345\244\232\347\261\263\350\257\272\351\252\250\347\211\214\345\257\271\347\232\204\346\225\260\351\207\217/5130-\347\255\211\344\273\267\345\244\232\347\261\263\350\257\272\351\252\250\347\211\214\345\257\271\347\232\204\346\225\260\351\207\217.py" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution(object): - def numEquivDominoPairs(self, dominoes): - """ - :type dominoes: List[List[int]] - :rtype: int - """ - from collections import defaultdict - dic = defaultdict(int) - res = 0 - for i, x in enumerate(dominoes): - pair = (x[1], x[0]) - if x[0] < x[1]: - pair = (x[0], x[1]) - res += dic[pair] - dic[pair] += 1 - - return res \ No newline at end of file diff --git "a/5132.\351\242\234\350\211\262\344\272\244\346\233\277\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/5132-\351\242\234\350\211\262\344\272\244\346\233\277\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" "b/5132.\351\242\234\350\211\262\344\272\244\346\233\277\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/5132-\351\242\234\350\211\262\344\272\244\346\233\277\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" deleted file mode 100644 index a957f4c..0000000 --- "a/5132.\351\242\234\350\211\262\344\272\244\346\233\277\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204/5132-\351\242\234\350\211\262\344\272\244\346\233\277\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.py" +++ /dev/null @@ -1,40 +0,0 @@ -class Solution(object): - def shortestAlternatingPaths(self, n, red_edges, blue_edges): - """ - :type n: int - :type red_edges: List[List[int]] - :type blue_edges: List[List[int]] - :rtype: List[int] - """ - from collections import defaultdict, deque - red_nei, blue_nei = defaultdict(set), defaultdict(set) - - for start, end in red_edges: #¼еĴӺɫ·Եھӽڵ - red_nei[start].add(end) - for start, end in blue_edges: #¼еĴɫ·Եھӽڵ - blue_nei[start].add(end) - - visited = set() - queue = deque() - queue.append((0, -1)) # -1 ɫ 0 ɫ 1ɫ - distance = 0 - res = [-1] * n - while queue: - next_queue = deque() - for cur, color in queue: - if res[cur] == -1: #BFS˵һεյʱľһ̾ - res[cur] = distance - if color == -1 or color == 0: #ǰǺɫһɫ - for nei in blue_nei[cur]: - if (cur, nei, 1) not in visited: - visited.add((cur, nei, 1)) - next_queue.append((nei, 1)) - if color == -1 or color == 1: #ǰɫһںɫ - for nei in red_nei[cur]: - if (cur, nei, 0) not in visited: - visited.add((cur, nei, 0)) - next_queue.append((nei, 0)) - queue = next_queue - distance += 1 - return res - \ No newline at end of file diff --git "a/5133.\347\273\235\345\257\271\345\200\274\350\241\250\350\276\276\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274/5133-\347\273\235\345\257\271\345\200\274\350\241\250\350\276\276\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/5133.\347\273\235\345\257\271\345\200\274\350\241\250\350\276\276\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274/5133-\347\273\235\345\257\271\345\200\274\350\241\250\350\276\276\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274.py" deleted file mode 100644 index 191c629..0000000 --- "a/5133.\347\273\235\345\257\271\345\200\274\350\241\250\350\276\276\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274/5133-\347\273\235\345\257\271\345\200\274\350\241\250\350\276\276\345\274\217\347\232\204\346\234\200\345\244\247\345\200\274.py" +++ /dev/null @@ -1,34 +0,0 @@ -class Solution(object): - def maxAbsValExpr(self, arr1, arr2): - """ - :type arr1: List[int] - :type arr2: List[int] - :rtype: int - """ - record = [-2 ** 31] * 8 - - for i in range(len(arr1)): - for k in range(8): #ɰֿܵĽ - t = 0 - if k & 1: - t += arr1[i] - else: - t -= arr1[i] - if k & 2: - t += arr2[i] - else: - t -= arr2[i] - if k & 4: - t += i - else: - t -= i - # print k, t - record[k] = max(record[k], t) - # print record - res = 0 - for k in range(8): - # print k, ~k - res = max(res, record[k] + record[-k-1]) #һһڶ͵ڶ֮һʽ - return res - - \ No newline at end of file diff --git "a/5140.\345\255\227\346\257\215\346\235\277\344\270\212\347\232\204\350\267\257\345\276\204/5140-\345\255\227\346\257\215\346\235\277\344\270\212\347\232\204\350\267\257\345\276\204.py" "b/5140.\345\255\227\346\257\215\346\235\277\344\270\212\347\232\204\350\267\257\345\276\204/5140-\345\255\227\346\257\215\346\235\277\344\270\212\347\232\204\350\267\257\345\276\204.py" deleted file mode 100644 index eaf1121..0000000 --- "a/5140.\345\255\227\346\257\215\346\235\277\344\270\212\347\232\204\350\267\257\345\276\204/5140-\345\255\227\346\257\215\346\235\277\344\270\212\347\232\204\350\267\257\345\276\204.py" +++ /dev/null @@ -1,50 +0,0 @@ -class Solution(object): - def alphabetBoardPath(self, target): - """ - :type target: str - :rtype: str - """ - board = ["abcde", - "fghij", - "klmno", - "pqrst", - "uvwxy", - "z"] - - dic = dict() - for i in range(len(board)): - for j in range(len(board[0])): - - dic[board[i][j]] = [i, j] - if i == 5: - break - - x0, y0 = 0,0 - res = "" - for char in target: - tmp = "" - des = dic[char] - x1, y1 = des[0], des[1] - if x0 == x1 and y0 == y1: - res += "!" - continue - if x0 < x1: - tmp += "D" * (x1 - x0) - elif x0 > x1: - tmp += "U" * (x0 - x1) - if x1 == 5 and tmp and tmp[-1] == "D": - tmp = tmp[:-1] - if y0 < y1: - tmp += "R" * (y1 - y0) - elif y1 < y0: - tmp += "L" * (y0 - y1) - x0 = x1 - y0 = y1 - if x1 == 5: - tmp += "D" - tmp += "!" - res += tmp - return res - - - \ No newline at end of file diff --git "a/5432.\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274/5432-\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274.py" "b/5432.\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274/5432-\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274.py" new file mode 100644 index 0000000..0aeb3b1 --- /dev/null +++ "b/5432.\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274/5432-\345\216\273\346\216\211\346\234\200\344\275\216\345\267\245\350\265\204\345\222\214\346\234\200\351\253\230\345\267\245\350\265\204\345\220\216\347\232\204\345\267\245\350\265\204\345\271\263\345\235\207\345\200\274.py" @@ -0,0 +1,7 @@ +class Solution(object): + def average(self, salary): + """ + :type salary: List[int] + :rtype: float + """ + return (sum(salary) - max(salary) - min(salary)) * 1.0 / (len(salary) - 2) \ No newline at end of file diff --git "a/5433.n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220/5433-n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220.py" "b/5433.n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220/5433-n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220.py" new file mode 100644 index 0000000..5f7b65d --- /dev/null +++ "b/5433.n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220/5433-n\347\232\204\347\254\254k\344\270\252\345\233\240\345\255\220.py" @@ -0,0 +1,21 @@ +class Solution(object): + def kthFactor(self, n, k): + """ + :type n: int + :type k: int + :rtype: int + """ + l = [] + for i in range(1, int(n ** 0.5) + 1): + if n % i == 0: + l.append(i) + total = len(l) * 2 if l[-1] ** 2 != n else len(l) * 2 - 1 + if total < k: + return -1 + + if k - 1 < len(l): + return l[k - 1] + else: + idx = (len(l) - 1) * 2 - k + return n // l[idx] if total % 2 else n // l[idx + 2] + \ No newline at end of file diff --git "a/5434.\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204/5434-\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204.py" "b/5434.\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204/5434-\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204.py" new file mode 100644 index 0000000..c41b8a5 --- /dev/null +++ "b/5434.\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204/5434-\345\210\240\346\216\211\344\270\200\344\270\252\345\205\203\347\264\240\344\273\245\345\220\216\345\205\250\344\270\2721\347\232\204\346\234\200\351\225\277\345\255\220\346\225\260\347\273\204.py" @@ -0,0 +1,21 @@ +class Solution(object): + def longestSubarray(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if sum(nums) == len(nums): + return len(nums) - 1 + zeroCnt = 0 + left = 0 + res = 0 + for right in range(len(nums)): + if nums[right] == 0: + zeroCnt += 1 + while zeroCnt > 1: + if nums[left] == 0: + zeroCnt -= 1 + left += 1 + res = max(res, right - left + 1 - zeroCnt) + return res + \ No newline at end of file diff --git "a/5435.\345\271\266\350\241\214\350\257\276\347\250\213II/5435-\345\271\266\350\241\214\350\257\276\347\250\213II.py" "b/5435.\345\271\266\350\241\214\350\257\276\347\250\213II/5435-\345\271\266\350\241\214\350\257\276\347\250\213II.py" new file mode 100644 index 0000000..f8a7f64 --- /dev/null +++ "b/5435.\345\271\266\350\241\214\350\257\276\347\250\213II/5435-\345\271\266\350\241\214\350\257\276\347\250\213II.py" @@ -0,0 +1,49 @@ +class Solution(object): + def minNumberOfSemesters(self, n, dependencies, k): + """ + :type n: int + :type dependencies: List[List[int]] + :type k: int + :rtype: int + """ + from collections import defaultdict + inDegree = defaultdict(int) + outDegree = defaultdict(int) + children = defaultdict(set) + + for src, dec in dependencies: # 建图 + inDegree[dec] += 1 + outDegree[src] += 1 + children[src].add(dec) + + queue = [] + for i in range(1, n + 1): + if inDegree[i] == 0: # 入度为0(没有先修课了)的课入队 + heappush(queue, (-outDegree[i], i, -1)) # 出度越大(以这门课作为先修课的课越多),优先级越高 + + semesterCnt = 0 + while queue: + semesterCnt += 1 + nextSemesterCourses = [] # 存放这个学期不能上的课 + courseCnt = 0 + while courseCnt < k and queue: # 每个学期最多上 k 门课 + priority, node, preFinishedSemester = heappop(queue) + + if preFinishedSemester >= semesterCnt: # 当前学期不能上这门课 + nextSemesterCourses.append((priority, node, preFinishedSemester)) + continue + + for child in children[node]: # 这门课可以学,学它,然后处理孩子课的入度 + inDegree[child] -= 1 + + if inDegree[child] == 0: # 孩子课的先修课全上完了 + heappush(queue, (-outDegree[child], child, semesterCnt)) + courseCnt += 1 + + for item in nextSemesterCourses: # 把之前存起来的本学期不能上的课再重新入队 + heappush(queue, item) + + return semesterCnt + + + \ 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/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/README.md b/README.md index 2a42799..763336d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# LeetCode-Python -这个文件夹里存放的是我在Leetcode-cn上的第一轮刷题的AC代码。 +# LeetCode-Solutions +This is my repo for my solutions of LeetCode Problems in Python2. -题目分析解答可以参考我的个人博客:https://blog.csdn.net/qq_32424059 +You can access my Chinese Analysis in my blog: https://blog.csdn.net/qq_32424059 -本文件夹由https://github.com/JiayangWu/LeetCodeCN-Submissions-Crawler 自动生成。 +This repo is generated automatically using my script: https://github.com/JiayangWu/LeetCodeCN-Submissions-Crawler diff --git "a/README.md\347\232\204\346\233\277\350\272\253" "b/README.md\347\232\204\346\233\277\350\272\253" new file mode 100644 index 0000000..92d601a Binary files /dev/null and "b/README.md\347\232\204\346\233\277\350\272\253" differ 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\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" new file mode 100644 index 0000000..7971e98 --- /dev/null +++ "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" @@ -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 + +class Solution: + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + child2par = {root: None} + stack = [root] + + 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\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" "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" new file mode 100644 index 0000000..59b4b34 --- /dev/null +++ "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" @@ -0,0 +1,8 @@ +class Solution(object): + def isFlipedString(self, s1, s2): + """ + :type s1: str + :type s2: str + :rtype: bool + """ + return s1 in s2 + s2 and len(s1) == len(s2) \ No newline at end of file diff --git "a/\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/\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..59b4b34 --- /dev/null +++ "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.py" @@ -0,0 +1,8 @@ +class Solution(object): + def isFlipedString(self, s1, s2): + """ + :type s1: str + :type s2: str + :rtype: bool + """ + return s1 in s2 + s2 and len(s1) == len(s2) \ No newline at end of file diff --git "a/\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" "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" new file mode 100644 index 0000000..647f3de --- /dev/null +++ "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" @@ -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 removeDuplicateNodes(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + visited = set() + + p = head + pre = None + while p: + if p.val in visited: + pre.next = p.next + p = p.next + else: + visited.add(p.val) + pre = p + p = p.next + return head + \ No newline at end of file diff --git "a/\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/\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..647f3de --- /dev/null +++ "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.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 removeDuplicateNodes(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + visited = set() + + p = head + pre = None + while p: + if p.val in visited: + pre.next = p.next + p = p.next + else: + visited.add(p.val) + pre = p + p = p.next + return head + \ No newline at end of file diff --git "a/\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" "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" new file mode 100644 index 0000000..5ef4970 --- /dev/null +++ "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" @@ -0,0 +1,22 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def kthToLast(self, head, k): + """ + :type head: ListNode + :type k: int + :rtype: int + """ + slow, fast = head, head + while k: + k -= 1 + fast = fast.next + + while fast: + slow = slow.next + fast = fast.next + return slow.val \ No newline at end of file diff --git "a/\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/\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..5ef4970 --- /dev/null +++ "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.py" @@ -0,0 +1,22 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def kthToLast(self, head, k): + """ + :type head: ListNode + :type k: int + :rtype: int + """ + slow, fast = head, head + while k: + k -= 1 + fast = fast.next + + while fast: + slow = slow.next + fast = fast.next + return slow.val \ No newline at end of file diff --git "a/\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" "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" new file mode 100644 index 0000000..c77b28f --- /dev/null +++ "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" @@ -0,0 +1,19 @@ +# 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. + """ + p = node + while p and p.next: + p.val = p.next.val + if not p.next.next: + p.next = None + p = p.next + \ No newline at end of file diff --git "a/\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/\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..c77b28f --- /dev/null +++ "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.py" @@ -0,0 +1,19 @@ +# 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. + """ + p = node + while p and p.next: + p.val = p.next.val + if not p.next.next: + p.next = None + p = p.next + \ No newline at end of file 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\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" "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" new file mode 100644 index 0000000..55295bc --- /dev/null +++ "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" @@ -0,0 +1,10 @@ +class Solution(object): + def findMagicIndex(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + for i, num in enumerate(nums): + if i == num: + return i + return -1 \ No newline at end of file diff --git "a/\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/\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..55295bc --- /dev/null +++ "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.py" @@ -0,0 +1,10 @@ +class Solution(object): + def findMagicIndex(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + for i, num in enumerate(nums): + if i == num: + return i + return -1 \ No newline at end of file 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.01.\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\230 10.01-\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204.py" "b/\351\235\242\350\257\225\351\242\230 10.01.\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\230 10.01-\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204.py" new file mode 100644 index 0000000..10722e8 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 10.01.\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\230 10.01-\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204.py" @@ -0,0 +1,10 @@ +class Solution(object): + def merge(self, A, m, B, n): + """ + :type A: List[int] + :type m: int + :type B: List[int] + :type n: int + :rtype: None Do not return anything, modify A in-place instead. + """ + A[:] = sorted(A[:m] + B) \ 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\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/\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..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.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\230 17.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\230 17.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\230 17.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\230 17.01-\344\270\215\347\224\250\345\212\240\345\217\267\347\232\204\345\212\240\346\263\225.py" new file mode 100644 index 0000000..02652bd --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\230 17.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\230 17.01-\344\270\215\347\224\250\345\212\240\345\217\267\347\232\204\345\212\240\346\263\225.py" @@ -0,0 +1,8 @@ +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\23001.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\23001.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\23001.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\23001.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..50fe13e --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23001.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\23001.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,7 @@ +class Solution(object): + def isUnique(self, astr): + """ + :type astr: str + :rtype: bool + """ + return len(set(astr)) == len(astr) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23001.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\23001.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\23001.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\23001.06-\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251.py" new file mode 100644 index 0000000..17bc3fc --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23001.06.\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251/\351\235\242\350\257\225\351\242\23001.06-\345\255\227\347\254\246\344\270\262\345\216\213\347\274\251.py" @@ -0,0 +1,23 @@ +class Solution(object): + def compressString(self, S): + """ + :type S: str + :rtype: str + """ + pre = None + cnt = 0 + res = "" + for ch in S: + if not pre: + cnt += 1 + pre = ch + else: + if ch == pre: + cnt += 1 + else: + res += pre + str(cnt) + cnt = 1 + pre = ch + if S: + res += pre + str(cnt) + 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\23001.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23001.08-\351\233\266\347\237\251\351\230\265.py" "b/\351\235\242\350\257\225\351\242\23001.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23001.08-\351\233\266\347\237\251\351\230\265.py" new file mode 100644 index 0000000..01d1edd --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23001.08.\351\233\266\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23001.08-\351\233\266\347\237\251\351\230\265.py" @@ -0,0 +1,25 @@ +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]) + + row0, col0 = set(), set() + + for i in range(m): + for j in range(n): + if not matrix[i][j]: + row0.add(i) + col0.add(j) + + for i in range(m): + for j in range(n): + if i in row0 or j in col0: + matrix[i][j] = 0 + + return matrix \ 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\23003.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23003-\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/\351\235\242\350\257\225\351\242\23003.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23003-\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..cf94ed9 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23003.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23003-\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,11 @@ +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/\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\23008.11.\347\241\254\345\270\201/\351\235\242\350\257\225\351\242\23008.11-\347\241\254\345\270\201.py" "b/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201/\351\235\242\350\257\225\351\242\23008.11-\347\241\254\345\270\201.py" new file mode 100644 index 0000000..7b069dd --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201/\351\235\242\350\257\225\351\242\23008.11-\347\241\254\345\270\201.py" @@ -0,0 +1,12 @@ +class Solution(object): + def waysToChange(self, n): + """ + :type n: int + :rtype: int + """ + coin = [1, 5, 10, 25] + dp = [1] + [0] * n + for c in coin: + for i in range(c, n + 1): + dp[i] += dp[i - c] + return dp[-1] % (10**9 + 7) \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23010.01.\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\23010.01-\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204.py" "b/\351\235\242\350\257\225\351\242\23010.01.\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\23010.01-\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204.py" new file mode 100644 index 0000000..10722e8 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23010.01.\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\23010.01-\345\220\210\345\271\266\346\216\222\345\272\217\347\232\204\346\225\260\347\273\204.py" @@ -0,0 +1,10 @@ +class Solution(object): + def merge(self, A, m, B, n): + """ + :type A: List[int] + :type m: int + :type B: List[int] + :type n: int + :rtype: None Do not return anything, modify A in-place instead. + """ + A[:] = sorted(A[:m] + B) \ 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" new file mode 100644 index 0000000..c795505 --- /dev/null +++ "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" @@ -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\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.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" new file mode 100644 index 0000000..109e90c --- /dev/null +++ "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" @@ -0,0 +1,14 @@ +class Solution(object): + 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] = max(dp[i - 1], dp[i - 2] + nums[i]) + return dp[-1] \ No newline at end of file diff --git "a/\351\235\242\350\257\225\351\242\23027.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\351\235\242\350\257\225\351\242\23027-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" "b/\351\235\242\350\257\225\351\242\23027.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\351\235\242\350\257\225\351\242\23027-\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..71ab723 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23027.\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/\351\235\242\350\257\225\351\242\23027-\344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217.py" @@ -0,0 +1,19 @@ +# 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/\351\235\242\350\257\225\351\242\23028.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/\351\235\242\350\257\225\351\242\23028-\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.py" "b/\351\235\242\350\257\225\351\242\23028.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/\351\235\242\350\257\225\351\242\23028-\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.py" new file mode 100644 index 0000000..ec0c7c3 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23028.\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/\351\235\242\350\257\225\351\242\23028-\345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221.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 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/\351\235\242\350\257\225\351\242\23029.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23029-\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.py" "b/\351\235\242\350\257\225\351\242\23029.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23029-\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.py" new file mode 100644 index 0000000..5c212b0 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23029.\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/\351\235\242\350\257\225\351\242\23029-\351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265.py" @@ -0,0 +1,45 @@ +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/\351\235\242\350\257\225\351\242\23030.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/\351\235\242\350\257\225\351\242\23030-\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.py" "b/\351\235\242\350\257\225\351\242\23030.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/\351\235\242\350\257\225\351\242\23030-\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.py" new file mode 100644 index 0000000..787a395 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23030.\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/\351\235\242\350\257\225\351\242\23030-\345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210.py" @@ -0,0 +1,50 @@ +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/\351\235\242\350\257\225\351\242\23031.\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/\351\235\242\350\257\225\351\242\23031-\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/\351\235\242\350\257\225\351\242\23031.\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/\351\235\242\350\257\225\351\242\23031-\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" new file mode 100644 index 0000000..4dfe5b1 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23031.\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/\351\235\242\350\257\225\351\242\23031-\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" @@ -0,0 +1,19 @@ +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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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" new file mode 100644 index 0000000..bffbb72 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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" @@ -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 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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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" new file mode 100644 index 0000000..0798c1d --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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" @@ -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 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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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" new file mode 100644 index 0000000..1456eae --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23032-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/\351\235\242\350\257\225\351\242\23032-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" @@ -0,0 +1,35 @@ +# 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/\351\235\242\350\257\225\351\242\23033.\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/\351\235\242\350\257\225\351\242\23033-\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/\351\235\242\350\257\225\351\242\23033.\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/\351\235\242\350\257\225\351\242\23033-\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" new file mode 100644 index 0000000..0d2fd26 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23033.\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/\351\235\242\350\257\225\351\242\23033-\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" @@ -0,0 +1,25 @@ +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/\351\235\242\350\257\225\351\242\23034.\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/\351\235\242\350\257\225\351\242\23034-\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/\351\235\242\350\257\225\351\242\23034.\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/\351\235\242\350\257\225\351\242\23034-\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" new file mode 100644 index 0000000..6f40ee3 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23034.\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/\351\235\242\350\257\225\351\242\23034-\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" @@ -0,0 +1,31 @@ +# 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/\351\235\242\350\257\225\351\242\23035.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\351\235\242\350\257\225\351\242\23035-\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266.py" "b/\351\235\242\350\257\225\351\242\23035.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\351\235\242\350\257\225\351\242\23035-\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..9de38d5 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23035.\345\244\215\346\235\202\351\223\276\350\241\250\347\232\204\345\244\215\345\210\266/\351\235\242\350\257\225\351\242\23035-\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,30 @@ +""" +# 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/\351\235\242\350\257\225\351\242\23036.\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/\351\235\242\350\257\225\351\242\23036-\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/\351\235\242\350\257\225\351\242\23036.\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/\351\235\242\350\257\225\351\242\23036-\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" new file mode 100644 index 0000000..669fb96 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23036.\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/\351\235\242\350\257\225\351\242\23036-\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" @@ -0,0 +1,47 @@ +""" +# 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/\351\235\242\350\257\225\351\242\23038.\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\23038-\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227.py" "b/\351\235\242\350\257\225\351\242\23038.\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\23038-\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227.py" new file mode 100644 index 0000000..c61bd6e --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23038.\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/\351\235\242\350\257\225\351\242\23038-\345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227.py" @@ -0,0 +1,16 @@ +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/\351\235\242\350\257\225\351\242\23039.\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/\351\235\242\350\257\225\351\242\23039-\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/\351\235\242\350\257\225\351\242\23039.\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/\351\235\242\350\257\225\351\242\23039-\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" new file mode 100644 index 0000000..b0e5086 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23039.\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/\351\235\242\350\257\225\351\242\23039-\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" @@ -0,0 +1,7 @@ +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/\351\235\242\350\257\225\351\242\23040.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\351\235\242\350\257\225\351\242\23040-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" "b/\351\235\242\350\257\225\351\242\23040.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\351\235\242\350\257\225\351\242\23040-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" new file mode 100644 index 0000000..50bd4bf --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23040.\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/\351\235\242\350\257\225\351\242\23040-\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.py" @@ -0,0 +1,18 @@ +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/\351\235\242\350\257\225\351\242\23042.\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/\351\235\242\350\257\225\351\242\23042-\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/\351\235\242\350\257\225\351\242\23042.\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/\351\235\242\350\257\225\351\242\23042-\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" new file mode 100644 index 0000000..d4b9334 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23042.\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/\351\235\242\350\257\225\351\242\23042-\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" @@ -0,0 +1,16 @@ +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/\351\235\242\350\257\225\351\242\23047.\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/\351\235\242\350\257\225\351\242\23047-\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274.py" "b/\351\235\242\350\257\225\351\242\23047.\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/\351\235\242\350\257\225\351\242\23047-\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274.py" new file mode 100644 index 0000000..86bf71d --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23047.\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/\351\235\242\350\257\225\351\242\23047-\347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274.py" @@ -0,0 +1,22 @@ +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/\351\235\242\350\257\225\351\242\23050.\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/\351\235\242\350\257\225\351\242\23050-\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/\351\235\242\350\257\225\351\242\23050.\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/\351\235\242\350\257\225\351\242\23050-\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" new file mode 100644 index 0000000..817227e --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23050.\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/\351\235\242\350\257\225\351\242\23050-\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" @@ -0,0 +1,14 @@ +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/\351\235\242\350\257\225\351\242\23052.\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/\351\235\242\350\257\225\351\242\23052-\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/\351\235\242\350\257\225\351\242\23052.\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/\351\235\242\350\257\225\351\242\23052-\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..120f097 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23052.\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/\351\235\242\350\257\225\351\242\23052-\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,39 @@ +# 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/\351\235\242\350\257\225\351\242\23053-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/\351\235\242\350\257\225\351\242\23053-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/\351\235\242\350\257\225\351\242\23053-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/\351\235\242\350\257\225\351\242\23053-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..8b4c0c6 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23053-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/\351\235\242\350\257\225\351\242\23053-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,8 @@ +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/\351\235\242\350\257\225\351\242\23053-II.0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23053-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/\351\235\242\350\257\225\351\242\23053-II.0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23053-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" new file mode 100644 index 0000000..f15c45d --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23053-II.0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23053-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" @@ -0,0 +1,16 @@ +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/\351\235\242\350\257\225\351\242\23054.\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/\351\235\242\350\257\225\351\242\23054-\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/\351\235\242\350\257\225\351\242\23054.\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/\351\235\242\350\257\225\351\242\23054-\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..6d799dd --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23054.\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/\351\235\242\350\257\225\351\242\23054-\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,36 @@ +# 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/\351\235\242\350\257\225\351\242\23055-I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\351\235\242\350\257\225\351\242\23055-I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" "b/\351\235\242\350\257\225\351\242\23055-I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\351\235\242\350\257\225\351\242\23055-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..e3b62e7 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23055-I.\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/\351\235\242\350\257\225\351\242\23055-I-\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246.py" @@ -0,0 +1,16 @@ +# 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/\351\235\242\350\257\225\351\242\23057-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/\351\235\242\350\257\225\351\242\23057-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/\351\235\242\350\257\225\351\242\23057-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/\351\235\242\350\257\225\351\242\23057-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" new file mode 100644 index 0000000..cc91968 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23057-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/\351\235\242\350\257\225\351\242\23057-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" @@ -0,0 +1,22 @@ +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/\351\235\242\350\257\225\351\242\23057.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23057-\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227.py" "b/\351\235\242\350\257\225\351\242\23057.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23057-\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..b1a4b6d --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23057.\345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/\351\235\242\350\257\225\351\242\23057-\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,17 @@ +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/\351\235\242\350\257\225\351\242\23058-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\351\235\242\350\257\225\351\242\23058-II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" "b/\351\235\242\350\257\225\351\242\23058-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\351\235\242\350\257\225\351\242\23058-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..8e4cadf --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23058-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/\351\235\242\350\257\225\351\242\23058-II-\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.py" @@ -0,0 +1,8 @@ +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/\351\235\242\350\257\225\351\242\23059-II.\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/\351\235\242\350\257\225\351\242\23059-II-\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274.py" "b/\351\235\242\350\257\225\351\242\23059-II.\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/\351\235\242\350\257\225\351\242\23059-II-\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274.py" new file mode 100644 index 0000000..8954ae7 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23059-II.\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/\351\235\242\350\257\225\351\242\23059-II-\351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274.py" @@ -0,0 +1,35 @@ +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/\351\235\242\350\257\225\351\242\23062.\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/\351\235\242\350\257\225\351\242\23062-\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/\351\235\242\350\257\225\351\242\23062.\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/\351\235\242\350\257\225\351\242\23062-\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" new file mode 100644 index 0000000..9690d6d --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23062.\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/\351\235\242\350\257\225\351\242\23062-\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" @@ -0,0 +1,10 @@ +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/\351\235\242\350\257\225\351\242\23063.\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/\351\235\242\350\257\225\351\242\23063-\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246.py" "b/\351\235\242\350\257\225\351\242\23063.\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/\351\235\242\350\257\225\351\242\23063-\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246.py" new file mode 100644 index 0000000..325ada7 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23063.\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/\351\235\242\350\257\225\351\242\23063-\350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246.py" @@ -0,0 +1,12 @@ +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/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n/\351\235\242\350\257\225\351\242\23064-\346\261\2021+2+\342\200\246+n.py" "b/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n/\351\235\242\350\257\225\351\242\23064-\346\261\2021+2+\342\200\246+n.py" new file mode 100644 index 0000000..c8160ca --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23064.\346\261\2021+2+\342\200\246+n/\351\235\242\350\257\225\351\242\23064-\346\261\2021+2+\342\200\246+n.py" @@ -0,0 +1,7 @@ +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/\351\235\242\350\257\225\351\242\23065.\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/\351\235\242\350\257\225\351\242\23065-\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/\351\235\242\350\257\225\351\242\23065.\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/\351\235\242\350\257\225\351\242\23065-\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" new file mode 100644 index 0000000..02652bd --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23065.\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/\351\235\242\350\257\225\351\242\23065-\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" @@ -0,0 +1,8 @@ +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\23066.\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\23066-\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204.py" "b/\351\235\242\350\257\225\351\242\23066.\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\23066-\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204.py" new file mode 100644 index 0000000..5c9dd47 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23066.\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/\351\235\242\350\257\225\351\242\23066-\346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204.py" @@ -0,0 +1,15 @@ +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/\351\235\242\350\257\225\351\242\23068-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/\351\235\242\350\257\225\351\242\23068-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/\351\235\242\350\257\225\351\242\23068-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/\351\235\242\350\257\225\351\242\23068-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" new file mode 100644 index 0000000..a00d561 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23068-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/\351\235\242\350\257\225\351\242\23068-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" @@ -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 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/\351\235\242\350\257\225\351\242\23068-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/\351\235\242\350\257\225\351\242\23068-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/\351\235\242\350\257\225\351\242\23068-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/\351\235\242\350\257\225\351\242\23068-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" new file mode 100644 index 0000000..09cbf64 --- /dev/null +++ "b/\351\235\242\350\257\225\351\242\23068-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/\351\235\242\350\257\225\351\242\23068-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" @@ -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 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) + + if left and right: + return root + return left or right \ No newline at end of file