题目名称
- Valid Palindrome(Leetcode #125)
- Implement strStr(Leetcode #28)
- String to Integer(Leetcode #8)
- Add Binary(Leetcode #67)
- Longest Palindrome(Leetcode #409)
- 二叉搜索树的后续遍历序列(剑指offer #33)
- 二叉树中和为某一值的路径(剑指offer #34)
- 复杂链表的复制(剑指offer #35)
- 二叉搜索树与双向链表(剑指offer #36)
- 字符串的排列(剑指offer #37)
Valid Palindrome(Leetcode #125)
题目描述
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
2Input: "A man, a plan, a canal: Panama"
Output: true
解题思路
双指针解题,避开所有无效字符后对二者进行比对。若匹配则执行下一轮,否则退出。这里注意isalnum的使用。
解题方案
1 | class Solution { |
Implement strStr(Leetcode #28)
题目描述
Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example :1
2Input: haystack = "hello", needle = "ll"
Output: 2
解题思路
本身使用暴力是很容易的,我们在暴力的基础上升级为KMP算法。
解题方案
暴力(不匹配则回退,i-j为返回值)
1 | class Solution { |
KMP(非常好记忆,是模式串的自匹配,只是在字符类型较多时效率未必高)
1 | class Solution { |
String to Integer(Leetcode #8)
题目描述
Implement atoi which converts a string to an integer.
解题思路
利用find_first_of找到第一个不为空的字符,然后判断其是否为+-,若是则设定正负。然后遍历字符串,利用isDigit作为while退出条件。若退出时字符串尚未结束则说明当前存在非法字符。
解题方案
1 | class Solution { |
Add Binary(Leetcode #67)
题目描述
Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.
Example 1:1
2Input: a = "11", b = "1"
Output: "100"
Example 2:1
2Input: a = "1010", b = "1011"
Output: "10101"
解题思路
非常类似于之前做的高精度加法或者链表求和,与链表不同的是,我们这里利用str的operator+,将每一次的结果添加于res之后,最终翻转res。
解题方案
1 | class Solution { |
Longest Palindrome(Leetcode #409)
题目描述
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example “Aa” is not considered a palindrome here.
Note:
Assume the length of given string will not exceed 1,010.
Example:1
2
3
4
5
6Input:
"abccccdd"
Output:
7
Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
解题思路
若某个字母存在奇数次,则我们可以认为最大长度必须要减1,否则不必减1。我们将针对每一个字符遍历依次字符串,尽管遍历需要O(n)的时间,但由于字符种类已知,总开销依然是O(n)。此外,如果存在奇数,返回的结果是size-odds+1,原因在于单独的那一个可以放在最中间。
解题方案
1 | class Solution { |
扩展(Longest Palindromic Subsequence)
题目描述
Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000.
Example:1
2
3
4
5Input:
"bbbab"
Output:
4
One possible longest palindromic subsequence is "bbbb".
解题思路
这题与上一题不同的地方在于规定了必须是subsequence。本题使用动态规划求解较为方便。设dp[i][j]为字符str[i]、str[j]之间形成的子串所能构建的最长回文串,那么对于dp,存在状态转移方程为:1
2
3if str[i]==str[j] dp[i][j]=dp[i+1][j-1]+2;
else dp[i][j]=max(dp[i+1][j],dp[i][j-1]);
dp[i][i]=1;
解题方案
1 | class Solution { |
扩展(Longest Palindromic Substring)
题目描述
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:1
2
3Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:1
2Input: "cbbd"
Output: "bb"
解题思路
两种做法。
- 若str存在n个元素,那么应当有2n-1个作为回文字符串中点的位置。我们遍历这些中点,求解这些中点所能形成的回文子串的最大长度。最后得到最终的中点及最大长度,使用substr。
- 动态规划,dp[i][j]表示str[i]到str[j]形成的子串是否为回文串。有状态转移方程为
1
2if(j==i+1 && str[i]==str[j]) dp[i][j]=1;
while(j>i+1) dp[i][j]=(str[i]==str[j] && dp[i+1][j-1])
解题方案
扩张法
1 | class Solution { |
动态规划
1 | class Solution { |
二叉搜索树的后序遍历序列
解题思路
递归式求解。首先找到数组的末尾元素,其必为根节点。然后向前找到第一个不大于根的元素,其必为左子树根节点。若左子树中存在大于根节点的元素则返回false,否则再次判断左右子树是否具备二叉搜索性质。
解题方案
1 | class Solution { |
二叉树中和为某一值的路径
解题思路
朴素深搜,值得注意的是某个节点检查完毕后需要pop_back。
解题方案
1 | class Solution { |
复杂链表的复制(剑指offer #35)
(在OJ exercise8中已经介绍)
二叉搜索树与双向链表(剑指offer #36)
解题思路
递归式地连接。先将左子树构造为双链表,然后连接左子与根节点,然后再将右子树构造为双链表。最终连接完毕,返回根节点,一路向左找到头即可。
解题方案
1 | class Solution { |
字符串的排列
解题思路
递归式的解法,需要注意的式aux的string参数并非引用,因此每一次都需要复制,如果采用引用,则需要在aux的最后一行将str复原。(本解法针对重复元素亦有效)
解题方案
1 | class Solution { |