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.

处理好大小写转换、非法字符忽略就可以。

  1. class Solution {
  2. public:
  3.  
  4. bool isPalindrome(string s) {
  5. if (s.empty()) return true;
  6. int l = , r = s.length() - ;
  7. char c1, c2;
  8. while (r > l) {
  9. while (true) {
  10. if (l >= r) break;
  11. if (s[l] >= 'a' && s[l] <= 'z') break;
  12. if (s[l] >= 'A' && s[l] <= 'Z') { s[l] += ; break; }
  13. if (s[l] >= '' && s[l] <= '') break;
  14. l++;
  15. }
  16.  
  17. while (true) {
  18. if (l >= r) break;
  19. if (s[r] >= 'a' && s[r] <= 'z') break;
  20. if (s[r] >= 'A' && s[r] <= 'Z') { s[r] += ; break; }
  21. if (s[r] >= '' && s[r] <= '') break;
  22. r--;
  23. }
  24.  
  25. if (s[l] != s[r]) return false;
  26. l++; r--;
  27. }
  28.  
  29. return true;
  30. }
  31. };

这样写好一点。

  1. class Solution {
  2. public:
  3. bool isDigit(char c) {
  4. return (c >= '' && c <= '');
  5. }
  6.  
  7. bool isUppercase(char c) {
  8. return (c >= 'A' && c <= 'Z');
  9. }
  10.  
  11. bool isLowercase(char c) {
  12. return (c >= 'a' && c <= 'z');
  13. }
  14.  
  15. bool isValid(char c) {
  16. return (isLowercase(c) || isDigit(c) || isUppercase(c));
  17. }
  18.  
  19. bool isPalindrome(string s) {
  20. if (s.empty()) return true;
  21. int n = s.length();
  22. for (int i = , j = n - ; i < j; ) {
  23. for (; i < j && !isValid(s[i]); i++);
  24. for (; i < j && !isValid(s[j]); j--);
  25. if (isUppercase(s[i])) s[i] += ;
  26. if (isUppercase(s[j])) s[j] += ;
  27. if (s[i] != s[j]) return false;
  28. i++, j--;
  29. }
  30. return true;
  31. }
  32. };

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"]
]

