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

[LeetCode] Valid Palindrome

时间:2015-07-10 21:57:44      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

The suggested solution to this problem has given a clear idea. The tricky part of this problem is to handle all the edge cases carefully and write a clean code.

The following code should be self-explanatory. Note that the use of toupper avoid some messy if-else statements.

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         int l = 0, r = s.length() - 1;
 5         while (l < r) {
 6             while (l < r && !isalnum(s[l])) l++;
 7             if (l >= r) break;
 8             while (r > l && !isalnum(s[r])) r--;
 9             if (toupper(s[l++]) != toupper(s[r--]))
10                 return false;
11         }
12         return true;
13     }
14 };

 

[LeetCode] Valid Palindrome

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4637228.html

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