From 5e0cb67b8a18debf47d788ce41fd949736390998 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Tue, 10 Aug 2021 20:28:49 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00116.=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87=E9=92=88?= =?UTF-8?q?java=E5=92=8Cpython3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...个节点的下一个右侧节点指针.md | 88 ++++++++++++++++++- 1 file changed, 85 insertions(+), 3 deletions(-) diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md index 6feb1ca9..b139067d 100644 --- a/problems/0116.填充每个节点的下一个右侧节点指针.md +++ b/problems/0116.填充每个节点的下一个右侧节点指针.md @@ -130,15 +130,97 @@ public: ## Java ```java - +// 递归法 +class Solution { + public void traversal(Node cur) { + if(cur == null) return; + if(cur.left != null) cur.left.next = cur.right; + if(cur.right != null){ + if(cur.next != null) cur.right.next = cur.next.left; + else cur.right.next = null; + } + traversal(cur.left); + traversal(cur.right); + } + public Node connect(Node root) { + traversal(root); + return root; + } +} +``` +```java +// 迭代法 +class Solution { + public Node connect(Node root) { + if(root == null) return root; + Queue que = new LinkedList(); + que.offer(root); + Node nodePre = null; + Node node = null; + while(!que.isEmpty()){ + int size = que.size(); + for(int i=0; i 'Node': + def traversal(cur: 'Node') -> 'Node': + if not cur: return [] + if cur.left: cur.left.next = cur.right # 操作1 + if cur.right: + if cur.next: + cur.right.next = cur.next.left # 操作2 + else: + cur.right.next = None + traversal(cur.left) # 左 + traversal(cur.right) # 右 + traversal(root) + return root +``` +```python +# 迭代法 +class Solution: + def connect(self, root: 'Node') -> 'Node': + if not root: return + res = [] + queue = [root] + while queue: + size = len(queue) + for i in range(size): # 开始每一层的遍历 + if i==0: + nodePre = queue.pop(0) # 记录一层的头结点 + node = nodePre + else: + node = queue.pop(0) + nodePre.next = node # 本层前一个节点next指向本节点 + nodePre = nodePre.next + if node.left: queue.append(node.left) + if node.right: queue.append(node.right) + nodePre.next = None # 本层最后一个节点指向None + return root ``` - ## Go ```go From b02e144d7a865a632db2bd025291c00f8583ee96 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Wed, 11 Aug 2021 20:13:02 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=9B=B4=E6=96=B00116.=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87=E9=92=88?= =?UTF-8?q?java=E6=B3=A8=E9=87=8A=E5=8F=8A=E4=BB=A3=E7=A0=81=E8=A7=84?= =?UTF-8?q?=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...个节点的下一个右侧节点指针.md | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md index 8b816f5e..34c666f3 100644 --- a/problems/0116.填充每个节点的下一个右侧节点指针.md +++ b/problems/0116.填充每个节点的下一个右侧节点指针.md @@ -133,14 +133,14 @@ public: // 递归法 class Solution { public void traversal(Node cur) { - if(cur == null) return; - if(cur.left != null) cur.left.next = cur.right; - if(cur.right != null){ - if(cur.next != null) cur.right.next = cur.next.left; + if (cur == null) return; + if (cur.left != null) cur.left.next = cur.right; // 操作1 + if (cur.right != null) { + if(cur.next != null) cur.right.next = cur.next.left; //操作2 else cur.right.next = null; } - traversal(cur.left); - traversal(cur.right); + traversal(cur.left); // 左 + traversal(cur.right); //右 } public Node connect(Node root) { traversal(root); @@ -152,26 +152,26 @@ class Solution { // 迭代法 class Solution { public Node connect(Node root) { - if(root == null) return root; + if (root == null) return root; Queue que = new LinkedList(); que.offer(root); Node nodePre = null; Node node = null; - while(!que.isEmpty()){ + while (!que.isEmpty()) { int size = que.size(); - for(int i=0; i Date: Wed, 11 Aug 2021 22:24:28 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A01382.=E5=B0=86=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E5=8F=98=E5=B9=B3=E8=A1=A1?= =?UTF-8?q?python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1382.将二叉搜索树变平衡.md | 24 ++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/problems/1382.将二叉搜索树变平衡.md b/problems/1382.将二叉搜索树变平衡.md index b9d4fb65..75530663 100644 --- a/problems/1382.将二叉搜索树变平衡.md +++ b/problems/1382.将二叉搜索树变平衡.md @@ -55,7 +55,7 @@ private: vec.push_back(cur->val); traversal(cur->right); } - 有序数组转平衡二叉树 + // 有序数组转平衡二叉树 TreeNode* getTree(vector& nums, int left, int right) { if (left > right) return nullptr; int mid = left + ((right - left) / 2); @@ -78,7 +78,27 @@ public: Java: Python: - +```python +class Solution: + def balanceBST(self, root: TreeNode) -> TreeNode: + res = [] + # 有序树转成有序数组 + def traversal(cur: TreeNode): + if not cur: return + traversal(cur.left) + res.append(cur.val) + traversal(cur.right) + # 有序数组转成平衡二叉树 + def getTree(nums: List, left, right): + if left > right: return + mid = left + (right -left) // 2 + root = TreeNode(nums[mid]) + root.left = getTree(nums, left, mid - 1) + root.right = getTree(nums, mid + 1, right) + return root + traversal(root) + return getTree(res, 0, len(res) - 1) +``` Go: JavaScript: From 2706b07a974af830beb0258563efc7663636b525 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 12 Aug 2021 09:37:26 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A01382.=E5=B0=86=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E5=8F=98=E5=B9=B3=E8=A1=A1?= =?UTF-8?q?java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1382.将二叉搜索树变平衡.md | 26 +++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/problems/1382.将二叉搜索树变平衡.md b/problems/1382.将二叉搜索树变平衡.md index 75530663..758d5ad8 100644 --- a/problems/1382.将二叉搜索树变平衡.md +++ b/problems/1382.将二叉搜索树变平衡.md @@ -76,7 +76,31 @@ public: # 其他语言版本 Java: - +```java +class Solution { + ArrayList res = new ArrayList(); + // 有序树转成有序数组 + private void travesal(TreeNode cur) { + if (cur == null) return; + travesal(cur.left); + res.add(cur.val); + travesal(cur.right); + } + // 有序数组转成平衡二叉树 + private TreeNode getTree(ArrayList nums, int left, int right) { + if (left > right) return null; + int mid = left + (right - left) / 2; + TreeNode root = new TreeNode(nums.get(mid)); + root.left = getTree(nums, left, mid - 1); + root.right = getTree(nums, mid + 1, right); + return root; + } + public TreeNode balanceBST(TreeNode root) { + travesal(root); + return getTree(res, 0, res.size() - 1); + } +} +``` Python: ```python class Solution: From 25fa9ad2f3704b7cf0e224f0c8439bb73ad0ec44 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 12 Aug 2021 14:19:07 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A00925.=E9=95=BF=E6=8C=89?= =?UTF-8?q?=E9=94=AE=E5=85=A5python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0925.长按键入.md | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index 4d3543f4..ef712252 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -8,7 +8,7 @@

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

# 925.长按键入 - +题目链接:https://leetcode-cn.com/problems/long-pressed-name/ 你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。 你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。 @@ -100,7 +100,31 @@ public: Java: Python: - +```python +class Solution: + def isLongPressedName(self, name: str, typed: str) -> bool: + i, j = 0, 0 + m, n = len(name) , len(typed) + while i< m and j < n: + if name[i] == typed[j]: # 相同时向后匹配 + i += 1 + j += 1 + else: # 不相同 + if j == 0: return False # 如果第一位不相同,直接返回false + # 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz" + while j < n - 1 and typed[j] == typed[j-1]: j += 1 + if name[i] == typed[j]: + i += 1 + j += 1 + else: return False + # 说明name没有匹配完 + if i < m: return False + # 说明type没有匹配完 + while j < n: + if typed[j] == typed[j-1]: j += 1 + else: return False + return True +``` Go: JavaScript: