From 7253dc9bc7aaec1fb8235d41ea75aa5eae1f606f Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Wed, 19 May 2021 11:08:53 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A019.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 3b89dabd..920c8110 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -135,6 +135,28 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode { } ``` +JavaScript: + +```js +/** + * @param {ListNode} head + * @param {number} n + * @return {ListNode} + */ +var removeNthFromEnd = function(head, n) { + let ret = new ListNode(0, head), + slow = fast = ret; + while(n--) fast = fast.next; + if(!fast) return ret.next; + while (fast.next) { + fast = fast.next; + slow = slow.next + }; + slow.next = slow.next.next; + return ret.next; +}; +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 0a7682998d60cf02caab772ba74955baf1ad806e Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Wed, 19 May 2021 11:22:07 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0142.=E7=8E=AF=E5=BD=A2?= =?UTF-8?q?=E9=93=BE=E8=A1=A8IIjavascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0142.环形链表II.md | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index b3a53c07..71f5cc4c 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -258,6 +258,50 @@ Go: } ``` +javaScript + +```js +// 两种循环实现方式 +/** + * @param {ListNode} head + * @return {ListNode} + */ +// 先判断是否是环形链表 +var detectCycle = function(head) { + if(!head || !head.next) return null; + let slow =head.next, fast = head.next.next; + while(fast && fast.next && fast!== slow) { + slow = slow.next; + fast = fast.next.next; + } + if(!fast || !fast.next ) return null; + slow = head; + while (fast !== slow) { + slow = slow.next; + fast = fast.next; + } + return slow; +}; + +var detectCycle = function(head) { + if(!head || !head.next) return null; + let slow =head.next, fast = head.next.next; + while(fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + if(fast == slow) { + slow = head; + while (fast !== slow) { + slow = slow.next; + fast = fast.next; + } + return slow; + } + } + return null; +}; +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 3f16de6462d9d80bf44ec8d0adc95988fccbd028 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Wed, 19 May 2021 11:42:18 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC202=E9=A2=98.=20?= =?UTF-8?q?=E5=BF=AB=E4=B9=90=E6=95=B0javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0202.快乐数.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 8c0dd1e7..90228b8c 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -112,6 +112,40 @@ Python: Go: +javaScript: + +```js +function getN(n) { + if (n == 1 || n == 0) return n; + let res = 0; + while (n) { + res += (n % 10) * (n % 10); + n = parseInt(n / 10); + } + return res; +} + +var isHappy = function(n) { + const sumSet = new Set(); + while (n != 1 && !sumSet.has(n)) { + sumSet.add(n); + n = getN(n); + } + return n == 1; +}; + +// 使用环形链表的思想 说明出现闭环 退出循环 +var isHappy = function(n) { + if (getN(n) == 1) return true; + let a = getN(n), b = getN(getN(n)); + // 如果 a === b + while (b !== 1 && getN(b) !== 1 && a !== b) { + a = getN(a); + b = getN(getN(b)); + } + return b === 1 || getN(b) === 1 ; +}; +``` From 8c77bb2565658fb19c3d1159988ff6c50804876b Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Wed, 19 May 2021 18:45:14 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86javascrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的递归遍历.md | 37 +++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 937ef603..2d571294 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -226,7 +226,7 @@ func PreorderTraversal(root *TreeNode) (res []int) { var traversal func(node *TreeNode) traversal = func(node *TreeNode) { if node == nil { - return + return } res = append(res,node.Val) traversal(node.Left) @@ -272,6 +272,41 @@ func PostorderTraversal(root *TreeNode) (res []int) { } ``` +javaScript: + +```js + +前序遍历: + +var preorderTraversal = function(root, res = []) { + if (!root) return res; + res.push(root.val); + preorderTraversal(root.left, res) + preorderTraversal(root.right, res) + return res; +}; + +中序遍历: + +var inorderTraversal = function(root, res = []) { + if (!root) return res; + inorderTraversal(root.left, res); + res.push(root.val); + inorderTraversal(root.right, res); + return res; +}; + +后序遍历: + +var postorderTraversal = function(root, res = []) { + if (!root) return res; + postorderTraversal(root.left, res); + postorderTraversal(root.right, res); + res.push(root.val); + return res; +}; +``` + From 1c267dc06458dfb1708484b0edb20e8f61f7f190 Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Wed, 19 May 2021 20:05:13 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86javaScrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的迭代遍历.md | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index 594d498f..60dd73a8 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -330,6 +330,70 @@ func inorderTraversal(root *TreeNode) []int { } ``` +javaScript + +```js + +前序遍历: + +// 入栈 右 -> 左 +// 出栈 中 -> 左 -> 右 +var preorderTraversal = function(root, res = []) { + if(!root) return res; + const stack = [root]; + let cur = null; + while(stack.length) { + cur = stack.pop(); + res.push(cur.val); + cur.right && stack.push(cur.right); + cur.left && stack.push(cur.left); + } + return res; +}; + +中序遍历: + +// 入栈 左 -> 右 +// 出栈 左 -> 中 -> 右 + +var inorderTraversal = function(root, res = []) { + const stack = []; + let cur = root; + while(stack.length || cur) { + if(cur) { + stack.push(cur); + // 左 + cur = cur.left; + } else { + // --> 弹出 中 + cur = stack.pop(); + res.push(cur.val); + // 右 + cur = cur.right; + } + }; + return res; +}; + +后序遍历: + +// 入栈 左 -> 右 +// 出栈 中 -> 右 -> 左 结果翻转 + +var postorderTraversal = function(root, res = []) { + if (!root) return res; + const stack = [root]; + let cur = null; + do { + cur = stack.pop(); + res.push(cur.val); + cur.left && stack.push(cur.left); + cur.right && stack.push(cur.right); + } while(stack.length); + return res.reverse(); +}; +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)