From 488ac49fb23300e2cdbc0262bb101df2ab4c7022 Mon Sep 17 00:00:00 2001 From: zhudongdong Date: Fri, 20 Mar 2020 17:30:26 +0800 Subject: [PATCH 1/8] =?UTF-8?q?13=20=E4=B8=AA=E9=93=BE=E8=A1=A8=E7=AE=97?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E4=BD=BF=E7=94=A8python3.7.4=E7=BC=96?= =?UTF-8?q?=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../3.1 Linked List Cycle/README.md | 29 ++++++++++++++ .../3.10 Remove Duplicates II/README.md | 30 ++++++++++++++ .../README.md | 26 +++++++++++++ .../3.12 Partition Linked List/README.md | 39 +++++++++++++++++++ [3]. Linked List/3.13 Odd Even List/README.md | 38 ++++++++++++++++++ .../README.md | 38 ++++++++++++++++++ .../3.3 Reverse Linked List/README.md | 25 ++++++++++++ .../3.4 Merge Two Linked Lists/README.md | 27 +++++++++++++ .../README.md | 25 ++++++++++++ .../README.md | 17 ++++++++ .../README.md | 32 +++++++++++++++ .../README.md | 19 +++++++++ .../3.9 Remove Duplicates I/README.md | 22 +++++++++++ 13 files changed, 367 insertions(+) diff --git a/[3]. Linked List/3.1 Linked List Cycle/README.md b/[3]. Linked List/3.1 Linked List Cycle/README.md index 9a2b551..d1e709f 100644 --- a/[3]. Linked List/3.1 Linked List Cycle/README.md +++ b/[3]. Linked List/3.1 Linked List Cycle/README.md @@ -85,3 +85,32 @@ public class Solution { } } ``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def is_circle(node): + if not node: + return False + l1 = node + l2 = node + res = False + while l1.next != None and l2.next != None: + l1 = l1.next + if l2.next.next: + l2 = l2.next.next + if l1 == l2: + # print(l1.value, l2.value, sep="**") + res = True + break + return res + +``` \ No newline at end of file diff --git a/[3]. Linked List/3.10 Remove Duplicates II/README.md b/[3]. Linked List/3.10 Remove Duplicates II/README.md index 41ca265..e3e50ee 100644 --- a/[3]. Linked List/3.10 Remove Duplicates II/README.md +++ b/[3]. Linked List/3.10 Remove Duplicates II/README.md @@ -119,4 +119,34 @@ class Solution { } } +``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def del_dup_node_2(node): + if not node: + return + fake_head = ListNode(0) + fake_head.next = node + pre = fake_head + cur = node + while cur.next: + while cur.next.value == cur.value: + cur = cur.next + if pre.next == cur: + pre = pre.next + else: + pre.next = cur.next + cur = cur.next + return fake_head.next + ``` \ No newline at end of file diff --git a/[3]. Linked List/3.11 Remove Linked List Elements/README.md b/[3]. Linked List/3.11 Remove Linked List Elements/README.md index 96f07fa..ecaa5a9 100644 --- a/[3]. Linked List/3.11 Remove Linked List Elements/README.md +++ b/[3]. Linked List/3.11 Remove Linked List Elements/README.md @@ -111,4 +111,30 @@ class Solution { return returnNode; } } +``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def del_node_value(node, value): + if not node: + return None + fake_head = ListNode(-1) + fake_head.next = node + cur = fake_head + while cur.next: + if cur.next.value == value: + cur.next = cur.next.next + else: + cur = cur.next + return fake_head.next + ``` \ No newline at end of file diff --git a/[3]. Linked List/3.12 Partition Linked List/README.md b/[3]. Linked List/3.12 Partition Linked List/README.md index ded40ba..f665af6 100644 --- a/[3]. Linked List/3.12 Partition Linked List/README.md +++ b/[3]. Linked List/3.12 Partition Linked List/README.md @@ -125,4 +125,43 @@ class Solution { } } +``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def partition_node_value(node, value): + if not node: + return + l, l_pre = None, None + h, h_pre = None, None + cur = node + while cur: + if cur.value > value: + if not h: + h = cur + h_pre = cur + else: + h_pre.next = cur + h_pre = cur + else: + if not l: + l = cur + l_pre = cur + else: + l_pre.next = cur + l_pre = cur + cur = cur.next + h_pre.next = None + l_pre.next = h + return l + ``` \ No newline at end of file diff --git a/[3]. Linked List/3.13 Odd Even List/README.md b/[3]. Linked List/3.13 Odd Even List/README.md index d33fc64..78de582 100644 --- a/[3]. Linked List/3.13 Odd Even List/README.md +++ b/[3]. Linked List/3.13 Odd Even List/README.md @@ -167,4 +167,42 @@ class Solution { } } } +``` +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def partition_node_value_odd(node): + if not node: + return + l, l_pre = None, None + h, h_pre = None, None + cur = node + while cur: + if cur.value % 2 == 0: + if not h: + h = cur + h_pre = cur + else: + h_pre.next = cur + h_pre = cur + else: + if not l: + l = cur + l_pre = cur + else: + l_pre.next = cur + l_pre = cur + cur = cur.next + h_pre.next = None + l_pre.next = h + return l + ``` \ No newline at end of file diff --git a/[3]. Linked List/3.2 Intersection Of Two Linked Lists/README.md b/[3]. Linked List/3.2 Intersection Of Two Linked Lists/README.md index 4717345..42bb33d 100644 --- a/[3]. Linked List/3.2 Intersection Of Two Linked Lists/README.md +++ b/[3]. Linked List/3.2 Intersection Of Two Linked Lists/README.md @@ -159,4 +159,42 @@ public class Solution { return count; } } +``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def get_intersection_begin(node1, node2): + if not node1 or not node2: + return None + def get_length(node): + if not node: + return -1 + count = 0 + while node: + count += 1 + node = node.next + return count + l1 = get_length(node1) + l2 = get_length(node2) + inter_count = l1 - l2 + n1,n2 = node1, node2 + for l in range(abs(inter_count)): + if inter_count > 0: + n1 = n1.next + else: + n2 = n2.next + while n1 != n2: + n1 = n1.next + n2 = n2.next + return n1 + ``` \ No newline at end of file diff --git a/[3]. Linked List/3.3 Reverse Linked List/README.md b/[3]. Linked List/3.3 Reverse Linked List/README.md index 5e9d495..1c747ee 100644 --- a/[3]. Linked List/3.3 Reverse Linked List/README.md +++ b/[3]. Linked List/3.3 Reverse Linked List/README.md @@ -81,3 +81,28 @@ class Solution { } } ``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def reverse_node(node): + if not node: + return + pre = None + cur = node + while cur: + node_next = cur.next + cur.next = pre + pre = cur + cur = node_next + return pre + +``` \ No newline at end of file diff --git a/[3]. Linked List/3.4 Merge Two Linked Lists/README.md b/[3]. Linked List/3.4 Merge Two Linked Lists/README.md index ee6d0a7..056f21b 100644 --- a/[3]. Linked List/3.4 Merge Two Linked Lists/README.md +++ b/[3]. Linked List/3.4 Merge Two Linked Lists/README.md @@ -109,3 +109,30 @@ class Solution { } } ``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def merge_node(node1, node2): + if not node1: + return node2 + if not node2: + return node1 + res = None + if node1.value < node2.value: + res = node1 + res.next = merge_node(node1.next, node2) + else: + res = node2 + res.next = merge_node(node2.next,node1) + return res + +``` \ No newline at end of file diff --git a/[3]. Linked List/3.5 Find the Kth to tail in Linked List/README.md b/[3]. Linked List/3.5 Find the Kth to tail in Linked List/README.md index 5731f70..bb8d28a 100644 --- a/[3]. Linked List/3.5 Find the Kth to tail in Linked List/README.md +++ b/[3]. Linked List/3.5 Find the Kth to tail in Linked List/README.md @@ -91,4 +91,29 @@ public ListNode FindKthToTail(ListNode pListHead, int k) { return pBehind; } ``` +### Python3.7 +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def get_node_k_reverse(node, k): + if not node or k <= 0: + return None + head1 = node + for _ in range(k): + if not head1: + return None + head1 = head1.next + head2 = node + while head1: + head1 = head1.next + head2 = head2.next + return head2 + +``` diff --git a/[3]. Linked List/3.6 Recursively Print Linked List /README.md b/[3]. Linked List/3.6 Recursively Print Linked List /README.md index 722122b..9d05f58 100644 --- a/[3]. Linked List/3.6 Recursively Print Linked List /README.md +++ b/[3]. Linked List/3.6 Recursively Print Linked List /README.md @@ -58,4 +58,21 @@ public static void PrintListReversingly(ListNode pHead){ } } ``` +### Python3.7 +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def print_reverse_node(node): + if node: + if node.next: + print_reverse_node(node.next) + print(node.value) + +``` diff --git a/[3]. Linked List/3.7 Remove Nth From End Of Linkded List/README.md b/[3]. Linked List/3.7 Remove Nth From End Of Linkded List/README.md index e289b8d..8510d23 100644 --- a/[3]. Linked List/3.7 Remove Nth From End Of Linkded List/README.md +++ b/[3]. Linked List/3.7 Remove Nth From End Of Linkded List/README.md @@ -111,4 +111,36 @@ class Solution { } } +``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def del_node_reverse_k(node, k): + if not node or k <= 0: + return + head1 = node + for _ in range(k): + if not head1: + return + head1 = head1.next + if head1 == None: + node = node.next + return node + head2 = node + while head1.next: + head1 = head1.next + pre = head2 + head2 = head2.next + head2.next = head2.next.next + return node + ``` \ No newline at end of file diff --git a/[3]. Linked List/3.8 Delete Node in a Linked List/README.md b/[3]. Linked List/3.8 Delete Node in a Linked List/README.md index c741e3a..daea7f7 100644 --- a/[3]. Linked List/3.8 Delete Node in a Linked List/README.md +++ b/[3]. Linked List/3.8 Delete Node in a Linked List/README.md @@ -67,3 +67,22 @@ class Solution { } } ``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def del_node_in(node): + if not node or not node.next: + return + node.value = node.next.value + node.next = node.next.next + +``` \ No newline at end of file diff --git a/[3]. Linked List/3.9 Remove Duplicates I/README.md b/[3]. Linked List/3.9 Remove Duplicates I/README.md index ffec46b..232a54a 100644 --- a/[3]. Linked List/3.9 Remove Duplicates I/README.md +++ b/[3]. Linked List/3.9 Remove Duplicates I/README.md @@ -105,4 +105,26 @@ class Solution { } } +``` + +### Python3.7 + +``` + +''' +class ListNode: + def __init__(self, value): + self.value = value + self.next = None +''' + +def del_dup_node_1(node): + if not node: + return + head = node + while head and head.next: + if head.next.value == head.value: + head.next = head.next.next + head = head.next + ``` \ No newline at end of file From 0aa6464602c4fdc57d30bca7ca8acd9fb8230675 Mon Sep 17 00:00:00 2001 From: zhudongdong Date: Fri, 20 Mar 2020 21:21:24 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20tree=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=20python3.7.4=20=E7=9A=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md | 20 ++++++++++++++++ [5]. Tree/5.2 Invert Binary Tree/README.md | 20 ++++++++++++++++ [5]. Tree/5.4 Symmetric Tree/README.md | 24 +++++++++++++++++++ [5]. Tree/5.5 Is Same Tree/README.md | 21 ++++++++++++++++ 4 files changed, 85 insertions(+) diff --git a/[5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md b/[5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md index 4a9ba3c..c750624 100644 --- a/[5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md +++ b/[5]. Tree/5.1 Maximum Depth Of Binary Tree/README.md @@ -87,3 +87,23 @@ class Solution { } } ``` +### Python3 +``` +''' +class TreeNode: + def __init__(self,value): + self.value = value + self.left = None + self.right = None +''' +def get_depth(tree): + if not tree: + return 0 + l = get_depth(tree.left) + r = get_depth(tree.right) + if l > r: + return l + 1 + else: + return r + 1 + +``` \ No newline at end of file diff --git a/[5]. Tree/5.2 Invert Binary Tree/README.md b/[5]. Tree/5.2 Invert Binary Tree/README.md index abc0fbb..27c8faa 100644 --- a/[5]. Tree/5.2 Invert Binary Tree/README.md +++ b/[5]. Tree/5.2 Invert Binary Tree/README.md @@ -102,4 +102,24 @@ class Solution { return root; } } +``` + +### Python3 +``` +''' +class TreeNode: + def __init__(self,value): + self.value = value + self.left = None + self.right = None +''' +def reverse_tree(tree): + if not tree: + return + l = reverse_tree(tree.left) + r = reverse_tree(tree.right) + tree.left = r + tree.right = l + return tree + ``` \ No newline at end of file diff --git a/[5]. Tree/5.4 Symmetric Tree/README.md b/[5]. Tree/5.4 Symmetric Tree/README.md index dec4f86..b81330a 100644 --- a/[5]. Tree/5.4 Symmetric Tree/README.md +++ b/[5]. Tree/5.4 Symmetric Tree/README.md @@ -140,3 +140,27 @@ class Solution { ``` +### Python3 +``` +''' +class TreeNode: + def __init__(self,value): + self.value = value + self.left = None + self.right = None +''' +def is_symmetric(tree): + if not tree: + return tree + def check_tree(left_tree, right_tree): + if not left_tree and not right_tree: + return True + if not left_tree or not right_tree: + return False + if left_tree.value != right_tree.value: + return False + else: + return check_tree(left_tree.left, right_tree.right) and check_tree(left_tree.right, right_tree.left) + return check_tree(tree.left, tree.right) + +``` diff --git a/[5]. Tree/5.5 Is Same Tree/README.md b/[5]. Tree/5.5 Is Same Tree/README.md index ab0702a..3cc763d 100644 --- a/[5]. Tree/5.5 Is Same Tree/README.md +++ b/[5]. Tree/5.5 Is Same Tree/README.md @@ -128,3 +128,24 @@ class Solution { } } ``` + +### Python3 +``` +''' +class TreeNode: + def __init__(self,value): + self.value = value + self.left = None + self.right = None +''' +def is_same(tree1, tree2): + if not tree1 and not tree2: + return True + if not tree1 or not tree2: + return False + if tree1.value != tree2.value: + return False + else: + return is_same(tree1.left, tree2.left) and is_same(tree1.right, tree2.right) + +``` \ No newline at end of file From 9769a83fc629be4178e00f37331e23634c0e82ff Mon Sep 17 00:00:00 2001 From: zhudongdong Date: Sat, 21 Mar 2020 14:53:01 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E9=83=A8=E5=88=86=20python=20=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- [4]. Array/4.1 Contains Duplicate I/README.md | 6 +++ .../4.10 Intersection of Two Arrays/README.md | 16 +++++++ .../README.md | 20 ++++++++ .../4.12 The Kth Largest Element/README.md | 13 +++++ .../4.13 Merge Two Sorted Array/README.md | 20 ++++++++ [4]. Array/4.14 Two Sum I/README.md | 11 +++++ [4]. Array/4.2 Majority Element/README.md | 15 ++++++ [4]. Array/4.3 Single Number/README.md | 12 +++++ [4]. Array/4.4 Missing Number/README.md | 12 +++++ [4]. Array/4.5 Move Zeros/README.md | 14 +++++- [4]. Array/4.6 Remove Element/README.md | 13 +++++ [4]. Array/4.7 Sort Colors/README.md | 24 ++++++++++ [4]. Array/4.8 Two Sum II/README.md | 22 +++++++++ .../4.9 Minimum Sub Array Sum/README.md | 47 +++++++++++++++++++ [5]. Tree/5.3 Balanced Binary Tree/README.md | 30 ++++++++++++ 15 files changed, 274 insertions(+), 1 deletion(-) diff --git a/[4]. Array/4.1 Contains Duplicate I/README.md b/[4]. Array/4.1 Contains Duplicate I/README.md index 1bb7589..5aba2c2 100644 --- a/[4]. Array/4.1 Contains Duplicate I/README.md +++ b/[4]. Array/4.1 Contains Duplicate I/README.md @@ -108,4 +108,10 @@ class Solution { return false; } } +``` + +### Python + +``` + ``` \ No newline at end of file diff --git a/[4]. Array/4.10 Intersection of Two Arrays/README.md b/[4]. Array/4.10 Intersection of Two Arrays/README.md index f5f27ff..a79c903 100644 --- a/[4]. Array/4.10 Intersection of Two Arrays/README.md +++ b/[4]. Array/4.10 Intersection of Two Arrays/README.md @@ -92,3 +92,19 @@ class Solution { ``` + +### Python + +``` +def intersection_arr(arr1, arr2): + dict1 = {} + for num in arr1: + if num not in dict1: + dict1[num] = None + res = [] + for num in arr2: + if num in dict1 and dict1[num] == None: + dict1[num] = num + res.append(num) + return res +``` diff --git a/[4]. Array/4.11 The Top Kth Frequent Element/README.md b/[4]. Array/4.11 The Top Kth Frequent Element/README.md index cd1e8d2..72c59c5 100644 --- a/[4]. Array/4.11 The Top Kth Frequent Element/README.md +++ b/[4]. Array/4.11 The Top Kth Frequent Element/README.md @@ -119,3 +119,23 @@ class Solution { } } ``` + + +### Python + +``` +def top_k_element(arr, k): + if not len(arr): + return + dic = {} + for num in arr: + if num not in dic: + dic[num] = 1 + else: + dic[num] += 1 + res = sorted(dic.items(), key=lambda x:x[1], reverse=True) + res1 = [] + for i in range(0, k): + res1.append(res[i][0]) + return res1 +``` diff --git a/[4]. Array/4.12 The Kth Largest Element/README.md b/[4]. Array/4.12 The Kth Largest Element/README.md index e56f346..0cba90a 100644 --- a/[4]. Array/4.12 The Kth Largest Element/README.md +++ b/[4]. Array/4.12 The Kth Largest Element/README.md @@ -178,3 +178,16 @@ class Solution { } ``` + +### Python + +``` +def find_top_k(arr, target): + if not len(arr): + return None + arr.sort() + count = len(arr) + if target > count + 1: + return None + return arr[count-target] +``` diff --git a/[4]. Array/4.13 Merge Two Sorted Array/README.md b/[4]. Array/4.13 Merge Two Sorted Array/README.md index ca9d283..10df3ed 100644 --- a/[4]. Array/4.13 Merge Two Sorted Array/README.md +++ b/[4]. Array/4.13 Merge Two Sorted Array/README.md @@ -107,3 +107,23 @@ class Solution { } } ``` + + +### Python + +``` +def merge_two_2(arr1, m, arr2, n): + i = m + n - 1 + while m > 0 and n > 0: + if arr1[m-1] > arr2[n-1]: + arr1[i] = arr1[m-1] + m -= 1 + else: + arr1[i] = arr2[n-1] + n -= 1 + i -= 1 + while n > 0: + arr1[i] = arr2[n-1] + i -= 1 + n -= 1 +``` \ No newline at end of file diff --git a/[4]. Array/4.14 Two Sum I/README.md b/[4]. Array/4.14 Two Sum I/README.md index c2fd521..8766bf6 100644 --- a/[4]. Array/4.14 Two Sum I/README.md +++ b/[4]. Array/4.14 Two Sum I/README.md @@ -82,3 +82,14 @@ class Solution { } ``` +### Python +``` +def find_two_unsort(arr, target): + res = [] + for num in arr: + if target - num in arr: + res.append(arr.index(num)) + res.append(arr.index(target - num)) + break + return res +``` \ No newline at end of file diff --git a/[4]. Array/4.2 Majority Element/README.md b/[4]. Array/4.2 Majority Element/README.md index d557042..18be50a 100644 --- a/[4]. Array/4.2 Majority Element/README.md +++ b/[4]. Array/4.2 Majority Element/README.md @@ -92,4 +92,19 @@ class Solution { } ``` +### Python +``` +def is_duplicated(arr): + if not len(arr): + return False + res = False + dict = {} + for i in arr: + if i not in dict: + dict[i] = i + else: + res = True + break + return res +``` diff --git a/[4]. Array/4.3 Single Number/README.md b/[4]. Array/4.3 Single Number/README.md index 69d0d15..fcc61dd 100644 --- a/[4]. Array/4.3 Single Number/README.md +++ b/[4]. Array/4.3 Single Number/README.md @@ -80,3 +80,15 @@ class Solution { } ``` + +### Python + +``` +def find_once(arr): + if not len(arr): + return + res = 0 + for i in arr: + res ^= i # 异或,按位取异或 + return res +``` \ No newline at end of file diff --git a/[4]. Array/4.4 Missing Number/README.md b/[4]. Array/4.4 Missing Number/README.md index 8dc28e3..bb30713 100644 --- a/[4]. Array/4.4 Missing Number/README.md +++ b/[4]. Array/4.4 Missing Number/README.md @@ -123,4 +123,16 @@ public int missingNumber(int[] nums) { ``` +### Python + +``` +def find_missing(arr): + if not len(arr): + return + n = len(arr) + sum = n * (n+1)/2 + for num in arr: + sum -= num + return sum +``` diff --git a/[4]. Array/4.5 Move Zeros/README.md b/[4]. Array/4.5 Move Zeros/README.md index a67c893..d367096 100644 --- a/[4]. Array/4.5 Move Zeros/README.md +++ b/[4]. Array/4.5 Move Zeros/README.md @@ -68,4 +68,16 @@ class Solution { } } } -``` \ No newline at end of file +``` +### Python + +``` +def find_missing(arr): + if not len(arr): + return + n = len(arr) + sum = n * (n+1)/2 + for num in arr: + sum -= num + return sum +``` diff --git a/[4]. Array/4.6 Remove Element/README.md b/[4]. Array/4.6 Remove Element/README.md index d572feb..5644949 100644 --- a/[4]. Array/4.6 Remove Element/README.md +++ b/[4]. Array/4.6 Remove Element/README.md @@ -111,4 +111,17 @@ class Solution { return j; } } +``` +### Python + +``` +def move_value(arr, value): + if not len(arr): + return + index = 0 + for num in arr: + if num != value: + arr[index] = num + index += 1 + return index ``` \ No newline at end of file diff --git a/[4]. Array/4.7 Sort Colors/README.md b/[4]. Array/4.7 Sort Colors/README.md index 62244ea..e1bcfd9 100644 --- a/[4]. Array/4.7 Sort Colors/README.md +++ b/[4]. Array/4.7 Sort Colors/README.md @@ -116,3 +116,27 @@ class Solution { } } ``` + +### Python + +``` +def sort_colors(arr): + if not len(arr): + return + low, mid = 0, 0 + high = len(arr) - 1 + while mid < high: + if arr[mid] == 0: + temp = arr[mid] + arr[mid] = arr[low] + arr[low] = temp + mid += 1 + low += 1 + elif arr[mid] == 1: + mid += 1 + elif arr[mid] == 2: + temp = arr[high] + arr[high] = arr[mid] + arr[mid] = temp + high -= 1 +``` \ No newline at end of file diff --git a/[4]. Array/4.8 Two Sum II/README.md b/[4]. Array/4.8 Two Sum II/README.md index 881386f..12a35a8 100644 --- a/[4]. Array/4.8 Two Sum II/README.md +++ b/[4]. Array/4.8 Two Sum II/README.md @@ -108,4 +108,26 @@ class Solution { return result; } } +``` + +### Python + +``` +def target_index(arr, target): + if len(arr) < 2: + return None + count = len(arr) + l,h = 0,count-1 + res = [] + while l count: + dic[target] = count + else: + dic[target] = count + sum = 0 + count = 0 + elif sum > target: + sum = 0 + count = 0 + if target in dic: + return dic[target] + return 0 + +def min_lenght_2(arr, target): + if not len(arr): + return 0 + l, r = 0, -1 + sum = 0 + count = len(arr) + res = count+1 + while l < count: + if sum < target: + r += 1 + sum += arr[r] + else: + sum -= arr[l] + l += 1 + if sum == target: + res = min(res, r-l+1) + if res == count + 1: + return 0 + return res +``` diff --git a/[5]. Tree/5.3 Balanced Binary Tree/README.md b/[5]. Tree/5.3 Balanced Binary Tree/README.md index 17cd243..45bce48 100644 --- a/[5]. Tree/5.3 Balanced Binary Tree/README.md +++ b/[5]. Tree/5.3 Balanced Binary Tree/README.md @@ -153,4 +153,34 @@ class Solution { } } } +``` + +### Python3 +``` +''' +class TreeNode: + def __init__(self,value): + self.value = value + self.left = None + self.right = None +''' +def is_balance_tree(tree): + if not tree: + return True + def depth_tree(tree): + if not tree: + return 0 + l = depth_tree(tree.left) + r = depth_tree(tree.right) + if l > r: + return l + 1 + else: + return r + 1 + l_depth = depth_tree(tree.left) + r_depth = depth_tree(tree.right) + if abs(l_depth - r_depth) > 1: + return False + else: + return is_balance_tree(tree.left) and is_balance_tree(tree.right) + ``` \ No newline at end of file From 8bce6f2d7adf9b83863c81f9a38d671025d9bc1e Mon Sep 17 00:00:00 2001 From: zhudongdong Date: Sat, 21 Mar 2020 15:11:33 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20string=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=20python=20=E8=AF=AD=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- [4]. Array/4.1 Contains Duplicate I/README.md | 13 +++++++- [4]. Array/4.2 Majority Element/README.md | 31 +++++++++++++++---- [4]. Array/4.3 Single Number/README.md | 1 - [4]. Array/4.4 Missing Number/README.md | 1 - [4]. Array/4.5 Move Zeros/README.md | 15 +++++---- [6]. String/6.1 Reverse String/README.md | 20 ++++++++++++ 6 files changed, 66 insertions(+), 15 deletions(-) diff --git a/[4]. Array/4.1 Contains Duplicate I/README.md b/[4]. Array/4.1 Contains Duplicate I/README.md index 5aba2c2..9b2a196 100644 --- a/[4]. Array/4.1 Contains Duplicate I/README.md +++ b/[4]. Array/4.1 Contains Duplicate I/README.md @@ -113,5 +113,16 @@ class Solution { ### Python ``` - +def is_duplicated(arr): + if not len(arr): + return False + res = False + dict = {} + for i in arr: + if i not in dict: + dict[i] = i + else: + res = True + break + return res ``` \ No newline at end of file diff --git a/[4]. Array/4.2 Majority Element/README.md b/[4]. Array/4.2 Majority Element/README.md index 18be50a..02b80c7 100644 --- a/[4]. Array/4.2 Majority Element/README.md +++ b/[4]. Array/4.2 Majority Element/README.md @@ -95,16 +95,35 @@ class Solution { ### Python ``` -def is_duplicated(arr): +def find_majority(arr): if not len(arr): - return False - res = False + return + count = len(arr) dict = {} + res = None for i in arr: if i not in dict: - dict[i] = i + dict[i] = 1 else: - res = True - break + dict[i] += 1 + if dict[i] > math.floor(count/2): + res = i + break + return res + +def find_majority_2(arr): + if not len(arr): + return + count = 0 + res = None + for i in arr: + if count == 0: + res = i + count = 1 + else: + if i == res: + count += 1 + else: + count -= 1 return res ``` diff --git a/[4]. Array/4.3 Single Number/README.md b/[4]. Array/4.3 Single Number/README.md index fcc61dd..3160f24 100644 --- a/[4]. Array/4.3 Single Number/README.md +++ b/[4]. Array/4.3 Single Number/README.md @@ -80,7 +80,6 @@ class Solution { } ``` - ### Python ``` diff --git a/[4]. Array/4.4 Missing Number/README.md b/[4]. Array/4.4 Missing Number/README.md index bb30713..973ec84 100644 --- a/[4]. Array/4.4 Missing Number/README.md +++ b/[4]. Array/4.4 Missing Number/README.md @@ -135,4 +135,3 @@ def find_missing(arr): sum -= num return sum ``` - diff --git a/[4]. Array/4.5 Move Zeros/README.md b/[4]. Array/4.5 Move Zeros/README.md index d367096..59c9e2e 100644 --- a/[4]. Array/4.5 Move Zeros/README.md +++ b/[4]. Array/4.5 Move Zeros/README.md @@ -72,12 +72,15 @@ class Solution { ### Python ``` -def find_missing(arr): +def move_zero(arr): if not len(arr): return - n = len(arr) - sum = n * (n+1)/2 - for num in arr: - sum -= num - return sum + index = 0 + for i in arr: + if i != 0: + arr[index] = i + index += 1 + count = len(arr) + for i in range(index, count): + arr[i] = 0 ``` diff --git a/[6]. String/6.1 Reverse String/README.md b/[6]. String/6.1 Reverse String/README.md index 0ba044d..87398d9 100644 --- a/[6]. String/6.1 Reverse String/README.md +++ b/[6]. String/6.1 Reverse String/README.md @@ -60,4 +60,24 @@ class Solution { } ``` +### Python +``` +def reverse_string(str): + if len(str) == 1: + return str + return reverse_string(str[1:]) + str[0] + +def reverse_string_2(str): + if len(str) <= 1: + return str + i,j = 0, len(str)-1 + res = list(str) + while i < j: + temp = res[j] + res[j] = res[i] + res[i] = temp + i += 1 + j -= 1 + return ''.join(res) +``` From b9898fa474f97094f13a327e6263b36eaed1137d Mon Sep 17 00:00:00 2001 From: zhudongdong Date: Sat, 21 Mar 2020 15:31:20 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E9=83=A8=E5=88=86=20python3=20=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1.1 Fibonacci Implementation/README.md | 7 ++++++ .../1.2 Binary Search/README.md | 9 ++++++++ .../1.4 Is Ugly Number/README.md | 22 +++++++++++++++++++ .../1.5 Is Power Of Two/README.md | 12 +++++++++- .../1.6 Is Power Of Three/README.md | 10 +++++++++ .../1.7 Count Primes/README.md | 20 +++++++++++++++++ 6 files changed, 79 insertions(+), 1 deletion(-) diff --git a/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md b/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md index a011151..a7d0ea6 100644 --- a/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md +++ b/[1]. Math Implementation/1.1 Fibonacci Implementation/README.md @@ -65,4 +65,11 @@ public static int Fibonacci( int n ){ } ``` +### Python +``` +def fibonacci(num): + if num == 0 or num == 1: + return 1 + return fibonacci(num-1) + fibonacci(num-2) +``` \ No newline at end of file diff --git a/[1]. Math Implementation/1.2 Binary Search/README.md b/[1]. Math Implementation/1.2 Binary Search/README.md index fa8b00e..b549f05 100644 --- a/[1]. Math Implementation/1.2 Binary Search/README.md +++ b/[1]. Math Implementation/1.2 Binary Search/README.md @@ -83,3 +83,12 @@ public static int binary_search( int[] array, int target){ } ``` + +### Python + +``` +def fibonacci(num): + if num == 0 or num == 1: + return 1 + return fibonacci(num-1) + fibonacci(num-2) +``` \ No newline at end of file diff --git a/[1]. Math Implementation/1.4 Is Ugly Number/README.md b/[1]. Math Implementation/1.4 Is Ugly Number/README.md index d91a538..c6aa718 100644 --- a/[1]. Math Implementation/1.4 Is Ugly Number/README.md +++ b/[1]. Math Implementation/1.4 Is Ugly Number/README.md @@ -126,3 +126,25 @@ class Solution { } } ``` + + + +### Python + +``` +def isUgly(num): + if num < 1: + return False + if num == 1: + return True + res = num + while res%2 == 0: + res = res//2 + while res%3 == 0: + res = res//3 + while res%5 == 0: + res = res//5 + if res == 1: + return True + return False +``` diff --git a/[1]. Math Implementation/1.5 Is Power Of Two/README.md b/[1]. Math Implementation/1.5 Is Power Of Two/README.md index 98c4c07..08251b6 100644 --- a/[1]. Math Implementation/1.5 Is Power Of Two/README.md +++ b/[1]. Math Implementation/1.5 Is Power Of Two/README.md @@ -59,5 +59,15 @@ class Solution { } ``` +### Python - +``` +def isPowerOfTwo(num): + if num <= 0: + return False + while num%2 == 0: + num = num//2 + if num == 1: + return True + return False +``` diff --git a/[1]. Math Implementation/1.6 Is Power Of Three/README.md b/[1]. Math Implementation/1.6 Is Power Of Three/README.md index b3f1ec2..6c50d87 100644 --- a/[1]. Math Implementation/1.6 Is Power Of Three/README.md +++ b/[1]. Math Implementation/1.6 Is Power Of Three/README.md @@ -80,3 +80,13 @@ public boolean isPowerOfThree(int n) { +### Python + +``` +def isPowerOfThree(num): + if num == 1 or num == 0: + return True + if num >= 3 and num % 3 == 0: + return isPowerOfThree(num//3) +``` + diff --git a/[1]. Math Implementation/1.7 Count Primes/README.md b/[1]. Math Implementation/1.7 Count Primes/README.md index a69037b..6127455 100644 --- a/[1]. Math Implementation/1.7 Count Primes/README.md +++ b/[1]. Math Implementation/1.7 Count Primes/README.md @@ -102,3 +102,23 @@ boolean isPrime(int n){ ``` +### Python + +``` +def isPrime(num): + if num <= 0: + return False + for i in range(2,num): + if num % i == 0: + return False + return True + +def get_prime_count(n): + count = 0 + if n > 2: + count += 1 + for i in range(3,n): + if isPrime(i): + count += 1 + return count +``` From 1fc03e769eebd833a21747b69c7ac18b9212b2d3 Mon Sep 17 00:00:00 2001 From: zhudongdong Date: Sat, 21 Mar 2020 15:32:16 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1.2 Binary Search/README.md | 16 ++++++++++++---- [1]. Math Implementation/1.3 Is Prime/README.md | 11 +++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/[1]. Math Implementation/1.2 Binary Search/README.md b/[1]. Math Implementation/1.2 Binary Search/README.md index b549f05..ab6e4e0 100644 --- a/[1]. Math Implementation/1.2 Binary Search/README.md +++ b/[1]. Math Implementation/1.2 Binary Search/README.md @@ -87,8 +87,16 @@ public static int binary_search( int[] array, int target){ ### Python ``` -def fibonacci(num): - if num == 0 or num == 1: - return 1 - return fibonacci(num-1) + fibonacci(num-2) +def binary_search(nums, low, high, key): + if not nums or low > high: + return -1 + if key < nums[low] or key > nums[high]: + return -1 + mid = (low + high)//2 + if nums[mid] == key: + return mid + elif nums[mid] > key: + return binary_search(nums, low, mid-1, key) + elif nums[mid] < key: + return binary_search(nums, mid+1, high, key) ``` \ No newline at end of file diff --git a/[1]. Math Implementation/1.3 Is Prime/README.md b/[1]. Math Implementation/1.3 Is Prime/README.md index d7d00c0..24d80c9 100644 --- a/[1]. Math Implementation/1.3 Is Prime/README.md +++ b/[1]. Math Implementation/1.3 Is Prime/README.md @@ -70,6 +70,17 @@ public static boolean is_prime_1 ( int num){ ``` +### Python + +``` +def isPrime(num): + if num <= 0: + return False + for i in range(2,num): + if num % i == 0: + return False + return True +``` From 0309e59d52003cc9a5780790f770aa13fcc4560e Mon Sep 17 00:00:00 2001 From: zhudongdong Date: Sat, 21 Mar 2020 15:53:53 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E9=83=A8=E5=88=86=20python=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2.1 Bubble Sort Implementation/README.md | 15 ++++++++++++ .../README.md | 15 ++++++++++++ .../README.md | 15 ++++++++++++ .../2.4 Quick Sort Implementation/README.md | 23 +++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/[2]. Algorithm Implementation/2.1 Bubble Sort Implementation/README.md b/[2]. Algorithm Implementation/2.1 Bubble Sort Implementation/README.md index 7a6f074..1615b5d 100644 --- a/[2]. Algorithm Implementation/2.1 Bubble Sort Implementation/README.md +++ b/[2]. Algorithm Implementation/2.1 Bubble Sort Implementation/README.md @@ -60,4 +60,19 @@ public static void BubbleSort(int[] arr) { } ``` +### Python +``` +def bubble_sort(nums): + if len(nums) == 0: + return + count = len(nums) + for i in range(0, count): + for j in range(count-1, i, -1): + last = nums[count-j] + last_two = nums[count-j-1] + if last < last_two: + temp = nums[count-j] + nums[count-j] = last_two + nums[count-j-1] = temp +``` diff --git a/[2]. Algorithm Implementation/2.2 Selection Sort Implementation/README.md b/[2]. Algorithm Implementation/2.2 Selection Sort Implementation/README.md index 2657e41..b306d7f 100644 --- a/[2]. Algorithm Implementation/2.2 Selection Sort Implementation/README.md +++ b/[2]. Algorithm Implementation/2.2 Selection Sort Implementation/README.md @@ -72,3 +72,18 @@ public static void SelectionSort(int[] arr) { } } ``` + +### Python + +``` +def select_sort(nums): + if len(nums) == 0: + return + count = len(nums) + for i in range(0, count): + for j in range(i+1, count): + if nums[i] > nums[j]: + temp = nums[i] + nums[i] = nums[j] + nums[j] = temp +``` diff --git a/[2]. Algorithm Implementation/2.3 Insertion Sort Implementation/README.md b/[2]. Algorithm Implementation/2.3 Insertion Sort Implementation/README.md index f905606..a9bfa22 100644 --- a/[2]. Algorithm Implementation/2.3 Insertion Sort Implementation/README.md +++ b/[2]. Algorithm Implementation/2.3 Insertion Sort Implementation/README.md @@ -69,3 +69,18 @@ public static void InsertionSort(int[] arr) { } } ``` + +### Python + +``` +def insert_sort_2(nums): + count = len(nums) + if count == 0: + return + for i in range(1,count): + for j in range(i,0, -1): + if nums[j] < nums[j-1]: + temp = nums[j-1] + nums[j-1] = nums[j] + nums[j] = temp +``` \ No newline at end of file diff --git a/[2]. Algorithm Implementation/2.4 Quick Sort Implementation/README.md b/[2]. Algorithm Implementation/2.4 Quick Sort Implementation/README.md index dfed197..6087f74 100644 --- a/[2]. Algorithm Implementation/2.4 Quick Sort Implementation/README.md +++ b/[2]. Algorithm Implementation/2.4 Quick Sort Implementation/README.md @@ -189,3 +189,26 @@ public static void swap(int a,int b){ } ``` + +### Python + +``` +def quick_sort(nums, left, right): + if left < right: + index = get_index(nums, left, right) + quick_sort(nums, left, index - 1) + quick_sort(nums, index + 1, right) +def get_index(nums, left, right): + base = nums[left] + i, j = left, right + while i < j : + '''直到不满足''' + while i < j and nums[j] > base: + j -= 1 + nums[i] = nums[j] + while i < j and nums[i] < base: + i += 1 + nums[j] = nums[i] + nums[i] = base + return i +``` \ No newline at end of file From 10a4881231d0e5d7840b3b84d6b31602a79bf4cc Mon Sep 17 00:00:00 2001 From: zhudongdong Date: Mon, 23 Mar 2020 22:55:09 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=90=86=E8=A7=A3?= =?UTF-8?q?=E9=A2=98=E6=84=8F=E4=B8=8D=E6=B8=85=E6=99=B0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- [4]. Array/4.6 Remove Element/README.md | 2 +- .../4.9 Minimum Sub Array Sum/README.md | 26 +------------------ 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/[4]. Array/4.6 Remove Element/README.md b/[4]. Array/4.6 Remove Element/README.md index 5644949..bbe4478 100644 --- a/[4]. Array/4.6 Remove Element/README.md +++ b/[4]. Array/4.6 Remove Element/README.md @@ -123,5 +123,5 @@ def move_value(arr, value): if num != value: arr[index] = num index += 1 - return index + return arr[:index] ``` \ No newline at end of file diff --git a/[4]. Array/4.9 Minimum Sub Array Sum/README.md b/[4]. Array/4.9 Minimum Sub Array Sum/README.md index fa2ac94..16b8260 100644 --- a/[4]. Array/4.9 Minimum Sub Array Sum/README.md +++ b/[4]. Array/4.9 Minimum Sub Array Sum/README.md @@ -110,30 +110,6 @@ class Solution { ### Python ``` -def min_length_target(arr, target): - if not len(arr): - return 0 - dic = {} - sum = 0 - count = 0 - for i in arr: - sum += i - count+=1 - if sum == target: - if target in dic: - if dic[target] > count: - dic[target] = count - else: - dic[target] = count - sum = 0 - count = 0 - elif sum > target: - sum = 0 - count = 0 - if target in dic: - return dic[target] - return 0 - def min_lenght_2(arr, target): if not len(arr): return 0 @@ -148,7 +124,7 @@ def min_lenght_2(arr, target): else: sum -= arr[l] l += 1 - if sum == target: + if sum >= target: res = min(res, r-l+1) if res == count + 1: return 0