diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md
index 41940487..11ca2d03 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
+ }
+}
+```
+
-----------------------
diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md
index a371864d..64d45853 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
+ }
+}
+```
+
+
-----------------------
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;
+};
+```
+
+
+
-----------------------
diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md
index 6b7c7e66..658bd868 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;
}
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/)
+

## 思路
@@ -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
//翻转链表