Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Jun 3, 2019
1 parent aa7dda6 commit dac830c
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 91 deletions.
13 changes: 0 additions & 13 deletions 131.palindrome-partitioning.js

This file was deleted.

1 change: 1 addition & 0 deletions assets/drawio/125.valid-palindrome.drawio
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>
Binary file added assets/problems/125.valid-palindrome-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/125.valid-palindrome-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions problems/125.valid-palindrome.md
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;
};
```
95 changes: 95 additions & 0 deletions problems/131.palindrome-partitioning.md
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)

2 changes: 2 additions & 0 deletions problems/39.combination-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,5 @@ var combinationSum = function(candidates, target) {
- [47.permutations-ii](./47.permutations-ii.md)
- [78.subsets](./78.subsets.md)
- [90.subsets-ii](./90.subsets-ii.md)
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)

1 change: 1 addition & 0 deletions problems/40.combination-sum-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ var combinationSum2 = function(candidates, target) {
- [47.permutations-ii](./47.permutations-ii.md)
- [78.subsets](./78.subsets.md)
- [90.subsets-ii](./90.subsets-ii.md)
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
1 change: 1 addition & 0 deletions problems/46.permutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,5 @@ var permute = function(nums) {
- [47.permutations-ii](./47.permutations-ii.md)
- [78.subsets](./78.subsets.md)
- [90.subsets-ii](./90.subsets-ii.md)
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)

1 change: 1 addition & 0 deletions problems/47.permutations-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ var permuteUnique = function(nums) {
- [46.permutations](./46.permutations.md)
- [78.subsets](./78.subsets.md)
- [90.subsets-ii](./90.subsets-ii.md)
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)
1 change: 1 addition & 0 deletions problems/78.subsets.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ var subsets = function(nums) {
- [46.permutations](./46.permutations.md)
- [47.permutations-ii](./47.permutations-ii.md)
- [90.subsets-ii](./90.subsets-ii.md)
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)


1 change: 1 addition & 0 deletions problems/90.subsets-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ var subsetsWithDup = function(nums) {
- [46.permutations](./46.permutations.md)
- [47.permutations-ii](./47.permutations-ii.md)
- [78.subsets](./78.subsets.md)
- [131.palindrome-partitioning](./131.palindrome-partitioning.md)



Expand Down
38 changes: 0 additions & 38 deletions todo/str/214.shortest-palindrome.js

This file was deleted.

40 changes: 0 additions & 40 deletions todo/str/5.longest-palindromic-substring.js

This file was deleted.

0 comments on commit dac830c

Please sign in to comment.