Skip to content

Commit dac830c

Browse files
author
luzhipeng
committed
1 parent aa7dda6 commit dac830c

14 files changed

+199
-91
lines changed

131.palindrome-partitioning.js

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile modified="2019-06-03T10:14:40.994Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" etag="msELe6QlU2sONxzcPuiZ" version="10.7.3"><diagram id="Qaq_nb1c47ayPGVTxkrD" name="第 1 页">7Vpbb9sgGP01eVwU40ucxzaXVlMnTevDpL1UBBObBRsP08bZrx/YOHFMq3ZSU5iWSmnM4TOXcz7gAzLy53l9w2GZfWEJpiMwSeqRvxgBEAFP/lfAvgXimd8CKSdJC3lH4J78xhqcaPSRJLg6MRSMUUHKUxCxosBInGCQc7Y7NdswelprCVNsAPcIUhP9ThKR6V6A6RG/xSTNupq9aNbm5LAz1j2pMpiwXQ/ylyN/zhkT7VNezzFV3HW8tO+tXsg9NIzjQrzlhbtVhG7X6edfN8u7nz+mi9nT7faTLuUJ0kfd4UK3Vuw7Cjh7LBKsSpmM/OtdRgS+LyFSuTupucQykVOZ8uSjLg9zgesXG+odui/dBrMcC76XJt0Lnfb7QXp3FCDUUNbjvsOgljw9lHxkRT5oYv6CJGCQxOyTFDlGku8gScA1TwocHG7ANU8KDZKgdZKGc5I/sUxSZJC0tk9S5BhJUwc9aTgnWScpdpEk1zxpZpBkcFQyUgjMl0+yj5Wm4hDzKdoSWGUHDnt8VYKzLZ4zynhTkh+DtR9FMofCNaZfWUUEYYXMQ1jVIDMUuUQGqXcDgzUTguU9gytKUpUhmBIJ6tShHNm+UjU/r1MVvY/zCkE8TnDJMYICJ+OSVdLyoYmkpf2GUNpraBLiOAneafIA4Ynkgak4+EjFu2F6kfxskoOpY5KbMexF8vOOcjmzW9bcDMkvmp93mNvX3IwL7W/D/NixbZhnBob2d/SB7xpLZmToAEuu+RIwgyn7Iy50zZe6LnzMWrSOwyCc/ANr0SZGGKF3GhnBYC3q0rbWok7ii+bn03zmmuZv2Gd0/JG8uaDqq/q8Aq8K10h+DdE2bSbaPtnNnzRpKruqyvYiTfkJ7BIbUisPu9btWWRCqBu4K8UEWKGk8MYEsWJD5BTOx0jWCFYJFFB+KbyS3wvItzcUVtXDN7xjfCvLAysPxLX8yCeIVIuVofQFjCir8Lgs0r57v9fyGA4cYmI6RPCMQwRncwhzE2L/DG4YkFo/gwPmxYD9M+9hQGqfJfNmwL4vDQNS+yy5eDUwDEjts2RuAc8YnPyXhyPDgNT2GSi43HScXfLZR0kuk8ffFzV5vR9p+cs/</diagram></mxfile>
16.2 KB
Loading
20.3 KB
Loading

