From cb0f5906fdf7a22a097958cbb89783905baf970f Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 8 Jan 2022 23:55:34 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0JS=E5=92=8CTS=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/链表理论基础.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index 02b7029e..109aa1ed 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -168,6 +168,32 @@ public class ListNode { } ``` +JavaScript: + +```javascript +class ListNode { + val; + next = null; + constructor(value) { + this.val = value; + this.next = null; + } +} +``` + +TypeScript: + +```typescript +class ListNode { + public val: number; + public next: ListNode = null; + constructor(value: number) { + this.val = value; + this.next = null; + } +} +``` + Python: From 1c595ed05192de37b7c9927b826a365c96ed2fae Mon Sep 17 00:00:00 2001 From: Du Date: Sun, 9 Jan 2022 08:52:20 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0105.=20=E4=BB=8E=E5=89=8D?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E4=B8=AD=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91=E3=80=81?= =?UTF-8?q?106.=20=E4=BB=8E=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=20Swift=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 102 +++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 487e4f7d..59e0ba53 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -684,7 +684,7 @@ class Solution: root.right = self.buildTree(preorder_right, inorder_right) return root -``` +``` 106.从中序与后序遍历序列构造二叉树 @@ -716,7 +716,7 @@ class Solution: root.right = self.buildTree(inorder_right, postorder_right) return root -``` +``` ## Go @@ -816,6 +816,7 @@ var buildTree = function(preorder, inorder) { ## C 106 从中序与后序遍历序列构造二叉树 + ```c int linearSearch(int* arr, int arrSize, int key) { int i; @@ -847,6 +848,7 @@ struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int po ``` 105 从前序与中序遍历序列构造二叉树 + ```c struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){ // 递归结束条件:传入的数组大小为0 @@ -887,7 +889,103 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in // 6.返回根节点 return root; } + ``` +## Swift + +105 从前序与中序遍历序列构造二叉树 + +```swift +class Solution { + func buildTree(_ preorder: [Int], _ inorder: [Int]) -> TreeNode? { + return helper(preorder: preorder, + preorderBegin: 0, + preorderEnd: preorder.count, + inorder: inorder, + inorderBegin: 0, + inorderEnd: inorder.count) + } + + func helper(preorder: [Int], preorderBegin: Int, preorderEnd: Int, inorder: [Int], inorderBegin: Int, inorderEnd: Int) -> TreeNode? { + if preorderBegin == preorderEnd { + return nil + } + + // 前序遍历数组的第一个元素作为分割点 + let rootValue = preorder[preorderBegin] + let root = TreeNode(rootValue) + + + if preorderEnd - preorderBegin == 1 { + return root + } + + var index = 0 // 从中序遍历数组中找到根节点的下标 + if let ind = inorder.firstIndex(of: rootValue) { + index = ind + } + + // 递归 + root.left = helper(preorder: preorder, + preorderBegin: preorderBegin + 1, + preorderEnd: preorderBegin + 1 + index - inorderBegin, + inorder: inorder, + inorderBegin: inorderBegin, + inorderEnd: index) + root.right = helper(preorder: preorder, + preorderBegin: preorderBegin + 1 + index - inorderBegin, + preorderEnd: preorderEnd, + inorder: inorder, + inorderBegin: index + 1, + inorderEnd: inorderEnd) + return root + } +} +``` + +106 从中序与后序遍历序列构造二叉树 + +```swift +class Solution_0106 { + func buildTree(inorder: [Int], inorderBegin: Int, inorderEnd: Int, postorder: [Int], postorderBegin: Int, postorderEnd: Int) -> TreeNode? { + if postorderEnd - postorderBegin < 1 { + return nil + } + + // 后序遍历数组的最后一个元素作为分割点 + let rootValue = postorder[postorderEnd - 1] + let root = TreeNode(rootValue) + + if postorderEnd - postorderBegin == 1 { + return root + } + + // 从中序遍历数组中找到根节点的下标 + var delimiterIndex = 0 + if let index = inorder.firstIndex(of: rootValue) { + delimiterIndex = index + } + + root.left = buildTree(inorder: inorder, + inorderBegin: inorderBegin, + inorderEnd: delimiterIndex, + postorder: postorder, + postorderBegin: postorderBegin, + postorderEnd: postorderBegin + (delimiterIndex - inorderBegin)) + + root.right = buildTree(inorder: inorder, + inorderBegin: delimiterIndex + 1, + inorderEnd: inorderEnd, + postorder: postorder, + postorderBegin: postorderBegin + (delimiterIndex - inorderBegin), + postorderEnd: postorderEnd - 1) + return root + } +} +``` + + + -----------------------
From 1f7f1617296bbb862b5ff6a265659ccd53659af4 Mon Sep 17 00:00:00 2001 From: Du Date: Sun, 9 Jan 2022 08:57:44 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0105.=20=E4=BB=8E=E5=89=8D?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E4=B8=AD=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91=E3=80=81?= =?UTF-8?q?106.=20=E4=BB=8E=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=20Swift=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0106.从中序与后序遍历序列构造二叉树.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 59e0ba53..40d75983 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -889,7 +889,6 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in // 6.返回根节点 return root; } - ``` ## Swift From a246b9b4e6dc344925259f64150400ddb7661ed4 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 9 Jan 2022 13:42:10 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 9d0ef91f..4e7bdd34 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -302,7 +302,63 @@ var removeElements = function(head, val) { }; ``` +TypeScript: + +版本一(在原链表上直接删除): + +```typescript +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ +function removeElements(head: ListNode | null, val: number): ListNode | null { + // 删除头部节点 + while (head !== null && head.val === val) { + head = head.next; + } + if (head === null) return head; + let pre: ListNode = head, cur: ListNode = head.next; + // 删除非头部节点 + while (cur) { + if (cur.val === val) { + pre.next = cur.next; + } else { + pre = pre.next; + } + cur = cur.next; + } + return head; +}; +``` + +版本二(虚拟头节点): + +```typescript +function removeElements(head: ListNode | null, val: number): ListNode | null { + head = new ListNode(0, head); + let pre: ListNode = head, cur: ListNode = head.next; + // 删除非头部节点 + while (cur) { + if (cur.val === val) { + pre.next = cur.next; + } else { + pre = pre.next; + } + cur = cur.next; + } + return head.next; +}; +``` + Swift: + ```swift /** * Definition for singly-linked list.