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 1/5] =?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
---
...15346円216円222円351円223円276円350円241円250円.md" | 76 +++++++++++++++++++
1 file changed, 76 insertions(+)
diff --git "a/problems/0143.351円207円215円346円216円222円351円223円276円350円241円250円.md" "b/problems/0143.351円207円215円346円216円222円351円223円276円350円241円250円.md"
index 790bcb482a..c60fc0f968 100644
--- "a/problems/0143.351円207円215円346円216円222円351円223円276円350円241円250円.md"
+++ "b/problems/0143.351円207円215円346円216円222円351円223円276円350円241円250円.md"
@@ -6,6 +6,8 @@
# 143.重排链表
+[力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/)
+

## 思路
@@ -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 2/5] =?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
---
...62345円233円236円346円226円207円344円270円262円.md" | 45 +++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git "a/problems/0131.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円.md" "b/problems/0131.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円.md"
index 7a702898dc..f361d1ef87 100644
--- "a/problems/0131.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円.md"
+++ "b/problems/0131.345円210円206円345円211円262円345円233円236円346円226円207円344円270円262円.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 3/5] =?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
---
...345円216円237円IP345円234円260円345円235円200円.md" | 42 +++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git "a/problems/0093.345円244円215円345円216円237円IP345円234円260円345円235円200円.md" "b/problems/0093.345円244円215円345円216円237円IP345円234円260円345円235円200円.md"
index 6401824b47..48bb6df229 100644
--- "a/problems/0093.345円244円215円345円216円237円IP345円234円260円345円235円200円.md"
+++ "b/problems/0093.345円244円215円345円216円237円IP345円234円260円345円235円200円.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 4/5] =?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
---
...57345円275円242円351円223円276円350円241円250円.md" | 21 ++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git "a/problems/0141.347円216円257円345円275円242円351円223円276円350円241円250円.md" "b/problems/0141.347円216円257円345円275円242円351円223円276円350円241円250円.md"
index ddd83c9422..ce90b6c404 100644
--- "a/problems/0141.347円216円257円345円275円242円351円223円276円350円241円250円.md"
+++ "b/problems/0141.347円216円257円345円275円242円351円223円276円350円241円250円.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 5/5] =?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
---
...2.347円216円257円345円275円242円351円223円276円350円241円250円II.md" | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git "a/problems/0142.347円216円257円345円275円242円351円223円276円350円241円250円II.md" "b/problems/0142.347円216円257円345円275円242円351円223円276円350円241円250円II.md"
index f8e62d4572..7d3c8443a1 100644
--- "a/problems/0142.347円216円257円345円275円242円351円223円276円350円241円250円II.md"
+++ "b/problems/0142.347円216円257円345円275円242円351円223円276円350円241円250円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;
}