首页 > 其他 > 详细

[Leetcode]Valid Palindrome

时间:2014-10-29 12:42:58      阅读:307      评论: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,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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!