forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
luzhipeng
committed
Jun 3, 2019
1 parent
aa7dda6
commit dac830c
Showing
14 changed files
with
199 additions
and
91 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
|
||
## 题目地址 | ||
|
||
https://leetcode.com/problems/valid-palindrome/description/ | ||
|
||
## 题目描述 | ||
|
||
``` | ||
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. | ||
Note: For the purpose of this problem, we define empty string as valid palindrome. | ||
Example 1: | ||
Input: "A man, a plan, a canal: Panama" | ||
Output: true | ||
Example 2: | ||
Input: "race a car" | ||
Output: false | ||
``` | ||
|
||
## 思路 | ||
|
||
这是一道考察回文的题目,而且是最简单的形式,即判断一个字符串是否是回文。 | ||
|
||
针对这个问题,我们可以使用头尾双指针, | ||
|
||
- 如果两个指针的元素不相同,则直接返回false, | ||
- 如果两个指针的元素相同,我们同时更新头尾指针,循环。 直到头尾指针相遇。 | ||
|
||
时间复杂度为O(n). | ||
|
||
拿“noon”这样一个回文串来说,我们的判断过程是这样的: | ||
|
||
![125.valid-palindrome-1](../assets/problems/125.valid-palindrome-1.png) | ||
|
||
拿“abaa”这样一个不是回文的字符串来说,我们的判断过程是这样的: | ||
|
||
![125.valid-palindrome-2](../assets/problems/125.valid-palindrome-2.png) | ||
|
||
|
||
|
||
## 关键点解析 | ||
|
||
- 双指针 | ||
|
||
## 代码 | ||
|
||
```js | ||
|
||
/* | ||
* @lc app=leetcode id=125 lang=javascript | ||
* | ||
* [125] Valid Palindrome | ||
*/ | ||
// 只处理英文字符(题目忽略大小写,我们前面全部转化成了小写, 因此这里我们只判断小写)和数字 | ||
function isValid(c) { | ||
const charCode = c.charCodeAt(0); | ||
const isDigit = | ||
charCode >= "0".charCodeAt(0) && charCode <= "9".charCodeAt(0); | ||
const isChar = charCode >= "a".charCodeAt(0) && charCode <= "z".charCodeAt(0); | ||
|
||
return isDigit || isChar; | ||
} | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isPalindrome = function(s) { | ||
s = s.toLowerCase(); | ||
let left = 0; | ||
let right = s.length - 1; | ||
|
||
while (left < right) { | ||
if (!isValid(s[left])) { | ||
left++; | ||
continue; | ||
} | ||
if (!isValid(s[right])) { | ||
right--; | ||
continue; | ||
} | ||
|
||
if (s[left] === s[right]) { | ||
left++; | ||
right--; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return right <= left; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
|
||
## 题目地址 | ||
|
||
https://leetcode.com/problems/palindrome-partitioning/description/ | ||
|
||
## 题目描述 | ||
|
||
``` | ||
Given a string s, partition s such that every substring of the partition is a palindrome. | ||
Return all possible palindrome partitioning of s. | ||
Example: | ||
Input: "aab" | ||
Output: | ||
[ | ||
["aa","b"], | ||
["a","a","b"] | ||
] | ||
``` | ||
|
||
## 思路 | ||
|
||
这是一道求解所有可能性的题目, 这时候可以考虑使用回溯法。 回溯法解题的模板我们已经在很多题目中用过了, | ||
这里就不多说了。大家可以结合其他几道题目加深一下理解。 | ||
|
||
|
||
## 关键点解析 | ||
|
||
- 回溯法 | ||
|
||
## 代码 | ||
|
||
```js | ||
|
||
|
||
/* | ||
* @lc app=leetcode id=131 lang=javascript | ||
* | ||
* [131] Palindrome Partitioning | ||
*/ | ||
|
||
function isPalindrom(s) { | ||
let left = 0; | ||
let right = s.length - 1; | ||
|
||
while(left < right && s[left] === s[right]) { | ||
left++; | ||
right--; | ||
} | ||
|
||
return left >= right; | ||
} | ||
function backtrack(s, list, tempList, start) { | ||
const sliced = s.slice(start); | ||
|
||
if (isPalindrom(sliced) && (tempList.join("").length === s.length)) list.push([...tempList]); | ||
|
||
for(let i = 0; i < sliced.length; i++) { | ||
const sub = sliced.slice(0, i + 1); | ||
if (isPalindrom(sub)) { | ||
tempList.push(sub); | ||
} else { | ||
continue; | ||
} | ||
backtrack(s, list, tempList, start + i + 1); | ||
tempList.pop(); | ||
} | ||
} | ||
/** | ||
* @param {string} s | ||
* @return {string[][]} | ||
*/ | ||
var partition = function(s) { | ||
// "aab" | ||
// ["aa", "b"] | ||
// ["a", "a", "b"] | ||
const list = []; | ||
backtrack(s, list, [], 0); | ||
return list; | ||
}; | ||
|
||
|
||
``` | ||
|
||
## 相关题目 | ||
- [39.combination-sum](./39.combination-sum.md) | ||
- [40.combination-sum-ii](./40.combination-sum-ii.md) | ||
- [46.permutations](./46.permutations.md) | ||
- [47.permutations-ii](./47.permutations-ii.md) | ||
- [78.subsets](./78.subsets.md) | ||
- [90.subsets-ii](./90.subsets-ii.md) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.