From 207fa2c3d427ad55c2c958c69a97ef2d448e89d8 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Fri, 13 Aug 2021 08:46:38 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A00925.=E9=95=BF=E6=8C=89?= =?UTF-8?q?=E9=94=AE=E5=85=A5java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0925.长按键入.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index ef712252..c851a8df 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -9,6 +9,7 @@ # 925.长按键入 题目链接:https://leetcode-cn.com/problems/long-pressed-name/ + 你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。 你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。 @@ -98,7 +99,36 @@ public: # 其他语言版本 Java: - +```java +class Solution { + public boolean isLongPressedName(String name, String typed) { + int i = 0, j = 0; + int m = name.length(), n = typed.length(); + while (i< m && j < n) { + if (name.charAt(i) == typed.charAt(j)) { // 相同则同时向后匹配 + i++; j++; + } + else { + if (j == 0) return false; // 如果是第一位就不相同直接返回false + // 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz" + while (j < n-1 && typed.charAt(j) == typed.charAt(j-1)) j++; + if (name.charAt(i) == typed.charAt(j)) { // j跨越重复项之后再次和name[i]匹配 + i++; j++; // 相同则同时向后匹配 + } + else return false; + } + } + // 说明name没有匹配完 + if (i < m) return false; + // 说明type没有匹配完 + while (j < n) { + if (typed.charAt(j) == typed.charAt(j-1)) j++; + else return false; + } + return true; + } +} +``` Python: ```python class Solution: From 5055e15cd9773ccfa036f8782e70aa00d86732e4 Mon Sep 17 00:00:00 2001 From: shuwen Date: Fri, 13 Aug 2021 10:45:58 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1.md=20Python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用collections.Counter实现 --- problems/0383.赎金信.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index f2c1685a..d4b73848 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -209,6 +209,22 @@ class Solution(object): return True ``` +Python写法四: + +```python3 +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + c1 = collections.Counter(ransomNote) + c2 = collections.Counter(magazine) + x = c1 - c2 + #x只保留值大于0的符号,当c1里面的符号个数小于c2时,不会被保留 + #所以x只保留下了,magazine不能表达的 + if(len(x)==0): + return True + else: + return False +``` + Go: ```go From 5fbf7fb1246423e877617a907b6414c49931c72a Mon Sep 17 00:00:00 2001 From: "Alex.Xu" <48598761+saibeach@users.noreply.github.com> Date: Fri, 13 Aug 2021 13:25:50 -0500 Subject: [PATCH 3/8] =?UTF-8?q?Update=20=E5=85=B3=E4=BA=8E=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6=EF=BC=8C=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E6=9C=89=E5=87=A0=E4=B8=AA=E7=96=91=E9=97=AE=EF=BC=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../前序/关于空间复杂度,可能有几个疑问?.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/前序/关于空间复杂度,可能有几个疑问?.md b/problems/前序/关于空间复杂度,可能有几个疑问?.md index 0f25f378..5b2f73e2 100644 --- a/problems/前序/关于空间复杂度,可能有几个疑问?.md +++ b/problems/前序/关于空间复杂度,可能有几个疑问?.md @@ -30,7 +30,7 @@ 2. 空间复杂度是准确算出程序运行时所占用的内存么? -不要以为空间复杂度就已经精准的掌握了程序的内存使用大小,很有多因素会影响程序真正内存使用大小,例如编译器的内存对齐,编程语言容器的底层实现等等这些都会影响到程序内存的开销。 +不要以为空间复杂度就已经精准的掌握了程序的内存使用大小,很多因素会影响程序真正内存使用大小,例如编译器的内存对齐,编程语言容器的底层实现等等这些都会影响到程序内存的开销。 所以空间复杂度是预先大体评估程序内存使用的大小。 From a395f6f9dfec9fd4c0a4ab560091beac5785cbfc Mon Sep 17 00:00:00 2001 From: hailincai Date: Fri, 13 Aug 2021 14:57:54 -0400 Subject: [PATCH 4/8] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add Java implementation for leetcode 116 --- problems/0102.二叉树的层序遍历.md | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 007eac44..0f9b2df6 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1205,6 +1205,35 @@ public: }; ``` +java代码: + +```java +class Solution { + public Node connect(Node root) { + Queue tmpQueue = new LinkedList(); + if (root != null) tmpQueue.add(root); + + while (tmpQueue.size() != 0){ + int size = tmpQueue.size(); + + Node cur = tmpQueue.poll(); + if (cur.left != null) tmpQueue.add(cur.left); + if (cur.right != null) tmpQueue.add(cur.right); + + for (int index = 1; index < size; index++){ + Node next = tmpQueue.poll(); + if (next.left != null) tmpQueue.add(next.left); + if (next.right != null) tmpQueue.add(next.right); + + cur.next = next; + cur = next; + } + } + + return root; + } +} +``` python代码: From 5812968d028519b4bc566995246d3ded4d6c2947 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Sat, 14 Aug 2021 09:00:32 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0100.相同的树.md | 2 +- problems/1002.查找常用字符.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md index e4eb412a..df0a5dd1 100644 --- a/problems/0100.相同的树.md +++ b/problems/0100.相同的树.md @@ -159,7 +159,7 @@ public: return false; } que.push(leftNode->left); // 添加p节点的左子树节点 - que.push(rightNode->left); // 添加q节点的左子树节点点 + que.push(rightNode->left); // 添加q节点的左子树节点 que.push(leftNode->right); // 添加p节点的右子树节点 que.push(rightNode->right); // 添加q节点的右子树节点 } diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 82dbf4cf..723e5656 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -169,6 +169,7 @@ class Solution { } } ``` +Python ```python class Solution: def commonChars(self, words: List[str]) -> List[str]: From 52cf4e0f18373b76a290ee2a923a45617ba7fad3 Mon Sep 17 00:00:00 2001 From: lichun-chen <81766312+lichun-chen@users.noreply.github.com> Date: Fri, 13 Aug 2021 21:42:03 -0500 Subject: [PATCH 6/8] =?UTF-8?q?Update=200042=E6=8E=A5=E9=9B=A8=E6=B0=B4.md?= =?UTF-8?q?=20=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E5=92=8C=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E6=A0=88Python3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0042.接雨水.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 235776a0..a2cb2345 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -388,6 +388,44 @@ class Solution: res += res1 return res ``` +动态规划 +```python3 +class Solution: + def trap(self, height: List[int]) -> int: + leftheight, rightheight = [0]*len(height), [0]*len(height) + + leftheight[0]=height[0] + for i in range(1,len(height)): + leftheight[i]=max(leftheight[i-1],height[i]) + rightheight[-1]=height[-1] + for i in range(len(height)-2,-1,-1): + rightheight[i]=max(rightheight[i+1],height[i]) + + result = 0 + for i in range(0,len(height)): + summ = min(leftheight[i],rightheight[i])-height[i] + result += summ + return result +``` +单调栈 +```python3 +class Solution: + def trap(self, height: List[int]) -> int: + st =[0] + result = 0 + for i in range(1,len(height)): + while st!=[] and height[i]>height[st[-1]]: + midh = height[st[-1]] + st.pop() + if st!=[]: + hright = height[i] + hleft = height[st[-1]] + h = min(hright,hleft)-midh + w = i-st[-1]-1 + result+=h*w + st.append(i) + return result +``` Go: From 368fa2b761aaf7bee03d6796dd62dd5f157811e7 Mon Sep 17 00:00:00 2001 From: lichun-chen <81766312+lichun-chen@users.noreply.github.com> Date: Fri, 13 Aug 2021 21:49:17 -0500 Subject: [PATCH 7/8] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6=E5=9B=BE?= =?UTF-8?q?=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2.md=20?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E5=92=8C=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E6=A0=88=20Python3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0084.柱状图中最大的矩形.md | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index f0f58c0f..941888db 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -191,4 +191,57 @@ public: 这里我依然建议大家按部就班把版本一写出来,把情况一二三分析清楚,然后在精简代码到版本二。 直接看版本二容易忽略细节! +## 其他语言版本 + +Java: + +Python: + +动态规划 +```python3 +class Solution: + def largestRectangleArea(self, heights: List[int]) -> int: + result = 0 + minleftindex, minrightindex = [0]*len(heights), [0]*len(heights) + + minleftindex[0]=-1 + for i in range(1,len(heights)): + t = i-1 + while t>=0 and heights[t]>=heights[i]: t=minleftindex[t] + minleftindex[i]=t + + minrightindex[-1]=len(heights) + for i in range(len(heights)-2,-1,-1): + t=i+1 + while t=heights[i]: t=minrightindex[t] + minrightindex[i]=t + + for i in range(0,len(heights)): + left = minleftindex[i] + right = minrightindex[i] + summ = (right-left-1)*heights[i] + result = max(result,summ) + return result +``` +单调栈 版本二 +```python3 +class Solution: + def largestRectangleArea(self, heights: List[int]) -> int: + heights.insert(0,0) # 数组头部加入元素0 + heights.append(0) # 数组尾部加入元素0 + st = [0] + result = 0 + for i in range(1,len(heights)): + while st!=[] and heights[i] From 0c4a4a68ce149d664226f836b8ebf8709cd49d08 Mon Sep 17 00:00:00 2001 From: shuwen Date: Sat, 14 Aug 2021 17:38:28 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87Offe?= =?UTF-8?q?r58-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=20Python3=E7=89=88=E6=9C=AC=20=E4=BD=BF=E7=94=A8=E6=A8=A1+?= =?UTF-8?q?=E4=B8=8B=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 考虑不能用切片的情况下,利用模+下标实现 --- problems/剑指Offer58-II.左旋转字符串.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 76908f2d..1073dafa 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -141,6 +141,18 @@ class Solution: # 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改 ``` +```python 3 +#方法三:考虑不能用切片的情况下,利用模+下标实现 +class Solution: + def reverseLeftWords(self, s: str, n: int) -> str: + new_s = '' + for i in range(len(s)): + j = (i+n)%len(s) + new_s = new_s + s[j] + return new_s + +``` + Go: ```go