LeetCode | Valid Palindrome
来源:互联网 发布:国际麻将知乎 编辑:程序博客网 时间:2024/04/28 02:31
题目:
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) { // Start typing your C/C++ solution below // DO NOT write int main() function int i = 0; int j = s.size()-1; while(i<j) { while(!isAlpha(s[i])) { i++; if(i>=s.size()) { break; } }; while(!isAlpha(s[j])) { j--; if(j<0) { break; } }; if(i<j&&!sameAlpha(s[i++],s[j--])) { return false; } }; return true; } bool isAlpha(char c) { if(c - 'a' >= 0 && c - 'a' < 26) { return true; } else if(c - 'A' >= 0 && c - 'A' < 26) { return true; } else if(c >= '0' && c <= '9') { return true; } else { return false; } } bool sameAlpha(char c1, char c2) { if(c1 == c2) { return true; } else if(c1 - 'A' == c2 - 'a') { return true; } else if(c1 - 'a' == c2 - 'A') { return true; } else { return false; } }};
- [Leetcode] Valid Palindrome
- Leetcode: Valid Palindrome
- leetcode 55: Valid Palindrome
- [LeetCode] Valid Palindrome
- 【leetcode】Valid Palindrome
- LeetCode - Valid Palindrome
- [leetcode]Valid Palindrome
- [LeetCode]Valid Palindrome
- [Leetcode]Valid Palindrome
- LeetCode-Valid Palindrome
- [leetcode] Valid Palindrome
- LeetCode 125: Valid Palindrome
- LeetCode - Valid Palindrome
- leetcode之Valid Palindrome
- LeetCode | Valid Palindrome
- [leetcode]Valid Palindrome
- 【leetcode】Valid Palindrome
- LeetCode题解:Valid Palindrome
- 任意长度的高精度大整数和浮点数的加法和乘法
- 深入浅出CChart 每日一课——第十五课 实习之旅,百年老店之经典MFC
- 可执行文件(ELF)格式的理解
- S3C2440 Linux驱动移植——NAND驱动
- 神奇的图像处理
- LeetCode | Valid Palindrome
- C++面向对象程序设计中如何判断一个对象的类类型
- 【leetcode】Add Two Numbers
- String类的简易实现,用到了构造函数、析构函数、重载运算符、拷贝构造函数、友元函数等知识
- Matlab ode45 解微分方程
- Oracle Database 11g SQL 开发指南学习笔记:从数据库中检索数据
- 【android】Socket简单用法 转
- LeetCode | Flatten Binary Tree to Linked List
- uva193