From 293d1a2259790e5049bdbdd61d41ddb600bb6ffe Mon Sep 17 00:00:00 2001 From: Jam <1248659692@qq.com> Date: Mon, 2 May 2022 10:55:22 +0800 Subject: [PATCH] Update leetcode_crawler.py --- .idea/leetcode.iml | 2 +- .idea/misc.xml | 5 +++- README.md | 3 +- .../backtracking/backtracking.py | 2 ++ py_tricks/maxsplit.py | 4 +-- spider/leetcode_crawler.py | 3 +- spider/problems/242-valid-anagram/README.md | 29 +++++++++++++++++++ spider/problems/454-4sum-ii/README.md | 21 ++++++++++++++ 8 files changed, 62 insertions(+), 7 deletions(-) diff --git a/.idea/leetcode.iml b/.idea/leetcode.iml index 8b8c3954..8ba5c6a2 100644 --- a/.idea/leetcode.iml +++ b/.idea/leetcode.iml @@ -2,7 +2,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index b96032a0..455e02ea 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ - + + + \ No newline at end of file diff --git a/README.md b/README.md index 074ef2ae..d5fb2ed9 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,8 @@ def sliding_window_template_with_examples(s, p): >* 剑指 Offer 04. 二维数组中的查找 >* 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 ```python3 -# two pointers scenario, famous applications such as binary search, quick sort and sliding window. +# two pointers scenario, famous applications such as binary search, quick sort and sliding window.""" +# --译:双指针场景,著名的应用包括二分查找、快速排序和滑动窗口(scenario:/səˈnærioʊ/ 场景;binary:/ˈbaɪnəri/ 二进制的;quicksort:/kwɪk sɔːrt/ 快速排序;sliding:/ˈslaɪdɪŋ/ 滑动的) ''' Classification: diff --git a/algorithm_templates/backtracking/backtracking.py b/algorithm_templates/backtracking/backtracking.py index d7147a5c..fe82d73e 100644 --- a/algorithm_templates/backtracking/backtracking.py +++ b/algorithm_templates/backtracking/backtracking.py @@ -3,7 +3,9 @@ # ("backtracks") as soon as it determines that the candidate cannot possibly be completed to a valid solution. # # backtracking vs. dfs +# ��:���ݷ��������������� # traverse in solution space vs. traverse in data structure space, pruned of dfs +# ��:�ڽ��ռ��б����������ݽṹ�ռ��б������������������ļ�֦ def backtracking(self, data, candidate): diff --git a/py_tricks/maxsplit.py b/py_tricks/maxsplit.py index 6c60b3b8..590c6702 100644 --- a/py_tricks/maxsplit.py +++ b/py_tricks/maxsplit.py @@ -1,10 +1,10 @@ #! /usr/bin/env python3 -"""split a string max times""" +"""split a string max times -- �룺���ַ�����������������split��/spl?t/ ���֣�""" string = "a_b_c" print(string.split("_", 1)) -"""use maxsplit with arbitrary whitespace""" +"""use max split with arbitrary whitespace -- �룺ʹ�ð������հ��ַ����������ִ�����arbitrary��/?��?rb?treri/ �����ģ�whitespace��/?wa?t.spe?s/ �հ��ַ� ��""" s = "foo bar foobar foo" diff --git a/spider/leetcode_crawler.py b/spider/leetcode_crawler.py index 31e1c734..d448d87c 100644 --- a/spider/leetcode_crawler.py +++ b/spider/leetcode_crawler.py @@ -142,7 +142,7 @@ def get_problems_describe(self, filters=None): print('sqlite ',IS_SUCCESS) while not IS_SUCCESS and req_retry<3: # try: - # 休眠随机 1 - 2 秒,以免爬去频率过高被服务器禁掉 + # 爬虫休眠随机 1 - 2 秒,爬取频率过高会被服务器检测到访问频率过快而禁掉ip的问题 time.sleep(random.randint(1, 5)) req_retry+=1 cursor = conn.cursor() @@ -577,7 +577,6 @@ def generate_questions_submission(self, path, filters): print('Specified tag is: {}'.format(args.tags)) leetcr.connect_mysql() - # leetcr.get_problems_describe() # leetcr.get_ac_questions_submission_json(filters,skipsubm=True) if args_dict.get('code'): diff --git a/spider/problems/242-valid-anagram/README.md b/spider/problems/242-valid-anagram/README.md index 67768255..415d4a21 100644 --- a/spider/problems/242-valid-anagram/README.md +++ b/spider/problems/242-valid-anagram/README.md @@ -32,4 +32,33 @@ class Solution: return sorted(list(s))==sorted(list(t)) ``` + +## 解法 +```python3 +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if not s or not t: + return False + if len(s) != len(t): + return False + #排序方式 + # return sorted(list(s)) == sorted(list(t)) + + # 字典方式: + # s 和 t 仅包含小写字母 ,这里也可以通过ord返回ASCII字符对应的整数直接定义数组的方式处理 + s_map = dict() + for c in s: + s_map[c] = s_map.get(c, 0) + 1 + for a in t: + if a not in s_map: + return False + else: + cnt = s_map.get(a, 0) - 1 + s_map[a] = cnt + if cnt < 0: + return False + return True + +``` + [title]: https://leetcode-cn.com/problems/valid-anagram diff --git a/spider/problems/454-4sum-ii/README.md b/spider/problems/454-4sum-ii/README.md index 5233cd46..089e2692 100644 --- a/spider/problems/454-4sum-ii/README.md +++ b/spider/problems/454-4sum-ii/README.md @@ -33,5 +33,26 @@ l)` 能满足: **Difficulty:** Medium ## 思路 +```python3 +from typing import List +# leetcode submit region begin(Prohibit modification and deletion) +class Solution: + def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: + res = 0 + dic = {} + # 计算前两个list对应的累计值字典:key:累加值,val:下标 + for i, a in enumerate(nums1): + for j, b in enumerate(nums2): + dic[a + b] = dic.get(a + b, 0) + 1 + # 遍历后两个数组计算,值是否存在 + for i, a in enumerate(nums3): + for j, b in enumerate(nums4): + c = 0 - a - b + if c in dic and dic.get(c)> -1: + # 累加时:注意,这里如果值相同,但是位置不同,算是多种组合,所以这里直接累加字典中该key的值即可 + res += dic.get(c) + return res +# leetcode submit region end(Prohibit modification and deletion) +``` [title]: https://leetcode-cn.com/problems/4sum-ii

AltStyle によって変換されたページ (->オリジナル) /