Longest Substring Without Repeating Characters
问题描述
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
分析
可以使用哈希表来记录字符串中字符的出现位置,遍历字符串,每个循环都更新最长字符串的长度。
解答
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s.empty())
return 0;
int result = 1;
vector<int> flag(256, -1);
flag[s[0]] = 0;
for (int i = 1, j = -1; i < s.size(); i++)
{
if (flag[s[i]] > j)
j = flag[s[i]] ;
flag[s[i]] = i;
if (result < i - j)
result = i - j;
}
return result;
}
};
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:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example:
Input: “cbbd”
Output: “bb”
分析
很经典的题目,求解的方法有多种,暴力求解仍然适用,也可以使用动态规划,但笔者觉得时间复杂度不够好,所以笔者的思路是:指定中间字符(start)向两边遍历字符,start 的选择由最左边开始跳跃选择,当上一位置的start与其右边或start与其两边均不满足回文性质时,start递增1,否则,start更新到上一包含start回文子串的最右端,记录最长子串的左端索引以及长度,当start未经过的字符串部分长度不大于目前最长子串的一半时停止遍历,由最长子串的左端和长度即可得到答案。
解答
class Solution {
public:
string longestPalindrome(string s) {
if (s.size() < 2)
return s;
int len = s.size();
int max_left = 0;
int max_len = 1;
int left = 0;
int right = 0;
for (int start = 0; start < len && len - start > max_len / 2;)
{
left = right = start;
while (right < len - 1 && s[right + 1] == s[right])
++right;
start = right + 1;
while (right < len - 1 && left > 0 && s[right + 1] == s[left - 1])
{
++right;
--left;
}
if (max_len < right - left + 1)
{
max_left = left;
max_len = right - left + 1;
}
}
return s.substr(max_left, max_len);
}
};
ZigZag Conversion
问题描述
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.
分析
根据题目找出规律,然后根据规律求解即可
P A H N
A P + L S + I I + G
Y I R
如上所示,拆分之字形并按从上到下再分为三部分,头部只加一个字符,中间加两个字符,尾部添加一个字符。
解答
class Solution {
public:
string convert(string s, int numRows) {
if (s.size() <= numRows || numRows == 1)
return s;
string result = "";
int add = numRows * 2 - 2;
for (int i = 0; i < s.size(); i += add)
result += s[i];
for (int i = 1; i < numRows - 1; i++)
{
for (int j = i; j < s.size(); j += add)
{
int l = (numRows - i - 1) * 2;
if (j + l < s.size())
result = result + s[j] + s[j + l];
else
result += s[j];
}
}
for (int i = numRows - 1; i < s.size(); i += add)
result += s[i];
return result;
}
};
String to Integer (atoi)
问题描述
Implement atoi to convert a string to an integer.
分析
需要考虑字符串的诸多可能,空串,不包含数值的串,有正负号的串,数值的大小超出范围。
解答
class Solution {
public:
int myAtoi(string str) {
long result = 0;
int flag = 1;
for(int i = 0; i < str.size(); i++)
{
if (str[i] == ' ')
continue;
if(str[i] == '-')
{
i++;
flag = -1;
}
else if (str[i] == '+')
{
i++;
}
while('0' <= str[i] && str[i] <= '9' && i < str.size())
{
result = result*10 + str[i]-'0';
i++;
if(result * flag >= INT_MAX)
return INT_MAX;
else if(result * flag <= INT_MIN)
return INT_MIN;
}
return result * flag;
}
return result;
}
};