From 8f9e261143103b6dcfc9b327a42ae7287411e984 Mon Sep 17 00:00:00 2001 From: luyunfang Date: Sun, 22 Nov 2020 22:36:48 +0800 Subject: [PATCH 1/7] learning summary updating --- .idea/algorithm020.iml | 11 + .idea/dictionaries/bigheadlu.xml | 3 + .idea/misc.xml | 35 +++ .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 363 ++++++++++++++++++++++++++++++ Week1/deque_add.py | 17 ++ Week1/easy/__init__.py | 0 Week1/easy/dedupe_array.py | 26 +++ Week1/easy/move_zero.py | 28 +++ Week1/hard/__init__.py | 0 Week1/hard/trapping_rain_water.py | 32 +++ Week1/medium/__init__.py | 0 13 files changed, 529 insertions(+) create mode 100644 .idea/algorithm020.iml create mode 100644 .idea/dictionaries/bigheadlu.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 Week1/deque_add.py create mode 100644 Week1/easy/__init__.py create mode 100644 Week1/easy/dedupe_array.py create mode 100644 Week1/easy/move_zero.py create mode 100644 Week1/hard/__init__.py create mode 100644 Week1/hard/trapping_rain_water.py create mode 100644 Week1/medium/__init__.py diff --git a/.idea/algorithm020.iml b/.idea/algorithm020.iml new file mode 100644 index 00000000..0117fde1 --- /dev/null +++ b/.idea/algorithm020.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dictionaries/bigheadlu.xml b/.idea/dictionaries/bigheadlu.xml new file mode 100644 index 00000000..579f2d71 --- /dev/null +++ b/.idea/dictionaries/bigheadlu.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..af69ba38 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + Buildout + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..137444ae --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..19625887 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,363 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1605966230842 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Week1/deque_add.py b/Week1/deque_add.py new file mode 100644 index 00000000..f4a037c1 --- /dev/null +++ b/Week1/deque_add.py @@ -0,0 +1,17 @@ +"""用 add first 或 add last 这套新的 API 改写 Deque 的代码""" +## 不太明白题意 + +from collections import deque + +class Deque_bis(object): + + # nums built from deque + def add_last(self, nums, obj): + return nums.append(obj) + + + def add_first(self, nums, obj): + return nums.appendleft(obj) + + + diff --git a/Week1/easy/__init__.py b/Week1/easy/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Week1/easy/dedupe_array.py b/Week1/easy/dedupe_array.py new file mode 100644 index 00000000..864969b6 --- /dev/null +++ b/Week1/easy/dedupe_array.py @@ -0,0 +1,26 @@ +# ============================================ +# Leetcode 26. 删除排序数组中的重复项 +# 审题: 1. 原地删除,空间复杂度为O(1); 2.数组已为排序过的数组; 3)只需返回新数组的长度。 +# seen before +# 解体思路: 快慢指针 +# time complexity: O(n) +# ============================================ + + +class Solution(object): + def removeDuplicates(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + slow = 0 + fast = 1 + while (fast <= len(nums) - 1): + if nums[slow] == nums[fast]: + fast += 1 + else: + slow += 1 + nums[slow] = nums[fast] + fast += 1 + return slow+1 + diff --git a/Week1/easy/move_zero.py b/Week1/easy/move_zero.py new file mode 100644 index 00000000..cab09150 --- /dev/null +++ b/Week1/easy/move_zero.py @@ -0,0 +1,28 @@ +# ============================================ +# Leetcode 283. 移动零 +# 审题: 1.必须在原数组上操作 +# seen before +# 解体思路: 快慢指针 +# time complexity: O(n) +# ============================================ + +class Solution(object): + def moveZeroes(self, nums): + """ + :type nums: List[int] + :rtype: None Do not return anything, modify nums in-place instead. + """ + slow = 0 + fast = 1 + while (fast <= len(nums) - 1): + if nums[slow] != 0: + slow += 1 + fast += 1 + else: + if nums[fast] != 0: + nums[slow], nums[fast] = nums[fast], nums[slow] + slow += 1 + fast += 1 + else: + fast += 1 + return nums \ No newline at end of file diff --git a/Week1/hard/__init__.py b/Week1/hard/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Week1/hard/trapping_rain_water.py b/Week1/hard/trapping_rain_water.py new file mode 100644 index 00000000..d7b7e019 --- /dev/null +++ b/Week1/hard/trapping_rain_water.py @@ -0,0 +1,32 @@ +# ============================================ +# Leetcode 283. 移动零 +# 审题: 1.non-negative integers +# seen similar one before: nextGreaterNumber, largest rectangle? +# 解体思路: 有序stack +# time complexity: O(n) +# ============================================ + + +class Solution(object): + def trap(self, height): + """ + :type height: List[int] + :rtype: int + """ + stack = [] + # record block value + memo = 0 + # record total trapped rainy water + total = 0 + for i, ele in enumerate(height[::-1]): + while (len(stack) != 0) and (stack[-1][1] < ele): + memo += stack.pop()[1] + # current ele as left bound, s.top() as right bound + if len(stack) != 0: + volume = ele * (stack[-1][0] - i - 1) - memo + if volume > 0: + total += volume + # stack.pop() + stack.append((i, ele)) + memo = 0 + return total diff --git a/Week1/medium/__init__.py b/Week1/medium/__init__.py new file mode 100644 index 00000000..e69de29b From 0521429290d3130fc236d6837f1bc73b23c19fb0 Mon Sep 17 00:00:00 2001 From: luyunfang Date: Sun, 22 Nov 2020 23:22:33 +0800 Subject: [PATCH 2/7] not finished learning summary --- .idea/workspace.xml | 13 ++++++++----- Week1/README.md | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 19625887..1819f7e3 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,9 @@ - + + + @@ -353,8 +356,8 @@ - - + + diff --git a/Week1/README.md b/Week1/README.md index 50de3041..07483e99 100644 --- a/Week1/README.md +++ b/Week1/README.md @@ -1 +1,3 @@ -学习笔记 \ No newline at end of file +学习笔记 +在python中,queue是基于deque实现的。deque拥有queue的全部特性,所以queue的实现相当于只利用了deque的部分特性。 +在python中,heapq基于堆的特性来实现的,其中堆则直接借助list来实现。其中,算法的核心就是维护这个堆。 \ No newline at end of file From 8fa432f8d1d7923bfda475e3a8e9548ae463f5ab Mon Sep 17 00:00:00 2001 From: luyunfang Date: Mon, 23 Nov 2020 07:58:23 +0800 Subject: [PATCH 3/7] modify deque_add --- .idea/workspace.xml | 59 ++++++++++++++++++++++++------- Week1/deque_add.py | 19 +++++----- Week1/hard/trapping_rain_water.py | 7 ++++ 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1819f7e3..2694bc28 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,8 @@ - + + @@ -211,7 +212,25 @@ - + + + + + + + + + + + + + + + + + + + - - + + - - + + diff --git a/Week1/deque_add.py b/Week1/deque_add.py index f4a037c1..25798232 100644 --- a/Week1/deque_add.py +++ b/Week1/deque_add.py @@ -3,15 +3,12 @@ from collections import deque -class Deque_bis(object): - - # nums built from deque - def add_last(self, nums, obj): - return nums.append(obj) - - - def add_first(self, nums, obj): - return nums.appendleft(obj) - - +# initiate a deque of python +test_deq = deque([1,2,3,4,5]) +# 相当于java里的add_last +test_deq.append(6) +# 相当于java里的add_first +test_deq.appendleft(0) + +print(test_deq) diff --git a/Week1/hard/trapping_rain_water.py b/Week1/hard/trapping_rain_water.py index d7b7e019..1b3fbc9f 100644 --- a/Week1/hard/trapping_rain_water.py +++ b/Week1/hard/trapping_rain_water.py @@ -30,3 +30,10 @@ def trap(self, height): stack.append((i, ele)) memo = 0 return total + + + + + + + From f9fa35cba98eb0cf7f9bbafd3413bf4bc6266132 Mon Sep 17 00:00:00 2001 From: luyunfang Date: Fri, 27 Nov 2020 10:43:26 +0800 Subject: [PATCH 4/7] add learning summary 2) for week2 --- .idea/workspace.xml | 152 +++++++++++++++++++++++--------------------- Week2/README.md | 10 ++- 2 files changed, 87 insertions(+), 75 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2694bc28..f7904747 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,8 @@ - - + + - + - + @@ -29,7 +29,7 @@ - + @@ -49,13 +49,13 @@ - + - + @@ -65,6 +65,16 @@ + + + + + + + + + + @@ -86,6 +96,7 @@ @@ -110,7 +121,6 @@ - @@ -133,66 +143,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -323,21 +280,21 @@ - - - - + + + + - + - - + + - - + + @@ -365,11 +322,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -384,7 +380,7 @@ - + @@ -397,5 +393,13 @@ + + + + + + + + \ No newline at end of file diff --git a/Week2/README.md b/Week2/README.md index 50de3041..62b2f48e 100644 --- a/Week2/README.md +++ b/Week2/README.md @@ -1 +1,9 @@ -学习笔记 \ No newline at end of file +学习笔记 + +2. 为什么和树相关的问题一般会使用递归的方式? + 1) 树的结构决定了树在大多数情况下是不适合用循环的方法: 如果树的每个节点除了有可以储存值的数据域,还有两个分别指向左孩子和右孩子的指针,这两个指针可以类比链表里的单指针,但是它们只能遍历子树的一个孩子一下的一些节点, + 由于树的这种特性也就决定了树在大多数情况下是不适合用循环的方法去解决问题的;但是这里也有一种可能树的结构和链表单指针结构是一样的,就是树的所有节点的度(即孩子的个数只为1). + 2) 树的节点 + (左子树) + (右子树), 左子树 = 左子树节点 + (subleftroot.left) + (subleftroot.right), 右子树 = 右子树节点 + (subrightroot.left) + (subrightroot.right), + 而所有节点都是同一属性的TreeNode, 这就是一个层层嵌套的结构,一个树是含有重复嵌套结构或者说重复子问题的结构。 + 3)在实际的一些应用场景,解决树的一些问题例如插入、删除操作需要先遍历再做访问,在这里可以使用循环的方法,但是要借助栈做缓存保留遍历的记忆,这个本质上还是和递归里层层走进嵌入的模式是一样的。 + \ No newline at end of file From 3c8c99d779880505aaa5baed58db8148632cad9c Mon Sep 17 00:00:00 2001 From: luyunfang Date: Sun, 29 Nov 2020 21:21:15 +0800 Subject: [PATCH 5/7] week2 initial commit --- .idea/workspace.xml | 190 +++++++++++++++++++++++++++------------- Week2/README.md | 1 - Week2/__init__.py | 0 Week2/easy/__init__.py | 0 Week2/easy/isAnagram.py | 53 +++++++++++ Week2/easy/preorder.py | 35 ++++++++ 6 files changed, 215 insertions(+), 64 deletions(-) create mode 100644 Week2/__init__.py create mode 100644 Week2/easy/__init__.py create mode 100644 Week2/easy/isAnagram.py create mode 100644 Week2/easy/preorder.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f7904747..dc331e9a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -15,61 +15,21 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + - - + + - - + + @@ -97,11 +57,14 @@ - @@ -121,6 +84,8 @@ + + @@ -147,16 +112,51 @@ + + + + + + + + + + + + + + + + + + - - + + + + + - + @@ -309,7 +309,7 @@ - + @@ -317,7 +317,7 @@ - + @@ -333,7 +333,7 @@ - + @@ -346,10 +346,18 @@ + + + + + + + + - + @@ -357,7 +365,31 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -369,6 +401,22 @@ + + + + + + + + + + + + + + + + @@ -377,18 +425,18 @@ - + - - + + - + - - + + @@ -396,7 +444,23 @@ - + + + + + + + + + + + + + + + + + diff --git a/Week2/README.md b/Week2/README.md index 62b2f48e..a608f8b9 100644 --- a/Week2/README.md +++ b/Week2/README.md @@ -6,4 +6,3 @@ 2) 树的节点 + (左子树) + (右子树), 左子树 = 左子树节点 + (subleftroot.left) + (subleftroot.right), 右子树 = 右子树节点 + (subrightroot.left) + (subrightroot.right), 而所有节点都是同一属性的TreeNode, 这就是一个层层嵌套的结构,一个树是含有重复嵌套结构或者说重复子问题的结构。 3)在实际的一些应用场景,解决树的一些问题例如插入、删除操作需要先遍历再做访问,在这里可以使用循环的方法,但是要借助栈做缓存保留遍历的记忆,这个本质上还是和递归里层层走进嵌入的模式是一样的。 - \ No newline at end of file diff --git a/Week2/__init__.py b/Week2/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Week2/easy/__init__.py b/Week2/easy/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Week2/easy/isAnagram.py b/Week2/easy/isAnagram.py new file mode 100644 index 00000000..9a2fbc67 --- /dev/null +++ b/Week2/easy/isAnagram.py @@ -0,0 +1,53 @@ +# ============================================ +# Leetco 242. 有效的字母异位词 +# 审题: 1. 进阶的情况: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况 +# not seen before +# 解体思路: 字典形式储存词频 +# time complexity: O(2n)≈O(n) +# ============================================ + + +# TODO Solution1 +#执行用时:48 ms, 在所有 Python 提交中击败了51.42%的用户 +#内存消耗:14.9 MB, 在所有 Python 提交中击败了11.08%的用户 +class Solution1(object): + def isAnagram(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + # record the frequency/count of letters in s by key-value data structure + dict_ct = {} + if len(s) != len(t): + return False + for i in range(len(s)): + if s[i] not in dict_ct: + dict_ct[s[i]] = 0 + dict_ct[s[i]] += 1 + for i in range(len(t)): + if t[i] in dict_ct: + dict_ct[t[i]] -= 1 + else: + return False + + if dict_ct[t[i]] == 0: + del dict_ct[t[i]] + + return (not dict_ct) + + +# TODO 内存还是没有上来 +class Solution2(object): + def isAnagram(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + if len(s) != len(s): + return False + if sorted(s) == sorted(t): + return True + else: + return False \ No newline at end of file diff --git a/Week2/easy/preorder.py b/Week2/easy/preorder.py new file mode 100644 index 00000000..e0b68d47 --- /dev/null +++ b/Week2/easy/preorder.py @@ -0,0 +1,35 @@ +# ============================================ +# Leetco 242. 有效的字母异位词 +# 审题: 1. 对一个node的定义,判断一个node,直接可以if node,无需node.val, 不然会有'NoneType has no attribute of val'。 +# not seen before +# 解体思路: 1.递归;2.stack 迭代 +# time complexity: 1.recursive: O(n), n=Tree.size +# ============================================ + +""" +# Definition for a Node. +class Node(object): + def __init__(self, val=None, children=None): + self.val = val + self.children = children +""" + + +class Solution(object): + + def preorder(self, root): + """ + :type root: Node + :rtype: List[int] + """ + vec = [] + if not root: + return vec + + def preorder_helper(node): + vec.append(node.val) + if node.children: + for child in node.children: + preorder_helper(child) + return vec + return preorder_helper(root) \ No newline at end of file From 9b2d6f8a85d7dea81d7a904e715ef379e3491c98 Mon Sep 17 00:00:00 2001 From: luyunfang Date: Sun, 6 Dec 2020 21:35:21 +0800 Subject: [PATCH 6/7] submit week3 --- .idea/workspace.xml | 153 +++++++++++++++------ Week2/easy/preorder.py | 18 ++- Week2/medium/__init__.py | 0 Week2/medium/topK.py | 8 ++ Week3/README.md | 12 +- Week3/medium/__init__.py | 0 Week3/medium/buildTree_preorder_inorder.py | 42 ++++++ Week3/medium/lca.py | 58 ++++++++ 8 files changed, 244 insertions(+), 47 deletions(-) create mode 100644 Week2/medium/__init__.py create mode 100644 Week2/medium/topK.py create mode 100644 Week3/medium/__init__.py create mode 100644 Week3/medium/buildTree_preorder_inorder.py create mode 100644 Week3/medium/lca.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index dc331e9a..0f0a3a1d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,8 @@ - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -59,12 +90,17 @@ - @@ -108,7 +144,7 @@