diff --git a/lcci/01.02.Check Permutation/README.md b/lcci/01.02.Check Permutation/README.md index 7402ae788a509..5a945177279ca 100644 --- a/lcci/01.02.Check Permutation/README.md +++ b/lcci/01.02.Check Permutation/README.md @@ -44,7 +44,7 @@ class Solution: n1, n2 = len(s1), len(s2) if n1 != n2: return False - counter = collections.Counter() + counter = Counter() for i in range(n1): counter[s1[i]] += 1 counter[s2[i]] -= 1 @@ -84,16 +84,18 @@ class Solution { ### **JavaScript** ```js -var CheckPermutation = function(s1, s2) { - let n1 = s1.length, n2 = s2.length; - if (n1 != n2) return false; - let counter = {}; - for (let i = 0; i < n1; i++) { - let cur1 = s1.charAt(i), cur2 = s2.charAt(i); - counter[cur1] = (counter[cur1] || 0) + 1; - counter[cur2] = (counter[cur2] || 0) - 1; - } - return Object.values(counter).every(v => v == 0); +var CheckPermutation = function (s1, s2) { + let n1 = s1.length, + n2 = s2.length; + if (n1 != n2) return false; + let counter = {}; + for (let i = 0; i < n1; i++) { + let cur1 = s1.charAt(i), + cur2 = s2.charAt(i); + counter[cur1] = (counter[cur1] || 0) + 1; + counter[cur2] = (counter[cur2] || 0) - 1; + } + return Object.values(counter).every((v) => v == 0); }; ``` diff --git a/lcci/01.02.Check Permutation/README_EN.md b/lcci/01.02.Check Permutation/README_EN.md index 2d25055957a97..f383e3844b13d 100644 --- a/lcci/01.02.Check Permutation/README_EN.md +++ b/lcci/01.02.Check Permutation/README_EN.md @@ -44,7 +44,7 @@ class Solution: n1, n2 = len(s1), len(s2) if n1 != n2: return False - counter = collections.Counter() + counter = Counter() for i in range(n1): counter[s1[i]] += 1 counter[s2[i]] -= 1 @@ -82,16 +82,18 @@ class Solution { ### **JavaScript** ```js -var CheckPermutation = function(s1, s2) { - let n1 = s1.length, n2 = s2.length; - if (n1 != n2) return false; - let counter = {}; - for (let i = 0; i < n1; i++) { - let cur1 = s1.charAt(i), cur2 = s2.charAt(i); - counter[cur1] = (counter[cur1] || 0) + 1; - counter[cur2] = (counter[cur2] || 0) - 1; - } - return Object.values(counter).every(v => v == 0); +var CheckPermutation = function (s1, s2) { + let n1 = s1.length, + n2 = s2.length; + if (n1 != n2) return false; + let counter = {}; + for (let i = 0; i < n1; i++) { + let cur1 = s1.charAt(i), + cur2 = s2.charAt(i); + counter[cur1] = (counter[cur1] || 0) + 1; + counter[cur2] = (counter[cur2] || 0) - 1; + } + return Object.values(counter).every((v) => v == 0); }; ``` diff --git a/lcci/01.02.Check Permutation/Solution.py b/lcci/01.02.Check Permutation/Solution.py index e0bfea3e3d228..43df02dfa1594 100644 --- a/lcci/01.02.Check Permutation/Solution.py +++ b/lcci/01.02.Check Permutation/Solution.py @@ -3,7 +3,7 @@ def CheckPermutation(self, s1: str, s2: str) -> bool: n1, n2 = len(s1), len(s2) if n1 != n2: return False - counter = collections.Counter() + counter = Counter() for i in range(n1): counter[s1[i]] += 1 counter[s2[i]] -= 1 diff --git a/lcci/01.04.Palindrome Permutation/README.md b/lcci/01.04.Palindrome Permutation/README.md index 7802b84befadc..9bee21c1d9643 100644 --- a/lcci/01.04.Palindrome Permutation/README.md +++ b/lcci/01.04.Palindrome Permutation/README.md @@ -36,7 +36,7 @@ ```python class Solution: def canPermutePalindrome(self, s: str) -> bool: - counter = collections.Counter(s) + counter = Counter(s) cnt = 0 for val in counter.values(): if (val & 1) == 1: diff --git a/lcci/01.04.Palindrome Permutation/README_EN.md b/lcci/01.04.Palindrome Permutation/README_EN.md index a79722fbaef6e..0331fa3f66680 100644 --- a/lcci/01.04.Palindrome Permutation/README_EN.md +++ b/lcci/01.04.Palindrome Permutation/README_EN.md @@ -27,7 +27,7 @@ ```python class Solution: def canPermutePalindrome(self, s: str) -> bool: - counter = collections.Counter(s) + counter = Counter(s) cnt = 0 for val in counter.values(): if (val & 1) == 1: diff --git a/lcci/01.04.Palindrome Permutation/Solution.py b/lcci/01.04.Palindrome Permutation/Solution.py index f3aec3ee06e3d..b299afc96a4c0 100644 --- a/lcci/01.04.Palindrome Permutation/Solution.py +++ b/lcci/01.04.Palindrome Permutation/Solution.py @@ -1,6 +1,6 @@ class Solution: def canPermutePalindrome(self, s: str) -> bool: - counter = collections.Counter(s) + counter = Counter(s) cnt = 0 for val in counter.values(): if (val & 1) == 1: diff --git a/lcci/10.02.Group Anagrams/README.md b/lcci/10.02.Group Anagrams/README.md index 11d5e30e63fef..78ca03d879025 100644 --- a/lcci/10.02.Group Anagrams/README.md +++ b/lcci/10.02.Group Anagrams/README.md @@ -49,7 +49,7 @@ ```python class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - chars = collections.defaultdict(list) + chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) diff --git a/lcci/10.02.Group Anagrams/README_EN.md b/lcci/10.02.Group Anagrams/README_EN.md index 761ba91527824..04ce0588b9001 100644 --- a/lcci/10.02.Group Anagrams/README_EN.md +++ b/lcci/10.02.Group Anagrams/README_EN.md @@ -48,7 +48,7 @@ ```python class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - chars = collections.defaultdict(list) + chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) diff --git a/lcci/10.02.Group Anagrams/Solution.py b/lcci/10.02.Group Anagrams/Solution.py index 3055679df73c6..b93f9a724aeba 100644 --- a/lcci/10.02.Group Anagrams/Solution.py +++ b/lcci/10.02.Group Anagrams/Solution.py @@ -1,6 +1,6 @@ class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - chars = collections.defaultdict(list) + chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) diff --git a/lcci/16.02.Words Frequency/README.md b/lcci/16.02.Words Frequency/README.md index 44725da6887d8..b00b4d1ecf39d 100644 --- a/lcci/16.02.Words Frequency/README.md +++ b/lcci/16.02.Words Frequency/README.md @@ -43,7 +43,7 @@ wordsFrequency.get("pen"); //返回1 class WordsFrequency: def __init__(self, book: List[str]): - self.counter = collections.Counter(book) + self.counter = Counter(book) def get(self, word: str) -> int: return self.counter[word] diff --git a/lcci/16.02.Words Frequency/README_EN.md b/lcci/16.02.Words Frequency/README_EN.md index 2ab0dbee36255..f77bbfbea32f7 100644 --- a/lcci/16.02.Words Frequency/README_EN.md +++ b/lcci/16.02.Words Frequency/README_EN.md @@ -50,7 +50,7 @@ wordsFrequency.get("pen"); //returns 1 class WordsFrequency: def __init__(self, book: List[str]): - self.counter = collections.Counter(book) + self.counter = Counter(book) def get(self, word: str) -> int: return self.counter[word] diff --git a/lcci/16.02.Words Frequency/Solution.py b/lcci/16.02.Words Frequency/Solution.py index bcf7572cfb5f6..5869dc8cefeaf 100644 --- a/lcci/16.02.Words Frequency/Solution.py +++ b/lcci/16.02.Words Frequency/Solution.py @@ -1,11 +1,11 @@ class WordsFrequency: def __init__(self, book: List[str]): - self.counter = collections.Counter(book) + self.counter = Counter(book) def get(self, word: str) -> int: return self.counter[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 +# param_1 = obj.get(word) diff --git a/lcci/17.07.Baby Names/README.md b/lcci/17.07.Baby Names/README.md index d75ba9492ecb0..cb1b80c853317 100644 --- a/lcci/17.07.Baby Names/README.md +++ b/lcci/17.07.Baby Names/README.md @@ -35,14 +35,14 @@ ```python class Solution: def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]: - mp = collections.defaultdict(int) - p = collections.defaultdict(str) + mp = defaultdict(int) + p = defaultdict(str) def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] - + def union(a, b): pa, pb = find(a), find(b) if pa == pb: diff --git a/lcci/17.07.Baby Names/README_EN.md b/lcci/17.07.Baby Names/README_EN.md index 72704c15e4875..fa62ebf034751 100644 --- a/lcci/17.07.Baby Names/README_EN.md +++ b/lcci/17.07.Baby Names/README_EN.md @@ -29,14 +29,14 @@ ```python class Solution: def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]: - mp = collections.defaultdict(int) - p = collections.defaultdict(str) + mp = defaultdict(int) + p = defaultdict(str) def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] - + def union(a, b): pa, pb = find(a), find(b) if pa == pb: diff --git a/lcci/17.07.Baby Names/Solution.py b/lcci/17.07.Baby Names/Solution.py index d1a918c5a2b9a..f0ed6b6034634 100644 --- a/lcci/17.07.Baby Names/Solution.py +++ b/lcci/17.07.Baby Names/Solution.py @@ -1,7 +1,7 @@ class Solution: def trulyMostPopular(self, names: List[str], synonyms: List[str]) -> List[str]: - mp = collections.defaultdict(int) - p = collections.defaultdict(str) + mp = defaultdict(int) + p = defaultdict(str) def find(x): if p[x] != x: diff --git "a/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" index d13c9ddfd046a..60d6a934c367d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" @@ -52,7 +52,7 @@ class Codec: """ if not root: return '[]' - queue = collections.deque() + queue = deque() queue.append(root) res = [] while queue: @@ -74,7 +74,7 @@ class Codec: """ if not data or data == '[]': return None - queue = collections.deque() + queue = deque() nodes = data[1:-1].split(',') root = TreeNode(nodes[0]) queue.append(root) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.py" "b/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.py" index f2178846ee999..e9ef19eb03d74 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.py" +++ "b/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.py" @@ -9,13 +9,13 @@ class Codec: def serialize(self, root): """Encodes a tree to a single string. - + :type root: TreeNode :rtype: str """ if not root: return '[]' - queue = collections.deque() + queue = deque() queue.append(root) res = [] while queue: @@ -27,17 +27,16 @@ def serialize(self, root): else: res.append('null') return f'[{",".join(res)}]' - def deserialize(self, data): """Decodes your encoded data to tree. - + :type data: str :rtype: TreeNode """ if not data or data == '[]': return None - queue = collections.deque() + queue = deque() nodes = data[1:-1].split(',') root = TreeNode(nodes[0]) queue.append(root) @@ -57,4 +56,4 @@ def deserialize(self, data): # Your Codec object will be instantiated and called as such: # codec = Codec() -# codec.deserialize(codec.serialize(root)) \ No newline at end of file +# codec.deserialize(codec.serialize(root)) diff --git "a/lcof/\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/README.md" "b/lcof/\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/README.md" index 2f3fe60342437..17ebc6eed9b54 100644 --- "a/lcof/\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/README.md" +++ "b/lcof/\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/README.md" @@ -33,7 +33,7 @@ s = "" ```python class Solution: def firstUniqChar(self, s: str) -> str: - counter = collections.Counter(s) + counter = Counter(s) for c in s: if counter[c] == 1: return c diff --git "a/lcof/\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/Solution.py" "b/lcof/\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/Solution.py" index da9d31dd1a18f..f66835dfd3413 100644 --- "a/lcof/\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/Solution.py" +++ "b/lcof/\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/Solution.py" @@ -1,6 +1,6 @@ class Solution: def firstUniqChar(self, s: str) -> str: - counter = collections.Counter(s) + counter = Counter(s) for c in s: if counter[c] == 1: return c diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" index d7ebc66f98a55..ea4fc1cc8a526 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" @@ -57,7 +57,7 @@ for i in range(n): ```python class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: - q, res = collections.deque(), [] + q, res = deque(), [] for i, num in enumerate(nums): if q and i - k + 1 > q[0]: q.popleft() diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.py" "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.py" index 7236c6eded818..c99b9934ec950 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.py" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.py" @@ -1,6 +1,6 @@ class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: - q, res = collections.deque(), [] + q, res = deque(), [] for i, num in enumerate(nums): if q and i - k + 1 > q[0]: q.popleft() diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/README.md" index 884c327234f1a..001e115efad21 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/README.md" @@ -66,7 +66,7 @@ ```python class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - chars = collections.defaultdict(list) + chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/Solution.py" index 3055679df73c6..b93f9a724aeba 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/Solution.py" @@ -1,6 +1,6 @@ class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - chars = collections.defaultdict(list) + chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/README.md" index 3d6140fd7c0ac..6c4a3a11dcacb 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/README.md" @@ -48,7 +48,6 @@ recentCounter.ping(3002); // requests = [1, 100, 3001<

注意:本题与主站 933 题相同: https://leetcode-cn.com/problems/number-of-recent-calls/

- ## 解法 @@ -67,7 +66,7 @@ recentCounter.ping(3002); // requests = [1, 100, 3001< class RecentCounter: def __init__(self): - self.q = collections.deque() + self.q = deque() def ping(self, t: int) -> int: self.q.append(t) @@ -92,7 +91,7 @@ class RecentCounter { public RecentCounter() { q = new LinkedList<>(); } - + public int ping(int t) { q.offerLast(t); while (q.peekFirst() < t - 3000) { @@ -119,7 +118,7 @@ public: RecentCounter() { } - + int ping(int t) { q.push_back(t); while (q.front() < t - 3000) { @@ -167,20 +166,20 @@ func (this *RecentCounter) Ping(t int) int { ### **JavaScript** ```js -var RecentCounter = function() { - this.q = []; +var RecentCounter = function () { + this.q = []; }; -/** +/** * @param {number} t * @return {number} */ -RecentCounter.prototype.ping = function(t) { - this.q.push(t); - while (this.q[0] < t - 3000) { - this.q.shift(); - } - return this.q.length; +RecentCounter.prototype.ping = function (t) { + this.q.push(t); + while (this.q[0] < t - 3000) { + this.q.shift(); + } + return this.q.length; }; /** diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/Solution.py" index 9c7d308401f2f..21fe7a59b334b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/Solution.py" @@ -1,7 +1,7 @@ class RecentCounter: def __init__(self): - self.q = collections.deque() + self.q = deque() def ping(self, t: int) -> int: self.q.append(t) @@ -12,4 +12,4 @@ def ping(self, t: int) -> int: # Your RecentCounter object will be instantiated and called as such: # obj = RecentCounter() -# param_1 = obj.ping(t) \ No newline at end of file +# param_1 = obj.ping(t) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/README.md" index 45d9464ee637e..2a01c775332ce 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/README.md" @@ -47,7 +47,6 @@

注意:本题与主站 919 题相同: https://leetcode-cn.com/problems/complete-binary-tree-inserter/

- ## 解法 @@ -69,7 +68,7 @@ class CBTInserter: def __init__(self, root: TreeNode): self.tree = [] - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for _ in range(n): diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/Solution.py" index c35288cb8761d..8f93893d8a904 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/Solution.py" @@ -8,7 +8,7 @@ class CBTInserter: def __init__(self, root: TreeNode): self.tree = [] - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for _ in range(n): @@ -36,4 +36,4 @@ def get_root(self) -> TreeNode: # Your CBTInserter object will be instantiated and called as such: # obj = CBTInserter(root) # param_1 = obj.insert(val) -# param_2 = obj.get_root() \ No newline at end of file +# param_2 = obj.get_root() diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/README.md" index 044f5d5c53e39..57c94cf3b81f2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/README.md" @@ -59,7 +59,6 @@ magicDictionary.search("leetcoded"); // 返回 False

注意:本题与主站 676 题相同: https://leetcode-cn.com/problems/implement-magic-dictionary/

- ## 解法 @@ -85,7 +84,7 @@ class MagicDictionary: def buildDict(self, dictionary: List[str]) -> None: self.words = set(dictionary) - self.counter = collections.Counter( + self.counter = Counter( p for word in dictionary for p in self._patterns(word)) def search(self, searchWord: str) -> bool: @@ -115,7 +114,7 @@ class MagicDictionary { words = new HashSet<>(); counter = new HashMap<>(); } - + public void buildDict(String[] dictionary) { for (String word : dictionary) { words.add(word); @@ -124,7 +123,7 @@ class MagicDictionary { } } } - + public boolean search(String searchWord) { for (String p : patterns(searchWord)) { int cnt = counter.getOrDefault(p, 0); @@ -165,7 +164,7 @@ public: MagicDictionary() { } - + void buildDict(vector dictionary) { for (string word : dictionary) { @@ -173,7 +172,7 @@ public: for (string p : patterns(word)) ++counter[p]; } } - + bool search(string searchWord) { for (string p : patterns(searchWord)) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/Solution.py" index 282726c1c88dd..c37bb9f8a5707 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/Solution.py" @@ -10,7 +10,7 @@ def _patterns(self, word): def buildDict(self, dictionary: List[str]) -> None: self.words = set(dictionary) - self.counter = collections.Counter( + self.counter = Counter( p for word in dictionary for p in self._patterns(word)) def search(self, searchWord: str) -> bool: diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" index 3ace0a19956f4..10310d0f8d6b9 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" @@ -46,7 +46,6 @@ mapSum.sum("ap"); // return 5 (apple + app = 3

注意:本题与主站 677 题相同: https://leetcode-cn.com/problems/map-sum-pairs/

- ## 解法 @@ -66,8 +65,8 @@ class MapSum: """ Initialize your data structure here. """ - self.data = collections.defaultdict(int) - self.t = collections.defaultdict(int) + self.data = defaultdict(int) + self.t = defaultdict(int) def insert(self, key: str, val: int) -> None: old = self.t[key] diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.py" index 458a397171292..b4a8b14b1b32a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.py" @@ -4,8 +4,8 @@ def __init__(self): """ Initialize your data structure here. """ - self.data = collections.defaultdict(int) - self.t = collections.defaultdict(int) + self.data = defaultdict(int) + self.t = defaultdict(int) def insert(self, key: str, val: int) -> None: old = self.t[key] @@ -20,4 +20,4 @@ def sum(self, prefix: str) -> int: # Your MapSum object will be instantiated and called as such: # obj = MapSum() # obj.insert(key,val) -# param_2 = obj.sum(prefix) \ No newline at end of file +# param_2 = obj.sum(prefix) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/README.md" index dcb477a321ed0..37b425d6a6b70 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/README.md" @@ -69,12 +69,12 @@ ```python class Solution: def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * numCourses for i, j in prerequisites: edges[j].append(i) indegree[i] += 1 - q = collections.deque() + q = deque() for i in range(numCourses): if indegree[i] == 0: q.append(i) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/Solution.py" index 2a72610e5d41b..bc7c486fa456b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/Solution.py" @@ -1,11 +1,11 @@ class Solution: def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * numCourses for i, j in prerequisites: edges[j].append(i) indegree[i] += 1 - q = collections.deque() + q = deque() for i in range(numCourses): if indegree[i] == 0: q.append(i) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/README.md" index f05c6c2e5991d..050221aaa8e3e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/README.md" @@ -56,7 +56,6 @@

注意:本题与主站 444 题相同:https://leetcode-cn.com/problems/sequence-reconstruction/

- ## 解法 @@ -82,7 +81,7 @@ class Solution: if len(nums) < n: return False - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * (n + 1) for seq in seqs: i = seq[0] @@ -90,7 +89,7 @@ class Solution: edges[i].append(j) indegree[j] += 1 i = j - q = collections.deque() + q = deque() for i in range(1, n + 1): if indegree[i] == 0: q.append(i) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/Solution.py" index 596223f947b75..e2494dcff35f6 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/Solution.py" @@ -10,7 +10,7 @@ def sequenceReconstruction(self, org: List[int], seqs: List[List[int]]) -> bool: if len(nums) < n: return False - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * (n + 1) for seq in seqs: i = seq[0] @@ -18,7 +18,7 @@ def sequenceReconstruction(self, org: List[int], seqs: List[List[int]]) -> bool: edges[i].append(j) indegree[j] += 1 i = j - q = collections.deque() + q = deque() for i in range(1, n + 1): if indegree[i] == 0: q.append(i) diff --git "a/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/README.md" "b/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/README.md" index 5c1ff56adca12..60cd201c8ee64 100644 --- "a/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/README.md" +++ "b/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/README.md" @@ -49,7 +49,7 @@ ```python class Solution: def halfQuestions(self, questions: List[int]) -> int: - counter = collections.Counter(questions) + counter = Counter(questions) counter = OrderedDict(counter.most_common()) n = len(questions) >> 1 res = 0 diff --git "a/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/Solution.py" "b/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/Solution.py" index 203c28e4067e2..3ed4b153639e2 100644 --- "a/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/Solution.py" +++ "b/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/Solution.py" @@ -1,6 +1,6 @@ class Solution: def halfQuestions(self, questions: List[int]) -> int: - counter = collections.Counter(questions) + counter = Counter(questions) counter = OrderedDict(counter.most_common()) n = len(questions) >> 1 res = 0 diff --git "a/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/README.md" "b/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/README.md" index 9f10cd7a91d0b..d53d1d32132dc 100644 --- "a/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/README.md" +++ "b/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/README.md" @@ -123,7 +123,7 @@ class Solution: if grid[i + x][j + y] == '0' or grid[i][j] == grid[i + x][j + y]: p[find(i * n + j)] = find((i + x) * n + j + y) - mp = collections.defaultdict(int) + mp = defaultdict(int) res = 0 for i in range(m): for j in range(n): @@ -201,7 +201,7 @@ public: { for (int j = 0; j < n; ++j) { - if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i][j] == '0') + if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i][j] == '0') p[find(i * n + j)] = find(m * n); else { diff --git "a/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/Solution.py" "b/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/Solution.py" index 698b809a70442..2bee6ba59e4cc 100644 --- "a/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/Solution.py" +++ "b/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/Solution.py" @@ -17,7 +17,7 @@ def find(x): if grid[i + x][j + y] == '0' or grid[i][j] == grid[i + x][j + y]: p[find(i * n + j)] = find((i + x) * n + j + y) - mp = collections.defaultdict(int) + mp = defaultdict(int) res = 0 for i in range(m): for j in range(n): diff --git a/solution/0000-0099/0049.Group Anagrams/README.md b/solution/0000-0099/0049.Group Anagrams/README.md index c89dcfc50b4d2..e5a7581c5db88 100644 --- a/solution/0000-0099/0049.Group Anagrams/README.md +++ b/solution/0000-0099/0049.Group Anagrams/README.md @@ -48,7 +48,7 @@ ```python class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - chars = collections.defaultdict(list) + chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) @@ -78,17 +78,17 @@ class Solution { ```ts function groupAnagrams(strs: string[]): string[][] { - let map = new Map(); - for (let str of strs) { - let arr = str.split(''); - arr.sort(); - let key = arr.join(''); - let value = map.get(key) ? map.get(key) : []; - value.push(str); - map.set(key, value); - } - return Array.from(map.values()); -}; + let map = new Map(); + for (let str of strs) { + let arr = str.split(""); + arr.sort(); + let key = arr.join(""); + let value = map.get(key) ? map.get(key) : []; + value.push(str); + map.set(key, value); + } + return Array.from(map.values()); +} ``` ### **C++** diff --git a/solution/0000-0099/0049.Group Anagrams/README_EN.md b/solution/0000-0099/0049.Group Anagrams/README_EN.md index 7b1a2560320b4..5ddce7799ca0e 100644 --- a/solution/0000-0099/0049.Group Anagrams/README_EN.md +++ b/solution/0000-0099/0049.Group Anagrams/README_EN.md @@ -37,7 +37,7 @@ ```python class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - chars = collections.defaultdict(list) + chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) @@ -65,17 +65,17 @@ class Solution { ```ts function groupAnagrams(strs: string[]): string[][] { - let map = new Map(); - for (let str of strs) { - let arr = str.split(''); - arr.sort(); - let key = arr.join(''); - let value = map.get(key) ? map.get(key) : []; - value.push(str); - map.set(key, value); - } - return Array.from(map.values()); -}; + let map = new Map(); + for (let str of strs) { + let arr = str.split(""); + arr.sort(); + let key = arr.join(""); + let value = map.get(key) ? map.get(key) : []; + value.push(str); + map.set(key, value); + } + return Array.from(map.values()); +} ``` ### **C++** diff --git a/solution/0000-0099/0049.Group Anagrams/Solution.py b/solution/0000-0099/0049.Group Anagrams/Solution.py index 3055679df73c6..b93f9a724aeba 100644 --- a/solution/0000-0099/0049.Group Anagrams/Solution.py +++ b/solution/0000-0099/0049.Group Anagrams/Solution.py @@ -1,6 +1,6 @@ class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - chars = collections.defaultdict(list) + chars = defaultdict(list) for s in strs: k = ''.join(sorted(list(s))) chars[k].append(s) diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md index 5dc8bacf1eced..f037e51423257 100644 --- a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md +++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md @@ -77,7 +77,7 @@ class Solution: def connect(self, root: 'Node') -> 'Node': if root is None or (root.left is None and root.right is None): return root - q = collections.deque([root]) + q = deque([root]) while q: size = len(q) cur = None diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md index bb55c9d4dc9b9..88f054b1cedaf 100644 --- a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md +++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md @@ -81,7 +81,7 @@ class Solution: def connect(self, root: 'Node') -> 'Node': if root is None or (root.left is None and root.right is None): return root - q = collections.deque([root]) + q = deque([root]) while q: size = len(q) cur = None diff --git a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.py b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.py index 794ab2d74053b..479ec24538697 100644 --- a/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.py +++ b/solution/0100-0199/0116.Populating Next Right Pointers in Each Node/Solution.py @@ -8,11 +8,12 @@ def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next self.next = next """ + class Solution: def connect(self, root: 'Node') -> 'Node': if root is None or (root.left is None and root.right is None): return root - q = collections.deque([root]) + q = deque([root]) while q: size = len(q) cur = None diff --git a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md index db2f646667508..77a2b95116ee5 100644 --- a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md +++ b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README.md @@ -81,7 +81,7 @@ class Solution: def connect(self, root: 'Node') -> 'Node': if root is None or (root.left is None and root.right is None): return root - q = collections.deque([root]) + q = deque([root]) while q: size = len(q) cur = None diff --git a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md index 2c903a3e1da27..a47224bcc1e7f 100644 --- a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md +++ b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md @@ -75,7 +75,7 @@ class Solution: def connect(self, root: 'Node') -> 'Node': if root is None or (root.left is None and root.right is None): return root - q = collections.deque([root]) + q = deque([root]) while q: size = len(q) cur = None diff --git a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.py b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.py index 794ab2d74053b..479ec24538697 100644 --- a/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.py +++ b/solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/Solution.py @@ -8,11 +8,12 @@ def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next self.next = next """ + class Solution: def connect(self, root: 'Node') -> 'Node': if root is None or (root.left is None and root.right is None): return root - q = collections.deque([root]) + q = deque([root]) while q: size = len(q) cur = None diff --git a/solution/0100-0199/0149.Max Points on a Line/README.md b/solution/0100-0199/0149.Max Points on a Line/README.md index 56f66d36672d5..1b2cf5f047f8b 100644 --- a/solution/0100-0199/0149.Max Points on a Line/README.md +++ b/solution/0100-0199/0149.Max Points on a Line/README.md @@ -63,7 +63,7 @@ class Solution: return n res = 0 for i in range(n - 1): - counter = collections.Counter() + counter = Counter() t_max = duplicate = 0 for j in range(i + 1, n): delta_x = points[i][0] - points[j][0] diff --git a/solution/0100-0199/0149.Max Points on a Line/README_EN.md b/solution/0100-0199/0149.Max Points on a Line/README_EN.md index 1800183809868..7501374e70920 100644 --- a/solution/0100-0199/0149.Max Points on a Line/README_EN.md +++ b/solution/0100-0199/0149.Max Points on a Line/README_EN.md @@ -48,7 +48,7 @@ class Solution: return n res = 0 for i in range(n - 1): - counter = collections.Counter() + counter = Counter() t_max = duplicate = 0 for j in range(i + 1, n): delta_x = points[i][0] - points[j][0] diff --git a/solution/0100-0199/0149.Max Points on a Line/Solution.py b/solution/0100-0199/0149.Max Points on a Line/Solution.py index 895cd846407e9..cf00afc0dc698 100644 --- a/solution/0100-0199/0149.Max Points on a Line/Solution.py +++ b/solution/0100-0199/0149.Max Points on a Line/Solution.py @@ -2,13 +2,13 @@ class Solution: def maxPoints(self, points: List[List[int]]) -> int: def gcd(a, b) -> int: return a if b == 0 else gcd(b, a % b) - + n = len(points) if n < 3: return n res = 0 for i in range(n - 1): - counter = collections.Counter() + counter = Counter() t_max = duplicate = 0 for j in range(i + 1, n): delta_x = points[i][0] - points[j][0] diff --git a/solution/0100-0199/0170.Two Sum III - Data structure design/README.md b/solution/0100-0199/0170.Two Sum III - Data structure design/README.md index 6a81de6c1c3f0..46e1192ce5262 100644 --- a/solution/0100-0199/0170.Two Sum III - Data structure design/README.md +++ b/solution/0100-0199/0170.Two Sum III - Data structure design/README.md @@ -64,7 +64,7 @@ class TwoSum: """ Initialize your data structure here. """ - self.counter = collections.Counter() + self.counter = Counter() def add(self, number: int) -> None: """ @@ -104,12 +104,12 @@ class TwoSum { public TwoSum() { counter = new HashMap<>(); } - + /** Add the number to an internal data structure.. */ public void add(int number) { counter.put(number, counter.getOrDefault(number, 0) + 1); } - + /** Find if there exists any pair of numbers which sum is equal to the value. */ public boolean find(int value) { for (int num : counter.keySet()) { diff --git a/solution/0100-0199/0170.Two Sum III - Data structure design/README_EN.md b/solution/0100-0199/0170.Two Sum III - Data structure design/README_EN.md index 9bdf5915d4b00..1ac0950905ce9 100644 --- a/solution/0100-0199/0170.Two Sum III - Data structure design/README_EN.md +++ b/solution/0100-0199/0170.Two Sum III - Data structure design/README_EN.md @@ -55,7 +55,7 @@ class TwoSum: """ Initialize your data structure here. """ - self.counter = collections.Counter() + self.counter = Counter() def add(self, number: int) -> None: """ @@ -93,12 +93,12 @@ class TwoSum { public TwoSum() { counter = new HashMap<>(); } - + /** Add the number to an internal data structure.. */ public void add(int number) { counter.put(number, counter.getOrDefault(number, 0) + 1); } - + /** Find if there exists any pair of numbers which sum is equal to the value. */ public boolean find(int value) { for (int num : counter.keySet()) { diff --git a/solution/0100-0199/0170.Two Sum III - Data structure design/Solution.py b/solution/0100-0199/0170.Two Sum III - Data structure design/Solution.py index db27316a1c270..b64e03dd825e7 100644 --- a/solution/0100-0199/0170.Two Sum III - Data structure design/Solution.py +++ b/solution/0100-0199/0170.Two Sum III - Data structure design/Solution.py @@ -4,7 +4,7 @@ def __init__(self): """ Initialize your data structure here. """ - self.counter = collections.Counter() + self.counter = Counter() def add(self, number: int) -> None: """ @@ -29,4 +29,4 @@ def find(self, value: int) -> bool: # 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 +# param_2 = obj.find(value) diff --git a/solution/0100-0199/0187.Repeated DNA Sequences/README.md b/solution/0100-0199/0187.Repeated DNA Sequences/README.md index 271b78d0eab50..b76099f5156d0 100644 --- a/solution/0100-0199/0187.Repeated DNA Sequences/README.md +++ b/solution/0100-0199/0187.Repeated DNA Sequences/README.md @@ -1,4 +1,4 @@ -# [187. 重复的DNA序列](https://leetcode-cn.com/problems/repeated-dna-sequences) +# [187. 重复的 DNA 序列](https://leetcode-cn.com/problems/repeated-dna-sequences) [English Version](/solution/0100-0199/0187.Repeated%20DNA%20Sequences/README_EN.md) @@ -35,7 +35,6 @@
  • s[i]'A''C''G''T'
  • - ## 解法 @@ -50,7 +49,7 @@ class Solution: def findRepeatedDnaSequences(self, s: str) -> List[str]: n = len(s) - 10 - cnt = collections.Counter() + cnt = Counter() ans = [] for i in range(n + 1): sub = s[i: i + 10] @@ -89,18 +88,18 @@ class Solution { * @param {string} s * @return {string[]} */ -var findRepeatedDnaSequences = function(s) { - const n = s.length - 10; - let cnt = new Map(); - let ans = []; - for (let i = 0; i <= n; ++i) { - let sub = s.slice(i, i + 10); - cnt[sub] = (cnt[sub] || 0) + 1; - if (cnt[sub] == 2) { - ans.push(sub); - } +var findRepeatedDnaSequences = function (s) { + const n = s.length - 10; + let cnt = new Map(); + let ans = []; + for (let i = 0; i <= n; ++i) { + let sub = s.slice(i, i + 10); + cnt[sub] = (cnt[sub] || 0) + 1; + if (cnt[sub] == 2) { + ans.push(sub); } - return ans; + } + return ans; }; ``` diff --git a/solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md b/solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md index 4578200f17767..ab415eb9e49b6 100644 --- a/solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md +++ b/solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md @@ -30,7 +30,6 @@
  • s[i] is either 'A', 'C', 'G', or 'T'.
  • - ## Solutions @@ -41,7 +40,7 @@ class Solution: def findRepeatedDnaSequences(self, s: str) -> List[str]: n = len(s) - 10 - cnt = collections.Counter() + cnt = Counter() ans = [] for i in range(n + 1): sub = s[i: i + 10] @@ -78,18 +77,18 @@ class Solution { * @param {string} s * @return {string[]} */ -var findRepeatedDnaSequences = function(s) { - const n = s.length - 10; - let cnt = new Map(); - let ans = []; - for (let i = 0; i <= n; ++i) { - let sub = s.slice(i, i + 10); - cnt[sub] = (cnt[sub] || 0) + 1; - if (cnt[sub] == 2) { - ans.push(sub); - } +var findRepeatedDnaSequences = function (s) { + const n = s.length - 10; + let cnt = new Map(); + let ans = []; + for (let i = 0; i <= n; ++i) { + let sub = s.slice(i, i + 10); + cnt[sub] = (cnt[sub] || 0) + 1; + if (cnt[sub] == 2) { + ans.push(sub); } - return ans; + } + return ans; }; ``` diff --git a/solution/0100-0199/0187.Repeated DNA Sequences/Solution.py b/solution/0100-0199/0187.Repeated DNA Sequences/Solution.py index ea6877c5d2e65..0fdfe2769be74 100644 --- a/solution/0100-0199/0187.Repeated DNA Sequences/Solution.py +++ b/solution/0100-0199/0187.Repeated DNA Sequences/Solution.py @@ -1,7 +1,7 @@ class Solution: def findRepeatedDnaSequences(self, s: str) -> List[str]: n = len(s) - 10 - cnt = collections.Counter() + cnt = Counter() ans = [] for i in range(n + 1): sub = s[i: i + 10] diff --git a/solution/0200-0299/0207.Course Schedule/README.md b/solution/0200-0299/0207.Course Schedule/README.md index 7260908da75b5..cff72ccec2c6f 100644 --- a/solution/0200-0299/0207.Course Schedule/README.md +++ b/solution/0200-0299/0207.Course Schedule/README.md @@ -59,12 +59,12 @@ ```python class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * numCourses for i, j in prerequisites: edges[j].append(i) indegree[i] += 1 - q = collections.deque() + q = deque() for i in range(numCourses): if indegree[i] == 0: q.append(i) diff --git a/solution/0200-0299/0207.Course Schedule/README_EN.md b/solution/0200-0299/0207.Course Schedule/README_EN.md index be08c025f9183..907839e5751b0 100644 --- a/solution/0200-0299/0207.Course Schedule/README_EN.md +++ b/solution/0200-0299/0207.Course Schedule/README_EN.md @@ -51,12 +51,12 @@ To take course 1 you should have finished course 0, and to take course 0 you sho ```python class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * numCourses for i, j in prerequisites: edges[j].append(i) indegree[i] += 1 - q = collections.deque() + q = deque() for i in range(numCourses): if indegree[i] == 0: q.append(i) diff --git a/solution/0200-0299/0207.Course Schedule/Solution.py b/solution/0200-0299/0207.Course Schedule/Solution.py index 74fd5d5423c98..df0ed59b21fd9 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.py +++ b/solution/0200-0299/0207.Course Schedule/Solution.py @@ -1,11 +1,11 @@ class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * numCourses for i, j in prerequisites: edges[j].append(i) indegree[i] += 1 - q = collections.deque() + q = deque() for i in range(numCourses): if indegree[i] == 0: q.append(i) diff --git a/solution/0200-0299/0210.Course Schedule II/README.md b/solution/0200-0299/0210.Course Schedule II/README.md index 65de1b85607f2..477adabb5e59a 100644 --- a/solution/0200-0299/0210.Course Schedule II/README.md +++ b/solution/0200-0299/0210.Course Schedule II/README.md @@ -60,12 +60,12 @@ ```python class Solution: def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * numCourses for i, j in prerequisites: edges[j].append(i) indegree[i] += 1 - q = collections.deque() + q = deque() for i in range(numCourses): if indegree[i] == 0: q.append(i) diff --git a/solution/0200-0299/0210.Course Schedule II/README_EN.md b/solution/0200-0299/0210.Course Schedule II/README_EN.md index abb1cf6148eda..8ded4ab51aca2 100644 --- a/solution/0200-0299/0210.Course Schedule II/README_EN.md +++ b/solution/0200-0299/0210.Course Schedule II/README_EN.md @@ -58,12 +58,12 @@ So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3]. ```python class Solution: def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * numCourses for i, j in prerequisites: edges[j].append(i) indegree[i] += 1 - q = collections.deque() + q = deque() for i in range(numCourses): if indegree[i] == 0: q.append(i) diff --git a/solution/0200-0299/0210.Course Schedule II/Solution.py b/solution/0200-0299/0210.Course Schedule II/Solution.py index 2a72610e5d41b..bc7c486fa456b 100644 --- a/solution/0200-0299/0210.Course Schedule II/Solution.py +++ b/solution/0200-0299/0210.Course Schedule II/Solution.py @@ -1,11 +1,11 @@ class Solution: def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * numCourses for i, j in prerequisites: edges[j].append(i) indegree[i] += 1 - q = collections.deque() + q = deque() for i in range(numCourses): if indegree[i] == 0: q.append(i) diff --git a/solution/0200-0299/0239.Sliding Window Maximum/README.md b/solution/0200-0299/0239.Sliding Window Maximum/README.md index a3436e9139599..0e45480cd84ba 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/README.md +++ b/solution/0200-0299/0239.Sliding Window Maximum/README.md @@ -65,7 +65,6 @@
  • 1 <= k <= nums.length
  • - ## 解法 @@ -94,7 +93,7 @@ for i in range(n): ```python class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: - q, res = collections.deque(), [] + q, res = deque(), [] for i, num in enumerate(nums): if q and i - k + 1 > q[0]: q.popleft() @@ -145,24 +144,22 @@ class Solution { * @return {number[]} */ var maxSlidingWindow = function (nums, k) { - let len = nums.length; - if (len < k) return []; - let res = [], win = []; - for (let i = 0; i < k; i++) { - while (win.length > 0 && nums[i] >= nums[win[win.length - 1]]) - win.pop(); - win.push(i); - } + let len = nums.length; + if (len < k) return []; + let res = [], + win = []; + for (let i = 0; i < k; i++) { + while (win.length > 0 && nums[i] >= nums[win[win.length - 1]]) win.pop(); + win.push(i); + } + res.push(nums[win[0]]); + for (let i = k; i < len; i++) { + while (win.length > 0 && nums[i] >= nums[win[win.length - 1]]) win.pop(); + if (win.length > 0 && win[0] < i - k + 1) win.shift(); + win.push(i); res.push(nums[win[0]]); - for (let i = k; i < len; i++) { - while (win.length > 0 && nums[i] >= nums[win[win.length - 1]]) - win.pop(); - if (win.length > 0 && win[0] < i - k + 1) - win.shift(); - win.push(i); - res.push(nums[win[0]]); - } - return res; + } + return res; }; ``` diff --git a/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md b/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md index 0be0162b1ef7c..09e4aadb74216 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md +++ b/solution/0200-0299/0239.Sliding Window Maximum/README_EN.md @@ -62,7 +62,6 @@ Window position Max
  • 1 <= k <= nums.length
  • - ## Solutions @@ -72,7 +71,7 @@ Window position Max ```python class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: - q, res = collections.deque(), [] + q, res = deque(), [] for i, num in enumerate(nums): if q and i - k + 1 > q[0]: q.popleft() @@ -121,24 +120,22 @@ class Solution { * @return {number[]} */ var maxSlidingWindow = function (nums, k) { - let len = nums.length; - if (len < k) return []; - let res = [], win = []; - for (let i = 0; i < k; i++) { - while (win.length > 0 && nums[i] >= nums[win[win.length - 1]]) - win.pop(); - win.push(i); - } + let len = nums.length; + if (len < k) return []; + let res = [], + win = []; + for (let i = 0; i < k; i++) { + while (win.length > 0 && nums[i] >= nums[win[win.length - 1]]) win.pop(); + win.push(i); + } + res.push(nums[win[0]]); + for (let i = k; i < len; i++) { + while (win.length > 0 && nums[i] >= nums[win[win.length - 1]]) win.pop(); + if (win.length > 0 && win[0] < i - k + 1) win.shift(); + win.push(i); res.push(nums[win[0]]); - for (let i = k; i < len; i++) { - while (win.length > 0 && nums[i] >= nums[win[win.length - 1]]) - win.pop(); - if (win.length > 0 && win[0] < i - k + 1) - win.shift(); - win.push(i); - res.push(nums[win[0]]); - } - return res; + } + return res; }; ``` diff --git a/solution/0200-0299/0239.Sliding Window Maximum/Solution.py b/solution/0200-0299/0239.Sliding Window Maximum/Solution.py index 7236c6eded818..c99b9934ec950 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/Solution.py +++ b/solution/0200-0299/0239.Sliding Window Maximum/Solution.py @@ -1,6 +1,6 @@ class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: - q, res = collections.deque(), [] + q, res = deque(), [] for i, num in enumerate(nums): if q and i - k + 1 > q[0]: q.popleft() diff --git a/solution/0200-0299/0290.Word Pattern/README.md b/solution/0200-0299/0290.Word Pattern/README.md index a1086ff53fc51..c29d4756ad0b5 100644 --- a/solution/0200-0299/0290.Word Pattern/README.md +++ b/solution/0200-0299/0290.Word Pattern/README.md @@ -50,7 +50,7 @@ class Solution: n = len(pattern) if n != len(s): return False - c2str, str2c = collections.defaultdict(), collections.defaultdict() + c2str, str2c = defaultdict(), defaultdict() for i in range(n): k, v = pattern[i], s[i] if k in c2str and c2str[k] != v: @@ -96,21 +96,22 @@ class Solution { ```ts function wordPattern(pattern: string, s: string): boolean { - let n = pattern.length; - let values = s.split(' '); - if (n != values.length) return false; - let table = new Array(128); - for (let i = 0; i < n; i++) { - let k = pattern.charCodeAt(i), v = values[i]; - if (!table[k]) { - if (table.includes(v)) return false; - table[k] = v; - } else { - if (table[k] != v) return false; - } + let n = pattern.length; + let values = s.split(" "); + if (n != values.length) return false; + let table = new Array(128); + for (let i = 0; i < n; i++) { + let k = pattern.charCodeAt(i), + v = values[i]; + if (!table[k]) { + if (table.includes(v)) return false; + table[k] = v; + } else { + if (table[k] != v) return false; } - return true; -}; + } + return true; +} ``` ### **C++** diff --git a/solution/0200-0299/0290.Word Pattern/README_EN.md b/solution/0200-0299/0290.Word Pattern/README_EN.md index e23e410293a79..22b3fbd8b36ca 100644 --- a/solution/0200-0299/0290.Word Pattern/README_EN.md +++ b/solution/0200-0299/0290.Word Pattern/README_EN.md @@ -49,7 +49,6 @@
  • All the words in s are separated by a single space.
  • - ## Solutions @@ -63,7 +62,7 @@ class Solution: n = len(pattern) if n != len(s): return False - c2str, str2c = collections.defaultdict(), collections.defaultdict() + c2str, str2c = defaultdict(), defaultdict() for i in range(n): k, v = pattern[i], s[i] if k in c2str and c2str[k] != v: @@ -107,21 +106,22 @@ class Solution { ```ts function wordPattern(pattern: string, s: string): boolean { - let n = pattern.length; - let values = s.split(' '); - if (n != values.length) return false; - let table = new Array(128); - for (let i = 0; i < n; i++) { - let k = pattern.charCodeAt(i), v = values[i]; - if (!table[k]) { - if (table.includes(v)) return false; - table[k] = v; - } else { - if (table[k] != v) return false; - } + let n = pattern.length; + let values = s.split(" "); + if (n != values.length) return false; + let table = new Array(128); + for (let i = 0; i < n; i++) { + let k = pattern.charCodeAt(i), + v = values[i]; + if (!table[k]) { + if (table.includes(v)) return false; + table[k] = v; + } else { + if (table[k] != v) return false; } - return true; -}; + } + return true; +} ``` ### **C++** diff --git a/solution/0200-0299/0290.Word Pattern/Solution.py b/solution/0200-0299/0290.Word Pattern/Solution.py index e6a78375f625e..151d9fd91083f 100644 --- a/solution/0200-0299/0290.Word Pattern/Solution.py +++ b/solution/0200-0299/0290.Word Pattern/Solution.py @@ -4,7 +4,7 @@ def wordPattern(self, pattern: str, s: str) -> bool: n = len(pattern) if n != len(s): return False - c2str, str2c = collections.defaultdict(), collections.defaultdict() + c2str, str2c = defaultdict(), defaultdict() for i in range(n): k, v = pattern[i], s[i] if k in c2str and c2str[k] != v: diff --git a/solution/0300-0399/0311.Sparse Matrix Multiplication/README.md b/solution/0300-0399/0311.Sparse Matrix Multiplication/README.md index 76af9c55e126b..d4a20f200d1ec 100644 --- a/solution/0300-0399/0311.Sparse Matrix Multiplication/README.md +++ b/solution/0300-0399/0311.Sparse Matrix Multiplication/README.md @@ -65,7 +65,7 @@ class Solution: def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]: r1, c1, c2 = len(mat1), len(mat1[0]), len(mat2[0]) res = [[0] * c2 for _ in range(r1)] - mp = collections.defaultdict(list) + mp = defaultdict(list) for i in range(r1): for j in range(c1): if mat1[i][j] != 0: diff --git a/solution/0300-0399/0311.Sparse Matrix Multiplication/README_EN.md b/solution/0300-0399/0311.Sparse Matrix Multiplication/README_EN.md index ed66bfa45c7d5..9ad08147da740 100644 --- a/solution/0300-0399/0311.Sparse Matrix Multiplication/README_EN.md +++ b/solution/0300-0399/0311.Sparse Matrix Multiplication/README_EN.md @@ -55,7 +55,7 @@ class Solution: def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]: r1, c1, c2 = len(mat1), len(mat1[0]), len(mat2[0]) res = [[0] * c2 for _ in range(r1)] - mp = collections.defaultdict(list) + mp = defaultdict(list) for i in range(r1): for j in range(c1): if mat1[i][j] != 0: diff --git a/solution/0300-0399/0311.Sparse Matrix Multiplication/Solution.py b/solution/0300-0399/0311.Sparse Matrix Multiplication/Solution.py index bdf76f249930c..e5e91c322edb7 100644 --- a/solution/0300-0399/0311.Sparse Matrix Multiplication/Solution.py +++ b/solution/0300-0399/0311.Sparse Matrix Multiplication/Solution.py @@ -2,7 +2,7 @@ class Solution: def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]: r1, c1, c2 = len(mat1), len(mat1[0]), len(mat2[0]) res = [[0] * c2 for _ in range(r1)] - mp = collections.defaultdict(list) + mp = defaultdict(list) for i in range(r1): for j in range(c1): if mat1[i][j] != 0: diff --git a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md index d32cd5579f971..720a97296f898 100644 --- a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md +++ b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md @@ -72,8 +72,8 @@ class Solution: def verticalOrder(self, root: TreeNode) -> List[List[int]]: if root is None: return [] - q = collections.deque([(root, 0)]) - offset_vals = collections.defaultdict(list) + q = deque([(root, 0)]) + offset_vals = defaultdict(list) while q: node, offset = q.popleft() offset_vals[offset].append(node.val) diff --git a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md index cdb7a634c4b60..62fddf0e5cabd 100644 --- a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md +++ b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md @@ -62,8 +62,8 @@ class Solution: def verticalOrder(self, root: TreeNode) -> List[List[int]]: if root is None: return [] - q = collections.deque([(root, 0)]) - offset_vals = collections.defaultdict(list) + q = deque([(root, 0)]) + offset_vals = defaultdict(list) while q: node, offset = q.popleft() offset_vals[offset].append(node.val) diff --git a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/Solution.py b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/Solution.py index 998c8cf38f451..9e1bdfdbf971b 100644 --- a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/Solution.py +++ b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/Solution.py @@ -8,8 +8,8 @@ class Solution: def verticalOrder(self, root: TreeNode) -> List[List[int]]: if root is None: return [] - q = collections.deque([(root, 0)]) - offset_vals = collections.defaultdict(list) + q = deque([(root, 0)]) + offset_vals = defaultdict(list) while q: node, offset = q.popleft() offset_vals[offset].append(node.val) diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md index 92647544cfe39..b81a27f26b63d 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md @@ -38,7 +38,6 @@
  • 如果 nums2 的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?
  • - ## 解法 @@ -54,7 +53,7 @@ ```python class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: - counter = collections.Counter(nums1) + counter = Counter(nums1) res = [] for num in nums2: if counter[num] > 0: @@ -98,19 +97,19 @@ class Solution { * @param {number[]} nums2 * @return {number[]} */ -var intersect = function(nums1, nums2) { - const counter = {}; - for (const num of nums1) { - counter[num] = (counter[num] || 0) + 1; - } - let res = []; - for (const num of nums2) { - if (counter[num] > 0) { - res.push(num); - counter[num] -= 1; - } +var intersect = function (nums1, nums2) { + const counter = {}; + for (const num of nums1) { + counter[num] = (counter[num] || 0) + 1; + } + let res = []; + for (const num of nums2) { + if (counter[num] > 0) { + res.push(num); + counter[num] -= 1; } - return res; + } + return res; }; ``` diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md index 195b637638b0f..9f8523a703c73 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md @@ -39,7 +39,6 @@
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
  • - ## Solutions @@ -49,7 +48,7 @@ ```python class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: - counter = collections.Counter(nums1) + counter = Counter(nums1) res = [] for num in nums2: if counter[num] > 0: @@ -91,19 +90,19 @@ class Solution { * @param {number[]} nums2 * @return {number[]} */ -var intersect = function(nums1, nums2) { - const counter = {}; - for (const num of nums1) { - counter[num] = (counter[num] || 0) + 1; - } - let res = []; - for (const num of nums2) { - if (counter[num] > 0) { - res.push(num); - counter[num] -= 1; - } +var intersect = function (nums1, nums2) { + const counter = {}; + for (const num of nums1) { + counter[num] = (counter[num] || 0) + 1; + } + let res = []; + for (const num of nums2) { + if (counter[num] > 0) { + res.push(num); + counter[num] -= 1; } - return res; + } + return res; }; ``` diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.py b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.py index 98423d09e5262..d0f34bd9e2524 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.py +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.py @@ -1,6 +1,6 @@ class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: - counter = collections.Counter(nums1) + counter = Counter(nums1) res = [] for num in nums2: if counter[num] > 0: diff --git a/solution/0300-0399/0355.Design Twitter/README.md b/solution/0300-0399/0355.Design Twitter/README.md index 8a1367f5a0734..33536365e053b 100644 --- a/solution/0300-0399/0355.Design Twitter/README.md +++ b/solution/0300-0399/0355.Design Twitter/README.md @@ -63,9 +63,9 @@ class Twitter: """ Initialize your data structure here. """ - self.user_tweets = collections.defaultdict(list) - self.user_following = collections.defaultdict(set) - self.tweets = collections.defaultdict() + self.user_tweets = defaultdict(list) + self.user_following = defaultdict(set) + self.tweets = defaultdict() self.time = 0 def postTweet(self, userId: int, tweetId: int) -> None: @@ -129,13 +129,13 @@ class Twitter { tweets = new HashMap<>(); time = 0; } - + /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { userTweets.computeIfAbsent(userId, k -> new ArrayList<>()).add(tweetId); tweets.put(tweetId, ++time); } - + /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */ public List getNewsFeed(int userId) { Set following = userFollowing.getOrDefault(userId, new HashSet<>()); @@ -156,12 +156,12 @@ class Twitter { } return res; } - + /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).add(followeeId); } - + /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ public void unfollow(int followerId, int followeeId) { userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).remove(followeeId); diff --git a/solution/0300-0399/0355.Design Twitter/README_EN.md b/solution/0300-0399/0355.Design Twitter/README_EN.md index 9e3e0b9d4572d..5b1c7c95574d6 100644 --- a/solution/0300-0399/0355.Design Twitter/README_EN.md +++ b/solution/0300-0399/0355.Design Twitter/README_EN.md @@ -60,9 +60,9 @@ class Twitter: """ Initialize your data structure here. """ - self.user_tweets = collections.defaultdict(list) - self.user_following = collections.defaultdict(set) - self.tweets = collections.defaultdict() + self.user_tweets = defaultdict(list) + self.user_following = defaultdict(set) + self.tweets = defaultdict() self.time = 0 def postTweet(self, userId: int, tweetId: int) -> None: @@ -124,13 +124,13 @@ class Twitter { tweets = new HashMap<>(); time = 0; } - + /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { userTweets.computeIfAbsent(userId, k -> new ArrayList<>()).add(tweetId); tweets.put(tweetId, ++time); } - + /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */ public List getNewsFeed(int userId) { Set following = userFollowing.getOrDefault(userId, new HashSet<>()); @@ -151,12 +151,12 @@ class Twitter { } return res; } - + /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).add(followeeId); } - + /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ public void unfollow(int followerId, int followeeId) { userFollowing.computeIfAbsent(followerId, k -> new HashSet<>()).remove(followeeId); diff --git a/solution/0300-0399/0355.Design Twitter/Solution.py b/solution/0300-0399/0355.Design Twitter/Solution.py index 3b52fbb5322e9..c1c85ef50f115 100644 --- a/solution/0300-0399/0355.Design Twitter/Solution.py +++ b/solution/0300-0399/0355.Design Twitter/Solution.py @@ -4,9 +4,9 @@ def __init__(self): """ Initialize your data structure here. """ - self.user_tweets = collections.defaultdict(list) - self.user_following = collections.defaultdict(set) - self.tweets = collections.defaultdict() + self.user_tweets = defaultdict(list) + self.user_following = defaultdict(set) + self.tweets = defaultdict() self.time = 0 def postTweet(self, userId: int, tweetId: int) -> None: @@ -43,10 +43,9 @@ def unfollow(self, followerId: int, followeeId: int) -> None: following.remove(followeeId) - # Your Twitter object will be instantiated and called as such: # obj = Twitter() # obj.postTweet(userId,tweetId) # param_2 = obj.getNewsFeed(userId) # obj.follow(followerId,followeeId) -# obj.unfollow(followerId,followeeId) \ No newline at end of file +# obj.unfollow(followerId,followeeId) diff --git a/solution/0300-0399/0362.Design Hit Counter/README.md b/solution/0300-0399/0362.Design Hit Counter/README.md index 9fcb66c5517cb..9b619251dbb6d 100644 --- a/solution/0300-0399/0362.Design Hit Counter/README.md +++ b/solution/0300-0399/0362.Design Hit Counter/README.md @@ -61,7 +61,7 @@ class HitCounter: """ Initialize your data structure here. """ - self.counter = collections.Counter() + self.counter = Counter() def hit(self, timestamp: int) -> None: """ diff --git a/solution/0300-0399/0362.Design Hit Counter/README_EN.md b/solution/0300-0399/0362.Design Hit Counter/README_EN.md index 1203c99e09ce3..45e440f8ce119 100644 --- a/solution/0300-0399/0362.Design Hit Counter/README_EN.md +++ b/solution/0300-0399/0362.Design Hit Counter/README_EN.md @@ -62,7 +62,7 @@ class HitCounter: """ Initialize your data structure here. """ - self.counter = collections.Counter() + self.counter = Counter() def hit(self, timestamp: int) -> None: """ diff --git a/solution/0300-0399/0362.Design Hit Counter/Solution.py b/solution/0300-0399/0362.Design Hit Counter/Solution.py index 3afd8b8d597e7..deb05d03d966a 100644 --- a/solution/0300-0399/0362.Design Hit Counter/Solution.py +++ b/solution/0300-0399/0362.Design Hit Counter/Solution.py @@ -4,7 +4,7 @@ def __init__(self): """ Initialize your data structure here. """ - self.counter = collections.Counter() + self.counter = Counter() def hit(self, timestamp: int) -> None: """ @@ -13,7 +13,6 @@ def hit(self, timestamp: int) -> None: """ self.counter[timestamp] += 1 - def getHits(self, timestamp: int) -> int: """ Return the number of hits in the past 5 minutes. @@ -25,4 +24,4 @@ def getHits(self, timestamp: int) -> int: # Your HitCounter object will be instantiated and called as such: # obj = HitCounter() # obj.hit(timestamp) -# param_2 = obj.getHits(timestamp) \ No newline at end of file +# param_2 = obj.getHits(timestamp) diff --git a/solution/0300-0399/0389.Find the Difference/README.md b/solution/0300-0399/0389.Find the Difference/README.md index 984dd5a995181..c1959c2408adf 100644 --- a/solution/0300-0399/0389.Find the Difference/README.md +++ b/solution/0300-0399/0389.Find the Difference/README.md @@ -64,7 +64,7 @@ ```python class Solution: def findTheDifference(self, s: str, t: str) -> str: - counter = collections.Counter(s) + counter = Counter(s) for c in t: if counter[c] <= 0: return c diff --git a/solution/0300-0399/0389.Find the Difference/README_EN.md b/solution/0300-0399/0389.Find the Difference/README_EN.md index 51fade829da31..662c09b515ff5 100644 --- a/solution/0300-0399/0389.Find the Difference/README_EN.md +++ b/solution/0300-0399/0389.Find the Difference/README_EN.md @@ -58,7 +58,7 @@ ```python class Solution: def findTheDifference(self, s: str, t: str) -> str: - counter = collections.Counter(s) + counter = Counter(s) for c in t: if counter[c] <= 0: return c diff --git a/solution/0300-0399/0389.Find the Difference/Solution.py b/solution/0300-0399/0389.Find the Difference/Solution.py index 58e8c5f176ce1..54b3428f53f7d 100644 --- a/solution/0300-0399/0389.Find the Difference/Solution.py +++ b/solution/0300-0399/0389.Find the Difference/Solution.py @@ -1,6 +1,6 @@ class Solution: def findTheDifference(self, s: str, t: str) -> str: - counter = collections.Counter(s) + counter = Counter(s) for c in t: if counter[c] <= 0: return c diff --git a/solution/0300-0399/0399.Evaluate Division/README.md b/solution/0300-0399/0399.Evaluate Division/README.md index 27e5286bcccbe..f97d0577e91e9 100644 --- a/solution/0300-0399/0399.Evaluate Division/README.md +++ b/solution/0300-0399/0399.Evaluate Division/README.md @@ -131,12 +131,12 @@ d[find(a)] = distance ```python class Solution: def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]: - w = collections.defaultdict(lambda: 1) - p = collections.defaultdict() + w = defaultdict(lambda: 1) + p = defaultdict() for a, b in equations: p[a] = a p[b] = b - + def find(x): if p[x] != x: origin = p[x] @@ -150,7 +150,7 @@ class Solution: continue p[pa] = pb w[pa] = w[e[1]] * values[i] / w[e[0]] - + return [-1 if c not in p or d not in p or find(c) != find(d) else w[c] / w[d] for c, d in queries] ``` diff --git a/solution/0300-0399/0399.Evaluate Division/README_EN.md b/solution/0300-0399/0399.Evaluate Division/README_EN.md index 8d52adbf05b7f..eb41261383b3f 100644 --- a/solution/0300-0399/0399.Evaluate Division/README_EN.md +++ b/solution/0300-0399/0399.Evaluate Division/README_EN.md @@ -64,12 +64,12 @@ Union find. ```python class Solution: def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]: - w = collections.defaultdict(lambda: 1) - p = collections.defaultdict() + w = defaultdict(lambda: 1) + p = defaultdict() for a, b in equations: p[a] = a p[b] = b - + def find(x): if p[x] != x: origin = p[x] @@ -83,7 +83,7 @@ class Solution: continue p[pa] = pb w[pa] = w[e[1]] * values[i] / w[e[0]] - + return [-1 if c not in p or d not in p or find(c) != find(d) else w[c] / w[d] for c, d in queries] ``` diff --git a/solution/0300-0399/0399.Evaluate Division/Solution.py b/solution/0300-0399/0399.Evaluate Division/Solution.py index 2bf85d60d7e24..7ffbb0155cdd5 100644 --- a/solution/0300-0399/0399.Evaluate Division/Solution.py +++ b/solution/0300-0399/0399.Evaluate Division/Solution.py @@ -1,7 +1,7 @@ class Solution: def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]: - w = collections.defaultdict(lambda: 1) - p = collections.defaultdict() + w = defaultdict(lambda: 1) + p = defaultdict() for a, b in equations: p[a] = a p[b] = b diff --git a/solution/0400-0499/0409.Longest Palindrome/README.md b/solution/0400-0499/0409.Longest Palindrome/README.md index 79f952f9db73f..d8cf21dc783a6 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README.md +++ b/solution/0400-0499/0409.Longest Palindrome/README.md @@ -26,7 +26,6 @@ 我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。 - ## 解法 @@ -41,7 +40,7 @@ class Solution: def longestPalindrome(self, s: str) -> int: n = len(s) - counter = collections.Counter(s) + counter = Counter(s) odd_cnt = sum(e % 2 for e in counter.values()) return n if odd_cnt == 0 else n - odd_cnt + 1 ``` @@ -71,18 +70,18 @@ class Solution { ```ts function longestPalindrome(s: string): number { - let n = s.length; - let ans = 0; - let record = new Array(128).fill(0); - for (let i = 0; i < n; i++) { - record[s.charCodeAt(i)]++; - } - for (let i = 65; i < 128; i++) { - let count = record[i]; - ans += (count % 2 == 0 ? count : count - 1); - } - return ans < s.length ? ans + 1 : ans; -}; + let n = s.length; + let ans = 0; + let record = new Array(128).fill(0); + for (let i = 0; i < n; i++) { + record[s.charCodeAt(i)]++; + } + for (let i = 65; i < 128; i++) { + let count = record[i]; + ans += count % 2 == 0 ? count : count - 1; + } + return ans < s.length ? ans + 1 : ans; +} ``` ### **C++** diff --git a/solution/0400-0499/0409.Longest Palindrome/README_EN.md b/solution/0400-0499/0409.Longest Palindrome/README_EN.md index e1238e3c12168..30f90f93fee0d 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README_EN.md +++ b/solution/0400-0499/0409.Longest Palindrome/README_EN.md @@ -40,7 +40,6 @@ One longest palindrome that can be built is "dccaccd", whose length is
  • s consists of lowercase and/or uppercase English letters only.
  • - ## Solutions @@ -51,7 +50,7 @@ One longest palindrome that can be built is "dccaccd", whose length is class Solution: def longestPalindrome(self, s: str) -> int: n = len(s) - counter = collections.Counter(s) + counter = Counter(s) odd_cnt = sum(e % 2 for e in counter.values()) return n if odd_cnt == 0 else n - odd_cnt + 1 ``` @@ -79,18 +78,18 @@ class Solution { ```ts function longestPalindrome(s: string): number { - let n = s.length; - let ans = 0; - let record = new Array(128).fill(0); - for (let i = 0; i < n; i++) { - record[s.charCodeAt(i)]++; - } - for (let i = 65; i < 128; i++) { - let count = record[i]; - ans += (count % 2 == 0 ? count : count - 1); - } - return ans < s.length ? ans + 1 : ans; -}; + let n = s.length; + let ans = 0; + let record = new Array(128).fill(0); + for (let i = 0; i < n; i++) { + record[s.charCodeAt(i)]++; + } + for (let i = 65; i < 128; i++) { + let count = record[i]; + ans += count % 2 == 0 ? count : count - 1; + } + return ans < s.length ? ans + 1 : ans; +} ``` ### **C++** diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution.py b/solution/0400-0499/0409.Longest Palindrome/Solution.py index b6466b07ae348..93ac30ebed352 100644 --- a/solution/0400-0499/0409.Longest Palindrome/Solution.py +++ b/solution/0400-0499/0409.Longest Palindrome/Solution.py @@ -1,6 +1,6 @@ class Solution: def longestPalindrome(self, s: str) -> int: n = len(s) - counter = collections.Counter(s) + counter = Counter(s) odd_cnt = sum(e % 2 for e in counter.values()) return n if odd_cnt == 0 else n - odd_cnt + 1 diff --git a/solution/0400-0499/0429.N-ary Tree Level Order Traversal/README.md b/solution/0400-0499/0429.N-ary Tree Level Order Traversal/README.md index b35de3d2acf44..02eb74d02380b 100644 --- a/solution/0400-0499/0429.N-ary Tree Level Order Traversal/README.md +++ b/solution/0400-0499/0429.N-ary Tree Level Order Traversal/README.md @@ -39,7 +39,6 @@
  • 树的节点总数在 [0, 10^4] 之间
  • - ## 解法 @@ -65,7 +64,7 @@ class Solution: def levelOrder(self, root: 'Node') -> List[List[int]]: if root is None: return [] - q = collections.deque([root]) + q = deque([root]) res = [] while q: n = len(q) diff --git a/solution/0400-0499/0429.N-ary Tree Level Order Traversal/README_EN.md b/solution/0400-0499/0429.N-ary Tree Level Order Traversal/README_EN.md index a90effe80fb02..e165ea676906c 100644 --- a/solution/0400-0499/0429.N-ary Tree Level Order Traversal/README_EN.md +++ b/solution/0400-0499/0429.N-ary Tree Level Order Traversal/README_EN.md @@ -6,11 +6,8 @@

    Given an n-ary tree, return the level order traversal of its nodes' values.

    - -

    Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

    -

     

    Example 1:

    @@ -38,7 +35,6 @@
  • The total number of nodes is between [0, 104]
  • - ## Solutions @@ -58,7 +54,7 @@ class Solution: def levelOrder(self, root: 'Node') -> List[List[int]]: if root is None: return [] - q = collections.deque([root]) + q = deque([root]) res = [] while q: n = len(q) diff --git a/solution/0400-0499/0429.N-ary Tree Level Order Traversal/Solution.py b/solution/0400-0499/0429.N-ary Tree Level Order Traversal/Solution.py index 4ef3acc1c5fb2..4b0d656bd5dce 100644 --- a/solution/0400-0499/0429.N-ary Tree Level Order Traversal/Solution.py +++ b/solution/0400-0499/0429.N-ary Tree Level Order Traversal/Solution.py @@ -6,11 +6,12 @@ def __init__(self, val=None, children=None): self.children = children """ + class Solution: def levelOrder(self, root: 'Node') -> List[List[int]]: if root is None: return [] - q = collections.deque([root]) + q = deque([root]) res = [] while q: n = len(q) diff --git a/solution/0400-0499/0444.Sequence Reconstruction/README.md b/solution/0400-0499/0444.Sequence Reconstruction/README.md index 5903c8bd22d34..04315030e172f 100644 --- a/solution/0400-0499/0444.Sequence Reconstruction/README.md +++ b/solution/0400-0499/0444.Sequence Reconstruction/README.md @@ -84,7 +84,7 @@ class Solution: if len(nums) < n: return False - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * (n + 1) for seq in seqs: i = seq[0] @@ -92,7 +92,7 @@ class Solution: edges[i].append(j) indegree[j] += 1 i = j - q = collections.deque() + q = deque() for i in range(1, n + 1): if indegree[i] == 0: q.append(i) diff --git a/solution/0400-0499/0444.Sequence Reconstruction/README_EN.md b/solution/0400-0499/0444.Sequence Reconstruction/README_EN.md index fc82f6a425174..a850cded94391 100644 --- a/solution/0400-0499/0444.Sequence Reconstruction/README_EN.md +++ b/solution/0400-0499/0444.Sequence Reconstruction/README_EN.md @@ -72,7 +72,7 @@ class Solution: if len(nums) < n: return False - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * (n + 1) for seq in seqs: i = seq[0] @@ -80,7 +80,7 @@ class Solution: edges[i].append(j) indegree[j] += 1 i = j - q = collections.deque() + q = deque() for i in range(1, n + 1): if indegree[i] == 0: q.append(i) diff --git a/solution/0400-0499/0444.Sequence Reconstruction/Solution.py b/solution/0400-0499/0444.Sequence Reconstruction/Solution.py index 596223f947b75..e2494dcff35f6 100644 --- a/solution/0400-0499/0444.Sequence Reconstruction/Solution.py +++ b/solution/0400-0499/0444.Sequence Reconstruction/Solution.py @@ -10,7 +10,7 @@ def sequenceReconstruction(self, org: List[int], seqs: List[List[int]]) -> bool: if len(nums) < n: return False - edges = collections.defaultdict(list) + edges = defaultdict(list) indegree = [0] * (n + 1) for seq in seqs: i = seq[0] @@ -18,7 +18,7 @@ def sequenceReconstruction(self, org: List[int], seqs: List[List[int]]) -> bool: edges[i].append(j) indegree[j] += 1 i = j - q = collections.deque() + q = deque() for i in range(1, n + 1): if indegree[i] == 0: q.append(i) diff --git a/solution/0400-0499/0447.Number of Boomerangs/README.md b/solution/0400-0499/0447.Number of Boomerangs/README.md index a3c382ef15034..9e485599e5c89 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/README.md +++ b/solution/0400-0499/0447.Number of Boomerangs/README.md @@ -64,7 +64,7 @@ class Solution: def numberOfBoomerangs(self, points: List[List[int]]) -> int: ans = 0 for p in points: - counter = collections.Counter() + counter = Counter() for q in points: distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]) counter[distance] += 1 @@ -99,19 +99,19 @@ class Solution { ```ts function numberOfBoomerangs(points: number[][]): number { - let ans = 0; - for (let p1 of points) { - let hashMap: Map = new Map(); - for (let p2 of points) { - const distance = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2; - hashMap.set(distance, (hashMap.get(distance) || 0) + 1); - } - for (let [ , v] of [...hashMap]) { - ans += (v * (v - 1)); - } + let ans = 0; + for (let p1 of points) { + let hashMap: Map = new Map(); + for (let p2 of points) { + const distance = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2; + hashMap.set(distance, (hashMap.get(distance) || 0) + 1); } - return ans; -}; + for (let [, v] of [...hashMap]) { + ans += v * (v - 1); + } + } + return ans; +} ``` ### **Go** diff --git a/solution/0400-0499/0447.Number of Boomerangs/README_EN.md b/solution/0400-0499/0447.Number of Boomerangs/README_EN.md index d2f11ed1a56ed..7afb7f1da51a3 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/README_EN.md +++ b/solution/0400-0499/0447.Number of Boomerangs/README_EN.md @@ -42,7 +42,6 @@
  • All the points are unique.
  • - ## Solutions @@ -54,7 +53,7 @@ class Solution: def numberOfBoomerangs(self, points: List[List[int]]) -> int: ans = 0 for p in points: - counter = collections.Counter() + counter = Counter() for q in points: distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]) counter[distance] += 1 @@ -87,19 +86,19 @@ class Solution { ```ts function numberOfBoomerangs(points: number[][]): number { - let ans = 0; - for (let p1 of points) { - let hashMap: Map = new Map(); - for (let p2 of points) { - const distance = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2; - hashMap.set(distance, (hashMap.get(distance) || 0) + 1); - } - for (let [ , v] of [...hashMap]) { - ans += (v * (v - 1)); - } + let ans = 0; + for (let p1 of points) { + let hashMap: Map = new Map(); + for (let p2 of points) { + const distance = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2; + hashMap.set(distance, (hashMap.get(distance) || 0) + 1); } - return ans; -}; + for (let [, v] of [...hashMap]) { + ans += v * (v - 1); + } + } + return ans; +} ``` ### **Go** diff --git a/solution/0400-0499/0447.Number of Boomerangs/Solution.py b/solution/0400-0499/0447.Number of Boomerangs/Solution.py index 39ff50ee6727d..fad84640e2dd3 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/Solution.py +++ b/solution/0400-0499/0447.Number of Boomerangs/Solution.py @@ -2,9 +2,10 @@ class Solution: def numberOfBoomerangs(self, points: List[List[int]]) -> int: ans = 0 for p in points: - counter = collections.Counter() + counter = Counter() for q in points: - distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]) + distance = (p[0] - q[0]) * (p[0] - q[0]) + \ + (p[1] - q[1]) * (p[1] - q[1]) counter[distance] += 1 ans += sum([val * (val - 1) for val in counter.values()]) return ans diff --git a/solution/0400-0499/0451.Sort Characters By Frequency/README.md b/solution/0400-0499/0451.Sort Characters By Frequency/README.md index 1739d1652ce9f..b5e89a66f294e 100644 --- a/solution/0400-0499/0451.Sort Characters By Frequency/README.md +++ b/solution/0400-0499/0451.Sort Characters By Frequency/README.md @@ -65,8 +65,8 @@ ```python class Solution: def frequencySort(self, s: str) -> str: - counter = collections.Counter(s) - buckets = collections.defaultdict(list) + counter = Counter(s) + buckets = defaultdict(list) for c, freq in counter.items(): buckets[freq].append(c) res = [] diff --git a/solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md b/solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md index 8f7ea7aaf768a..f92d9a49784dd 100644 --- a/solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md +++ b/solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md @@ -51,8 +51,8 @@ Note that 'A' and 'a' are treated as two different characters. ```python class Solution: def frequencySort(self, s: str) -> str: - counter = collections.Counter(s) - buckets = collections.defaultdict(list) + counter = Counter(s) + buckets = defaultdict(list) for c, freq in counter.items(): buckets[freq].append(c) res = [] diff --git a/solution/0400-0499/0451.Sort Characters By Frequency/Solution.py b/solution/0400-0499/0451.Sort Characters By Frequency/Solution.py index 45570cd0284a9..538e162bc8ef2 100644 --- a/solution/0400-0499/0451.Sort Characters By Frequency/Solution.py +++ b/solution/0400-0499/0451.Sort Characters By Frequency/Solution.py @@ -1,7 +1,7 @@ class Solution: def frequencySort(self, s: str) -> str: - counter = collections.Counter(s) - buckets = collections.defaultdict(list) + counter = Counter(s) + buckets = defaultdict(list) for c, freq in counter.items(): buckets[freq].append(c) res = [] diff --git a/solution/0500-0599/0533.Lonely Pixel II/README.md b/solution/0500-0599/0533.Lonely Pixel II/README.md index 19182b53676cf..990aa3803c2c3 100644 --- a/solution/0500-0599/0533.Lonely Pixel II/README.md +++ b/solution/0500-0599/0533.Lonely Pixel II/README.md @@ -66,7 +66,7 @@ class Solution: def findBlackPixel(self, picture: List[List[str]], target: int) -> int: m, n = len(picture), len(picture[0]) rows = [0] * m - cols = collections.defaultdict(list) + cols = defaultdict(list) for i in range(m): for j in range(n): if picture[i][j] == 'B': diff --git a/solution/0500-0599/0533.Lonely Pixel II/README_EN.md b/solution/0500-0599/0533.Lonely Pixel II/README_EN.md index 6dba0bd5b2fdf..09a86910e0e2c 100644 --- a/solution/0500-0599/0533.Lonely Pixel II/README_EN.md +++ b/solution/0500-0599/0533.Lonely Pixel II/README_EN.md @@ -85,7 +85,7 @@ class Solution: def findBlackPixel(self, picture: List[List[str]], target: int) -> int: m, n = len(picture), len(picture[0]) rows = [0] * m - cols = collections.defaultdict(list) + cols = defaultdict(list) for i in range(m): for j in range(n): if picture[i][j] == 'B': diff --git a/solution/0500-0599/0533.Lonely Pixel II/Solution.py b/solution/0500-0599/0533.Lonely Pixel II/Solution.py index d6400a3a9c40e..a51ce58767c71 100644 --- a/solution/0500-0599/0533.Lonely Pixel II/Solution.py +++ b/solution/0500-0599/0533.Lonely Pixel II/Solution.py @@ -2,7 +2,7 @@ class Solution: def findBlackPixel(self, picture: List[List[str]], target: int) -> int: m, n = len(picture), len(picture[0]) rows = [0] * m - cols = collections.defaultdict(list) + cols = defaultdict(list) for i in range(m): for j in range(n): if picture[i][j] == 'B': @@ -14,7 +14,8 @@ def findBlackPixel(self, picture: List[List[str]], target: int) -> int: if i == k: t[i][k] = True else: - t[i][k] = all([picture[i][j] == picture[k][j] for j in range(n)]) + t[i][k] = all([picture[i][j] == picture[k][j] + for j in range(n)]) t[k][i] = t[i][k] res = 0 for i in range(m): diff --git a/solution/0500-0599/0554.Brick Wall/README.md b/solution/0500-0599/0554.Brick Wall/README.md index 3295b325b5d0e..792444c37fc59 100644 --- a/solution/0500-0599/0554.Brick Wall/README.md +++ b/solution/0500-0599/0554.Brick Wall/README.md @@ -55,7 +55,7 @@ ```python class Solution: def leastBricks(self, wall: List[List[int]]) -> int: - cnt = collections.defaultdict(int) + cnt = defaultdict(int) for row in wall: width = 0 for brick in row[:-1]: diff --git a/solution/0500-0599/0554.Brick Wall/README_EN.md b/solution/0500-0599/0554.Brick Wall/README_EN.md index dcf43abe415cc..e46627a2015be 100644 --- a/solution/0500-0599/0554.Brick Wall/README_EN.md +++ b/solution/0500-0599/0554.Brick Wall/README_EN.md @@ -62,7 +62,7 @@ The question can be understood as, let the vertical line pass through the edge o ```python class Solution: def leastBricks(self, wall: List[List[int]]) -> int: - cnt = collections.defaultdict(int) + cnt = defaultdict(int) for row in wall: width = 0 for brick in row[:-1]: diff --git a/solution/0500-0599/0554.Brick Wall/Solution.py b/solution/0500-0599/0554.Brick Wall/Solution.py index c9df2756ee93a..cfd160aa3d1f3 100644 --- a/solution/0500-0599/0554.Brick Wall/Solution.py +++ b/solution/0500-0599/0554.Brick Wall/Solution.py @@ -1,6 +1,6 @@ class Solution: def leastBricks(self, wall: List[List[int]]) -> int: - cnt = collections.defaultdict(int) + cnt = defaultdict(int) for row in wall: width = 0 for brick in row[:-1]: diff --git a/solution/0500-0599/0560.Subarray Sum Equals K/README.md b/solution/0500-0599/0560.Subarray Sum Equals K/README.md index 1076236a2d911..12e2c13cb2d6c 100644 --- a/solution/0500-0599/0560.Subarray Sum Equals K/README.md +++ b/solution/0500-0599/0560.Subarray Sum Equals K/README.md @@ -1,4 +1,4 @@ -# [560. 和为K的子数组](https://leetcode-cn.com/problems/subarray-sum-equals-k) +# [560. 和为 K 的子数组](https://leetcode-cn.com/problems/subarray-sum-equals-k) [English Version](/solution/0500-0599/0560.Subarray%20Sum%20Equals%20K/README_EN.md) @@ -22,7 +22,6 @@
  • 数组中元素的范围是 [-1000, 1000] ,且整数 的范围是 [-1e7, 1e7]。
  • - ## 解法 @@ -36,7 +35,7 @@ ```python class Solution: def subarraySum(self, nums: List[int], k: int) -> int: - mp = collections.Counter() + mp = Counter() mp[0] = 1 res = s = 0 for num in nums: @@ -71,16 +70,17 @@ class Solution { ```ts function subarraySum(nums: number[], k: number): number { - let ans = 0, pre = 0; - let hashTable = new Map(); - hashTable.set(0, 1); - for (let num of nums) { - pre += num; - ans += (hashTable.get(pre - k) || 0); - hashTable.set(pre, (hashTable.get(pre) || 0) + 1); - } - return ans; -}; + let ans = 0, + pre = 0; + let hashTable = new Map(); + hashTable.set(0, 1); + for (let num of nums) { + pre += num; + ans += hashTable.get(pre - k) || 0; + hashTable.set(pre, (hashTable.get(pre) || 0) + 1); + } + return ans; +} ``` ### **C++** diff --git a/solution/0500-0599/0560.Subarray Sum Equals K/README_EN.md b/solution/0500-0599/0560.Subarray Sum Equals K/README_EN.md index b9a82df1cff1c..703e708310de2 100644 --- a/solution/0500-0599/0560.Subarray Sum Equals K/README_EN.md +++ b/solution/0500-0599/0560.Subarray Sum Equals K/README_EN.md @@ -23,7 +23,6 @@
  • -107 <= k <= 107
  • - ## Solutions @@ -33,7 +32,7 @@ ```python class Solution: def subarraySum(self, nums: List[int], k: int) -> int: - mp = collections.Counter() + mp = Counter() mp[0] = 1 res = s = 0 for num in nums: @@ -66,16 +65,17 @@ class Solution { ```ts function subarraySum(nums: number[], k: number): number { - let ans = 0, pre = 0; - let hashTable = new Map(); - hashTable.set(0, 1); - for (let num of nums) { - pre += num; - ans += (hashTable.get(pre - k) || 0); - hashTable.set(pre, (hashTable.get(pre) || 0) + 1); - } - return ans; -}; + let ans = 0, + pre = 0; + let hashTable = new Map(); + hashTable.set(0, 1); + for (let num of nums) { + pre += num; + ans += hashTable.get(pre - k) || 0; + hashTable.set(pre, (hashTable.get(pre) || 0) + 1); + } + return ans; +} ``` ### **C++** diff --git a/solution/0500-0599/0560.Subarray Sum Equals K/Solution.py b/solution/0500-0599/0560.Subarray Sum Equals K/Solution.py index d46c68ee1ebb8..67aff52cf4fe9 100644 --- a/solution/0500-0599/0560.Subarray Sum Equals K/Solution.py +++ b/solution/0500-0599/0560.Subarray Sum Equals K/Solution.py @@ -1,6 +1,6 @@ class Solution: def subarraySum(self, nums: List[int], k: int) -> int: - mp = collections.Counter() + mp = Counter() mp[0] = 1 res = s = 0 for num in nums: diff --git a/solution/0600-0699/0637.Average of Levels in Binary Tree/README.md b/solution/0600-0699/0637.Average of Levels in Binary Tree/README.md index 29364003d0bfb..42a877d959f2a 100644 --- a/solution/0600-0699/0637.Average of Levels in Binary Tree/README.md +++ b/solution/0600-0699/0637.Average of Levels in Binary Tree/README.md @@ -31,7 +31,6 @@
  • 节点值的范围在32位有符号整数范围内。
  • - ## 解法 @@ -52,7 +51,7 @@ class Solution: def averageOfLevels(self, root: TreeNode) -> List[float]: res = [] - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) s = 0 @@ -126,25 +125,25 @@ class Solution { * @param {TreeNode} root * @return {number[]} */ -var averageOfLevels = function(root) { - let res = []; - let queue = [root]; - while (queue.length > 0) { - n = queue.length; - let sum = 0; - for (let i = 0; i < n; i++) { - let node = queue.shift(); - sum += node.val; - if (node.left) { - queue.push(node.left); - } - if (node.right) { - queue.push(node.right); - } - } - res.push(sum / n); +var averageOfLevels = function (root) { + let res = []; + let queue = [root]; + while (queue.length > 0) { + n = queue.length; + let sum = 0; + for (let i = 0; i < n; i++) { + let node = queue.shift(); + sum += node.val; + if (node.left) { + queue.push(node.left); + } + if (node.right) { + queue.push(node.right); + } } - return res; + res.push(sum / n); + } + return res; }; ``` diff --git a/solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md b/solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md index d36e9889de5e2..e2d7b5670bdaa 100644 --- a/solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md +++ b/solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md @@ -5,6 +5,7 @@ ## Description Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted. +

     

    Example 1:

    @@ -30,7 +31,6 @@ Hence return [3, 14.5, 11].
  • -231 <= Node.val <= 231 - 1
  • - ## Solutions @@ -47,7 +47,7 @@ Hence return [3, 14.5, 11]. class Solution: def averageOfLevels(self, root: TreeNode) -> List[float]: res = [] - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) s = 0 @@ -119,25 +119,25 @@ class Solution { * @param {TreeNode} root * @return {number[]} */ -var averageOfLevels = function(root) { - let res = []; - let queue = [root]; - while (queue.length > 0) { - n = queue.length; - let sum = 0; - for (let i = 0; i < n; i++) { - let node = queue.shift(); - sum += node.val; - if (node.left) { - queue.push(node.left); - } - if (node.right) { - queue.push(node.right); - } - } - res.push(sum / n); +var averageOfLevels = function (root) { + let res = []; + let queue = [root]; + while (queue.length > 0) { + n = queue.length; + let sum = 0; + for (let i = 0; i < n; i++) { + let node = queue.shift(); + sum += node.val; + if (node.left) { + queue.push(node.left); + } + if (node.right) { + queue.push(node.right); + } } - return res; + res.push(sum / n); + } + return res; }; ``` diff --git a/solution/0600-0699/0637.Average of Levels in Binary Tree/Solution.py b/solution/0600-0699/0637.Average of Levels in Binary Tree/Solution.py index 685572f80755b..af9367014d64b 100644 --- a/solution/0600-0699/0637.Average of Levels in Binary Tree/Solution.py +++ b/solution/0600-0699/0637.Average of Levels in Binary Tree/Solution.py @@ -7,7 +7,7 @@ class Solution: def averageOfLevels(self, root: TreeNode) -> List[float]: res = [] - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) s = 0 diff --git a/solution/0600-0699/0676.Implement Magic Dictionary/README.md b/solution/0600-0699/0676.Implement Magic Dictionary/README.md index 0f5da3342bc58..6ab6f6d78a28c 100644 --- a/solution/0600-0699/0676.Implement Magic Dictionary/README.md +++ b/solution/0600-0699/0676.Implement Magic Dictionary/README.md @@ -82,7 +82,7 @@ class MagicDictionary: def buildDict(self, dictionary: List[str]) -> None: self.words = set(dictionary) - self.counter = collections.Counter( + self.counter = Counter( p for word in dictionary for p in self._patterns(word)) def search(self, searchWord: str) -> bool: diff --git a/solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md b/solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md index d4a31393dc775..dfba29a481a53 100644 --- a/solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md +++ b/solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md @@ -66,7 +66,7 @@ class MagicDictionary: def buildDict(self, dictionary: List[str]) -> None: self.words = set(dictionary) - self.counter = collections.Counter( + self.counter = Counter( p for word in dictionary for p in self._patterns(word)) def search(self, searchWord: str) -> bool: diff --git a/solution/0600-0699/0676.Implement Magic Dictionary/Solution.py b/solution/0600-0699/0676.Implement Magic Dictionary/Solution.py index 282726c1c88dd..c37bb9f8a5707 100644 --- a/solution/0600-0699/0676.Implement Magic Dictionary/Solution.py +++ b/solution/0600-0699/0676.Implement Magic Dictionary/Solution.py @@ -10,7 +10,7 @@ def _patterns(self, word): def buildDict(self, dictionary: List[str]) -> None: self.words = set(dictionary) - self.counter = collections.Counter( + self.counter = Counter( p for word in dictionary for p in self._patterns(word)) def search(self, searchWord: str) -> bool: diff --git a/solution/0600-0699/0677.Map Sum Pairs/README.md b/solution/0600-0699/0677.Map Sum Pairs/README.md index 5a316455b0abe..3d74820395096 100644 --- a/solution/0600-0699/0677.Map Sum Pairs/README.md +++ b/solution/0600-0699/0677.Map Sum Pairs/README.md @@ -63,8 +63,8 @@ class MapSum: """ Initialize your data structure here. """ - self.data = collections.defaultdict(int) - self.t = collections.defaultdict(int) + self.data = defaultdict(int) + self.t = defaultdict(int) def insert(self, key: str, val: int) -> None: old = self.t[key] diff --git a/solution/0600-0699/0677.Map Sum Pairs/README_EN.md b/solution/0600-0699/0677.Map Sum Pairs/README_EN.md index c10c02e44848d..3f33577726c05 100644 --- a/solution/0600-0699/0677.Map Sum Pairs/README_EN.md +++ b/solution/0600-0699/0677.Map Sum Pairs/README_EN.md @@ -53,8 +53,8 @@ class MapSum: """ Initialize your data structure here. """ - self.data = collections.defaultdict(int) - self.t = collections.defaultdict(int) + self.data = defaultdict(int) + self.t = defaultdict(int) def insert(self, key: str, val: int) -> None: old = self.t[key] @@ -84,7 +84,7 @@ class MapSum { data = new HashMap<>(); t = new HashMap<>(); } - + public void insert(String key, int val) { int old = t.getOrDefault(key, 0); t.put(key, val); @@ -93,7 +93,7 @@ class MapSum { data.put(k, data.getOrDefault(k, 0) + (val - old)); } } - + public int sum(String prefix) { return data.getOrDefault(prefix, 0); } diff --git a/solution/0600-0699/0677.Map Sum Pairs/Solution.py b/solution/0600-0699/0677.Map Sum Pairs/Solution.py index 458a397171292..b4a8b14b1b32a 100644 --- a/solution/0600-0699/0677.Map Sum Pairs/Solution.py +++ b/solution/0600-0699/0677.Map Sum Pairs/Solution.py @@ -4,8 +4,8 @@ def __init__(self): """ Initialize your data structure here. """ - self.data = collections.defaultdict(int) - self.t = collections.defaultdict(int) + self.data = defaultdict(int) + self.t = defaultdict(int) def insert(self, key: str, val: int) -> None: old = self.t[key] @@ -20,4 +20,4 @@ def sum(self, prefix: str) -> int: # Your MapSum object will be instantiated and called as such: # obj = MapSum() # obj.insert(key,val) -# param_2 = obj.sum(prefix) \ No newline at end of file +# param_2 = obj.sum(prefix) diff --git a/solution/0600-0699/0692.Top K Frequent Words/README.md b/solution/0600-0699/0692.Top K Frequent Words/README.md index 53b68ce89a3bc..31d1464a81d71 100644 --- a/solution/0600-0699/0692.Top K Frequent Words/README.md +++ b/solution/0600-0699/0692.Top K Frequent Words/README.md @@ -60,7 +60,7 @@ ```python class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: - counter = collections.Counter(words) + counter = Counter(words) res = sorted(counter, key=lambda word: (-counter[word], word)) return res[:k] ``` diff --git a/solution/0600-0699/0692.Top K Frequent Words/README_EN.md b/solution/0600-0699/0692.Top K Frequent Words/README_EN.md index 4f7be35bc8dc8..462046219523c 100644 --- a/solution/0600-0699/0692.Top K Frequent Words/README_EN.md +++ b/solution/0600-0699/0692.Top K Frequent Words/README_EN.md @@ -71,7 +71,7 @@ ```python class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: - counter = collections.Counter(words) + counter = Counter(words) res = sorted(counter, key=lambda word: (-counter[word], word)) return res[:k] ``` diff --git a/solution/0600-0699/0692.Top K Frequent Words/Solution.py b/solution/0600-0699/0692.Top K Frequent Words/Solution.py index 49b7e133d081a..21b10376a60c9 100644 --- a/solution/0600-0699/0692.Top K Frequent Words/Solution.py +++ b/solution/0600-0699/0692.Top K Frequent Words/Solution.py @@ -1,5 +1,5 @@ class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: - counter = collections.Counter(words) + counter = Counter(words) res = sorted(counter, key=lambda word: (-counter[word], word)) return res[:k] diff --git a/solution/0700-0799/0721.Accounts Merge/README.md b/solution/0700-0799/0721.Accounts Merge/README.md index 530b31b22e00a..7df07868c9984 100644 --- a/solution/0700-0799/0721.Accounts Merge/README.md +++ b/solution/0700-0799/0721.Accounts Merge/README.md @@ -131,7 +131,7 @@ class Solution: else: email_id[email] = i - mp = collections.defaultdict(set) + mp = defaultdict(set) for i in range(n): pa = find(i) for email in accounts[i][1:]: diff --git a/solution/0700-0799/0721.Accounts Merge/README_EN.md b/solution/0700-0799/0721.Accounts Merge/README_EN.md index a1fca3319e997..e59872980f8bd 100644 --- a/solution/0700-0799/0721.Accounts Merge/README_EN.md +++ b/solution/0700-0799/0721.Accounts Merge/README_EN.md @@ -67,7 +67,7 @@ class Solution: else: email_id[email] = i - mp = collections.defaultdict(set) + mp = defaultdict(set) for i in range(n): pa = find(i) for email in accounts[i][1:]: diff --git a/solution/0700-0799/0721.Accounts Merge/Solution.py b/solution/0700-0799/0721.Accounts Merge/Solution.py index c05b0f88dd686..0a87a38d3c691 100644 --- a/solution/0700-0799/0721.Accounts Merge/Solution.py +++ b/solution/0700-0799/0721.Accounts Merge/Solution.py @@ -17,7 +17,7 @@ def find(x): else: email_id[email] = i - mp = collections.defaultdict(set) + mp = defaultdict(set) for i in range(n): pa = find(i) for email in accounts[i][1:]: diff --git a/solution/0700-0799/0760.Find Anagram Mappings/README.md b/solution/0700-0799/0760.Find Anagram Mappings/README.md index 21ac432b6c28f..2455a5d2f1c49 100644 --- a/solution/0700-0799/0760.Find Anagram Mappings/README.md +++ b/solution/0700-0799/0760.Find Anagram Mappings/README.md @@ -38,7 +38,6 @@ B = [50, 12, 32, 46, 28]

     

    - ## 解法 @@ -52,7 +51,7 @@ B = [50, 12, 32, 46, 28] ```python class Solution: def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]: - mapper = collections.defaultdict(set) + mapper = defaultdict(set) for i, num in enumerate(nums2): mapper[num].add(i) return [mapper[num].pop() for num in nums1] diff --git a/solution/0700-0799/0760.Find Anagram Mappings/README_EN.md b/solution/0700-0799/0760.Find Anagram Mappings/README_EN.md index abeaf324300d6..d88ee08e84b52 100644 --- a/solution/0700-0799/0760.Find Anagram Mappings/README_EN.md +++ b/solution/0700-0799/0760.Find Anagram Mappings/README_EN.md @@ -65,7 +65,7 @@ and so on. ```python class Solution: def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]: - mapper = collections.defaultdict(set) + mapper = defaultdict(set) for i, num in enumerate(nums2): mapper[num].add(i) return [mapper[num].pop() for num in nums1] diff --git a/solution/0700-0799/0760.Find Anagram Mappings/Solution.py b/solution/0700-0799/0760.Find Anagram Mappings/Solution.py index 47888445419fb..515418ea7a56d 100644 --- a/solution/0700-0799/0760.Find Anagram Mappings/Solution.py +++ b/solution/0700-0799/0760.Find Anagram Mappings/Solution.py @@ -1,6 +1,6 @@ class Solution: def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]: - mapper = collections.defaultdict(set) + mapper = defaultdict(set) for i, num in enumerate(nums2): mapper[num].add(i) return [mapper[num].pop() for num in nums1] diff --git a/solution/0700-0799/0763.Partition Labels/README.md b/solution/0700-0799/0763.Partition Labels/README.md index 397f34e7d14e3..2203393ca9cf1 100644 --- a/solution/0700-0799/0763.Partition Labels/README.md +++ b/solution/0700-0799/0763.Partition Labels/README.md @@ -43,7 +43,7 @@ ```python class Solution: def partitionLabels(self, s: str) -> List[int]: - last = collections.defaultdict(int) + last = defaultdict(int) n = len(s) for i in range(n): last[s[i]] = i @@ -86,22 +86,23 @@ class Solution { ```ts function partitionLabels(s: string): number[] { - const n = s.length; - let last = new Array(128); - for (let i = 0; i < n; i++) { - last[s.charCodeAt(i)] = i; + const n = s.length; + let last = new Array(128); + for (let i = 0; i < n; i++) { + last[s.charCodeAt(i)] = i; + } + let ans = []; + let left = 0, + right = 0; + for (let i = 0; i < n; i++) { + right = Math.max(right, last[s.charCodeAt(i)]); + if (i == right) { + ans.push(right - left + 1); + left = right + 1; } - let ans = []; - let left = 0, right = 0; - for (let i = 0; i < n; i++) { - right = Math.max(right, last[s.charCodeAt(i)]); - if (i == right) { - ans.push(right - left + 1); - left = right + 1; - } - } - return ans; -}; + } + return ans; +} ``` ### **C++** diff --git a/solution/0700-0799/0763.Partition Labels/README_EN.md b/solution/0700-0799/0763.Partition Labels/README_EN.md index 53baa3a7c73b2..4a4b4bbbcecdc 100644 --- a/solution/0700-0799/0763.Partition Labels/README_EN.md +++ b/solution/0700-0799/0763.Partition Labels/README_EN.md @@ -39,7 +39,7 @@ A partition like "ababcbacadefegde", "hijhklij" is incorrect ```python class Solution: def partitionLabels(self, s: str) -> List[int]: - last = collections.defaultdict(int) + last = defaultdict(int) n = len(s) for i in range(n): last[s[i]] = i @@ -80,22 +80,23 @@ class Solution { ```ts function partitionLabels(s: string): number[] { - const n = s.length; - let last = new Array(128); - for (let i = 0; i < n; i++) { - last[s.charCodeAt(i)] = i; + const n = s.length; + let last = new Array(128); + for (let i = 0; i < n; i++) { + last[s.charCodeAt(i)] = i; + } + let ans = []; + let left = 0, + right = 0; + for (let i = 0; i < n; i++) { + right = Math.max(right, last[s.charCodeAt(i)]); + if (i == right) { + ans.push(right - left + 1); + left = right + 1; } - let ans = []; - let left = 0, right = 0; - for (let i = 0; i < n; i++) { - right = Math.max(right, last[s.charCodeAt(i)]); - if (i == right) { - ans.push(right - left + 1); - left = right + 1; - } - } - return ans; -}; + } + return ans; +} ``` ### **C++** diff --git a/solution/0700-0799/0763.Partition Labels/Solution.py b/solution/0700-0799/0763.Partition Labels/Solution.py index a63e8240b2cc5..f7a95bbbeaf33 100644 --- a/solution/0700-0799/0763.Partition Labels/Solution.py +++ b/solution/0700-0799/0763.Partition Labels/Solution.py @@ -1,6 +1,6 @@ class Solution: def partitionLabels(self, s: str) -> List[int]: - last = collections.defaultdict(int) + last = defaultdict(int) n = len(s) for i in range(n): last[s[i]] = i diff --git a/solution/0700-0799/0781.Rabbits in Forest/README.md b/solution/0700-0799/0781.Rabbits in Forest/README.md index 5ed2e6cf4cc31..69d7244c48fcb 100644 --- a/solution/0700-0799/0781.Rabbits in Forest/README.md +++ b/solution/0700-0799/0781.Rabbits in Forest/README.md @@ -35,7 +35,6 @@
  • answers[i] 是在 [0, 999] 范围内的整数。
  • - ## 解法 @@ -55,7 +54,7 @@ ```python class Solution: def numRabbits(self, answers: List[int]) -> int: - counter = collections.Counter(answers) + counter = Counter(answers) return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()]) ``` diff --git a/solution/0700-0799/0781.Rabbits in Forest/README_EN.md b/solution/0700-0799/0781.Rabbits in Forest/README_EN.md index c34b16a9c2d80..8af4e46630dba 100644 --- a/solution/0700-0799/0781.Rabbits in Forest/README_EN.md +++ b/solution/0700-0799/0781.Rabbits in Forest/README_EN.md @@ -6,12 +6,8 @@

    In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how many other rabbits have the same color as them. Those answers are placed in an array.

    - -

    Return the minimum number of rabbits that could be in the forest.

    - -
     
     Examples:
    @@ -46,19 +42,13 @@ The smallest possible number of rabbits in the forest is therefore 5: 3 that ans
     
     
    - -

    Note:

    - -
    1. answers will have length at most 1000.
    2. Each answers[i] will be an integer in the range [0, 999].
    - - ## Solutions @@ -68,7 +58,7 @@ The smallest possible number of rabbits in the forest is therefore 5: 3 that ans ```python class Solution: def numRabbits(self, answers: List[int]) -> int: - counter = collections.Counter(answers) + counter = Counter(answers) return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()]) ``` diff --git a/solution/0700-0799/0781.Rabbits in Forest/Solution.py b/solution/0700-0799/0781.Rabbits in Forest/Solution.py index b13520783dfc1..9adfd9193eb15 100644 --- a/solution/0700-0799/0781.Rabbits in Forest/Solution.py +++ b/solution/0700-0799/0781.Rabbits in Forest/Solution.py @@ -1,4 +1,4 @@ class Solution: def numRabbits(self, answers: List[int]) -> int: - counter = collections.Counter(answers) + counter = Counter(answers) return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()]) diff --git a/solution/0700-0799/0792.Number of Matching Subsequences/README.md b/solution/0700-0799/0792.Number of Matching Subsequences/README.md index 9d2b0a1e1a8c2..cff49686a6f74 100644 --- a/solution/0700-0799/0792.Number of Matching Subsequences/README.md +++ b/solution/0700-0799/0792.Number of Matching Subsequences/README.md @@ -54,7 +54,7 @@ buckets = { ```python class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: - buckets = collections.defaultdict(list) + buckets = defaultdict(list) for word in words: buckets[word[0]].append(word) res = 0 diff --git a/solution/0700-0799/0792.Number of Matching Subsequences/README_EN.md b/solution/0700-0799/0792.Number of Matching Subsequences/README_EN.md index c39dbfbb9f795..57005e73702a6 100644 --- a/solution/0700-0799/0792.Number of Matching Subsequences/README_EN.md +++ b/solution/0700-0799/0792.Number of Matching Subsequences/README_EN.md @@ -47,7 +47,7 @@ ```python class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: - buckets = collections.defaultdict(list) + buckets = defaultdict(list) for word in words: buckets[word[0]].append(word) res = 0 diff --git a/solution/0700-0799/0792.Number of Matching Subsequences/Solution.py b/solution/0700-0799/0792.Number of Matching Subsequences/Solution.py index 6c728a4923d4a..1834f5d40ef14 100644 --- a/solution/0700-0799/0792.Number of Matching Subsequences/Solution.py +++ b/solution/0700-0799/0792.Number of Matching Subsequences/Solution.py @@ -1,6 +1,6 @@ class Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: - buckets = collections.defaultdict(list) + buckets = defaultdict(list) for word in words: buckets[word[0]].append(word) res = 0 diff --git a/solution/0800-0899/0811.Subdomain Visit Count/README.md b/solution/0800-0899/0811.Subdomain Visit Count/README.md index cec347c9b2733..4b9c5ed650adf 100644 --- a/solution/0800-0899/0811.Subdomain Visit Count/README.md +++ b/solution/0800-0899/0811.Subdomain Visit Count/README.md @@ -42,7 +42,6 @@
  • 输入中任意一个域名的访问次数都小于10000
  • - ## 解法 @@ -56,7 +55,7 @@ ```python class Solution: def subdomainVisits(self, cpdomains: List[str]) -> List[str]: - domains = collections.Counter() + domains = Counter() for item in cpdomains: count, domain = item.split() count = int(count) diff --git a/solution/0800-0899/0811.Subdomain Visit Count/README_EN.md b/solution/0800-0899/0811.Subdomain Visit Count/README_EN.md index 57eedf09641e0..083471e754b8a 100644 --- a/solution/0800-0899/0811.Subdomain Visit Count/README_EN.md +++ b/solution/0800-0899/0811.Subdomain Visit Count/README_EN.md @@ -6,16 +6,10 @@

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.

    - -

    Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".

    - -

    We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.

    - -
     
     Example 1:
    @@ -36,8 +30,6 @@ We only have one website domain: "discuss.leetcode.com". As discussed
     
     
    - -
     
     Example 2:
    @@ -58,12 +50,8 @@ We will visit "google.mail.com" 900 times, "yahoo.com" 50 ti
     
     
    - -

    Notes:

    - -
    • The length of cpdomains will not exceed 100
    • The length of each domain name will not exceed 100.
    • @@ -72,8 +60,6 @@ We will visit "google.mail.com" 900 times, "yahoo.com" 50 ti
    • The answer output can be returned in any order.
    - - ## Solutions @@ -83,7 +69,7 @@ We will visit "google.mail.com" 900 times, "yahoo.com" 50 ti ```python class Solution: def subdomainVisits(self, cpdomains: List[str]) -> List[str]: - domains = collections.Counter() + domains = Counter() for item in cpdomains: count, domain = item.split() count = int(count) diff --git a/solution/0800-0899/0811.Subdomain Visit Count/Solution.py b/solution/0800-0899/0811.Subdomain Visit Count/Solution.py index 22063cfa85b10..a535accd0b576 100644 --- a/solution/0800-0899/0811.Subdomain Visit Count/Solution.py +++ b/solution/0800-0899/0811.Subdomain Visit Count/Solution.py @@ -1,6 +1,6 @@ class Solution: def subdomainVisits(self, cpdomains: List[str]) -> List[str]: - domains = collections.Counter() + domains = Counter() for item in cpdomains: count, domain = item.split() count = int(count) diff --git a/solution/0800-0899/0819.Most Common Word/README.md b/solution/0800-0899/0819.Most Common Word/README.md index f6a9d829327f0..887cb6f5727ac 100644 --- a/solution/0800-0899/0819.Most Common Word/README.md +++ b/solution/0800-0899/0819.Most Common Word/README.md @@ -56,7 +56,7 @@ banned = ["hit"] ```python class Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: - paragraph = collections.Counter(re.findall('[a-z]+', paragraph.lower())) + paragraph = Counter(re.findall('[a-z]+', paragraph.lower())) banned_words = set(banned) for word, _ in paragraph.most_common(): if word not in banned_words: diff --git a/solution/0800-0899/0819.Most Common Word/README_EN.md b/solution/0800-0899/0819.Most Common Word/README_EN.md index 95730eb0ebe24..2f401b2f6ac07 100644 --- a/solution/0800-0899/0819.Most Common Word/README_EN.md +++ b/solution/0800-0899/0819.Most Common Word/README_EN.md @@ -49,7 +49,7 @@ and that "hit" isn't the answer even though it occurs more because ```python class Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: - paragraph = collections.Counter(re.findall('[a-z]+', paragraph.lower())) + paragraph = Counter(re.findall('[a-z]+', paragraph.lower())) banned_words = set(banned) for word, _ in paragraph.most_common(): if word not in banned_words: diff --git a/solution/0800-0899/0819.Most Common Word/Solution.py b/solution/0800-0899/0819.Most Common Word/Solution.py index 07fdea9925b11..c3fa6036051f1 100644 --- a/solution/0800-0899/0819.Most Common Word/Solution.py +++ b/solution/0800-0899/0819.Most Common Word/Solution.py @@ -1,6 +1,6 @@ class Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: - paragraph = collections.Counter(re.findall('[a-z]+', paragraph.lower())) + paragraph = Counter(re.findall('[a-z]+', paragraph.lower())) banned_words = set(banned) for word, _ in paragraph.most_common(): if word not in banned_words: diff --git a/solution/0800-0899/0886.Possible Bipartition/README.md b/solution/0800-0899/0886.Possible Bipartition/README.md index f4d41c1f40979..a64a3812d7d06 100644 --- a/solution/0800-0899/0886.Possible Bipartition/README.md +++ b/solution/0800-0899/0886.Possible Bipartition/README.md @@ -99,6 +99,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: x + ```python # 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) @@ -133,7 +134,7 @@ class Solution: p[x] = find(p[x]) return p[x] - mp = collections.defaultdict(list) + mp = defaultdict(list) for i, j in dislikes: mp[i - 1].append(j - 1) mp[j - 1].append(i - 1) diff --git a/solution/0800-0899/0886.Possible Bipartition/README_EN.md b/solution/0800-0899/0886.Possible Bipartition/README_EN.md index e9637b98ca063..9ac45bbd0c798 100644 --- a/solution/0800-0899/0886.Possible Bipartition/README_EN.md +++ b/solution/0800-0899/0886.Possible Bipartition/README_EN.md @@ -101,7 +101,7 @@ class Solution: p[x] = find(p[x]) return p[x] - mp = collections.defaultdict(list) + mp = defaultdict(list) for i, j in dislikes: mp[i - 1].append(j - 1) mp[j - 1].append(i - 1) diff --git a/solution/0800-0899/0886.Possible Bipartition/Solution.py b/solution/0800-0899/0886.Possible Bipartition/Solution.py index d61b6b1606a31..d1d0f4e111b51 100644 --- a/solution/0800-0899/0886.Possible Bipartition/Solution.py +++ b/solution/0800-0899/0886.Possible Bipartition/Solution.py @@ -6,8 +6,8 @@ def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] - - mp = collections.defaultdict(list) + + mp = defaultdict(list) for i, j in dislikes: mp[i - 1].append(j - 1) mp[j - 1].append(i - 1) diff --git a/solution/0900-0999/0904.Fruit Into Baskets/README.md b/solution/0900-0999/0904.Fruit Into Baskets/README.md index 2465becdeb0f4..3b44ae641df0c 100644 --- a/solution/0900-0999/0904.Fruit Into Baskets/README.md +++ b/solution/0900-0999/0904.Fruit Into Baskets/README.md @@ -62,7 +62,6 @@
  • 0 <= tree[i] < tree.length
  • - ## 解法 @@ -78,7 +77,7 @@ ```python class Solution: def totalFruit(self, tree: List[int]) -> int: - counter = collections.Counter() + counter = Counter() i = res = 0 for j, type in enumerate(tree): counter[type] += 1 diff --git a/solution/0900-0999/0904.Fruit Into Baskets/README_EN.md b/solution/0900-0999/0904.Fruit Into Baskets/README_EN.md index c89d7029bbc09..6f3f27ac912b1 100644 --- a/solution/0900-0999/0904.Fruit Into Baskets/README_EN.md +++ b/solution/0900-0999/0904.Fruit Into Baskets/README_EN.md @@ -6,39 +6,23 @@

    In a row of trees, the i-th tree produces fruit with type tree[i].

    - -

    You start at any tree of your choice, then repeatedly perform the following steps:

    - -
    1. Add one piece of fruit from this tree to your baskets.  If you cannot, stop.
    2. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop.
    - -

    Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop.

    - -

    You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each.

    - -

    What is the total amount of fruit you can collect with this procedure?

    - -

     

    - -

    Example 1:

    - -
     
     Input: [1,2,1]
    @@ -49,14 +33,10 @@
     
     
    - -

    Example 2:

    - -
     
     Input: [0,1,2,2]
    @@ -69,14 +49,10 @@ If we started at the first tree, we would only collect [0, 1].
     
     
    - -

    Example 3:

    - -
     
     Input: [1,2,3,2,2]
    @@ -89,14 +65,10 @@ If we started at the first tree, we would only collect [0, 1].
     
     
    - -

    Example 4:

    - -
     
     Input: [3,3,3,1,2,1,1,2,3,3,4]
    @@ -109,8 +81,6 @@ If we started at the first tree, we would only collect [0, 1].
     
     
    - -

     

    @@ -119,19 +89,13 @@ If we started at the first tree, we would only collect [0, 1].
    - -

    Note:

    - -
    1. 1 <= tree.length <= 40000
    2. 0 <= tree[i] < tree.length
    - - ## Solutions @@ -141,7 +105,7 @@ If we started at the first tree, we would only collect [0, 1]. ```python class Solution: def totalFruit(self, tree: List[int]) -> int: - counter = collections.Counter() + counter = Counter() i = res = 0 for j, type in enumerate(tree): counter[type] += 1 diff --git a/solution/0900-0999/0904.Fruit Into Baskets/Solution.py b/solution/0900-0999/0904.Fruit Into Baskets/Solution.py index a29c0664c7fe4..bb4e12c27201b 100644 --- a/solution/0900-0999/0904.Fruit Into Baskets/Solution.py +++ b/solution/0900-0999/0904.Fruit Into Baskets/Solution.py @@ -1,6 +1,6 @@ class Solution: def totalFruit(self, tree: List[int]) -> int: - counter = collections.Counter() + counter = Counter() i = res = 0 for j, type in enumerate(tree): counter[type] += 1 diff --git a/solution/0900-0999/0919.Complete Binary Tree Inserter/README.md b/solution/0900-0999/0919.Complete Binary Tree Inserter/README.md index e3a565405aab3..27be8e77e1875 100644 --- a/solution/0900-0999/0919.Complete Binary Tree Inserter/README.md +++ b/solution/0900-0999/0919.Complete Binary Tree Inserter/README.md @@ -64,7 +64,7 @@ class CBTInserter: def __init__(self, root: TreeNode): self.tree = [] - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for _ in range(n): diff --git a/solution/0900-0999/0919.Complete Binary Tree Inserter/README_EN.md b/solution/0900-0999/0919.Complete Binary Tree Inserter/README_EN.md index dc1f14a98177e..2578c60160a8e 100644 --- a/solution/0900-0999/0919.Complete Binary Tree Inserter/README_EN.md +++ b/solution/0900-0999/0919.Complete Binary Tree Inserter/README_EN.md @@ -85,7 +85,7 @@ class CBTInserter: def __init__(self, root: TreeNode): self.tree = [] - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for _ in range(n): diff --git a/solution/0900-0999/0919.Complete Binary Tree Inserter/Solution.py b/solution/0900-0999/0919.Complete Binary Tree Inserter/Solution.py index c35288cb8761d..8f93893d8a904 100644 --- a/solution/0900-0999/0919.Complete Binary Tree Inserter/Solution.py +++ b/solution/0900-0999/0919.Complete Binary Tree Inserter/Solution.py @@ -8,7 +8,7 @@ class CBTInserter: def __init__(self, root: TreeNode): self.tree = [] - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for _ in range(n): @@ -36,4 +36,4 @@ def get_root(self) -> TreeNode: # Your CBTInserter object will be instantiated and called as such: # obj = CBTInserter(root) # param_1 = obj.insert(val) -# param_2 = obj.get_root() \ No newline at end of file +# param_2 = obj.get_root() diff --git a/solution/0900-0999/0928.Minimize Malware Spread II/README.md b/solution/0900-0999/0928.Minimize Malware Spread II/README.md index b7acb2241dee3..f333b4345fc57 100644 --- a/solution/0900-0999/0928.Minimize Malware Spread II/README.md +++ b/solution/0900-0999/0928.Minimize Malware Spread II/README.md @@ -155,7 +155,7 @@ class Solution: p[pa] = pb size[pb] += size[pa] - cnt = collections.Counter() + cnt = Counter() mp = {} for i in initial: s = set() diff --git a/solution/0900-0999/0928.Minimize Malware Spread II/README_EN.md b/solution/0900-0999/0928.Minimize Malware Spread II/README_EN.md index 0263c8ddee715..c9466360b3cb3 100644 --- a/solution/0900-0999/0928.Minimize Malware Spread II/README_EN.md +++ b/solution/0900-0999/0928.Minimize Malware Spread II/README_EN.md @@ -75,7 +75,7 @@ class Solution: p[pa] = pb size[pb] += size[pa] - cnt = collections.Counter() + cnt = Counter() mp = {} for i in initial: s = set() diff --git a/solution/0900-0999/0928.Minimize Malware Spread II/Solution.py b/solution/0900-0999/0928.Minimize Malware Spread II/Solution.py index 42a341c5fa4fa..c651bdbb24ef9 100644 --- a/solution/0900-0999/0928.Minimize Malware Spread II/Solution.py +++ b/solution/0900-0999/0928.Minimize Malware Spread II/Solution.py @@ -26,7 +26,7 @@ def find(x): p[pa] = pb size[pb] += size[pa] - cnt = collections.Counter() + cnt = Counter() mp = {} for i in initial: s = set() diff --git a/solution/0900-0999/0933.Number of Recent Calls/README.md b/solution/0900-0999/0933.Number of Recent Calls/README.md index dcc6facd8b8cd..183fc30738cd3 100644 --- a/solution/0900-0999/0933.Number of Recent Calls/README.md +++ b/solution/0900-0999/0933.Number of Recent Calls/README.md @@ -64,7 +64,7 @@ recentCounter.ping(3002); // requests = [1, 100, 3001< class RecentCounter: def __init__(self): - self.q = collections.deque() + self.q = deque() def ping(self, t: int) -> int: self.q.append(t) @@ -89,7 +89,7 @@ class RecentCounter { public RecentCounter() { q = new LinkedList<>(); } - + public int ping(int t) { q.offerLast(t); while (q.peekFirst() < t - 3000) { @@ -116,7 +116,7 @@ public: RecentCounter() { } - + int ping(int t) { q.push_back(t); while (q.front() < t - 3000) { @@ -164,20 +164,20 @@ func (this *RecentCounter) Ping(t int) int { ### **JavaScript** ```js -var RecentCounter = function() { - this.q = []; +var RecentCounter = function () { + this.q = []; }; -/** +/** * @param {number} t * @return {number} */ -RecentCounter.prototype.ping = function(t) { - this.q.push(t); - while (this.q[0] < t - 3000) { - this.q.shift(); - } - return this.q.length; +RecentCounter.prototype.ping = function (t) { + this.q.push(t); + while (this.q[0] < t - 3000) { + this.q.shift(); + } + return this.q.length; }; /** diff --git a/solution/0900-0999/0933.Number of Recent Calls/README_EN.md b/solution/0900-0999/0933.Number of Recent Calls/README_EN.md index 993b1107d2487..cd95186f5d1c3 100644 --- a/solution/0900-0999/0933.Number of Recent Calls/README_EN.md +++ b/solution/0900-0999/0933.Number of Recent Calls/README_EN.md @@ -52,7 +52,7 @@ recentCounter.ping(3002); // requests = [1, 100, 3001, 3002 int: self.q.append(t) @@ -75,7 +75,7 @@ class RecentCounter { public RecentCounter() { q = new LinkedList<>(); } - + public int ping(int t) { q.offerLast(t); while (q.peekFirst() < t - 3000) { @@ -102,7 +102,7 @@ public: RecentCounter() { } - + int ping(int t) { q.push_back(t); while (q.front() < t - 3000) { @@ -150,20 +150,20 @@ func (this *RecentCounter) Ping(t int) int { ### **JavaScript** ```js -var RecentCounter = function() { - this.q = []; +var RecentCounter = function () { + this.q = []; }; -/** +/** * @param {number} t * @return {number} */ -RecentCounter.prototype.ping = function(t) { - this.q.push(t); - while (this.q[0] < t - 3000) { - this.q.shift(); - } - return this.q.length; +RecentCounter.prototype.ping = function (t) { + this.q.push(t); + while (this.q[0] < t - 3000) { + this.q.shift(); + } + return this.q.length; }; /** diff --git a/solution/0900-0999/0933.Number of Recent Calls/Solution.py b/solution/0900-0999/0933.Number of Recent Calls/Solution.py index 9c7d308401f2f..21fe7a59b334b 100644 --- a/solution/0900-0999/0933.Number of Recent Calls/Solution.py +++ b/solution/0900-0999/0933.Number of Recent Calls/Solution.py @@ -1,7 +1,7 @@ class RecentCounter: def __init__(self): - self.q = collections.deque() + self.q = deque() def ping(self, t: int) -> int: self.q.append(t) @@ -12,4 +12,4 @@ def ping(self, t: int) -> int: # Your RecentCounter object will be instantiated and called as such: # obj = RecentCounter() -# param_1 = obj.ping(t) \ No newline at end of file +# param_1 = obj.ping(t) diff --git a/solution/0900-0999/0981.Time Based Key-Value Store/README.md b/solution/0900-0999/0981.Time Based Key-Value Store/README.md index c51020967e40b..41b72e99aeef6 100644 --- a/solution/0900-0999/0981.Time Based Key-Value Store/README.md +++ b/solution/0900-0999/0981.Time Based Key-Value Store/README.md @@ -76,7 +76,7 @@ class TimeMap: """ Initialize your data structure here. """ - self.ktv = collections.defaultdict(list) + self.ktv = defaultdict(list) def set(self, key: str, value: str, timestamp: int) -> None: self.ktv[key].append((timestamp, value)) @@ -109,11 +109,11 @@ class TimeMap { public TimeMap() { ktv = new HashMap<>(); } - + public void set(String key, String value, int timestamp) { ktv.computeIfAbsent(key, k -> new TreeMap<>()).put(timestamp, value); } - + public String get(String key, int timestamp) { if (!ktv.containsKey(key)) { return ""; diff --git a/solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md b/solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md index e9c666304f680..31654023d6ea6 100644 --- a/solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md +++ b/solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md @@ -93,7 +93,7 @@ class TimeMap: """ Initialize your data structure here. """ - self.ktv = collections.defaultdict(list) + self.ktv = defaultdict(list) def set(self, key: str, value: str, timestamp: int) -> None: self.ktv[key].append((timestamp, value)) @@ -123,11 +123,11 @@ class TimeMap { public TimeMap() { ktv = new HashMap<>(); } - + public void set(String key, String value, int timestamp) { ktv.computeIfAbsent(key, k -> new TreeMap<>()).put(timestamp, value); } - + public String get(String key, int timestamp) { if (!ktv.containsKey(key)) { return ""; diff --git a/solution/0900-0999/0981.Time Based Key-Value Store/Solution.py b/solution/0900-0999/0981.Time Based Key-Value Store/Solution.py index 479e1a12c3964..9d8a92219f004 100644 --- a/solution/0900-0999/0981.Time Based Key-Value Store/Solution.py +++ b/solution/0900-0999/0981.Time Based Key-Value Store/Solution.py @@ -4,7 +4,7 @@ def __init__(self): """ Initialize your data structure here. """ - self.ktv = collections.defaultdict(list) + self.ktv = defaultdict(list) def set(self, key: str, value: str, timestamp: int) -> None: self.ktv[key].append((timestamp, value)) @@ -15,10 +15,9 @@ def get(self, key: str, timestamp: int) -> str: tv = self.ktv[key] i = bisect.bisect_right(tv, (timestamp, chr(127))) return tv[i - 1][1] if i else '' - # Your TimeMap object will be instantiated and called as such: # obj = TimeMap() # obj.set(key,value,timestamp) -# param_2 = obj.get(key,timestamp) \ No newline at end of file +# param_2 = obj.get(key,timestamp) diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/README.md b/solution/0900-0999/0993.Cousins in Binary Tree/README.md index 5c9673456e209..c3ff0bf40e61c 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/README.md +++ b/solution/0900-0999/0993.Cousins in Binary Tree/README.md @@ -80,7 +80,7 @@ class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: p = list(range(110)) d = list(range(110)) - q = collections.deque([root]) + q = deque([root]) i = 0 while q: n = len(q) diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/README_EN.md b/solution/0900-0999/0993.Cousins in Binary Tree/README_EN.md index 6064a394506e1..c77b72fd3d3ec 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/README_EN.md +++ b/solution/0900-0999/0993.Cousins in Binary Tree/README_EN.md @@ -72,7 +72,7 @@ class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: p = list(range(110)) d = list(range(110)) - q = collections.deque([root]) + q = deque([root]) i = 0 while q: n = len(q) diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.py b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.py index f6f70c61688d0..62ad68381b760 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.py +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.py @@ -8,7 +8,7 @@ class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: p = list(range(110)) d = list(range(110)) - q = collections.deque([root]) + q = deque([root]) i = 0 while q: n = len(q) diff --git a/solution/1100-1199/1133.Largest Unique Number/README.md b/solution/1100-1199/1133.Largest Unique Number/README.md index 9ebe988aa761a..c04668bd07a9c 100644 --- a/solution/1100-1199/1133.Largest Unique Number/README.md +++ b/solution/1100-1199/1133.Largest Unique Number/README.md @@ -52,7 +52,7 @@ ```python class Solution: def largestUniqueNumber(self, A: List[int]) -> int: - counter = collections.Counter(A) + counter = Counter(A) for i in range(1000, -1, -1): if counter[i] == 1: return i diff --git a/solution/1100-1199/1133.Largest Unique Number/README_EN.md b/solution/1100-1199/1133.Largest Unique Number/README_EN.md index d922aacc67b15..b58b1f612e502 100644 --- a/solution/1100-1199/1133.Largest Unique Number/README_EN.md +++ b/solution/1100-1199/1133.Largest Unique Number/README_EN.md @@ -56,7 +56,7 @@ There is no number that occurs only once. ```python class Solution: def largestUniqueNumber(self, A: List[int]) -> int: - counter = collections.Counter(A) + counter = Counter(A) for i in range(1000, -1, -1): if counter[i] == 1: return i diff --git a/solution/1100-1199/1133.Largest Unique Number/Solution.py b/solution/1100-1199/1133.Largest Unique Number/Solution.py index d25e8530cfd6f..617663a4b3409 100644 --- a/solution/1100-1199/1133.Largest Unique Number/Solution.py +++ b/solution/1100-1199/1133.Largest Unique Number/Solution.py @@ -1,6 +1,6 @@ class Solution: def largestUniqueNumber(self, A: List[int]) -> int: - counter = collections.Counter(A) + counter = Counter(A) for i in range(1000, -1, -1): if counter[i] == 1: return i diff --git a/solution/1100-1199/1181.Before and After Puzzle/README.md b/solution/1100-1199/1181.Before and After Puzzle/README.md index ea7228f078cd3..01fdbc17a7877 100644 --- a/solution/1100-1199/1181.Before and After Puzzle/README.md +++ b/solution/1100-1199/1181.Before and After Puzzle/README.md @@ -74,7 +74,7 @@ ```python class Solution: def beforeAndAfterPuzzles(self, phrases: List[str]) -> List[str]: - same_first_word = collections.defaultdict(set) + same_first_word = defaultdict(set) for i, phrase in enumerate(phrases): same_first_word[phrase.split()[0]].add(i) res = set() diff --git a/solution/1100-1199/1181.Before and After Puzzle/README_EN.md b/solution/1100-1199/1181.Before and After Puzzle/README_EN.md index b513b4c11b964..b4f227011f9cc 100644 --- a/solution/1100-1199/1181.Before and After Puzzle/README_EN.md +++ b/solution/1100-1199/1181.Before and After Puzzle/README_EN.md @@ -65,7 +65,7 @@ ```python class Solution: def beforeAndAfterPuzzles(self, phrases: List[str]) -> List[str]: - same_first_word = collections.defaultdict(set) + same_first_word = defaultdict(set) for i, phrase in enumerate(phrases): same_first_word[phrase.split()[0]].add(i) res = set() diff --git a/solution/1100-1199/1181.Before and After Puzzle/Solution.py b/solution/1100-1199/1181.Before and After Puzzle/Solution.py index 806c44b6936b8..70f07d272fe6b 100644 --- a/solution/1100-1199/1181.Before and After Puzzle/Solution.py +++ b/solution/1100-1199/1181.Before and After Puzzle/Solution.py @@ -1,6 +1,6 @@ class Solution: def beforeAndAfterPuzzles(self, phrases: List[str]) -> List[str]: - same_first_word = collections.defaultdict(set) + same_first_word = defaultdict(set) for i, phrase in enumerate(phrases): same_first_word[phrase.split()[0]].add(i) res = set() diff --git a/solution/1100-1199/1182.Shortest Distance to Target Color/README.md b/solution/1100-1199/1182.Shortest Distance to Target Color/README.md index 0077b7f717ec1..9e79664312a6d 100644 --- a/solution/1100-1199/1182.Shortest Distance to Target Color/README.md +++ b/solution/1100-1199/1182.Shortest Distance to Target Color/README.md @@ -63,7 +63,7 @@ ```python class Solution: def shortestDistanceColor(self, colors: List[int], queries: List[List[int]]) -> List[int]: - color_indexes = collections.defaultdict(list) + color_indexes = defaultdict(list) for i, c in enumerate(colors): color_indexes[c].append(i) res = [] diff --git a/solution/1100-1199/1182.Shortest Distance to Target Color/README_EN.md b/solution/1100-1199/1182.Shortest Distance to Target Color/README_EN.md index ab15ac39fe708..e9fc6de6c7400 100644 --- a/solution/1100-1199/1182.Shortest Distance to Target Color/README_EN.md +++ b/solution/1100-1199/1182.Shortest Distance to Target Color/README_EN.md @@ -40,7 +40,6 @@ The nearest 1 from index 6 is at index 3 (3 steps away).
  • 1 <= queries[i][1] <= 3
  • - ## Solutions Binary search. @@ -52,7 +51,7 @@ Binary search. ```python class Solution: def shortestDistanceColor(self, colors: List[int], queries: List[List[int]]) -> List[int]: - color_indexes = collections.defaultdict(list) + color_indexes = defaultdict(list) for i, c in enumerate(colors): color_indexes[c].append(i) res = [] diff --git a/solution/1100-1199/1182.Shortest Distance to Target Color/Solution.py b/solution/1100-1199/1182.Shortest Distance to Target Color/Solution.py index 1e290f01bacbe..cc3d6175f6cd3 100644 --- a/solution/1100-1199/1182.Shortest Distance to Target Color/Solution.py +++ b/solution/1100-1199/1182.Shortest Distance to Target Color/Solution.py @@ -1,6 +1,6 @@ class Solution: def shortestDistanceColor(self, colors: List[int], queries: List[List[int]]) -> List[int]: - color_indexes = collections.defaultdict(list) + color_indexes = defaultdict(list) for i, c in enumerate(colors): color_indexes[c].append(i) res = [] diff --git a/solution/1100-1199/1198.Find Smallest Common Element in All Rows/README.md b/solution/1100-1199/1198.Find Smallest Common Element in All Rows/README.md index b7b4f51b96d5f..86670a2d11667 100644 --- a/solution/1100-1199/1198.Find Smallest Common Element in All Rows/README.md +++ b/solution/1100-1199/1198.Find Smallest Common Element in All Rows/README.md @@ -44,7 +44,7 @@ ```python class Solution: def smallestCommonElement(self, mat: List[List[int]]) -> int: - counter = collections.Counter() + counter = Counter() for row in mat: for num in row: counter[num] += 1 diff --git a/solution/1100-1199/1198.Find Smallest Common Element in All Rows/README_EN.md b/solution/1100-1199/1198.Find Smallest Common Element in All Rows/README_EN.md index 8c682ec827911..49d15da17e964 100644 --- a/solution/1100-1199/1198.Find Smallest Common Element in All Rows/README_EN.md +++ b/solution/1100-1199/1198.Find Smallest Common Element in All Rows/README_EN.md @@ -43,7 +43,7 @@ ```python class Solution: def smallestCommonElement(self, mat: List[List[int]]) -> int: - counter = collections.Counter() + counter = Counter() for row in mat: for num in row: counter[num] += 1 diff --git a/solution/1100-1199/1198.Find Smallest Common Element in All Rows/Solution.py b/solution/1100-1199/1198.Find Smallest Common Element in All Rows/Solution.py index 9a40fa856feae..eb726829bbd61 100644 --- a/solution/1100-1199/1198.Find Smallest Common Element in All Rows/Solution.py +++ b/solution/1100-1199/1198.Find Smallest Common Element in All Rows/Solution.py @@ -1,6 +1,6 @@ class Solution: def smallestCommonElement(self, mat: List[List[int]]) -> int: - counter = collections.Counter() + counter = Counter() for row in mat: for num in row: counter[num] += 1 diff --git a/solution/1200-1299/1202.Smallest String With Swaps/README.md b/solution/1200-1299/1202.Smallest String With Swaps/README.md index b59bd6e146801..491b6f2d095e1 100644 --- a/solution/1200-1299/1202.Smallest String With Swaps/README.md +++ b/solution/1200-1299/1202.Smallest String With Swaps/README.md @@ -140,7 +140,7 @@ class Solution: for a, b in pairs: p[find(a)] = find(b) - mp = collections.defaultdict(list) + mp = defaultdict(list) for i in range(n): heapq.heappush(mp[find(i)], s[i]) chars = list(s) diff --git a/solution/1200-1299/1202.Smallest String With Swaps/README_EN.md b/solution/1200-1299/1202.Smallest String With Swaps/README_EN.md index d01f45232aae9..9d2596254e916 100644 --- a/solution/1200-1299/1202.Smallest String With Swaps/README_EN.md +++ b/solution/1200-1299/1202.Smallest String With Swaps/README_EN.md @@ -52,7 +52,6 @@ Swap s[0] and s[1], s = "abc"
  • s only contains lower case English letters.
  • - ## Solutions @@ -73,7 +72,7 @@ class Solution: for a, b in pairs: p[find(a)] = find(b) - mp = collections.defaultdict(list) + mp = defaultdict(list) for i in range(n): heapq.heappush(mp[find(i)], s[i]) chars = list(s) diff --git a/solution/1200-1299/1202.Smallest String With Swaps/Solution.py b/solution/1200-1299/1202.Smallest String With Swaps/Solution.py index 851b1d341e404..22b7deaf5fc33 100644 --- a/solution/1200-1299/1202.Smallest String With Swaps/Solution.py +++ b/solution/1200-1299/1202.Smallest String With Swaps/Solution.py @@ -11,7 +11,7 @@ def find(x): for a, b in pairs: p[find(a)] = find(b) - mp = collections.defaultdict(list) + mp = defaultdict(list) for i in range(n): heapq.heappush(mp[find(i)], s[i]) chars = list(s) diff --git a/solution/1200-1299/1207.Unique Number of Occurrences/README.md b/solution/1200-1299/1207.Unique Number of Occurrences/README.md index 8805be0488dd9..0e6869712395e 100644 --- a/solution/1200-1299/1207.Unique Number of Occurrences/README.md +++ b/solution/1200-1299/1207.Unique Number of Occurrences/README.md @@ -54,7 +54,7 @@ ```python class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: - counter = collections.Counter(arr) + counter = Counter(arr) s = set() for num in counter.values(): if num in s: diff --git a/solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md b/solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md index e197ef4872670..9ddbb07e8a0f8 100644 --- a/solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md +++ b/solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md @@ -45,7 +45,7 @@ ```python class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: - counter = collections.Counter(arr) + counter = Counter(arr) s = set() for num in counter.values(): if num in s: diff --git a/solution/1200-1299/1207.Unique Number of Occurrences/Solution.py b/solution/1200-1299/1207.Unique Number of Occurrences/Solution.py index 87303d7735520..fc0aa2ce9621d 100644 --- a/solution/1200-1299/1207.Unique Number of Occurrences/Solution.py +++ b/solution/1200-1299/1207.Unique Number of Occurrences/Solution.py @@ -1,6 +1,6 @@ class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: - counter = collections.Counter(arr) + counter = Counter(arr) s = set() for num in counter.values(): if num in s: diff --git a/solution/1200-1299/1258.Synonymous Sentences/README.md b/solution/1200-1299/1258.Synonymous Sentences/README.md index efdd64c525701..73b2c45c45fc3 100644 --- a/solution/1200-1299/1258.Synonymous Sentences/README.md +++ b/solution/1200-1299/1258.Synonymous Sentences/README.md @@ -71,7 +71,7 @@ class Solution: for a, b in synonyms: p[find(a)] = find(b) - s = collections.defaultdict(set) + s = defaultdict(set) for a, b in synonyms: s[find(a)].add(a) s[find(b)].add(b) diff --git a/solution/1200-1299/1258.Synonymous Sentences/README_EN.md b/solution/1200-1299/1258.Synonymous Sentences/README_EN.md index 7bc0314abfe19..2717aece379c5 100644 --- a/solution/1200-1299/1258.Synonymous Sentences/README_EN.md +++ b/solution/1200-1299/1258.Synonymous Sentences/README_EN.md @@ -76,7 +76,7 @@ class Solution: for a, b in synonyms: p[find(a)] = find(b) - s = collections.defaultdict(set) + s = defaultdict(set) for a, b in synonyms: s[find(a)].add(a) s[find(b)].add(b) diff --git a/solution/1200-1299/1258.Synonymous Sentences/Solution.py b/solution/1200-1299/1258.Synonymous Sentences/Solution.py index e9e550452124d..d00231b17a381 100644 --- a/solution/1200-1299/1258.Synonymous Sentences/Solution.py +++ b/solution/1200-1299/1258.Synonymous Sentences/Solution.py @@ -13,7 +13,7 @@ def find(x): for a, b in synonyms: p[find(a)] = find(b) - s = collections.defaultdict(set) + s = defaultdict(set) for a, b in synonyms: s[find(a)].add(a) s[find(b)].add(b) diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md index af6ab0a8897c6..c2637517d2662 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md @@ -49,7 +49,7 @@ ```python class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: - mp = collections.defaultdict(list) + mp = defaultdict(list) for i, x in enumerate(groupSizes): mp[x].append(i) res = [] diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md index 3e1f965865db0..d35cface9a643 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md @@ -50,7 +50,7 @@ Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]]. ```python class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: - mp = collections.defaultdict(list) + mp = defaultdict(list) for i, x in enumerate(groupSizes): mp[x].append(i) res = [] diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.py b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.py index 7fba269914e41..409ecedda7de9 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.py +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.py @@ -1,6 +1,6 @@ class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: - mp = collections.defaultdict(list) + mp = defaultdict(list) for i, x in enumerate(groupSizes): mp[x].append(i) res = [] diff --git a/solution/1300-1399/1302.Deepest Leaves Sum/README.md b/solution/1300-1399/1302.Deepest Leaves Sum/README.md index aa0fa93208853..b3acfd8c1cea9 100644 --- a/solution/1300-1399/1302.Deepest Leaves Sum/README.md +++ b/solution/1300-1399/1302.Deepest Leaves Sum/README.md @@ -35,7 +35,6 @@
  • 1 <= Node.val <= 100
  • - ## 解法 @@ -57,7 +56,7 @@ # self.right = right class Solution: def deepestLeavesSum(self, root: TreeNode) -> int: - q = collections.deque([root]) + q = deque([root]) s = 0 while q: n = len(q) diff --git a/solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md b/solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md index 086394ff26b57..b2c5d4fbccea8 100644 --- a/solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md +++ b/solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md @@ -46,7 +46,7 @@ BFS. # self.right = right class Solution: def deepestLeavesSum(self, root: TreeNode) -> int: - q = collections.deque([root]) + q = deque([root]) s = 0 while q: n = len(q) diff --git a/solution/1300-1399/1302.Deepest Leaves Sum/Solution.py b/solution/1300-1399/1302.Deepest Leaves Sum/Solution.py index 7e3a2aa81cdfa..57d05969bece9 100644 --- a/solution/1300-1399/1302.Deepest Leaves Sum/Solution.py +++ b/solution/1300-1399/1302.Deepest Leaves Sum/Solution.py @@ -6,7 +6,7 @@ # self.right = right class Solution: def deepestLeavesSum(self, root: TreeNode) -> int: - q = collections.deque([root]) + q = deque([root]) s = 0 while q: n = len(q) diff --git a/solution/1300-1399/1346.Check If N and Its Double Exist/README.md b/solution/1300-1399/1346.Check If N and Its Double Exist/README.md index 98c19e1350b9c..80004125d1258 100644 --- a/solution/1300-1399/1346.Check If N and Its Double Exist/README.md +++ b/solution/1300-1399/1346.Check If N and Its Double Exist/README.md @@ -48,7 +48,6 @@
  • -10^3 <= arr[i] <= 10^3
  • - ## 解法 @@ -62,7 +61,7 @@ ```python class Solution: def checkIfExist(self, arr: List[int]) -> bool: - map = collections.defaultdict(int) + map = defaultdict(int) for i, num in enumerate(arr): map[num] = i for i, num in enumerate(arr): @@ -95,19 +94,19 @@ class Solution { ```ts function checkIfExist(arr: number[]): boolean { - for (let i = arr.length - 1; i >= 0; --i) { - let cur = arr[i]; - let t1 = 2 * cur; - if (arr.includes(t1) && arr.indexOf(t1) != i) { - return true; - } - let t2 = cur >> 1; - if (cur % 2 == 0 && arr.includes(t2) && arr.indexOf(t2) != i) { - return true; - } + for (let i = arr.length - 1; i >= 0; --i) { + let cur = arr[i]; + let t1 = 2 * cur; + if (arr.includes(t1) && arr.indexOf(t1) != i) { + return true; } - return false; -}; + let t2 = cur >> 1; + if (cur % 2 == 0 && arr.includes(t2) && arr.indexOf(t2) != i) { + return true; + } + } + return false; +} ``` ### **C++** diff --git a/solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md b/solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md index f74173a360526..c03cd6f14c046 100644 --- a/solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md +++ b/solution/1300-1399/1346.Check If N and Its Double Exist/README_EN.md @@ -47,7 +47,6 @@
  • -10^3 <= arr[i] <= 10^3
  • - ## Solutions @@ -57,7 +56,7 @@ ```python class Solution: def checkIfExist(self, arr: List[int]) -> bool: - map = collections.defaultdict(int) + map = defaultdict(int) for i, num in enumerate(arr): map[num] = i for i, num in enumerate(arr): @@ -88,19 +87,19 @@ class Solution { ```ts function checkIfExist(arr: number[]): boolean { - for (let i = arr.length - 1; i >= 0; --i) { - let cur = arr[i]; - let t1 = 2 * cur; - if (arr.includes(t1) && arr.indexOf(t1) != i) { - return true; - } - let t2 = cur >> 1; - if (cur % 2 == 0 && arr.includes(t2) && arr.indexOf(t2) != i) { - return true; - } + for (let i = arr.length - 1; i >= 0; --i) { + let cur = arr[i]; + let t1 = 2 * cur; + if (arr.includes(t1) && arr.indexOf(t1) != i) { + return true; } - return false; -}; + let t2 = cur >> 1; + if (cur % 2 == 0 && arr.includes(t2) && arr.indexOf(t2) != i) { + return true; + } + } + return false; +} ``` ### **C++** diff --git a/solution/1300-1399/1346.Check If N and Its Double Exist/Solution.py b/solution/1300-1399/1346.Check If N and Its Double Exist/Solution.py index e720d289c1913..6db5f3f67824a 100644 --- a/solution/1300-1399/1346.Check If N and Its Double Exist/Solution.py +++ b/solution/1300-1399/1346.Check If N and Its Double Exist/Solution.py @@ -1,6 +1,6 @@ class Solution: def checkIfExist(self, arr: List[int]) -> bool: - map = collections.defaultdict(int) + map = defaultdict(int) for i, num in enumerate(arr): map[num] = i for i, num in enumerate(arr): diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md index f635050f34047..4d13173db5f72 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md @@ -72,7 +72,7 @@ ```python class Solution: def minSteps(self, s: str, t: str) -> int: - counter = collections.Counter(s) + counter = Counter(s) res = 0 for c in t: if counter[c] > 0: diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md index 3d4fc8fbd8e50..69c1b0b93f9cc 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md @@ -67,7 +67,7 @@ ```python class Solution: def minSteps(self, s: str, t: str) -> int: - counter = collections.Counter(s) + counter = Counter(s) res = 0 for c in t: if counter[c] > 0: diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.py b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.py index afaab280ca051..d310519da2e85 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.py +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.py @@ -1,6 +1,6 @@ class Solution: def minSteps(self, s: str, t: str) -> int: - counter = collections.Counter(s) + counter = Counter(s) res = 0 for c in t: if counter[c] > 0: diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md b/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md index 9dfffd75ff92b..9b94f104bf8ba 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md @@ -72,7 +72,7 @@ ```python class Solution: def findLucky(self, arr: List[int]) -> int: - counter = collections.Counter(arr) + counter = Counter(arr) ans = -1 for num, n in counter.items(): if num == n and ans < num: diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md b/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md index eacf299ed0fbc..fe32d8c8c8f10 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md @@ -84,7 +84,7 @@ ```python class Solution: def findLucky(self, arr: List[int]) -> int: - counter = collections.Counter(arr) + counter = Counter(arr) ans = -1 for num, n in counter.items(): if num == n and ans < num: diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.py b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.py index b538d367a5e29..0bfae7ae86fbf 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.py +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.py @@ -1,6 +1,6 @@ class Solution: def findLucky(self, arr: List[int]) -> int: - counter = collections.Counter(arr) + counter = Counter(arr) ans = -1 for num, n in counter.items(): if num == n and ans < num: diff --git a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README.md b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README.md index c6153c6e01cb1..95fe64ccc8ae8 100644 --- a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README.md +++ b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README.md @@ -71,7 +71,7 @@ class Solution: def displayTable(self, orders: List[List[str]]) -> List[List[str]]: tables = set() foods = set() - mp = collections.Counter() + mp = Counter() for _, table, food in orders: tables.add(int(table)) foods.add(food) diff --git a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README_EN.md b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README_EN.md index 3431d99c6b37e..0d027eeafa8bb 100644 --- a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README_EN.md +++ b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README_EN.md @@ -87,7 +87,7 @@ class Solution: def displayTable(self, orders: List[List[str]]) -> List[List[str]]: tables = set() foods = set() - mp = collections.Counter() + mp = Counter() for _, table, food in orders: tables.add(int(table)) foods.add(food) diff --git a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/Solution.py b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/Solution.py index e80fbe845f834..e602f22e225cc 100644 --- a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/Solution.py +++ b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/Solution.py @@ -2,7 +2,7 @@ class Solution: def displayTable(self, orders: List[List[str]]) -> List[List[str]]: tables = set() foods = set() - mp = collections.Counter() + mp = Counter() for _, table, food in orders: tables.add(int(table)) foods.add(food) diff --git a/solution/1400-1499/1429.First Unique Number/README.md b/solution/1400-1499/1429.First Unique Number/README.md index 55c4109a7bf88..7afc30afa3931 100644 --- a/solution/1400-1499/1429.First Unique Number/README.md +++ b/solution/1400-1499/1429.First Unique Number/README.md @@ -98,8 +98,8 @@ firstUnique.showFirstUnique(); // 返回 -1 class FirstUnique: def __init__(self, nums: List[int]): - self.counter = collections.OrderedDict() - self.unique_nums = collections.OrderedDict() + self.counter = OrderedDict() + self.unique_nums = OrderedDict() for num in nums: self.counter[num] = self.counter.get(num, 0) + 1 for k, v in self.counter.items(): diff --git a/solution/1400-1499/1429.First Unique Number/README_EN.md b/solution/1400-1499/1429.First Unique Number/README_EN.md index 2651e11107897..888396d942c79 100644 --- a/solution/1400-1499/1429.First Unique Number/README_EN.md +++ b/solution/1400-1499/1429.First Unique Number/README_EN.md @@ -88,8 +88,8 @@ firstUnique.showFirstUnique(); // return -1 class FirstUnique: def __init__(self, nums: List[int]): - self.counter = collections.OrderedDict() - self.unique_nums = collections.OrderedDict() + self.counter = OrderedDict() + self.unique_nums = OrderedDict() for num in nums: self.counter[num] = self.counter.get(num, 0) + 1 for k, v in self.counter.items(): diff --git a/solution/1400-1499/1429.First Unique Number/Solution.py b/solution/1400-1499/1429.First Unique Number/Solution.py index 7047a063d6e54..25bad8f2175b4 100644 --- a/solution/1400-1499/1429.First Unique Number/Solution.py +++ b/solution/1400-1499/1429.First Unique Number/Solution.py @@ -1,8 +1,8 @@ class FirstUnique: def __init__(self, nums: List[int]): - self.counter = collections.OrderedDict() - self.unique_nums = collections.OrderedDict() + self.counter = OrderedDict() + self.unique_nums = OrderedDict() for num in nums: self.counter[num] = self.counter.get(num, 0) + 1 for k, v in self.counter.items(): @@ -27,4 +27,4 @@ def add(self, value: int) -> None: # Your FirstUnique object will be instantiated and called as such: # obj = FirstUnique(nums) # param_1 = obj.showFirstUnique() -# obj.add(value) \ No newline at end of file +# obj.add(value) diff --git a/solution/1500-1599/1500.Design a File Sharing System/README.md b/solution/1500-1599/1500.Design a File Sharing System/README.md index 8e9e724e60ca0..6faf2f91c0484 100644 --- a/solution/1500-1599/1500.Design a File Sharing System/README.md +++ b/solution/1500-1599/1500.Design a File Sharing System/README.md @@ -96,7 +96,7 @@ class FileSharing: self.cur = 0 self.chunks = m self.reused = [] - self.user_chunks = collections.defaultdict(set) + self.user_chunks = defaultdict(set) def join(self, ownedChunks: List[int]) -> int: if self.reused: @@ -146,7 +146,7 @@ class FileSharing { reused = new TreeSet<>(); userChunks = new TreeMap<>(); } - + public int join(List ownedChunks) { int userID; if (reused.isEmpty()) { @@ -158,12 +158,12 @@ class FileSharing { userChunks.put(userID, new HashSet<>(ownedChunks)); return userID; } - + public void leave(int userID) { reused.add(userID); userChunks.remove(userID); } - + public List request(int userID, int chunkID) { if (chunkID < 1 || chunkID > chunks) { return Collections.emptyList(); diff --git a/solution/1500-1599/1500.Design a File Sharing System/README_EN.md b/solution/1500-1599/1500.Design a File Sharing System/README_EN.md index 32083e3a7a19f..fad08858e21a8 100644 --- a/solution/1500-1599/1500.Design a File Sharing System/README_EN.md +++ b/solution/1500-1599/1500.Design a File Sharing System/README_EN.md @@ -118,7 +118,7 @@ class FileSharing: self.cur = 0 self.chunks = m self.reused = [] - self.user_chunks = collections.defaultdict(set) + self.user_chunks = defaultdict(set) def join(self, ownedChunks: List[int]) -> int: if self.reused: @@ -166,7 +166,7 @@ class FileSharing { reused = new TreeSet<>(); userChunks = new TreeMap<>(); } - + public int join(List ownedChunks) { int userID; if (reused.isEmpty()) { @@ -178,12 +178,12 @@ class FileSharing { userChunks.put(userID, new HashSet<>(ownedChunks)); return userID; } - + public void leave(int userID) { reused.add(userID); userChunks.remove(userID); } - + public List request(int userID, int chunkID) { if (chunkID < 1 || chunkID > chunks) { return Collections.emptyList(); diff --git a/solution/1500-1599/1500.Design a File Sharing System/Solution.py b/solution/1500-1599/1500.Design a File Sharing System/Solution.py index cacca5753b2c6..310263df81300 100644 --- a/solution/1500-1599/1500.Design a File Sharing System/Solution.py +++ b/solution/1500-1599/1500.Design a File Sharing System/Solution.py @@ -4,7 +4,7 @@ def __init__(self, m: int): self.cur = 0 self.chunks = m self.reused = [] - self.user_chunks = collections.defaultdict(set) + self.user_chunks = defaultdict(set) def join(self, ownedChunks: List[int]) -> int: if self.reused: diff --git a/solution/1500-1599/1512.Number of Good Pairs/README.md b/solution/1500-1599/1512.Number of Good Pairs/README.md index 8b720c2b6049b..039ad2e3045a9 100644 --- a/solution/1500-1599/1512.Number of Good Pairs/README.md +++ b/solution/1500-1599/1512.Number of Good Pairs/README.md @@ -55,7 +55,7 @@ ```python class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: - counter = collections.Counter(nums) + counter = Counter(nums) return sum([x * (x - 1) for x in counter.values()]) >> 1 ``` diff --git a/solution/1500-1599/1512.Number of Good Pairs/README_EN.md b/solution/1500-1599/1512.Number of Good Pairs/README_EN.md index 39d8df0d0a100..8a5b2d2afa380 100644 --- a/solution/1500-1599/1512.Number of Good Pairs/README_EN.md +++ b/solution/1500-1599/1512.Number of Good Pairs/README_EN.md @@ -64,7 +64,7 @@ ```python class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: - counter = collections.Counter(nums) + counter = Counter(nums) return sum([x * (x - 1) for x in counter.values()]) >> 1 ``` diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution.py b/solution/1500-1599/1512.Number of Good Pairs/Solution.py index 8d9454ab28538..379772cbfd12e 100644 --- a/solution/1500-1599/1512.Number of Good Pairs/Solution.py +++ b/solution/1500-1599/1512.Number of Good Pairs/Solution.py @@ -1,4 +1,4 @@ class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: - counter = collections.Counter(nums) + counter = Counter(nums) return sum([x * (x - 1) for x in counter.values()]) >> 1 diff --git a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md index 121ec5a0239d9..51673cc0fae37 100644 --- a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md +++ b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md @@ -70,7 +70,7 @@ BFS 层序遍历。 # self.right = right class Solution: def findNearestRightNode(self, root: TreeNode, u: TreeNode) -> TreeNode: - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for i in range(n): diff --git a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md index 67df3558c91cd..891188c2abe2d 100644 --- a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md +++ b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md @@ -68,7 +68,7 @@ BFS. # self.right = right class Solution: def findNearestRightNode(self, root: TreeNode, u: TreeNode) -> TreeNode: - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for i in range(n): diff --git a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.py b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.py index 8c700546e8945..ca96fd294880a 100644 --- a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.py +++ b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.py @@ -6,7 +6,7 @@ # self.right = right class Solution: def findNearestRightNode(self, root: TreeNode, u: TreeNode) -> TreeNode: - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for i in range(n): diff --git a/solution/1600-1699/1609.Even Odd Tree/README.md b/solution/1600-1699/1609.Even Odd Tree/README.md index 2c30f2b4e9645..2728743b0f642 100644 --- a/solution/1600-1699/1609.Even Odd Tree/README.md +++ b/solution/1600-1699/1609.Even Odd Tree/README.md @@ -102,7 +102,7 @@ BFS。 class Solution: def isEvenOddTree(self, root: TreeNode) -> bool: even = True - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) prev = 0 if even else 10 ** 6 diff --git a/solution/1600-1699/1609.Even Odd Tree/README_EN.md b/solution/1600-1699/1609.Even Odd Tree/README_EN.md index 840537911e550..332d3d71ff484 100644 --- a/solution/1600-1699/1609.Even Odd Tree/README_EN.md +++ b/solution/1600-1699/1609.Even Odd Tree/README_EN.md @@ -94,7 +94,7 @@ BFS. class Solution: def isEvenOddTree(self, root: TreeNode) -> bool: even = True - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) prev = 0 if even else 10 ** 6 diff --git a/solution/1600-1699/1609.Even Odd Tree/Solution.py b/solution/1600-1699/1609.Even Odd Tree/Solution.py index 35c717bea96ae..1ce8c3e686422 100644 --- a/solution/1600-1699/1609.Even Odd Tree/Solution.py +++ b/solution/1600-1699/1609.Even Odd Tree/Solution.py @@ -7,7 +7,7 @@ class Solution: def isEvenOddTree(self, root: TreeNode) -> bool: even = True - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) prev = 0 if even else 10 ** 6 diff --git a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md index 51a40eecd16ba..f047585a6b1d8 100644 --- a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md +++ b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md @@ -48,7 +48,6 @@
  • s 仅含小写英文字母
  • - ## 解法 @@ -64,7 +63,7 @@ ```python class Solution: def minDeletions(self, s: str) -> int: - counter = collections.Counter(s) + counter = Counter(s) vals = [v for v in counter.values()] vals.sort(reverse=True) ans = 0 @@ -103,21 +102,21 @@ class Solution { ```ts function minDeletions(s: string): number { - let map = {}; - for (let c of s) { - map[c] = (map[c] || 0) + 1; - } - let ans = 0; - let vals: number[] = Object.values(map); - vals.sort((a, b) => a - b); - for (let i = 1; i < vals.length; ++i) { - while(vals[i] > 0 && i != vals.indexOf(vals[i])) { - --vals[i]; - ++ans; - } + let map = {}; + for (let c of s) { + map[c] = (map[c] || 0) + 1; + } + let ans = 0; + let vals: number[] = Object.values(map); + vals.sort((a, b) => a - b); + for (let i = 1; i < vals.length; ++i) { + while (vals[i] > 0 && i != vals.indexOf(vals[i])) { + --vals[i]; + ++ans; } - return ans; -}; + } + return ans; +} ``` ### **...** diff --git a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md index 2b846243e0b87..025d1d6898891 100644 --- a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md +++ b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md @@ -44,7 +44,6 @@ Note that we only care about characters that are still in the string at the end
  • s contains only lowercase English letters.
  • - ## Solutions @@ -54,7 +53,7 @@ Note that we only care about characters that are still in the string at the end ```python class Solution: def minDeletions(self, s: str) -> int: - counter = collections.Counter(s) + counter = Counter(s) vals = [v for v in counter.values()] vals.sort(reverse=True) ans = 0 @@ -91,21 +90,21 @@ class Solution { ```ts function minDeletions(s: string): number { - let map = {}; - for (let c of s) { - map[c] = (map[c] || 0) + 1; - } - let ans = 0; - let vals: number[] = Object.values(map); - vals.sort((a, b) => a - b); - for (let i = 1; i < vals.length; ++i) { - while(vals[i] > 0 && i != vals.indexOf(vals[i])) { - --vals[i]; - ++ans; - } + let map = {}; + for (let c of s) { + map[c] = (map[c] || 0) + 1; + } + let ans = 0; + let vals: number[] = Object.values(map); + vals.sort((a, b) => a - b); + for (let i = 1; i < vals.length; ++i) { + while (vals[i] > 0 && i != vals.indexOf(vals[i])) { + --vals[i]; + ++ans; } - return ans; -}; + } + return ans; +} ``` ### **...** diff --git a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/Solution.py b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/Solution.py index c712c0c6b2b7c..acad0ebf257e1 100644 --- a/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/Solution.py +++ b/solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/Solution.py @@ -1,6 +1,6 @@ class Solution: def minDeletions(self, s: str) -> int: - counter = collections.Counter(s) + counter = Counter(s) vals = [v for v in counter.values()] vals.sort(reverse=True) ans = 0 diff --git a/solution/1600-1699/1660.Correct a Binary Tree/README.md b/solution/1600-1699/1660.Correct a Binary Tree/README.md index bfea7d2c7d04d..2d818b803f6f7 100644 --- a/solution/1600-1699/1660.Correct a Binary Tree/README.md +++ b/solution/1600-1699/1660.Correct a Binary Tree/README.md @@ -79,7 +79,7 @@ # self.right = right class Solution: def correctBinaryTree(self, root: TreeNode) -> TreeNode: - q = collections.deque([root]) + q = deque([root]) res = root p = {} while q: @@ -115,7 +115,7 @@ class Solution: # self.right = right class Solution: def correctBinaryTree(self, root: TreeNode) -> TreeNode: - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for _ in range(n): diff --git a/solution/1600-1699/1660.Correct a Binary Tree/README_EN.md b/solution/1600-1699/1660.Correct a Binary Tree/README_EN.md index 4ca6fce358a83..ffd681e2808ab 100644 --- a/solution/1600-1699/1660.Correct a Binary Tree/README_EN.md +++ b/solution/1600-1699/1660.Correct a Binary Tree/README_EN.md @@ -79,7 +79,7 @@ # self.right = right class Solution: def correctBinaryTree(self, root: TreeNode) -> TreeNode: - q = collections.deque([root]) + q = deque([root]) res = root p = {} while q: @@ -113,7 +113,7 @@ class Solution: # self.right = right class Solution: def correctBinaryTree(self, root: TreeNode) -> TreeNode: - q = collections.deque([root]) + q = deque([root]) while q: n = len(q) for _ in range(n): diff --git a/solution/1600-1699/1670.Design Front Middle Back Queue/README.md b/solution/1600-1699/1670.Design Front Middle Back Queue/README.md index 45713304914ff..caa21ce0e1d83 100644 --- a/solution/1600-1699/1670.Design Front Middle Back Queue/README.md +++ b/solution/1600-1699/1670.Design Front Middle Back Queue/README.md @@ -76,8 +76,8 @@ q.popFront(); // 返回 -1 -> [] (队列为空) class FrontMiddleBackQueue: def __init__(self): - self.left = collections.deque() - self.right = collections.deque() + self.left = deque() + self.right = deque() def pushFront(self, val: int) -> None: self.left.appendleft(val) @@ -223,80 +223,83 @@ class FrontMiddleBackQueue { ### **JavaScript** ```js -var FrontMiddleBackQueue = function() { - this.left = []; - this.right = []; +var FrontMiddleBackQueue = function () { + this.left = []; + this.right = []; }; -/** +/** * @param {number} val * @return {void} */ -FrontMiddleBackQueue.prototype.pushFront = function(val) { - this.left.unshift(val); - this.rebalance(); +FrontMiddleBackQueue.prototype.pushFront = function (val) { + this.left.unshift(val); + this.rebalance(); }; -/** +/** * @param {number} val * @return {void} */ -FrontMiddleBackQueue.prototype.pushMiddle = function(val) { - this.left.push(val); - this.rebalance(); +FrontMiddleBackQueue.prototype.pushMiddle = function (val) { + this.left.push(val); + this.rebalance(); }; -/** +/** * @param {number} val * @return {void} */ -FrontMiddleBackQueue.prototype.pushBack = function(val) { - this.right.push(val); - this.rebalance(); +FrontMiddleBackQueue.prototype.pushBack = function (val) { + this.right.push(val); + this.rebalance(); }; /** * @return {number} */ -FrontMiddleBackQueue.prototype.popFront = function() { - if (this.isEmpty()) return -1; - let num = this.left.length == 0 ? this.right.shift() : this.left.shift(); - this.rebalance(); - return num; +FrontMiddleBackQueue.prototype.popFront = function () { + if (this.isEmpty()) return -1; + let num = this.left.length == 0 ? this.right.shift() : this.left.shift(); + this.rebalance(); + return num; }; /** * @return {number} */ -FrontMiddleBackQueue.prototype.popMiddle = function() { - if (this.isEmpty()) return -1; - let num = this.left.length == this.right.length ? this.left.pop() : this.right.shift(); - this.rebalance(); - return num; +FrontMiddleBackQueue.prototype.popMiddle = function () { + if (this.isEmpty()) return -1; + let num = + this.left.length == this.right.length + ? this.left.pop() + : this.right.shift(); + this.rebalance(); + return num; }; /** * @return {number} */ -FrontMiddleBackQueue.prototype.popBack = function() { - if (this.isEmpty()) return -1; - let num = this.right.pop(); - this.rebalance(); - return num; +FrontMiddleBackQueue.prototype.popBack = function () { + if (this.isEmpty()) return -1; + let num = this.right.pop(); + this.rebalance(); + return num; }; FrontMiddleBackQueue.prototype.rebalance = function () { - while (this.left.length > this.right.length) { - this.right.unshift(this.left.pop()); - } - while (this.right.length > this.left.length + 1) { - this.left.push(this.right.shift()); - } -} + while (this.left.length > this.right.length) { + this.right.unshift(this.left.pop()); + } + while (this.right.length > this.left.length + 1) { + this.left.push(this.right.shift()); + } +}; FrontMiddleBackQueue.prototype.isEmpty = function () { - return this.left.length == 0 && this.right.length == 0; -} + return this.left.length == 0 && this.right.length == 0; +}; /** * Your FrontMiddleBackQueue object will be instantiated and called as such: diff --git a/solution/1600-1699/1670.Design Front Middle Back Queue/README_EN.md b/solution/1600-1699/1670.Design Front Middle Back Queue/README_EN.md index bea6e284413b9..cd7f8eb5a257a 100644 --- a/solution/1600-1699/1670.Design Front Middle Back Queue/README_EN.md +++ b/solution/1600-1699/1670.Design Front Middle Back Queue/README_EN.md @@ -66,8 +66,8 @@ q.popFront(); // return -1 -> [] (The queue is empty) class FrontMiddleBackQueue: def __init__(self): - self.left = collections.deque() - self.right = collections.deque() + self.left = deque() + self.right = deque() def pushFront(self, val: int) -> None: self.left.appendleft(val) @@ -211,80 +211,83 @@ class FrontMiddleBackQueue { ### **JavaScript** ```js -var FrontMiddleBackQueue = function() { - this.left = []; - this.right = []; +var FrontMiddleBackQueue = function () { + this.left = []; + this.right = []; }; -/** +/** * @param {number} val * @return {void} */ -FrontMiddleBackQueue.prototype.pushFront = function(val) { - this.left.unshift(val); - this.rebalance(); +FrontMiddleBackQueue.prototype.pushFront = function (val) { + this.left.unshift(val); + this.rebalance(); }; -/** +/** * @param {number} val * @return {void} */ -FrontMiddleBackQueue.prototype.pushMiddle = function(val) { - this.left.push(val); - this.rebalance(); +FrontMiddleBackQueue.prototype.pushMiddle = function (val) { + this.left.push(val); + this.rebalance(); }; -/** +/** * @param {number} val * @return {void} */ -FrontMiddleBackQueue.prototype.pushBack = function(val) { - this.right.push(val); - this.rebalance(); +FrontMiddleBackQueue.prototype.pushBack = function (val) { + this.right.push(val); + this.rebalance(); }; /** * @return {number} */ -FrontMiddleBackQueue.prototype.popFront = function() { - if (this.isEmpty()) return -1; - let num = this.left.length == 0 ? this.right.shift() : this.left.shift(); - this.rebalance(); - return num; +FrontMiddleBackQueue.prototype.popFront = function () { + if (this.isEmpty()) return -1; + let num = this.left.length == 0 ? this.right.shift() : this.left.shift(); + this.rebalance(); + return num; }; /** * @return {number} */ -FrontMiddleBackQueue.prototype.popMiddle = function() { - if (this.isEmpty()) return -1; - let num = this.left.length == this.right.length ? this.left.pop() : this.right.shift(); - this.rebalance(); - return num; +FrontMiddleBackQueue.prototype.popMiddle = function () { + if (this.isEmpty()) return -1; + let num = + this.left.length == this.right.length + ? this.left.pop() + : this.right.shift(); + this.rebalance(); + return num; }; /** * @return {number} */ -FrontMiddleBackQueue.prototype.popBack = function() { - if (this.isEmpty()) return -1; - let num = this.right.pop(); - this.rebalance(); - return num; +FrontMiddleBackQueue.prototype.popBack = function () { + if (this.isEmpty()) return -1; + let num = this.right.pop(); + this.rebalance(); + return num; }; FrontMiddleBackQueue.prototype.rebalance = function () { - while (this.left.length > this.right.length) { - this.right.unshift(this.left.pop()); - } - while (this.right.length > this.left.length + 1) { - this.left.push(this.right.shift()); - } -} + while (this.left.length > this.right.length) { + this.right.unshift(this.left.pop()); + } + while (this.right.length > this.left.length + 1) { + this.left.push(this.right.shift()); + } +}; FrontMiddleBackQueue.prototype.isEmpty = function () { - return this.left.length == 0 && this.right.length == 0; -} + return this.left.length == 0 && this.right.length == 0; +}; /** * Your FrontMiddleBackQueue object will be instantiated and called as such: diff --git a/solution/1600-1699/1670.Design Front Middle Back Queue/Solution.py b/solution/1600-1699/1670.Design Front Middle Back Queue/Solution.py index 8317e44cb0126..4450fc04d364d 100644 --- a/solution/1600-1699/1670.Design Front Middle Back Queue/Solution.py +++ b/solution/1600-1699/1670.Design Front Middle Back Queue/Solution.py @@ -1,8 +1,8 @@ class FrontMiddleBackQueue: def __init__(self): - self.left = collections.deque() - self.right = collections.deque() + self.left = deque() + self.right = deque() def pushFront(self, val: int) -> None: self.left.appendleft(val) diff --git a/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README.md b/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README.md index 73fe9048618e1..a7d66f9c97a81 100644 --- a/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README.md +++ b/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README.md @@ -70,7 +70,7 @@ ```python class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: - counter = collections.Counter(students) + counter = Counter(students) for i, sandwich in enumerate(sandwiches): if counter[sandwich] == 0: return len(students) - i diff --git a/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README_EN.md b/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README_EN.md index 6ce35ad906259..83b2f77f02c85 100644 --- a/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README_EN.md +++ b/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README_EN.md @@ -61,7 +61,7 @@ Hence all students are able to eat. ```python class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: - counter = collections.Counter(students) + counter = Counter(students) for i, sandwich in enumerate(sandwiches): if counter[sandwich] == 0: return len(students) - i diff --git a/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/Solution.py b/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/Solution.py index 81fdb89cbb195..28e3797ecdce9 100644 --- a/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/Solution.py +++ b/solution/1700-1799/1700.Number of Students Unable to Eat Lunch/Solution.py @@ -1,6 +1,6 @@ class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: - counter = collections.Counter(students) + counter = Counter(students) for i, sandwich in enumerate(sandwiches): if counter[sandwich] == 0: return len(students) - i diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md b/solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md index 36cc6365da934..52e26289aa985 100644 --- a/solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md @@ -47,11 +47,12 @@
  • 只有在 apples[i] = 0 时,days[i] = 0 才成立
  • - ## 解法 +优先队列。先吃掉最容易腐烂的苹果。 + ### **Python3** @@ -59,7 +60,24 @@ ```python - +class Solution: + def eatenApples(self, apples: List[int], days: List[int]) -> int: + q = [] + n = len(apples) + i = ans = 0 + while i < n or q: + if i < n and apples[i] > 0: + heappush(q, [i + days[i] - 1, apples[i]]) + while q and q[0][0] < i: + heappop(q) + if q: + end, num = heappop(q) + num -= 1 + ans += 1 + if num > 0 and end > i: + heappush(q, [end, num]) + i += 1 + return ans ``` ### **Java** @@ -67,7 +85,58 @@ ```java +class Solution { + public int eatenApples(int[] apples, int[] days) { + PriorityQueue q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); + int ans = 0, i = 0, n = apples.length; + while (i < n || !q.isEmpty()) { + if (i < n && apples[i] > 0) { + q.offer(new int[]{i + days[i] - 1, apples[i]}); + } + while (!q.isEmpty() && q.peek()[0] < i) { + q.poll(); + } + if (!q.isEmpty()) { + int[] t = q.poll(); + if (--t[1] > 0 && t[0] > i) { + q.offer(t); + } + ++ans; + } + ++i; + } + return ans; + } +} +``` +### C++ + +```cpp +typedef pair PII; + +class Solution { +public: + int eatenApples(vector& apples, vector& days) { + priority_queue, greater> q; + int i = 0, n = apples.size(), ans = 0; + while (i < n || !q.empty()) + { + if (i < n && apples[i] > 0) q.emplace(i + days[i] - 1, apples[i]); + while (!q.empty() && q.top().first < i) q.pop(); + if (!q.empty()) + { + PII t = q.top(); + q.pop(); + --t.second; + ++ans; + if (t.second > 0 && t.first > i) q.emplace(t); + } + ++i; + } + return ans; + } +}; ``` ### **...** diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md b/solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md index 69a95016fd277..bff4369da605b 100644 --- a/solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md @@ -45,7 +45,6 @@
  • days[i] = 0 if and only if apples[i] = 0.
  • - ## Solutions @@ -53,13 +52,81 @@ ### **Python3** ```python - +class Solution: + def eatenApples(self, apples: List[int], days: List[int]) -> int: + q = [] + n = len(apples) + i = ans = 0 + while i < n or q: + if i < n and apples[i] > 0: + heappush(q, [i + days[i] - 1, apples[i]]) + while q and q[0][0] < i: + heappop(q) + if q: + end, num = heappop(q) + num -= 1 + ans += 1 + if num > 0 and end > i: + heappush(q, [end, num]) + i += 1 + return ans ``` ### **Java** ```java +class Solution { + public int eatenApples(int[] apples, int[] days) { + PriorityQueue q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); + int ans = 0, i = 0, n = apples.length; + while (i < n || !q.isEmpty()) { + if (i < n && apples[i] > 0) { + q.offer(new int[]{i + days[i] - 1, apples[i]}); + } + while (!q.isEmpty() && q.peek()[0] < i) { + q.poll(); + } + if (!q.isEmpty()) { + int[] t = q.poll(); + if (--t[1] > 0 && t[0] > i) { + q.offer(t); + } + ++ans; + } + ++i; + } + return ans; + } +} +``` +### C++ + +```cpp +typedef pair PII; + +class Solution { +public: + int eatenApples(vector& apples, vector& days) { + priority_queue, greater> q; + int i = 0, n = apples.size(), ans = 0; + while (i < n || !q.empty()) + { + if (i < n && apples[i] > 0) q.emplace(i + days[i] - 1, apples[i]); + while (!q.empty() && q.top().first < i) q.pop(); + if (!q.empty()) + { + PII t = q.top(); + q.pop(); + --t.second; + ++ans; + if (t.second > 0 && t.first > i) q.emplace(t); + } + ++i; + } + return ans; + } +}; ``` ### **...** diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.cpp b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.cpp new file mode 100644 index 0000000000000..7560571c0a20b --- /dev/null +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.cpp @@ -0,0 +1,24 @@ +typedef pair PII; + +class Solution { +public: + int eatenApples(vector& apples, vector& days) { + priority_queue, greater> q; + int i = 0, n = apples.size(), ans = 0; + while (i < n || !q.empty()) + { + if (i < n && apples[i] > 0) q.emplace(i + days[i] - 1, apples[i]); + while (!q.empty() && q.top().first < i) q.pop(); + if (!q.empty()) + { + PII t = q.top(); + q.pop(); + --t.second; + ++ans; + if (t.second > 0 && t.first > i) q.emplace(t); + } + ++i; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.java b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.java new file mode 100644 index 0000000000000..36f980c3377c4 --- /dev/null +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public int eatenApples(int[] apples, int[] days) { + PriorityQueue q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); + int ans = 0, i = 0, n = apples.length; + while (i < n || !q.isEmpty()) { + if (i < n && apples[i] > 0) { + q.offer(new int[]{i + days[i] - 1, apples[i]}); + } + while (!q.isEmpty() && q.peek()[0] < i) { + q.poll(); + } + if (!q.isEmpty()) { + int[] t = q.poll(); + if (--t[1] > 0 && t[0] > i) { + q.offer(t); + } + ++ans; + } + ++i; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.py b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.py new file mode 100644 index 0000000000000..d13de4c8a7dd6 --- /dev/null +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.py @@ -0,0 +1,18 @@ +class Solution: + def eatenApples(self, apples: List[int], days: List[int]) -> int: + q = [] + n = len(apples) + i = ans = 0 + while i < n or q: + if i < n and apples[i] > 0: + heappush(q, [i + days[i] - 1, apples[i]]) + while q and q[0][0] < i: + heappop(q) + if q: + end, num = heappop(q) + num -= 1 + ans += 1 + if num > 0 and end > i: + heappush(q, [end, num]) + i += 1 + return ans \ No newline at end of file diff --git a/solution/1700-1799/1711.Count Good Meals/README.md b/solution/1700-1799/1711.Count Good Meals/README.md index a7e3fbb809642..d63dda196105d 100644 --- a/solution/1700-1799/1711.Count Good Meals/README.md +++ b/solution/1700-1799/1711.Count Good Meals/README.md @@ -41,7 +41,6 @@
  • 0 <= deliciousness[i] <= 220
  • - ## 解法 用最暴力的方法枚举每对元素肯定会超时,可以用哈希表优化对**之前元素出现次数**的查询。 @@ -60,7 +59,7 @@ class Solution: mod = 1000000007 limit = max(deliciousness) * 2 pairs = 0 - freq = collections.defaultdict(int) + freq = defaultdict(int) for d in deliciousness: target = 1 while target <= limit: diff --git a/solution/1700-1799/1711.Count Good Meals/README_EN.md b/solution/1700-1799/1711.Count Good Meals/README_EN.md index 5b174de449fe3..8e7a7268f5783 100644 --- a/solution/1700-1799/1711.Count Good Meals/README_EN.md +++ b/solution/1700-1799/1711.Count Good Meals/README_EN.md @@ -37,7 +37,6 @@ Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
  • 0 <= deliciousness[i] <= 220
  • - ## Solutions @@ -50,7 +49,7 @@ class Solution: mod = 1000000007 limit = max(deliciousness) * 2 pairs = 0 - freq = collections.defaultdict(int) + freq = defaultdict(int) for d in deliciousness: target = 1 while target <= limit: diff --git a/solution/1700-1799/1711.Count Good Meals/Solution.py b/solution/1700-1799/1711.Count Good Meals/Solution.py index 2b9283e63232c..cd9bee59c3ab7 100644 --- a/solution/1700-1799/1711.Count Good Meals/Solution.py +++ b/solution/1700-1799/1711.Count Good Meals/Solution.py @@ -3,7 +3,7 @@ def countPairs(self, deliciousness: List[int]) -> int: mod = 1000000007 limit = max(deliciousness) * 2 pairs = 0 - freq = collections.defaultdict(int) + freq = defaultdict(int) for d in deliciousness: target = 1 while target <= limit: diff --git a/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/README.md b/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/README.md index 4dd1e9819452d..ed644b59ee351 100644 --- a/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/README.md +++ b/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/README.md @@ -134,7 +134,7 @@ class Solution: for i, j in allowedSwaps: p[find(i)] = find(j) - mp = collections.defaultdict(collections.Counter) + mp = defaultdict(Counter) for i in range(n): mp[find(i)][source[i]] += 1 res = 0 diff --git a/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/README_EN.md b/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/README_EN.md index c3615601b9130..509d10e577c48 100644 --- a/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/README_EN.md +++ b/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/README_EN.md @@ -73,7 +73,7 @@ class Solution: for i, j in allowedSwaps: p[find(i)] = find(j) - mp = collections.defaultdict(collections.Counter) + mp = defaultdict(Counter) for i in range(n): mp[find(i)][source[i]] += 1 res = 0 diff --git a/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/Solution.py b/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/Solution.py index 76291df0d2f9f..75de733a236a5 100644 --- a/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/Solution.py +++ b/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/Solution.py @@ -11,7 +11,7 @@ def find(x): for i, j in allowedSwaps: p[find(i)] = find(j) - mp = collections.defaultdict(collections.Counter) + mp = defaultdict(Counter) for i in range(n): mp[find(i)][source[i]] += 1 res = 0 diff --git a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md index c8ec86253ede5..654f0e073860b 100644 --- a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md +++ b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README.md @@ -54,7 +54,6 @@
  • 题目数据保证存在一些以 adjacentPairs 作为元素对的数组 nums
  • - ## 解法 @@ -70,7 +69,7 @@ ```python class Solution: def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]: - graph = collections.defaultdict(list) + graph = defaultdict(list) for pair in adjacentPairs: graph[pair[0]].append(pair[1]) graph[pair[1]].append(pair[0]) diff --git a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md index b9b5c6b34622e..6bd68f5e425bd 100644 --- a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md +++ b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/README_EN.md @@ -50,7 +50,6 @@ Another solution is [-3,1,4,-2], which would also be accepted.
  • There exists some nums that has adjacentPairs as its pairs.
  • - ## Solutions Traverse the graph from the point where the degree is one. @@ -62,7 +61,7 @@ Traverse the graph from the point where the degree is one. ```python class Solution: def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]: - graph = collections.defaultdict(list) + graph = defaultdict(list) for pair in adjacentPairs: graph[pair[0]].append(pair[1]) graph[pair[1]].append(pair[0]) diff --git a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/Solution.py b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/Solution.py index 9131e849ac139..f233c3ebb400d 100644 --- a/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/Solution.py +++ b/solution/1700-1799/1743.Restore the Array From Adjacent Pairs/Solution.py @@ -1,6 +1,6 @@ class Solution: def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]: - graph = collections.defaultdict(list) + graph = defaultdict(list) for pair in adjacentPairs: graph[pair[0]].append(pair[1]) graph[pair[1]].append(pair[0]) diff --git a/solution/1700-1799/1772.Sort Features by Popularity/README.md b/solution/1700-1799/1772.Sort Features by Popularity/README.md index 20360f69b9eaf..d4d77bf92b732 100644 --- a/solution/1700-1799/1772.Sort Features by Popularity/README.md +++ b/solution/1700-1799/1772.Sort Features by Popularity/README.md @@ -61,7 +61,7 @@ class Solution: def sortFeatures(self, features: List[str], responses: List[str]) -> List[str]: feature_set = set(features) - counter = collections.Counter() + counter = Counter() for resp in responses: for feat in set(resp.split(' ')): if feat in feature_set: diff --git a/solution/1700-1799/1772.Sort Features by Popularity/README_EN.md b/solution/1700-1799/1772.Sort Features by Popularity/README_EN.md index 9459eb91728a8..2ea342fc942d8 100644 --- a/solution/1700-1799/1772.Sort Features by Popularity/README_EN.md +++ b/solution/1700-1799/1772.Sort Features by Popularity/README_EN.md @@ -51,7 +51,7 @@ class Solution: def sortFeatures(self, features: List[str], responses: List[str]) -> List[str]: feature_set = set(features) - counter = collections.Counter() + counter = Counter() for resp in responses: for feat in set(resp.split(' ')): if feat in feature_set: diff --git a/solution/1700-1799/1772.Sort Features by Popularity/Solution.py b/solution/1700-1799/1772.Sort Features by Popularity/Solution.py index 2b81fe606a3ea..0b26ad4936d14 100644 --- a/solution/1700-1799/1772.Sort Features by Popularity/Solution.py +++ b/solution/1700-1799/1772.Sort Features by Popularity/Solution.py @@ -1,7 +1,7 @@ class Solution: def sortFeatures(self, features: List[str], responses: List[str]) -> List[str]: feature_set = set(features) - counter = collections.Counter() + counter = Counter() for resp in responses: for feat in set(resp.split(' ')): if feat in feature_set: diff --git a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README.md b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README.md index 78d7cd9693f7a..f60e5137a1ba8 100644 --- a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README.md +++ b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README.md @@ -8,12 +8,8 @@

    Given the head of a linked list, find all the values that appear more than once in the list and delete the nodes that have any of those values.

    - -

    Return the linked list after the deletions.

    - -

     

    Example 1:

    @@ -30,8 +26,6 @@ - -

    Example 2:

    @@ -46,8 +40,6 @@ - -

    Example 3:

    @@ -62,14 +54,10 @@ - -

     

    Constraints:

    - -
    • The number of nodes in the list is in the range [1, 105]
    • 1 <= Node.val <= 105
    • @@ -96,7 +84,7 @@ class Solution: def deleteDuplicatesUnsorted(self, head: ListNode) -> ListNode: cur = head - counter = collections.Counter() + counter = Counter() while cur: counter[cur.val] += 1 cur = cur.next diff --git a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md index 3445f7acf30ba..26b6d14cebea4 100644 --- a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md +++ b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md @@ -76,7 +76,7 @@ class Solution: def deleteDuplicatesUnsorted(self, head: ListNode) -> ListNode: cur = head - counter = collections.Counter() + counter = Counter() while cur: counter[cur.val] += 1 cur = cur.next diff --git a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/Solution.py b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/Solution.py index 15ce75a680097..4a393e4384dc0 100644 --- a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/Solution.py +++ b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/Solution.py @@ -6,7 +6,7 @@ class Solution: def deleteDuplicatesUnsorted(self, head: ListNode) -> ListNode: cur = head - counter = collections.Counter() + counter = Counter() while cur: counter[cur.val] += 1 cur = cur.next diff --git a/solution/1800-1899/1861.Rotating the Box/README.md b/solution/1800-1899/1861.Rotating the Box/README.md index 9c5ff3ebfce46..a164448cc9211 100644 --- a/solution/1800-1899/1861.Rotating the Box/README.md +++ b/solution/1800-1899/1861.Rotating the Box/README.md @@ -90,7 +90,7 @@ class Solution: for j in range(n): res[j][m - i - 1] = box[i][j] for j in range(m): - q = collections.deque() + q = deque() for i in range(n - 1, -1, -1): if res[i][j] == '*': q.clear() diff --git a/solution/1800-1899/1861.Rotating the Box/README_EN.md b/solution/1800-1899/1861.Rotating the Box/README_EN.md index 197f1c8cbc703..7432da55581bd 100644 --- a/solution/1800-1899/1861.Rotating the Box/README_EN.md +++ b/solution/1800-1899/1861.Rotating the Box/README_EN.md @@ -108,7 +108,7 @@ class Solution: for j in range(n): res[j][m - i - 1] = box[i][j] for j in range(m): - q = collections.deque() + q = deque() for i in range(n - 1, -1, -1): if res[i][j] == '*': q.clear() diff --git a/solution/1800-1899/1861.Rotating the Box/Solution.py b/solution/1800-1899/1861.Rotating the Box/Solution.py index 6b75fd1f7ba9a..08b4c4ffe676c 100644 --- a/solution/1800-1899/1861.Rotating the Box/Solution.py +++ b/solution/1800-1899/1861.Rotating the Box/Solution.py @@ -6,7 +6,7 @@ def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]: for j in range(n): res[j][m - i - 1] = box[i][j] for j in range(m): - q = collections.deque() + q = deque() for i in range(n - 1, -1, -1): if res[i][j] == '*': q.clear() diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md index 72be7b082979c..59f610a1d8243 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md @@ -76,7 +76,7 @@ class FindSumPairs: def __init__(self, nums1: List[int], nums2: List[int]): self.nums1 = nums1 self.nums2 = nums2 - self.counter = collections.Counter(nums2) + self.counter = Counter(nums2) def add(self, index: int, val: int) -> None: old_val = self.nums2[index] diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md index 2ec280dd6ae15..21b2d5dc3947c 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md @@ -66,7 +66,7 @@ class FindSumPairs: def __init__(self, nums1: List[int], nums2: List[int]): self.nums1 = nums1 self.nums2 = nums2 - self.counter = collections.Counter(nums2) + self.counter = Counter(nums2) def add(self, index: int, val: int) -> None: old_val = self.nums2[index] diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.py b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.py index f71c29b779522..d0accd338f4da 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.py +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.py @@ -3,7 +3,7 @@ class FindSumPairs: def __init__(self, nums1: List[int], nums2: List[int]): self.nums1 = nums1 self.nums2 = nums2 - self.counter = collections.Counter(nums2) + self.counter = Counter(nums2) def add(self, index: int, val: int) -> None: old_val = self.nums2[index] diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md index 0502eaaa7da65..bbcba629fce63 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README.md @@ -40,7 +40,6 @@
    • words[i] 由小写英文字母组成
    - ## 解法 @@ -54,7 +53,7 @@ ```python class Solution: def makeEqual(self, words: List[str]) -> bool: - counter = collections.Counter() + counter = Counter() for word in words: for c in word: counter[c] += 1 @@ -90,21 +89,21 @@ class Solution { ```ts function makeEqual(words: string[]): boolean { - let n = words.length; - let letters = new Array(26).fill(0); - for (let word of words) { - for (let i = 0; i < word.length; ++i) { - ++letters[word.charCodeAt(i) - 97]; - } + let n = words.length; + let letters = new Array(26).fill(0); + for (let word of words) { + for (let i = 0; i < word.length; ++i) { + ++letters[word.charCodeAt(i) - 97]; } + } - for (let i = 0; i < letters.length; ++i) { - if (letters[i] % n != 0) { - return false; - } + for (let i = 0; i < letters.length; ++i) { + if (letters[i] % n != 0) { + return false; } - return true; -}; + } + return true; +} ``` ### **C++** diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md index 02b3a898dd4e0..454d19a832a11 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/README_EN.md @@ -38,7 +38,6 @@ All the strings are now equal to "abc", so return true.
  • words[i] consists of lowercase English letters.
  • - ## Solutions @@ -48,7 +47,7 @@ All the strings are now equal to "abc", so return true. ```python class Solution: def makeEqual(self, words: List[str]) -> bool: - counter = collections.Counter() + counter = Counter() for word in words: for c in word: counter[c] += 1 @@ -82,21 +81,21 @@ class Solution { ```ts function makeEqual(words: string[]): boolean { - let n = words.length; - let letters = new Array(26).fill(0); - for (let word of words) { - for (let i = 0; i < word.length; ++i) { - ++letters[word.charCodeAt(i) - 97]; - } + let n = words.length; + let letters = new Array(26).fill(0); + for (let word of words) { + for (let i = 0; i < word.length; ++i) { + ++letters[word.charCodeAt(i) - 97]; } + } - for (let i = 0; i < letters.length; ++i) { - if (letters[i] % n != 0) { - return false; - } + for (let i = 0; i < letters.length; ++i) { + if (letters[i] % n != 0) { + return false; } - return true; -}; + } + return true; +} ``` ### **C++** diff --git a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.py b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.py index 231221375a012..d6648dbd82e6a 100644 --- a/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.py +++ b/solution/1800-1899/1897.Redistribute Characters to Make All Strings Equal/Solution.py @@ -1,6 +1,6 @@ class Solution: def makeEqual(self, words: List[str]) -> bool: - counter = collections.Counter() + counter = Counter() for word in words: for c in word: counter[c] += 1 diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md index 93f4ff0aed9ae..dc2ed6e87878d 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README.md @@ -65,7 +65,7 @@ class Solution: def longestCommomSubsequence(self, arrays: List[List[int]]) -> List[int]: n = len(arrays) - counter = collections.defaultdict(int) + counter = defaultdict(int) for array in arrays: for e in array: counter[e] += 1 diff --git a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md index 8d38488f8b209..ca9dfaa073521 100644 --- a/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md +++ b/solution/1900-1999/1940.Longest Common Subsequence Between Sorted Arrays/README_EN.md @@ -57,7 +57,7 @@ class Solution: def longestCommomSubsequence(self, arrays: List[List[int]]) -> List[int]: n = len(arrays) - counter = collections.defaultdict(int) + counter = defaultdict(int) for array in arrays: for e in array: counter[e] += 1 diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md index ae1d30cd1f7d9..676fde5c895fa 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README.md @@ -36,7 +36,6 @@
  • s 只包含小写英文字母。
  • - ## 解法 @@ -50,7 +49,7 @@ ```python class Solution: def areOccurrencesEqual(self, s: str) -> bool: - counter = collections.Counter(s) + counter = Counter(s) cnt = -1 for c, times in counter.items(): if cnt == -1: diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md index 5255de6f1b8e8..3175c7c7b157b 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/README_EN.md @@ -34,7 +34,6 @@
  • s consists of lowercase English letters.
  • - ## Solutions @@ -44,7 +43,7 @@ ```python class Solution: def areOccurrencesEqual(self, s: str) -> bool: - counter = collections.Counter(s) + counter = Counter(s) cnt = -1 for c, times in counter.items(): if cnt == -1: diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.py b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.py index c76d3b49d7981..7b3d6c4a7ac73 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.py +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.py @@ -1,6 +1,6 @@ class Solution: def areOccurrencesEqual(self, s: str) -> bool: - counter = collections.Counter(s) + counter = Counter(s) cnt = -1 for c, times in counter.items(): if cnt == -1: diff --git a/solution/1900-1999/1998.GCD Sort of an Array/README.md b/solution/1900-1999/1998.GCD Sort of an Array/README.md index 60729e6db9e22..7947b3afb2a29 100644 --- a/solution/1900-1999/1998.GCD Sort of an Array/README.md +++ b/solution/1900-1999/1998.GCD Sort of an Array/README.md @@ -134,7 +134,7 @@ class Solution: def gcdSort(self, nums: List[int]) -> bool: n = 10 ** 5 + 10 p = list(range(n)) - f = collections.defaultdict(list) + f = defaultdict(list) mx = max(nums) for i in range(2, mx + 1): if f[i]: diff --git a/solution/1900-1999/1998.GCD Sort of an Array/README_EN.md b/solution/1900-1999/1998.GCD Sort of an Array/README_EN.md index f1cfdfea675ee..452d4370060d8 100644 --- a/solution/1900-1999/1998.GCD Sort of an Array/README_EN.md +++ b/solution/1900-1999/1998.GCD Sort of an Array/README_EN.md @@ -50,7 +50,6 @@ We can sort [10,5,9,3,15] by performing the following operations:
  • 2 <= nums[i] <= 105
  • - ## Solutions Union find. @@ -64,23 +63,23 @@ class Solution: def gcdSort(self, nums: List[int]) -> bool: n = 10 ** 5 + 10 p = list(range(n)) - f = collections.defaultdict(list) + f = defaultdict(list) mx = max(nums) for i in range(2, mx + 1): if f[i]: continue for j in range(i, mx + 1, i): f[j].append(i) - + def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] - + for i in nums: for j in f[i]: p[find(i)] = find(j) - + s = sorted(nums) for i, num in enumerate(nums): if s[i] != num and find(num) != find(s[i]): diff --git a/solution/1900-1999/1998.GCD Sort of an Array/Solution.py b/solution/1900-1999/1998.GCD Sort of an Array/Solution.py index e8a0bf66276cc..f8288cf22316a 100644 --- a/solution/1900-1999/1998.GCD Sort of an Array/Solution.py +++ b/solution/1900-1999/1998.GCD Sort of an Array/Solution.py @@ -2,23 +2,23 @@ class Solution: def gcdSort(self, nums: List[int]) -> bool: n = 10 ** 5 + 10 p = list(range(n)) - f = collections.defaultdict(list) + f = defaultdict(list) mx = max(nums) for i in range(2, mx + 1): if f[i]: continue for j in range(i, mx + 1, i): f[j].append(i) - + def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] - + for i in nums: for j in f[i]: p[find(i)] = find(j) - + s = sorted(nums) for i, num in enumerate(nums): if s[i] != num and find(num) != find(s[i]): diff --git a/solution/2000-2099/2034.Stock Price Fluctuation/README.md b/solution/2000-2099/2034.Stock Price Fluctuation/README.md index 7a0dfdb50263a..0ff69f27acf14 100644 --- a/solution/2000-2099/2034.Stock Price Fluctuation/README.md +++ b/solution/2000-2099/2034.Stock Price Fluctuation/README.md @@ -62,7 +62,6 @@ stockPrice.minimum(); // 返回 2 ,最低价格时间戳为 4 ,价格为
  • currentmaximum 和 minimum 被调用时,update 操作 至少 已经被调用过 一次 。
  • - ## 解法 @@ -81,19 +80,19 @@ class StockPrice: self.mp = {} self.mi = [] self.mx = [] - self.counter = collections.Counter() + self.counter = Counter() def update(self, timestamp: int, price: int) -> None: if timestamp in self.mp: old_price = self.mp[timestamp] self.counter[old_price] -= 1 - + self.mp[timestamp] = price self.last_ts = max(self.last_ts, timestamp) self.counter[price] += 1 heapq.heappush(self.mi, price) heapq.heappush(self.mx, -price) - + def current(self) -> int: return self.mp[self.last_ts] @@ -132,7 +131,7 @@ class StockPrice { public StockPrice() { } - + public void update(int timestamp, int price) { if (mp.containsKey(timestamp)) { int oldPrice = mp.get(timestamp); @@ -144,18 +143,18 @@ class StockPrice { mi.offer(price); mx.offer(price); } - + public int current() { return mp.get(lastTs); } - + public int maximum() { while (counter.getOrDefault(mx.peek(), 0) == 0) { mx.poll(); } return mx.peek(); } - + public int minimum() { while (counter.getOrDefault(mi.peek(), 0) == 0) { mi.poll(); @@ -186,9 +185,9 @@ private: unordered_map counter; public: StockPrice() { - + } - + void update(int timestamp, int price) { if (mp.find(timestamp) != mp.end()) { @@ -201,16 +200,16 @@ public: mi.push(price); mx.push(price); } - + int current() { return mp[lastTs]; } - + int maximum() { while (!counter[mx.top()]) mx.pop(); return mx.top(); } - + int minimum() { while (!counter[mi.top()]) mi.pop(); return mi.top(); diff --git a/solution/2000-2099/2034.Stock Price Fluctuation/README_EN.md b/solution/2000-2099/2034.Stock Price Fluctuation/README_EN.md index f4b96d9d900ae..48cff4370423a 100644 --- a/solution/2000-2099/2034.Stock Price Fluctuation/README_EN.md +++ b/solution/2000-2099/2034.Stock Price Fluctuation/README_EN.md @@ -73,19 +73,19 @@ class StockPrice: self.mp = {} self.mi = [] self.mx = [] - self.counter = collections.Counter() + self.counter = Counter() def update(self, timestamp: int, price: int) -> None: if timestamp in self.mp: old_price = self.mp[timestamp] self.counter[old_price] -= 1 - + self.mp[timestamp] = price self.last_ts = max(self.last_ts, timestamp) self.counter[price] += 1 heapq.heappush(self.mi, price) heapq.heappush(self.mx, -price) - + def current(self) -> int: return self.mp[self.last_ts] @@ -122,7 +122,7 @@ class StockPrice { public StockPrice() { } - + public void update(int timestamp, int price) { if (mp.containsKey(timestamp)) { int oldPrice = mp.get(timestamp); @@ -134,18 +134,18 @@ class StockPrice { mi.offer(price); mx.offer(price); } - + public int current() { return mp.get(lastTs); } - + public int maximum() { while (counter.getOrDefault(mx.peek(), 0) == 0) { mx.poll(); } return mx.peek(); } - + public int minimum() { while (counter.getOrDefault(mi.peek(), 0) == 0) { mi.poll(); @@ -176,9 +176,9 @@ private: unordered_map counter; public: StockPrice() { - + } - + void update(int timestamp, int price) { if (mp.find(timestamp) != mp.end()) { @@ -191,16 +191,16 @@ public: mi.push(price); mx.push(price); } - + int current() { return mp[lastTs]; } - + int maximum() { while (!counter[mx.top()]) mx.pop(); return mx.top(); } - + int minimum() { while (!counter[mi.top()]) mi.pop(); return mi.top(); diff --git a/solution/2000-2099/2034.Stock Price Fluctuation/Solution.py b/solution/2000-2099/2034.Stock Price Fluctuation/Solution.py index 43030dd60c118..d5f39ef903a14 100644 --- a/solution/2000-2099/2034.Stock Price Fluctuation/Solution.py +++ b/solution/2000-2099/2034.Stock Price Fluctuation/Solution.py @@ -5,19 +5,18 @@ def __init__(self): self.mp = {} self.mi = [] self.mx = [] - self.counter = collections.Counter() + self.counter = Counter() def update(self, timestamp: int, price: int) -> None: if timestamp in self.mp: old_price = self.mp[timestamp] self.counter[old_price] -= 1 - + self.mp[timestamp] = price self.last_ts = max(self.last_ts, timestamp) self.counter[price] += 1 heapq.heappush(self.mi, price) heapq.heappush(self.mx, -price) - def current(self) -> int: return self.mp[self.last_ts] @@ -38,4 +37,4 @@ def minimum(self) -> int: # obj.update(timestamp,price) # param_2 = obj.current() # param_3 = obj.maximum() -# param_4 = obj.minimum() \ No newline at end of file +# param_4 = obj.minimum() diff --git a/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/README.md b/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/README.md index e476c6712a858..f52045a41a53e 100644 --- a/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/README.md +++ b/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/README.md @@ -64,8 +64,8 @@ class Solution: def minimumDifference(self, nums: List[int]) -> int: n = len(nums) >> 1 - f = collections.defaultdict(set) - g = collections.defaultdict(set) + f = defaultdict(set) + g = defaultdict(set) for i in range(1 << n): s = cnt = 0 s1 = cnt1 = 0 diff --git a/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/README_EN.md b/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/README_EN.md index 9c29714b7eea6..c7eac8abeb3bf 100644 --- a/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/README_EN.md +++ b/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/README_EN.md @@ -55,8 +55,8 @@ The absolute difference between the sums of the arrays is abs((2 + 4 + -9) - (-1 class Solution: def minimumDifference(self, nums: List[int]) -> int: n = len(nums) >> 1 - f = collections.defaultdict(set) - g = collections.defaultdict(set) + f = defaultdict(set) + g = defaultdict(set) for i in range(1 << n): s = cnt = 0 s1 = cnt1 = 0 diff --git a/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/Solution.py b/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/Solution.py index 94529e27cb45a..dfbde532d40d6 100644 --- a/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/Solution.py +++ b/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/Solution.py @@ -1,8 +1,8 @@ class Solution: def minimumDifference(self, nums: List[int]) -> int: n = len(nums) >> 1 - f = collections.defaultdict(set) - g = collections.defaultdict(set) + f = defaultdict(set) + g = defaultdict(set) for i in range(1 << n): s = cnt = 0 s1 = cnt1 = 0