leetcode Palindromic Substring
5. Longest Palindromic Substring
class Solution {
public:
string longestPalindrome(string s) {
//Insert '#'
string t = "$#";
for(int i = 0; i < s.size(); ++i){
t += s[i];
t += "#";
}
//Process t
vector<int> p(t.size(), 0);
int mx = 0;//可延伸的子串最右端
int id = 0;//可延伸的子串的中心点
int resLen = 0;//结果半径
int resCenter = 0;//结果中心点
for(int i = 1; i < t.size(); ++i){
p[i] = mx > i ? min( p[2*id - i], mx - i ) : 1;//遍历每个点,求半半径p[i];
while( t[ i + p[i]] == t[ i - p[i]] ){//如果关于i点半径的p[i]的元素值t[ i + p[i] ], t[ i - p[i]] 相等,半径p[i]就++
++p[i];
}
if ( mx < i + p[i] ) {
mx = i + p[i];
id = i;
}
if(resLen < p[i]){//更新
resLen = p[i];
resCenter = i;
}
}
return s.substr( (resCenter - resLen) / 2, resLen - 1);
}
};
125. Valid Palindrome
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
class Solution {
public:
bool isPalindrome(string s) {
int left = 0;
int right = s.size()-1;
while(left < right){
if ( !isAlphaNum(s[left]) ) ++left;//跳过非数字、非字母的
else if ( !isAlphaNum(s[right]) ) --right;
else if ( (s[left] + 32 - 'a')%32 != (s[right] + 32 - 'a')%32 )//见备注
return false;
else{
++left; --right;
}
}
return true;
}
bool isAlphaNum(char &ch){
if (ch >='a' && ch <= 'z') return true;
if (ch >='A' && ch <= 'Z') return true;
if (ch >='0' && ch <= '9') return true;
return false;
}
};
//备注:
//统一处理大小写字母的情况,因为小写字母比其对应的大写字母的ASCII码大32,
//所以如果遇到了大写字母,我们需要先加上32,然后再减去'a',就知道其相对于'a'的位置了,
//这个值肯定是小于32的,所以对32取余没啥影响。如果遇到小写字母,
//虽然加上了32,但是最后对32取余了,多加的32也就没了,所以还是能得到其相对于'a'的正确位置。
131. Palindrome Partitioning
class Solution {
public:
vector<string> path;
vector<vector<string>> result;
vector<vector<string>> partition(string s) {
solve(s, 0);
return result;
}
// 检查是否为回文串
bool isPalindrome(const string& s){
int begin = 0, end = s.length() - 1;
while(begin < end){
if(s[begin] != s[end])
return false;
++begin;
--end;
}
return true;
}
// 递归解决
void solve(const string &s,int pos){
if(pos == s.length()){
result.push_back(path);
return;
}
for(int i = pos; i < s.length(); ++i){
string prefix = s.substr(pos, i - pos + 1);//以s[pos]字符开头的所有前缀
if(!isPalindrome(prefix)) //不是回文串就继续找
continue;
path.push_back(prefix); //加入path
solve(s, i + 1); //从已经找到的回文串的下一个字符继续找
path.pop_back(); //记得移除,path还要继续复用
}
}
};
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
---------------------
作者:吴贝贝97
来源:CSDN
原文:https://blog.csdn.net/wbb1997/article/details/81158151
版权声明:本文为博主原创文章,转载请附上博文链接!
还有另外一个参考博客 https://blog.csdn.net/qq_17550379/article/details/85466081
409. Longest Palindrome
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:
Input:
“abccccdd”
Output:
7
Explanation:
One longest palindrome that can be built is “dccaccd”, whose length is 7.
class Solution {
public:
int longestPalindrome(string s) {
int odds = 0;
for (char c='A'; c<='z'; c++)
odds += count(s.begin(), s.end(), c) & 1;//如果字符串中的某个字符个数为奇数,那么odds+=1。1=b00000001。如果odds是偶数,结果为0;如果是奇数结果为1
//统计个数为奇数的字符,然后减去(odds-1)个,如果odds=0,那么就减0。假如aaa,即便是3个,也是减1,因为减去1之后就是偶数个了。
return s.size() - max(0, odds - 1);
}
};
转载请注明出处