Merge branch 'youngyangyang04:master' into master

This commit is contained in:
Ezralin
2022-07-11 14:54:12 +08:00
committed by GitHub
5 changed files with 186 additions and 4 deletions

View File

@ -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
}
}
```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -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
}
}
```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -7,6 +7,8 @@
# 141. 环形链表 # 141. 环形链表
[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle/submissions/)
给定一个链表,判断链表中是否有环。 给定一个链表,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1则在该链表中没有环。注意pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1则在该链表中没有环。注意pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
@ -103,7 +105,7 @@ class Solution:
return False return False
``` ```
## Go ### Go
```go ```go
func hasCycle(head *ListNode) bool { 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;
};
```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -301,13 +301,13 @@ function detectCycle(head: ListNode | null): ListNode | null {
let slowNode: ListNode | null = head, let slowNode: ListNode | null = head,
fastNode: ListNode | null = head; fastNode: ListNode | null = head;
while (fastNode !== null && fastNode.next !== null) { while (fastNode !== null && fastNode.next !== null) {
slowNode = (slowNode as ListNode).next; slowNode = slowNode!.next;
fastNode = fastNode.next.next; fastNode = fastNode.next.next;
if (slowNode === fastNode) { if (slowNode === fastNode) {
slowNode = head; slowNode = head;
while (slowNode !== fastNode) { while (slowNode !== fastNode) {
slowNode = (slowNode as ListNode).next; slowNode = slowNode!.next;
fastNode = (fastNode as ListNode).next; fastNode = fastNode!.next;
} }
return slowNode; return slowNode;
} }

View File

@ -6,6 +6,8 @@
# 143.重排链表 # 143.重排链表
[力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/)
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726160122.png) ![](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
方法三:反转链表 方法三:反转链表
```c ```c
//翻转链表 //翻转链表