mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
添加131.分割回文串JavaScript版本
This commit is contained in:
@ -292,7 +292,7 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python3
|
```py
|
||||||
class Solution:
|
class Solution:
|
||||||
def partition(self, s: str) -> List[List[str]]:
|
def partition(self, s: str) -> List[List[str]]:
|
||||||
res = []
|
res = []
|
||||||
@ -313,7 +313,38 @@ class Solution:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
javaScript:
|
||||||
|
|
||||||
|
```js
|
||||||
|
/**
|
||||||
|
* @param {string} s
|
||||||
|
* @return {string[][]}
|
||||||
|
*/
|
||||||
|
const isPalindrome = (s, l, r) => {
|
||||||
|
for (let i = l, j = r; i < j; i++, j--) {
|
||||||
|
if(s[i] !== s[j]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var partition = function(s) {
|
||||||
|
const res = [], path = [], len = s.length;
|
||||||
|
backtracking(0);
|
||||||
|
return res;
|
||||||
|
function backtracking(i) {
|
||||||
|
if(i >= len) {
|
||||||
|
res.push(Array.from(path));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(let j = i; j < len; j++) {
|
||||||
|
if(!isPalindrome(s, i, j)) continue;
|
||||||
|
path.push(s.substr(i, j - i + 1));
|
||||||
|
backtracking(j + 1);
|
||||||
|
path.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user