码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode | Palindrome

时间:2014-05-12 16:27:07      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

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.

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

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     
 4     bool isPalindrome(string s) {
 5         if (s.empty()) return true;
 6         int l = 0, r = s.length() - 1;
 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] += 32; break; }
13                 if (s[l] >= 0 && s[l] <= 9) 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] += 32; break; }
21                 if (s[r] >= 0 && s[r] <= 9) break;
22                 r--;
23             }
24             
25             if (s[l] != s[r]) return false;
26             l++; r--;
27         }
28         
29         return true;
30     }
31 };
bubuko.com,布布扣

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

回溯法就可以了。

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     
 4     bool isPalindrome(string &s) {
 5         int n = s.length();
 6         if (n <= 1) return true;
 7         int l = 0, r = n - 1;
 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, 0, 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 + 1));
32             if (isPalindrome(tmp)) {
33                 ret.push_back(tmp);
34                 bt(s, i + 1, ret, rets);
35                 ret.pop_back();
36             }
37         }
38     }
39 };
bubuko.com,布布扣

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.

bubuko.com,布布扣
 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] == 0) return true;
 6         if (p[l][r] != -1) return false;
 7         if (s[l] != s[r]) return false;
 8         
 9         bool isPalin = isPalindrome(s, l + 1, r - 1, p);
10         if (isPalin) {
11             p[l][r] = 0;
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 <= 1) return 0;
21         vector<vector<int> > p(n, vector<int>(n, -1));
22         for (int i = 0; i < n; ++i) {
23             p[i][i] = 0;
24         }
25         for (int i = n - 2; i >= 0; --i) {
26             p[i][n - 1] = n - i - 1;
27             for (int j = i; j < n; ++j) {
28                 if (s[j] == s[i] && isPalindrome(s, i + 1, j - 1, p)) {
29                     p[i][j] = 0;
30                     
31                     if (j < n - 1 && p[j + 1][n - 1] + 1 < p[i][n - 1]) {
32                         p[i][n - 1] = p[j + 1][n - 1] + 1;
33                     }
34                 }
35             }
36         }
37         
38         return p[0][n - 1];
39     }
40 };
bubuko.com,布布扣

 

Leetcode | Palindrome,布布扣,bubuko.com

Leetcode | Palindrome

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/linyx/p/3704081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!