Skip to content

Commit b6b04cc

Browse files
committed
添加131.分割回文串JavaScript版本
1 parent ece55fe commit b6b04cc

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

problems/0131.分割回文串.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ class Solution {
292292
```
293293

294294
Python:
295-
```python3
295+
```py
296296
class Solution:
297297
def partition(self, s: str) -> List[List[str]]:
298298
res = []
@@ -313,7 +313,38 @@ class Solution:
313313

314314
Go:
315315

316+
javaScript:
316317

318+
```js
319+
/**
320+
* @param {string} s
321+
* @return {string[][]}
322+
*/
323+
const isPalindrome = (s, l, r) => {
324+
for (let i = l, j = r; i < j; i++, j--) {
325+
if(s[i] !== s[j]) return false;
326+
}
327+
return true;
328+
}
329+
330+
var partition = function(s) {
331+
const res = [], path = [], len = s.length;
332+
backtracking(0);
333+
return res;
334+
function backtracking(i) {
335+
if(i >= len) {
336+
res.push(Array.from(path));
337+
return;
338+
}
339+
for(let j = i; j < len; j++) {
340+
if(!isPalindrome(s, i, j)) continue;
341+
path.push(s.substr(i, j - i + 1));
342+
backtracking(j + 1);
343+
path.pop();
344+
}
345+
}
346+
};
347+
```
317348

318349

319350
-----------------------

0 commit comments

Comments
 (0)