【题解】【字符串】【Leetcode】Valid Palindrome
2014-02-01 18:00
417 查看
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
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.
思路:
这题不能更简单了,考察的是徒手写bug的能力= =
代码:
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.
思路:
这题不能更简单了,考察的是徒手写bug的能力= =
代码:
bool isPalindrome(string s) { int n = s.length(); int i = 0; int j = n-1; while(i<j){//while(i>j){//蠢哭了 while(!isalnum(s[i]) && i<n) i++;//没加i<n会超时,不看题用isalpha() while(!isalnum(s[j]) && j>=0) j--; if(i<j && tolower(s[i++]) != tolower(s[j--])) //if(s[i++] != s[j--] && i>j)//自加自减最坑了, 不看题没用tolower() return false; } return true; }
相关文章推荐
- LeetCode242_Valid Anagram(判断两个字符串是不是由完全一样字符组成) Java题解
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
- LeetCode 415. Add Strings题解 字符串加和
- [LeetCode] Max Points on a Line 题解
- [leetcode, python] Reverse Words in a String 反转字符串
- LeetCode题解:Maximum Subarray
- 【LeetCode题解】21.Merge Two Sorted Lists
- LeetCode 438. Find All Anagrams in a String (在字符串中找到所有的变位词)
- leetcode_205. Isomorphic Strings 字符串同构,由一个字符串映射到另一个字符串,一一映射
- LeetCode题解:Implement strStr()
- [LeetCode]题解(python):045-Jump Game II
- [LeetCode]题解(python):047-Permutations II
- Leetcode题解(25)
- LeetCode简易题解--098、099
- [LeetCode]Word Ladder 最短距离字符串转换 (Dijkstra)
- leetcode题解-81 Search in Rotated Sorted Array II
- leetcode104题 题解 翻译 C语言版 Python版
- leetcode题解-15. 3Sum && 16. 3Sum Closest
- LeetCode题解:Pascal's Triangle
- LeetCode题解:Implement Queue using Stacks