Leetcode | Palindrome
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
处理好大小写转换、非法字符忽略就可以。
class Solution {
public: bool isPalindrome(string s) {
if (s.empty()) return true;
int l = , r = s.length() - ;
char c1, c2;
while (r > l) {
while (true) {
if (l >= r) break;
if (s[l] >= 'a' && s[l] <= 'z') break;
if (s[l] >= 'A' && s[l] <= 'Z') { s[l] += ; break; }
if (s[l] >= '' && s[l] <= '') break;
l++;
} while (true) {
if (l >= r) break;
if (s[r] >= 'a' && s[r] <= 'z') break;
if (s[r] >= 'A' && s[r] <= 'Z') { s[r] += ; break; }
if (s[r] >= '' && s[r] <= '') break;
r--;
} if (s[l] != s[r]) return false;
l++; r--;
} return true;
}
};
这样写好一点。
class Solution {
public:
bool isDigit(char c) {
return (c >= '' && c <= '');
} bool isUppercase(char c) {
return (c >= 'A' && c <= 'Z');
} bool isLowercase(char c) {
return (c >= 'a' && c <= 'z');
} bool isValid(char c) {
return (isLowercase(c) || isDigit(c) || isUppercase(c));
} bool isPalindrome(string s) {
if (s.empty()) return true;
int n = s.length();
for (int i = , j = n - ; i < j; ) {
for (; i < j && !isValid(s[i]); i++);
for (; i < j && !isValid(s[j]); j--);
if (isUppercase(s[i])) s[i] += ;
if (isUppercase(s[j])) s[j] += ;
if (s[i] != s[j]) return false;
i++, j--;
}
return true;
}
};
Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
回溯法就可以了。
class Solution {
public: bool isPalindrome(string &s) {
int n = s.length();
if (n <= ) return true;
int l = , r = n - ;
while (r > l) {
if (s[l] != s[r]) return false;
r--; l++;
}
return true;
} vector<vector<string>> partition(string s) {
vector<vector<string>> rets; vector<string> ret;
bt(s, , ret, rets); return rets;
} void bt(string &s, int index, vector<string> &ret, vector<vector<string>> &rets) {
if (index >= s.length()) {
rets.push_back(ret);
return;
} for (int i = index; i < s.length(); ++i) {
string tmp(s.substr(index, i - index + ));
if (isPalindrome(tmp)) {
ret.push_back(tmp);
bt(s, i + , ret, rets);
ret.pop_back();
}
}
}
};
Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
这里主要有两层需要dp的。
1. 令p[i][j]为i到j之间需要的最小cut个数。我们要求的是p[0][n - 1]。第一个dp很简单,p[i][n - 1] = min{p[j+1][n-1]} + 1, 其中i<=j<n,s(i, j)是回文。
2. 判断回文其实也是一个dp的过程,不过每次都用循环。如果s(i, j)是回文,则p[i][j]=0。p[i][j] = 0 当且仅当str[i]== str[j] && p[i + 1][j - 1]=0。没有这一部分dp就会TLE了。这一步骤用递归就可以,注意的是,比较后要设置p[i][j],无论是否等于0.
class Solution {
public:
bool isPalindrome(string &s, int l, int r, vector<vector<int> > &p) {
if (l > r) return true;
if (p[l][r] == ) return true;
if (p[l][r] != -) return false;
if (s[l] != s[r]) return false; bool isPalin = isPalindrome(s, l + , r - , p);
if (isPalin) {
p[l][r] = ;
} else {
p[l][r] = r - l;
}
return isPalin;
} int minCut(string s) {
int n = s.length();
if (n <= ) return ;
vector<vector<int> > p(n, vector<int>(n, -));
for (int i = ; i < n; ++i) {
p[i][i] = ;
}
for (int i = n - ; i >= ; --i) {
p[i][n - ] = n - i - ;
for (int j = i; j < n; ++j) {
if (s[j] == s[i] && isPalindrome(s, i + , j - , p)) {
p[i][j] = ; if (j < n - && p[j + ][n - ] + < p[i][n - ]) {
p[i][n - ] = p[j + ][n - ] + ;
}
}
}
} return p[][n - ];
}
};
第三次写,用了两个数组。不过思路也算简单了。
class Solution {
public:
int minCut(string s) {
if (s.empty()) return ;
int n = s.length();
vector<vector<bool> > dp(n, vector<bool>(n, false));
vector<int> min(n, );
for (int i = ; i < n; ++i) {
dp[i][i] = true;
min[i] = min[i - ] + ;
for (int j = i - ; j >= ; --j) {
if ((j > i - || dp[j + ][i - ]) && s[i] == s[j]) {
dp[j][i] = true;
if (j == ) min[i] = ;
else if (min[j - ] + < min[i]) min[i] = min[j - ] + ;
}
}
}
return min[n - ];
}
};
空间上比起用vector<vector<int> >还是省了。因为用bool的话,最终用了O(n^2+n),用int虽然看起来只用了一个变量,但是却是O(4n^2)。
Leetcode | Palindrome的更多相关文章
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- LeetCode: Palindrome 回文相关题目
LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- NEFU 1146 又见A+B
又见a+b Problem:1146 Time Limit:1000ms Memory Limit:65535K Description 给定两个非负整数A,B,求他们的和. Input 多组输入,每 ...
- August 3rd, 2016, Week 32nd, Wednesday
I am looking for someone to share in an adventure. 我在找能和我一起分享冒险之旅的人. We are all looking for someone ...
- GLSL Entry point not found
解决方案: 在引用OpenGL的头文件 #include <GL/glew.h>#include <GL/glut.h> 前添加 #define GLUT_DISABLE_AT ...
- c# 正则表达式 匹配回车
1 "." 匹配除 "\n" 之外的任何单个字符,一般用".*?"匹配不包括回车的任意字符. 2 我们在用正则表达式分析html或者是xml ...
- sysctl命令详解
个人一般sysctl -p 或sysctl -a比较多使用 sysctl配置与显示在/proc/sys目录中的内核参数.可以用sysctl来设置或重新设置联网功能,如IP转发.IP碎片去除以及源路由检 ...
- yum rpm
本文多选自鸟哥的私房菜,非常感谢鸟哥^_ _^
- ***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)
客户端的请求体中的数据:[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id& ...
- .net学习之类与对象、new关键字、构造函数、常量和只读变量、枚举、结构、垃圾回收、静态成员、静态类等
1.类与对象的关系类是对一类事务的统称,是抽象的,不能拿来直接使用,比如汽车,没有具体指哪一辆汽车对象是一个具体存在的,看的见,摸得着的,可以拿来直接使用,比如我家的那辆刚刚买的新汽车,就是具体的对象 ...
- Visual Studio从此走入非Windows程序猿家
(此文章同时发表在本人微信公众号"dotNET每日精华文章") 在Build 2015大会上,微软放了很多大招,其中一个让普通(不管是微软生态还是非微软生态的)程序猿都密切关注的就 ...
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...