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

[LeetCode]Valid Palindrome

时间:2015-11-10 00:21:47      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:(链接)

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.

解题思路:

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         int len = s.size();
 5         if (len <= 1) {
 6             return true;
 7         }
 8         
 9         int start = 0;
10         int last = len - 1;
11         while (start <= last) {
12             while (start <=last && !isalnum(s[start])) { ++start; }
13             while (start <= last && !isalnum(s[last])) { -- last; }
14             
15             if (start <= last && tolower(s[start]) != tolower(s[last])) {
16                 return false;
17             }
18             
19             ++start;
20             --last;
21         }
22         
23         return true;
24     }
25 };

 

[LeetCode]Valid Palindrome

标签:

原文地址:http://www.cnblogs.com/skycore/p/4951623.html

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