problems/125.valid-palindrome.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
## 题目地址
3+
4+
https://leetcode.com/problems/valid-palindrome/description/
5+
6+
## 题目描述
7+
8+
```
9+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
10+
11+
Note: For the purpose of this problem, we define empty string as valid palindrome.
12+
13+
Example 1:
14+
15+
Input: "A man, a plan, a canal: Panama"
16+
Output: true
17+
Example 2:
18+
19+
Input: "race a car"
20+
Output: false
21+
22+
```
23+
24+
## 思路
25+
26+
这是一道考察回文的题目,而且是最简单的形式,即判断一个字符串是否是回文。
27+
28+
针对这个问题,我们可以使用头尾双指针,
29+
30+
- 如果两个指针的元素不相同,则直接返回false,
31+
- 如果两个指针的元素相同,我们同时更新头尾指针,循环。 直到头尾指针相遇。
32+
33+
时间复杂度为O(n).
34+
35+
拿“noon”这样一个回文串来说,我们的判断过程是这样的:
36+
37+
![125.valid-palindrome-1](../assets/problems/125.valid-palindrome-1.png)
38+
39+
拿“abaa”这样一个不是回文的字符串来说,我们的判断过程是这样的:
40+
41+
![125.valid-palindrome-2](../assets/problems/125.valid-palindrome-2.png)
42+
43+
44+
45+
## 关键点解析
46+
47+
- 双指针
48+
49+
## 代码
50+
51+
```js
52+
53+
/*
54+
* @lc app=leetcode id=125 lang=javascript
55+
*
56+
* [125] Valid Palindrome
57+
*/
58+
// 只处理英文字符(题目忽略大小写,我们前面全部转化成了小写, 因此这里我们只判断小写)和数字
59+
function isValid(c) {
60+
const charCode = c.charCodeAt(0);
61+
const isDigit =
62+
charCode >= "0".charCodeAt(0) && charCode <= "9".charCodeAt(0);
63+
const isChar = charCode >= "a".charCodeAt(0) && charCode <= "z".charCodeAt(0);
64+
65+
return isDigit || isChar;
66+
}
67+
/**
68+
* @param {string} s
69+
* @return {boolean}
70+
*/
71+
var isPalindrome = function(s) {
72+
s = s.toLowerCase();
73+
let left = 0;
74+
let right = s.length - 1;
75+
76+
while (left < right) {
77+
if (!isValid(s[left])) {
78+
left++;
79+
continue;
80+
}
81+
if (!isValid(s[right])) {
82+
right--;
83+
continue;
84+
}
85+
86+
if (s[left] === s[right]) {
87+
left++;
88+
right--;
89+
} else {
90+
break;
91+
}
92+
}
93+
94+
return right <= left;
95+
};
96+
```
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
## 题目地址
3+
4+
https://leetcode.com/problems/palindrome-partitioning/description/
5+
6+
## 题目描述
7+
8+
```
9+
Given a string s, partition s such that every substring of the partition is a palindrome.
10+
11+
Return all possible palindrome partitioning of s.
12+
13+
Example:
14+
15+
Input: "aab"
16+
Output:
17+
[
18+
["aa","b"],
19+
["a","a","b"]
20+
]
21+
22+
```
23+
24+
## 思路
25+
26+
这是一道求解所有可能性的题目, 这时候可以考虑使用回溯法。 回溯法解题的模板我们已经在很多题目中用过了,
27+
这里就不多说了。大家可以结合其他几道题目加深一下理解。
28+
29+
30+
## 关键点解析
31+
32+
- 回溯法
33+
34+
## 代码
35+
36+
```js
37+
38+
39+
/*
40+
* @lc app=leetcode id=131 lang=javascript
41+
*
42+
* [131] Palindrome Partitioning
43+
*/
44+
45+
function isPalindrom(s) {
46+
let left = 0;
47+
let right = s.length - 1;
48+
49+
while(left < right && s[left] === s[right]) {
50+
left++;
51+
right--;
52+
}
53+
54+
return left >= right;
55+
}
56+
function backtrack(s, list, tempList, start) {
57+
const sliced = s.slice(start);
58+
59+
if (isPalindrom(sliced) && (tempList.join("").length === s.length)) list.push([...tempList]);
60+
61+
for(let i = 0; i < sliced.length; i++) {
62+
const sub = sliced.slice(0, i + 1);
63+
if (isPalindrom(sub)) {
64+
tempList.push(sub);
65+
} else {
66+
continue;
67+
}
68+
backtrack(s, list, tempList, start + i + 1);
69+
tempList.pop();
70+
}
71+
}
72+
/**
73+
* @param {string} s
74+
* @return {string[][]}
75+
*/
76+
var partition = function(s) {
77+
// "aab"
78+
// ["aa", "b"]
79+
// ["a", "a", "b"]
80+
const list = [];
81+
backtrack(s, list, [], 0);
82+
return list;
83+
};
84+
85+
86+
```
87+
88+
## 相关题目
89+
- [39.combination-sum](./39.combination-sum.md)
90+
- [40.combination-sum-ii](./40.combination-sum-ii.md)
91+
- [46.permutations](./46.permutations.md)
92+
- [47.permutations-ii](./47.permutations-ii.md)
93+
- [78.subsets](./78.subsets.md)
94+
- [90.subsets-ii](./90.subsets-ii.md)
95+

problems/39.combination-sum.md

+2
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,5 @@ var combinationSum = function(candidates, target) {
134134
- [47.permutations-ii](./47.permutations-ii.md)
135135
- [78.subsets](./78.subsets.md)
136136
- [90.subsets-ii](./90.subsets-ii.md)
137+
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
138+

problems/40.combination-sum-ii.md

+1
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,4 @@ var combinationSum2 = function(candidates, target) {
138138
- [47.permutations-ii](./47.permutations-ii.md)
139139
- [78.subsets](./78.subsets.md)
140140
- [90.subsets-ii](./90.subsets-ii.md)
141+
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)

problems/46.permutations.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,5 @@ var permute = function(nums) {
102102
- [47.permutations-ii](./47.permutations-ii.md)
103103
- [78.subsets](./78.subsets.md)
104104
- [90.subsets-ii](./90.subsets-ii.md)
105+
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
105106

problems/47.permutations-ii.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,4 @@ var permuteUnique = function(nums) {
104104
- [46.permutations](./46.permutations.md)
105105
- [78.subsets](./78.subsets.md)
106106
- [90.subsets-ii](./90.subsets-ii.md)
107+
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)

problems/78.subsets.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,6 @@ var subsets = function(nums) {
112112
- [46.permutations](./46.permutations.md)
113113
- [47.permutations-ii](./47.permutations-ii.md)
114114
- [90.subsets-ii](./90.subsets-ii.md)
115+
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
115116

116117

problems/90.subsets-ii.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ var subsetsWithDup = function(nums) {
112112
- [46.permutations](./46.permutations.md)
113113
- [47.permutations-ii](./47.permutations-ii.md)
114114
- [78.subsets](./78.subsets.md)
115+
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
115116

116117

117118

todo/str/214.shortest-palindrome.js

-38
This file was deleted.

todo/str/5.longest-palindromic-substring.js

-40
This file was deleted.

0 commit comments

Comments
 (0)