Leetcode_valid-palindrome
来源:互联网 发布:法律咨询 知乎 编辑:程序博客网 时间:2024/05/19 17:58
地址:http://oj.leetcode.com/problems/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.
思路:只把大小写字母和数字留下,用stl反置一下再对比是否相同。另外有个函数可以检测是否是字母和数字。不过在这题里不是很合适,因为有大小写的缘故。
int isalnum ( int c )
参考代码:
class Solution {public: bool isPalindrome(string s) { if(s.empty()) return true; string str; for(int i = 0; i <s.length(); ++i) { if((s[i]>='a'&&s[i]<='z') || (s[i]>='0'&&s[i]<='9')) str+=s[i]; else if(s[i]>='A'&&s[i]<='Z') str+=s[i]-'A'+'a'; } string rev_str = str; reverse(rev_str.begin(), rev_str.end()); return rev_str == str; }};
//SECOND TRIALclass Solution {public:bool isPalindrome(string s) {if(s.empty())return true;int cnt = 0;for(int i = 0; i<s.length(); ++i){if((s[i]>='0' && s[i]<='9')||(s[i]>='a' &&s[i]<='z'))s[cnt++] = s[i];else if(s[i]>='A' && s[i]<='Z')s[cnt++] = s[i] + 'a' - 'A';}s = s.substr(0, cnt);string revs(s.rbegin(), s.rend());return revs==s;}};
0 0
- Leetcode_valid-palindrome
- Leetcode_Valid Palindrome
- leetcode_Valid Palindrome
- Leetcode_valid-parentheses
- Leetcode_valid-sudoku
- LeetCode_Valid Sudoku
- leetcode_Valid Number
- leetcode_Valid Parentheses
- leetcode_Valid Sudoku
- leetcode_Valid Anagram
- leetcode_Valid Anagram_easy
- LeetCode_Valid Parentheses
- LeetCode_valid-anagram
- leetCode_Valid Number
- leetcode_Valid Perfect Square
- palindrome
- Palindrome
- Palindrome
- 639 - Don't Get Rooked(搜索回溯)
- hdu 4635 Strongly connected(连通分量)
- eclipse 恢复已删除文件
- 第五章 面向对象编程(二)
- 邮件发送插件
- Leetcode_valid-palindrome
- windows内核情景分析--系统调用
- Cygwin编译生成So文件时出错
- 如何向外行人解释什么是内存溢出
- String.Format格式说明
- JS中使用EL表达式的问题 http://tfy1332.iteye.com/blog/
- FIQ(快速中断请求)和IRQ(外部中断请求)
- SIM300命令参考
- POJ-3579-Median