首页 > 代码库 > LeetCode --- Valid Palindrome
LeetCode --- Valid Palindrome
题目链接
判断字符串是否为回文串。
附上代码:
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 };
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。