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

LeetCode --- Valid Palindrome

时间:2014-05-30 21:48:12      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

题目链接

判断字符串是否为回文串。

附上代码:

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         if (s.empty()) return true; // consider empty string as valid palindrome
 5         unsigned int len = s.size();
 6         unsigned int beg = 0, end = len - 1;
 7         while (beg < end) {
 8             while (beg <= end && !isdigit(s[beg]) && !isalpha(s[beg]))
 9                 beg++;
10             while (end >= beg && !isdigit(s[end]) && !isalpha(s[end]))
11                 end--;
12             //if (beg == end) return true;
13             if (beg >= end) return true;
14             if (isalpha(s[beg]) && isalpha(s[end])) {
15                 if (toupper(s[beg]) != toupper(s[end]))
16                     return false;
17             } else if (s[beg] != s[end]) {
18                 return false;
19             }
20             beg++;
21             end--;
22         }
23         
24         return true;
25     }
26 };
bubuko.com,布布扣

 

LeetCode --- Valid Palindrome,布布扣,bubuko.com

LeetCode --- Valid Palindrome

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/Stomach-ache/p/3760099.html

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