回溯法就可以了。

  1. class Solution {
  2. public:
  3.  
  4. bool isPalindrome(string &s) {
  5. int n = s.length();
  6. if (n <= ) return true;
  7. int l = , r = n - ;
  8. while (r > l) {
  9. if (s[l] != s[r]) return false;
  10. r--; l++;
  11. }
  12. return true;
  13. }
  14.  
  15. vector<vector<string>> partition(string s) {
  16. vector<vector<string>> rets;
  17.  
  18. vector<string> ret;
  19. bt(s, , ret, rets);
  20.  
  21. return rets;
  22. }
  23.  
  24. void bt(string &s, int index, vector<string> &ret, vector<vector<string>> &rets) {
  25. if (index >= s.length()) {
  26. rets.push_back(ret);
  27. return;
  28. }
  29.  
  30. for (int i = index; i < s.length(); ++i) {
  31. string tmp(s.substr(index, i - index + ));
  32. if (isPalindrome(tmp)) {
  33. ret.push_back(tmp);
  34. bt(s, i + , ret, rets);
  35. ret.pop_back();
  36. }
  37. }
  38. }
  39. };

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.

  1. class Solution {
  2. public:
  3. bool isPalindrome(string &s, int l, int r, vector<vector<int> > &p) {
  4. if (l > r) return true;
  5. if (p[l][r] == ) return true;
  6. if (p[l][r] != -) return false;
  7. if (s[l] != s[r]) return false;
  8.  
  9. bool isPalin = isPalindrome(s, l + , r - , p);
  10. if (isPalin) {
  11. p[l][r] = ;
  12. } else {
  13. p[l][r] = r - l;
  14. }
  15. return isPalin;
  16. }
  17.  
  18. int minCut(string s) {
  19. int n = s.length();
  20. if (n <= ) return ;
  21. vector<vector<int> > p(n, vector<int>(n, -));
  22. for (int i = ; i < n; ++i) {
  23. p[i][i] = ;
  24. }
  25. for (int i = n - ; i >= ; --i) {
  26. p[i][n - ] = n - i - ;
  27. for (int j = i; j < n; ++j) {
  28. if (s[j] == s[i] && isPalindrome(s, i + , j - , p)) {
  29. p[i][j] = ;
  30.  
  31. if (j < n - && p[j + ][n - ] + < p[i][n - ]) {
  32. p[i][n - ] = p[j + ][n - ] + ;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. return p[][n - ];
  39. }
  40. };

第三次写,用了两个数组。不过思路也算简单了。

  1. class Solution {
  2. public:
  3. int minCut(string s) {
  4. if (s.empty()) return ;
  5. int n = s.length();
  6. vector<vector<bool> > dp(n, vector<bool>(n, false));
  7. vector<int> min(n, );
  8. for (int i = ; i < n; ++i) {
  9. dp[i][i] = true;
  10. min[i] = min[i - ] + ;
  11. for (int j = i - ; j >= ; --j) {
  12. if ((j > i - || dp[j + ][i - ]) && s[i] == s[j]) {
  13. dp[j][i] = true;
  14. if (j == ) min[i] = ;
  15. else if (min[j - ] + < min[i]) min[i] = min[j - ] + ;
  16. }
  17. }
  18. }
  19. return min[n - ];
  20. }
  21. };

空间上比起用vector<vector<int> >还是省了。因为用bool的话,最终用了O(n^2+n),用int虽然看起来只用了一个变量,但是却是O(4n^2)。

Leetcode | Palindrome的更多相关文章

  1. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  2. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

  3. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. LeetCode: Palindrome 回文相关题目

    LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...

  5. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  6. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  8. [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 ...

  9. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  10. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. Java性能优化权威指南-读书笔记(三)-JVM性能调优-内存占用

    新生代.老年代.永久代的概念不多说,这三个空间中任何一个不能满足内存分配请求时,就会发生垃圾收集. 新生代不满足内存分配请求时,发生Minor GC,老年代.永久代不满足内存分配请求时,发生Full ...

  2. css3学习总结8--CSS3 3D转换

    3D 转换 1. rotateX() 2. rotateY() otateX() 方法 通过 rotateX() 方法,元素围绕其 X 轴以给定的度数进行旋转. 示例: div { transform ...

  3. HTTP状态码和常用对照表

    http://tool.oschina.net/commons 响应码:“200” : OK: “302” : Found 暂时转移,用于重定向, Response.Redirect()会让浏览器再请 ...

  4. Quartus signal tapii 的使用

    此功能原来已经试验过,没有笔记.这次复习巩固下. 使用PLL 的程序. 1.新建signaltap ii 文件 注意以下几个地方,会用到 添加采样时钟 . 添加采样信号: 完成之后,编译下载 运行 两 ...

  5. Shell脚本入门与应用

    编写第一个shell脚本 如同其他语言一样,通过我们使用任意一种文字编辑器,比如 nedit.kedit.emacs.vi 等来编写我们的 shell 程序.程序必须以下面的行开始(必须方在文件的第一 ...

  6. [原]ASP.NET 数据库访问通用工具

    在工作中,有很多项目已上线后,很多项目的数据库服务器都不会对外开放的,外网想直接访问客户数据库服务器时,可能会出现困难. 这时就需要一个可以查询,更新数据库操作的页面了: 本来用sql语句直接操作数据 ...

  7. html 表单 dom 注意跟表单的name值一致

    html 表单 dom 注意跟表单的name值一致 <script type="text/javascript"> function checkForm() { var ...

  8. Struts2中ActionContext和ServletActionContext

    转自:http://blog.sina.com.cn/s/blog_6c9bac050100y9iw.html 在Web应用程序开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在A ...

  9. MATLAB学习笔记(十)——MATLAB图形句柄

    (一)图形对象及其句柄 一.图形对象 MATLAB图形对象包括: 1.MATLAB每一个具体图形一定包括计算机屏幕和图形窗口两个对象 二.图形对象句柄 1.定义 MATLAB在创建每一个图形对象时,都 ...

  10. CDH 的Cloudera Manager免费与收费版的对比表

    CDH 特性 免费版 付费版 Deployment, Configuration & Management 系统管理 Automated Deployment & Hadoop Rea ...