From b838c85113bc5b304a8fd0ea9e7fda4516ed0449 Mon Sep 17 00:00:00 2001 From: xxd Date: Sun, 28 Apr 2019 09:14:50 +0800 Subject: [PATCH 1/4] week2 --- Week_02/id_55/LeetCode441_55.py | 20 ++++++++++++++++++++ Week_02/id_55/LeetCode_236_55.py | 17 +++++++++++++++++ Week_02/id_55/LeetCode_242_55.py | 12 ++++++++++++ Week_02/id_55/LeetCode_671_55.py | 27 +++++++++++++++++++++++++++ Week_02/id_55/LeetCode_783_55.py | 22 ++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 Week_02/id_55/LeetCode441_55.py create mode 100644 Week_02/id_55/LeetCode_236_55.py create mode 100644 Week_02/id_55/LeetCode_242_55.py create mode 100644 Week_02/id_55/LeetCode_671_55.py create mode 100644 Week_02/id_55/LeetCode_783_55.py diff --git a/Week_02/id_55/LeetCode441_55.py b/Week_02/id_55/LeetCode441_55.py new file mode 100644 index 00000000..e323f2b9 --- /dev/null +++ b/Week_02/id_55/LeetCode441_55.py @@ -0,0 +1,20 @@ +# 这是上周的题一直就想到这个解,感觉拿不出手 +class Solution: + def arrangeCoins(self, n: int) -> int: + level = 1 + while level <= n: + n -= level + level += 1 + return level-1 + + def arrangeCoins2(self, n: int) -> int: + level = 1 + m = int(n/2)+1 #细想了想最多有int(n/2)+1行了吧 + while level <= m: + n -= level + if n < 0: + break + level += 1 + return level-1 + + # 看了别人的二分法思路思路O(logn) diff --git a/Week_02/id_55/LeetCode_236_55.py b/Week_02/id_55/LeetCode_236_55.py new file mode 100644 index 00000000..9e26a314 --- /dev/null +++ b/Week_02/id_55/LeetCode_236_55.py @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +class TreeNode: + def __init__(self, x): + self.val = x + self.left = None + self.right = None + +class Solution: + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + if root in (None, p, q): + return root + left=self.lowestCommonAncestor(root.left, p, q) + right=self.lowestCommonAncestor(root.right, p, q) + if left and right: + return root + else: + return left or right diff --git a/Week_02/id_55/LeetCode_242_55.py b/Week_02/id_55/LeetCode_242_55.py new file mode 100644 index 00000000..1f5851e4 --- /dev/null +++ b/Week_02/id_55/LeetCode_242_55.py @@ -0,0 +1,12 @@ +# 上周有个相同题目用数组但是没法处理unicode,换成字典就可以了 + def isAnagramUnicode(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + alpha = {} + beta = {} + for c in s: + alpha[c] = alpha.get(c, 0) + 1 + for c in t: + beta[c] = beta.get(c, 0) + 1 + return alpha == beta \ No newline at end of file diff --git a/Week_02/id_55/LeetCode_671_55.py b/Week_02/id_55/LeetCode_671_55.py new file mode 100644 index 00000000..c76daa1f --- /dev/null +++ b/Week_02/id_55/LeetCode_671_55.py @@ -0,0 +1,27 @@ +class TreeNode: + def __init__(self, x): + self.val = x + self.left = None + self.right = None + + +class Solution: + + def findSecondMinimumValue(self, root: TreeNode) -> int: + val_set = set([]) + + def findRun(root): + if not root: + return + val_set.add(root.val) + if (root.left): + findRun(root.left) + if (root.right): + findRun(root.right) + + findRun(root) + a = list(val_set) + a.sort() + if len(a) < 2: + return -1 + return a[1] \ No newline at end of file diff --git a/Week_02/id_55/LeetCode_783_55.py b/Week_02/id_55/LeetCode_783_55.py new file mode 100644 index 00000000..918a5115 --- /dev/null +++ b/Week_02/id_55/LeetCode_783_55.py @@ -0,0 +1,22 @@ +# Definition for a binary tree node. +class TreeNode: + def __init__(self, x): + self.val = x + self.left = None + self.right = None + +class Solution: + def minDiffInBST(self, root: TreeNode) -> int: + self.prev = None + self.min_val = float('inf') + self.inOrder(root) + return self.min_val + + def inOrder(self, root): + if not root: + return + self.inOrder(root.left) + if self.prev: + self.min_val = min(self.min_val, root.val - self.prev.val) + self.prev = root + self.inOrder(root.right) \ No newline at end of file From 2e789b4be597601162a68077ae0416ea203d026e Mon Sep 17 00:00:00 2001 From: xxd Date: Mon, 29 Apr 2019 21:29:56 +0800 Subject: [PATCH 2/4] rename --- Week_02/id_55/{LeetCode441_55.py => LeetCode_441_55.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week_02/id_55/{LeetCode441_55.py => LeetCode_441_55.py} (100%) diff --git a/Week_02/id_55/LeetCode441_55.py b/Week_02/id_55/LeetCode_441_55.py similarity index 100% rename from Week_02/id_55/LeetCode441_55.py rename to Week_02/id_55/LeetCode_441_55.py From bf27b9aac45dd946003df28dca1064baf8ee062e Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 6 May 2019 13:42:45 +0800 Subject: [PATCH 3/4] week3 104 429 --- Week_03/id_55/LeetCode_104.55.py | 42 ++++++++++++++++++++++++++++++++ Week_03/id_55/LeetCode_429_55.py | 24 ++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Week_03/id_55/LeetCode_104.55.py create mode 100644 Week_03/id_55/LeetCode_429_55.py diff --git a/Week_03/id_55/LeetCode_104.55.py b/Week_03/id_55/LeetCode_104.55.py new file mode 100644 index 00000000..6ca074a7 --- /dev/null +++ b/Week_03/id_55/LeetCode_104.55.py @@ -0,0 +1,42 @@ +# Definition for a binary tree node. +class TreeNode: + def __init__(self, x): + self.val = x + self.left = None + self.right = None + + +class Solution: + # 深度优先搜索DFS递归解法: + def maxDepthDFS(self, root: TreeNode) -> int: + # print("root val:%d" % (root.val)) + print("root:%s" % (root)) + if root == None: # 递归边界 + return 0 + else: + depth_l = self.maxDepthDFS(root.left) + if depth_l: + print("depth_l:%d" % (depth_l)) + depth_r = self.maxDepthDFS(root.right) + if depth_r: + print("depth_r:%d" % (depth_r)) + return max(depth_l, depth_r) + 1 + + def maxDepthBFS(self, root): + stack = [] + if root is not None: + stack.append((1, root)) # 如果root不是空就把root加入stack,深度为1 + + depth = 0 + while stack != []: # 开始循环stack + print("stack before pop :%s" % (stack)) # 打印pop前的栈 + current_depth, root = stack.pop() # 先取出最后放进去的 + print("current_depth:%d" % (current_depth)) # 打印当前深度从1开始 + print("stack after pop :%s" % (stack)) # 打印pop后的栈 + if root: + print("root.val:%d" % (root.val)) + if root is not None: + depth = max(depth, current_depth) + stack.append((current_depth + 1, root.left)) + stack.append((current_depth + 1, root.right)) + return depth \ No newline at end of file diff --git a/Week_03/id_55/LeetCode_429_55.py b/Week_03/id_55/LeetCode_429_55.py new file mode 100644 index 00000000..5de960f0 --- /dev/null +++ b/Week_03/id_55/LeetCode_429_55.py @@ -0,0 +1,24 @@ +# Definition for a Node. + + +class Node: + def __init__(self, val, children): + self.val = val + self.children = children + + +class Solution: + def levelOrder(self, root: 'Node') -> List[List[int]]: + level = [] + if not root: + return [] + level.append((0, root)) + res = [] + while level: + depth, root = level.pop(0) + if depth >= len(res): + res.append([]) + res[depth].append(root.val) + for node in root.children: + level.append((depth+1, node)) + return res \ No newline at end of file From 002011bc5ada99e36f491017d975c9b28a109cf7 Mon Sep 17 00:00:00 2001 From: xxd Date: Sun, 12 May 2019 21:19:23 +0800 Subject: [PATCH 4/4] week 4 --- Week_02/.DS_Store | Bin 0 -> 6148 bytes Week_04/.DS_Store | Bin 0 -> 6148 bytes Week_04/id_55/LeetCode_169_55.py | 45 +++++++++++++++++++++ Week_04/id_55/LeetCode_455_55.py | 66 +++++++++++++++++++++++++++++++ Week_04/id_55/LeetCode_720_55.py | 38 ++++++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 Week_02/.DS_Store create mode 100644 Week_04/.DS_Store create mode 100644 Week_04/id_55/LeetCode_169_55.py create mode 100644 Week_04/id_55/LeetCode_455_55.py create mode 100644 Week_04/id_55/LeetCode_720_55.py diff --git a/Week_02/.DS_Store b/Week_02/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6761ee2a66ca0473eba12b2c3fbb46830124ea31 GIT binary patch literal 6148 zcmeHK%}T>S5Z=)s?wkYEELku+@&r4W1oA3(4E zJc#-ZK8a^%cDJHbkBZ6+%zl&Enc41_vXfMUDC-ei)O%FmpYwiW{Mig{{cst#4vmiah;JqY?AVAr!w;u-GhBdvG z#F-n$K`QI18b=3V>?hrN(vJO>jMv4bz2jz0sAJ6Ar;1L~FdNovuUB1ODT~Ewt%_{hMB zdbF_rZ+w0K|CmHi!~ikyuNdIPb!WW}ThhICXmi+W1?VX#3dZFM-%`Mktr%jl70-Z5 Z0lz>4(AJnM1P=&(2uK=eAO?Pvfma-sU#S29 literal 0 HcmV?d00001 diff --git a/Week_04/.DS_Store b/Week_04/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a7b7b483b030d8b2e7dfa8c2c2fd1e47de93bf45 GIT binary patch literal 6148 zcmeHK%}T>S5Z-O8CKe=uhYB779=s@2q=>grttUY-#UA`=6B2A7B$B4>K?=bK@B#Gd z&wHp3;fr{8W_K%<>QS*W1GC>`c4oHwrR-!GW4zk+))_MxV;U%8PKV(O!Fkj%$te#a z7d0wwzjNV+u{#z^j=#wOzPl+FFpo7^g^j+yH}@h8I%&E5%5(X`)bxy|&ldIKTv0!@ z+i~AcowVDjI!V)5vIEa|oumSr%Fwiw^ab@@Ltb28Rb9;CH@c8sB(>VNHh0H4)!V4H9boA^b3ghS;yu*xPBq1?C z3=jho%z!%5wBiKsfqptMKn(m019(1|poo^jOrkhCV8G8uoG&1vfQ@$vL{p)qFp~%~ zAY7&b%2ckd7+j`<-&Apy!c3w}XI!idb=1mS-B7q#9sH&UXWWuVBQZb>d}bgo9}VpP zweRo$pOff`7$64z6$8AmWA9X9ORBeyZ4P^_3EBch!MIG~M+z9C6+ majority: + theBiggest = key + return theBiggest + +# 学一波counter的用法... + def majorityElement2(self, nums): + c = Counter(nums) + res = c.most_common(1)[0][0] + return res + +words = [1, 2, 2, 2, 1, 1, 1, 2, 2] +demo = Solution() +print("result: %s" % demo.majorityElement3(words)) diff --git a/Week_04/id_55/LeetCode_455_55.py b/Week_04/id_55/LeetCode_455_55.py new file mode 100644 index 00000000..ff7a0b6a --- /dev/null +++ b/Week_04/id_55/LeetCode_455_55.py @@ -0,0 +1,66 @@ +# coding=UTF8 +# --------------------- +# 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。 +# 对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j , +# 都有一个尺寸 sj 。如果 sj >= gi ,我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。 +# 注意: +# 你可以假设胃口值为正。 +# 一个小朋友最多只能拥有一块饼干。 +# 示例 1: +# 输入: [1,2,3], [1,1] +# 输出: 1 +# 解释: +# 你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。 +# 虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。 +# 所以你应该输出1。 +# 示例 2: +# 输入: [1,2], [1,2,3] +# 输出: 2 +# 解释: +# 你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。 +# 你拥有的饼干数量和尺寸都足以让所有孩子满足。 +# 所以你应该输出2. + + +class Solution: + # 执行用时 : 2564 ms, 在Assign Cookies的Python3提交中击败了5.08% 的用户 + # 内存消耗 : 14.7 MB, 在Assign Cookies的Python3提交中击败了17.98% 的用户 + def findContentChildren(self, g, s): + g = sorted(g) # 胃口 + s = sorted(s) # 饼干 + num, ss, gg = 0, 0, 0 + for i in range(ss, len(s)): + for j in range(gg, len(g)): + if s[i] >= g[j]: + num += 1 + gg = j + 1 + break + else: + ss = i + 1 + continue + return num + + # 用while改造了一下居然提高这么多 + # 执行用时 : 108 ms, 在Assign Cookies的Python3提交中击败了50.07% 的用户 + # 内存消耗 : 14.4 MB, 在Assign Cookies的Python3提交中击败了87.93% 的用户 + def findContentChildren(self, g, s): + g.sort() + s.sort() + count = 0 + i = 0 + j = 0 + while i < len(g) and j < len(s): + if g[i] <= s[j]: + count += 1 + i += 1 + j += 1 + else: + j += 1 + return count + +s = [1, 1] # 饼干 +g = [1, 2, 3] # 孩子 + +demo = Solution() +print("result: %s" % demo.findContentChildren3(g, s)) + diff --git a/Week_04/id_55/LeetCode_720_55.py b/Week_04/id_55/LeetCode_720_55.py new file mode 100644 index 00000000..ab9bb148 --- /dev/null +++ b/Week_04/id_55/LeetCode_720_55.py @@ -0,0 +1,38 @@ +class Solution: + # 暴力方法破解,暴力遍历每个字符串前缀是不是都在数组中,在比较是不是最长的、如果等长看是不是字母序更小 + # 开始没用set,直接用words List结果超时,果然差别很大,Set居然是O(1) + def longestWord(self, words): + wordSet = set(words) + theWord = "" + for w in words: + print(w) + isIn = True + for i in range(1, len(w)): + if w[:i] not in wordSet: + print("%s is not in " % w[:i]) + isIn = False + break + if isIn: + if not theWord or len(theWord) < len(w): + theWord = w + elif len(w) == len(theWord) and theWord > w: + theWord = w + return theWord + + def longestWord2(self, words): + words.sort() # sort成树状结构 + res = set(['']) # 建立一个空的set 直接去重了 + longestWord = '' + for word in words: + print(word) + if word[:-1] in res: # 判断每个单词的除去最后一个字母是否在set里 + res.add(word) + print(res) + if len(word) > len(longestWord): + longestWord = word # 保存最长的词 + return longestWord + + +words = ["a", "banana", "app", "appl", "ap", "apply", "apple"] +demo = Solution() +print("result: %s" % demo.longestWord(words))