From 60799a2f446b0b54bb6dcf29ef302ecf9dfeb3e6 Mon Sep 17 00:00:00 2001 From: posper Date: Tue, 3 Aug 2021 20:53:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E5=B1=82?= =?UTF-8?q?=E5=BA=8F=E9=81=8D=E5=8E=86=20116.=E5=A1=AB=E5=85=85=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87=E9=92=88=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 8be3ac47..7d459ad9 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1254,6 +1254,44 @@ func connect(root *Node) *Node { } ``` +Java 代码: + +```java +// 二叉树之层次遍历 +class Solution { + public Node connect(Node root) { + Queue queue = new LinkedList<>(); + if (root != null) { + queue.add(root); + } + while (!queue.isEmpty()) { + int size = queue.size(); + Node node = null; + Node nodePre = null; + + for (int i = 0; i < size; i++) { + if (i == 0) { + nodePre = queue.poll(); // 取出本层头一个节点 + node = nodePre; + } else { + node = queue.poll(); + nodePre.next = node; // 本层前一个节点 next 指向当前节点 + nodePre = nodePre.next; + } + if (node.left != null) { + queue.add(node.left); + } + if (node.right != null) { + queue.add(node.right); + } + } + nodePre.next = null; // 本层最后一个节点 next 指向 null + } + return root; + } +} +``` + ## 117.填充每个节点的下一个右侧节点指针II 题目地址:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/ From 26a07b9bf1597ceda73c4a616aea29395f2c0341 Mon Sep 17 00:00:00 2001 From: fixme Date: Tue, 3 Aug 2021 22:10:15 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E3=80=9024.=20=E4=B8=A4=E4=B8=A4=E4=BA=A4?= =?UTF-8?q?=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E3=80=91C=E5=AE=9E=E7=8E=B0(=E5=8F=8C=E6=8C=87=E9=92=88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 66e149e6..b285b2d4 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -86,6 +86,34 @@ public: ## 其他语言版本 +C: +``` +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + + +struct ListNode* swapPairs(struct ListNode* head){ + //使用双指针避免使用中间变量 + typedef struct ListNode ListNode; + ListNode *fakehead = (ListNode *)malloc(sizeof(ListNode)); + fakehead->next = head; + ListNode* right = fakehead->next; + ListNode* left = fakehead; + while(left && right && right->next ){ + left->next = right->next; + right->next = left->next->next; + left->next->next = right; + left = right; + right = left->next; + } + return fakehead->next; +} +``` Java: From 098bb7b262e57d7c0ae9a51ab484665577835b4f Mon Sep 17 00:00:00 2001 From: Jack <965555169@qq.com> Date: Wed, 4 Aug 2021 12:08:00 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0198.=E6=89=93=E5=AE=B6?= =?UTF-8?q?=E5=8A=AB=E8=88=8D-JavaScript=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0198.打家劫舍.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index 63a68c36..5b5863a4 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -25,7 +25,7 @@ 输出:12 解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。   偷窃到的最高金额 = 2 + 9 + 1 = 12 。 -  + 提示: @@ -175,6 +175,22 @@ func max(a, b int) int { } ``` +JavaScript: + +```javascript +const rob = nums => { + // 数组长度 + const len = nums.length; + // dp数组初始化 + const dp = [nums[0], Math.max(nums[0], nums[1])]; + // 从下标2开始遍历 + for (let i = 2; i < len; i++) { + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[len - 1]; +}; +``` +