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

[Leetcode]Valid Palindrome

时间:2014-10-29 12:44:21      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   for   sp   div   on   

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,continue在这的作用有点费解,没有的话会报错,待研究。。。

 1 class Solution {
 2 public:
 3     bool isValid(char c) {
 4     return (c >= a && c <= z) || (c >= A && c <= Z) || (c >= 0 && c <= 9);
 5 }
 6 bool isPalindrome(string s) {
 7     int start = 0, end = s.size() - 1;
 8     while (start < end) {
 9         if (!isValid(s[start])) { start ++; continue; }
10         if (!isValid(s[end])) { end --; continue; }
11         if (s[start] != s[end] && abs(s[start] - s[end]) != 32) return false;
12         start ++;
13         end --;
14     }
15     return true;
16 }
17 };

[Leetcode]Valid Palindrome

标签:style   blog   io   color   ar   for   sp   div   on   

原文地址:http://www.cnblogs.com/sofeii/p/4058968.html

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