mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
更新 0131.分割回文串 排版格式更新
This commit is contained in:
@ -23,12 +23,12 @@
|
|||||||
["a","a","b"]
|
["a","a","b"]
|
||||||
]
|
]
|
||||||
|
|
||||||
# 算法公开课
|
## 算法公开课
|
||||||
|
|
||||||
**《代码随想录》算法视频公开课:[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
|
||||||
# 思路
|
## 思路
|
||||||
|
|
||||||
本题这涉及到两个关键问题:
|
本题这涉及到两个关键问题:
|
||||||
|
|
||||||
@ -58,7 +58,7 @@
|
|||||||
|
|
||||||
此时可以发现,切割问题的回溯搜索的过程和组合问题的回溯搜索的过程是差不多的。
|
此时可以发现,切割问题的回溯搜索的过程和组合问题的回溯搜索的过程是差不多的。
|
||||||
|
|
||||||
## 回溯三部曲
|
### 回溯三部曲
|
||||||
|
|
||||||
* 递归函数参数
|
* 递归函数参数
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ for (int i = startIndex; i < s.size(); i++) {
|
|||||||
|
|
||||||
**注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1**。
|
**注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1**。
|
||||||
|
|
||||||
## 判断回文子串
|
### 判断回文子串
|
||||||
|
|
||||||
最后我们看一下回文子串要如何判断了,判断一个字符串是否是回文。
|
最后我们看一下回文子串要如何判断了,判断一个字符串是否是回文。
|
||||||
|
|
||||||
@ -147,8 +147,6 @@ for (int i = startIndex; i < s.size(); i++) {
|
|||||||
|
|
||||||
此时关键代码已经讲解完毕,整体代码如下(详细注释了)
|
此时关键代码已经讲解完毕,整体代码如下(详细注释了)
|
||||||
|
|
||||||
## C++整体代码
|
|
||||||
|
|
||||||
根据Carl给出的回溯算法模板:
|
根据Carl给出的回溯算法模板:
|
||||||
|
|
||||||
```CPP
|
```CPP
|
||||||
@ -212,7 +210,7 @@ public:
|
|||||||
* 时间复杂度: O(n * 2^n)
|
* 时间复杂度: O(n * 2^n)
|
||||||
* 空间复杂度: O(n^2)
|
* 空间复杂度: O(n^2)
|
||||||
|
|
||||||
# 优化
|
## 优化
|
||||||
|
|
||||||
上面的代码还存在一定的优化空间, 在于如何更高效的计算一个子字符串是否是回文字串。上述代码```isPalindrome```函数运用双指针的方法来判定对于一个字符串```s```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在:
|
上面的代码还存在一定的优化空间, 在于如何更高效的计算一个子字符串是否是回文字串。上述代码```isPalindrome```函数运用双指针的方法来判定对于一个字符串```s```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在:
|
||||||
|
|
||||||
@ -272,7 +270,7 @@ public:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# 总结
|
## 总结
|
||||||
|
|
||||||
这道题目在leetcode上是中等,但可以说是hard的题目了,但是代码其实就是按照模板的样子来的。
|
这道题目在leetcode上是中等,但可以说是hard的题目了,但是代码其实就是按照模板的样子来的。
|
||||||
|
|
||||||
@ -306,10 +304,10 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
## Java
|
### Java
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
List<List<String>> lists = new ArrayList<>();
|
List<List<String>> lists = new ArrayList<>();
|
||||||
@ -351,7 +349,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Python
|
### Python
|
||||||
回溯 基本版
|
回溯 基本版
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -473,7 +471,7 @@ class Solution:
|
|||||||
return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2))
|
return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2))
|
||||||
|
|
||||||
```
|
```
|
||||||
## Go
|
### Go
|
||||||
```go
|
```go
|
||||||
var (
|
var (
|
||||||
path []string // 放已经回文的子串
|
path []string // 放已经回文的子串
|
||||||
@ -512,7 +510,7 @@ func isPalindrome(s string) bool {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## javaScript
|
### JavaScript
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
/**
|
||||||
@ -545,7 +543,7 @@ var partition = function(s) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## TypeScript
|
### TypeScript
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function partition(s: string): string[][] {
|
function partition(s: string): string[][] {
|
||||||
@ -582,7 +580,7 @@ function partition(s: string): string[][] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## C
|
### C
|
||||||
|
|
||||||
```c
|
```c
|
||||||
char** path;
|
char** path;
|
||||||
@ -679,7 +677,7 @@ char*** partition(char* s, int* returnSize, int** returnColumnSizes){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Swift
|
### Swift
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func partition(_ s: String) -> [[String]] {
|
func partition(_ s: String) -> [[String]] {
|
||||||
@ -719,7 +717,7 @@ func partition(_ s: String) -> [[String]] {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Rust
|
### Rust
|
||||||
|
|
||||||
**回溯+函数判断回文串**
|
**回溯+函数判断回文串**
|
||||||
```Rust
|
```Rust
|
||||||
@ -808,7 +806,7 @@ impl Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Scala
|
### Scala
|
||||||
|
|
||||||
```scala
|
```scala
|
||||||
object Solution {
|
object Solution {
|
||||||
@ -855,3 +853,4 @@ object Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user