From 3605046879d02c5f6b931f7440935ef4142ef121 Mon Sep 17 00:00:00 2001 From: yalexu <61576631+alexgy1@users.noreply.github.com> Date: Fri, 3 Jun 2022 18:24:51 +0800 Subject: [PATCH 01/11] Add JS correct reverse method --- problems/0344.反转字符串.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 58bada05..192a397c 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -190,13 +190,13 @@ javaScript: * @return {void} Do not return anything, modify s in-place instead. */ var reverseString = function(s) { - return s.reverse(); + //Do not return anything, modify s in-place instead. + reverse(s) }; -var reverseString = function(s) { +var reverse = function(s) { let l = -1, r = s.length; while(++l < --r) [s[l], s[r]] = [s[r], s[l]]; - return s; }; ``` From a2a2cf60272094c3c9d962723132502d93db097a Mon Sep 17 00:00:00 2001 From: lizhendong128 Date: Fri, 3 Jun 2022 21:42:36 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E4=BF=AE=E6=94=B90106=E4=BB=8E=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E5=90=8E=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=20Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将0106和0105的Java版本进行了修改,采用了map来存储位置信息,加快定位;并且代码更容易看懂 --- ...序与后序遍历序列构造二叉树.md | 86 +++++++++---------- 1 file changed, 39 insertions(+), 47 deletions(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 188ad3cb..878c3572 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -584,35 +584,29 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 ```java class Solution { + Map map; // 方便根据数值查找位置 public TreeNode buildTree(int[] inorder, int[] postorder) { - return buildTree1(inorder, 0, inorder.length, postorder, 0, postorder.length); + map = new HashMap<>(); + for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置 + map.put(inorder[i], i); + } + + return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开 } - public TreeNode buildTree1(int[] inorder, int inLeft, int inRight, - int[] postorder, int postLeft, int postRight) { - // 没有元素了 - if (inRight - inLeft < 1) { + + public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) { + // 参数里的范围都是前闭后开 + if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树 return null; } - // 只有一个元素了 - if (inRight - inLeft == 1) { - return new TreeNode(inorder[inLeft]); - } - // 后序数组postorder里最后一个即为根结点 - int rootVal = postorder[postRight - 1]; - TreeNode root = new TreeNode(rootVal); - int rootIndex = 0; - // 根据根结点的值找到该值在中序数组inorder里的位置 - for (int i = inLeft; i < inRight; i++) { - if (inorder[i] == rootVal) { - rootIndex = i; - break; - } - } - // 根据rootIndex划分左右子树 - root.left = buildTree1(inorder, inLeft, rootIndex, - postorder, postLeft, postLeft + (rootIndex - inLeft)); - root.right = buildTree1(inorder, rootIndex + 1, inRight, - postorder, postLeft + (rootIndex - inLeft), postRight - 1); + int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置 + TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点 + int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数 + root.left = findNode(inorder, inBegin, rootIndex, + postorder, postBegin, postBegin + lenOfLeft); + root.right = findNode(inorder, rootIndex + 1, inEnd, + postorder, postBegin + lenOfLeft, postEnd - 1); + return root; } } @@ -622,31 +616,29 @@ class Solution { ```java class Solution { + Map map; public TreeNode buildTree(int[] preorder, int[] inorder) { - return helper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1); - } - - public TreeNode helper(int[] preorder, int preLeft, int preRight, - int[] inorder, int inLeft, int inRight) { - // 递归终止条件 - if (inLeft > inRight || preLeft > preRight) return null; - - // val 为前序遍历第一个的值,也即是根节点的值 - // idx 为根据根节点的值来找中序遍历的下标 - int idx = inLeft, val = preorder[preLeft]; - TreeNode root = new TreeNode(val); - for (int i = inLeft; i <= inRight; i++) { - if (inorder[i] == val) { - idx = i; - break; - } + map = new HashMap<>(); + for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置 + map.put(inorder[i], i); } - // 根据 idx 来递归找左右子树 - root.left = helper(preorder, preLeft + 1, preLeft + (idx - inLeft), - inorder, inLeft, idx - 1); - root.right = helper(preorder, preLeft + (idx - inLeft) + 1, preRight, - inorder, idx + 1, inRight); + return findNode(preorder, 0, preorder.length, inorder, 0, inorder.length); // 前闭后开 + } + + public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) { + // 参数里的范围都是前闭后开 + if (preBegin >= preEnd || inBegin >= inEnd) { // 不满足左闭右开,说明没有元素,返回空树 + return null; + } + int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历中的位置 + TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点 + int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数 + root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1, + inorder, inBegin, rootIndex); + root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd, + inorder, rootIndex + 1, inEnd); + return root; } } From af1b3e3e32676d6037645c0553b4fc2bb2cf149d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?= <20304773@qq.com> Date: Sat, 4 Jun 2022 00:22:14 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880209.=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0=20C#=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0209.长度最小的子数组.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index fbef7692..ceb06ce9 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -448,6 +448,27 @@ object Solution { } } ``` - +C#: +```csharp +public class Solution { + public int MinSubArrayLen(int s, int[] nums) { + int n = nums.Length; + int ans = int.MaxValue; + int start = 0, end = 0; + int sum = 0; + while (end < n) { + sum += nums[end]; + while (sum >= s) + { + ans = Math.Min(ans, end - start + 1); + sum -= nums[start]; + start++; + } + end++; + } + return ans == int.MaxValue ? 0 : ans; + } +} +``` -----------------------
From cccb4973aeccde06a00e5c693510c824ae98c99b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 10:44:25 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880035.=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.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/0035.搜索插入位置.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 6c04e7de..3de7c4f7 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -283,6 +283,28 @@ var searchInsert = function (nums, target) { }; ``` +### TypeScript + +```typescript +// 第一种二分法 +function searchInsert(nums: number[], target: number): number { + const length: number = nums.length; + let left: number = 0, + right: number = length - 1; + while (left <= right) { + const mid: number = Math.floor((left + right) / 2); + if (nums[mid] < target) { + left = mid + 1; + } else if (nums[mid] === target) { + return mid; + } else { + right = mid - 1; + } + } + return right + 1; +}; +``` + ### Swift ```swift From 6cd58dfef0a22f568bb5c6b03f4026520b7283de Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 13:13:33 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880024.=E4=B8=A4?= =?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md=EF=BC=89=EF=BC=9A=E4=BC=98=E5=8C=96typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E6=98=93=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 2289c229..0d848e4d 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -254,20 +254,19 @@ TypeScript: ```typescript function swapPairs(head: ListNode | null): ListNode | null { - const dummyHead: ListNode = new ListNode(0, head); - let cur: ListNode = dummyHead; - while(cur.next !== null && cur.next.next !== null) { - const tem: ListNode = cur.next; - const tem1: ListNode = cur.next.next.next; - - cur.next = cur.next.next; // step 1 - cur.next.next = tem; // step 2 - cur.next.next.next = tem1; // step 3 - - cur = cur.next.next; - } - return dummyHead.next; -} + const dummyNode: ListNode = new ListNode(0, head); + let curNode: ListNode | null = dummyNode; + while (curNode && curNode.next && curNode.next.next) { + let firstNode: ListNode = curNode.next, + secNode: ListNode = curNode.next.next, + thirdNode: ListNode | null = curNode.next.next.next; + curNode.next = secNode; + secNode.next = firstNode; + firstNode.next = thirdNode; + curNode = firstNode; + } + return dummyNode.next; +}; ``` Kotlin: From 7d5856d0b82baad2266555f33bc1eae43538fd70 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 15:56:22 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880234.=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=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/0234.回文链表.md | 59 ++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md index db910d4e..b19a2408 100644 --- a/problems/0234.回文链表.md +++ b/problems/0234.回文链表.md @@ -273,7 +273,7 @@ class Solution: return pre ``` -## Go +### Go ```go @@ -319,6 +319,63 @@ var isPalindrome = function(head) { }; ``` +### TypeScript + +> 数组模拟 + +```typescript +function isPalindrome(head: ListNode | null): boolean { + const helperArr: number[] = []; + let curNode: ListNode | null = head; + while (curNode !== null) { + helperArr.push(curNode.val); + curNode = curNode.next; + } + let left: number = 0, + right: number = helperArr.length - 1; + while (left < right) { + if (helperArr[left++] !== helperArr[right--]) return false; + } + return true; +}; +``` + +> 反转后半部分链表 + +```typescript +function isPalindrome(head: ListNode | null): boolean { + if (head === null || head.next === null) return true; + let fastNode: ListNode | null = head, + slowNode: ListNode = head, + preNode: ListNode = head; + while (fastNode !== null && fastNode.next !== null) { + preNode = slowNode; + slowNode = slowNode.next!; + fastNode = fastNode.next.next; + } + preNode.next = null; + let cur1: ListNode | null = head; + let cur2: ListNode | null = reverseList(slowNode); + while (cur1 !== null) { + if (cur1.val !== cur2!.val) return false; + cur1 = cur1.next; + cur2 = cur2!.next; + } + return true; +}; +function reverseList(head: ListNode | null): ListNode | null { + let curNode: ListNode | null = head, + preNode: ListNode | null = null; + while (curNode !== null) { + let tempNode: ListNode | null = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + } + return preNode; +} +``` + ----------------------- From bb484a70ac2c8d2fcbbba08a1897bc053c9354d1 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 21:01:59 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880143.=E9=87=8D?= =?UTF-8?q?=E6=8E=92=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=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/0143.重排链表.md | 76 +++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index 790bcb48..c60fc0f9 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -6,6 +6,8 @@ # 143.重排链表 +[力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/) + ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726160122.png) ## 思路 @@ -465,7 +467,81 @@ var reorderList = function(head, s = [], tmp) { } ``` +### TypeScript + +> 辅助数组法: + +```typescript +function reorderList(head: ListNode | null): void { + if (head === null) return; + const helperArr: ListNode[] = []; + let curNode: ListNode | null = head; + while (curNode !== null) { + helperArr.push(curNode); + curNode = curNode.next; + } + let node: ListNode = head; + let left: number = 1, + right: number = helperArr.length - 1; + let count: number = 0; + while (left <= right) { + if (count % 2 === 0) { + node.next = helperArr[right--]; + } else { + node.next = helperArr[left++]; + } + count++; + node = node.next; + } + node.next = null; +}; +``` + +> 分割链表法: + +```typescript +function reorderList(head: ListNode | null): void { + if (head === null || head.next === null) return; + let fastNode: ListNode = head, + slowNode: ListNode = head; + while (fastNode.next !== null && fastNode.next.next !== null) { + slowNode = slowNode.next!; + fastNode = fastNode.next.next; + } + let head1: ListNode | null = head; + // 反转后半部分链表 + let head2: ListNode | null = reverseList(slowNode.next); + // 分割链表 + slowNode.next = null; + /** + 直接在head1链表上进行插入 + head1 链表长度一定大于或等于head2, + 因此在下面的循环中,只要head2不为null, head1 一定不为null + */ + while (head2 !== null) { + const tempNode1: ListNode | null = head1!.next, + tempNode2: ListNode | null = head2.next; + head1!.next = head2; + head2.next = tempNode1; + head1 = tempNode1; + head2 = tempNode2; + } +}; +function reverseList(head: ListNode | null): ListNode | null { + let curNode: ListNode | null = head, + preNode: ListNode | null = null; + while (curNode !== null) { + const tempNode: ListNode | null = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + } + return preNode; +} +``` + ### C + 方法三:反转链表 ```c //翻转链表 From c0a73e2544202baa3839852777e053e82937004c Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 4 Jun 2022 21:35:24 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 7a702898..f361d1ef 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -676,5 +676,50 @@ impl Solution { } } ``` + + +## Scala + +```scala +object Solution { + + import scala.collection.mutable + + def partition(s: String): List[List[String]] = { + var result = mutable.ListBuffer[List[String]]() + var path = mutable.ListBuffer[String]() + + // 判断字符串是否回文 + def isPalindrome(start: Int, end: Int): Boolean = { + var (left, right) = (start, end) + while (left < right) { + if (s(left) != s(right)) return false + left += 1 + right -= 1 + } + true + } + + // 回溯算法 + def backtracking(startIndex: Int): Unit = { + if (startIndex >= s.size) { + result.append(path.toList) + return + } + // 添加循环守卫,如果当前分割是回文子串则进入回溯 + for (i <- startIndex until s.size if isPalindrome(startIndex, i)) { + path.append(s.substring(startIndex, i + 1)) + backtracking(i + 1) + path = path.take(path.size - 1) + } + } + + backtracking(0) + result.toList + } +} +``` + + -----------------------
From a8e4d4da767758488dbbc20564eab01a08e3e2dc Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 4 Jun 2022 22:04:15 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200093.=E5=A4=8D?= =?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 6401824b..48bb6df2 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -659,6 +659,48 @@ func restoreIpAddresses(_ s: String) -> [String] { } ``` +## Scala + +```scala +object Solution { + import scala.collection.mutable + def restoreIpAddresses(s: String): List[String] = { + var result = mutable.ListBuffer[String]() + if (s.size < 4 || s.length > 12) return result.toList + var path = mutable.ListBuffer[String]() + + // 判断IP中的一个字段是否为正确的 + def isIP(sub: String): Boolean = { + if (sub.size > 1 && sub(0) == '0') return false + if (sub.toInt > 255) return false + true + } + + def backtracking(startIndex: Int): Unit = { + if (startIndex >= s.size) { + if (path.size == 4) { + result.append(path.mkString(".")) // mkString方法可以把集合里的数据以指定字符串拼接 + return + } + return + } + // subString + for (i <- startIndex until startIndex + 3 if i < s.size) { + var subString = s.substring(startIndex, i + 1) + if (isIP(subString)) { // 如果合法则进行下一轮 + path.append(subString) + backtracking(i + 1) + path = path.take(path.size - 1) + } + } + } + + backtracking(0) + result.toList + } +} +``` + -----------------------
From f5268ee213dafe8e7ab1c28d722a6b7dc81f4029 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 22:26:58 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880141.=E7=8E=AF?= =?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=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/0141.环形链表.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/problems/0141.环形链表.md b/problems/0141.环形链表.md index ddd83c94..ce90b6c4 100644 --- a/problems/0141.环形链表.md +++ b/problems/0141.环形链表.md @@ -7,6 +7,8 @@ # 141. 环形链表 +[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle/submissions/) + 给定一个链表,判断链表中是否有环。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。 @@ -103,7 +105,7 @@ class Solution: return False ``` -## Go +### Go ```go func hasCycle(head *ListNode) bool { @@ -139,6 +141,23 @@ var hasCycle = function(head) { }; ``` +### TypeScript + +```typescript +function hasCycle(head: ListNode | null): boolean { + let slowNode: ListNode | null = head, + fastNode: ListNode | null = head; + while (fastNode !== null && fastNode.next !== null) { + slowNode = slowNode!.next; + fastNode = fastNode.next.next; + if (slowNode === fastNode) return true; + } + return false; +}; +``` + + + -----------------------
From 0245b3ae50cc612ac7765976171b670a954f6a34 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 23:38:12 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880142.=E7=8E=AF?= =?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8II.md=EF=BC=89=EF=BC=9A=E4=BC=98?= =?UTF-8?q?=E5=8C=96typescript=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0142.环形链表II.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index f8e62d45..7d3c8443 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -301,13 +301,13 @@ function detectCycle(head: ListNode | null): ListNode | null { let slowNode: ListNode | null = head, fastNode: ListNode | null = head; while (fastNode !== null && fastNode.next !== null) { - slowNode = (slowNode as ListNode).next; + slowNode = slowNode!.next; fastNode = fastNode.next.next; if (slowNode === fastNode) { slowNode = head; while (slowNode !== fastNode) { - slowNode = (slowNode as ListNode).next; - fastNode = (fastNode as ListNode).next; + slowNode = slowNode!.next; + fastNode = fastNode!.next; } return slowNode; }