首页 > 其他 > 详细

[LeetCode] Valid Palindrome

时间:2015-07-10 21:56:08      阅读:254      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!