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

[LeetCode]125 Valid Palindrome

时间:2015-01-07 19:15:41      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:leetcode

https://oj.leetcode.com/problems/valid-palindrome/

http://blog.csdn.net/linhuanmars/article/details/22775045

public class Solution {
    public boolean isPalindrome(String s) {
    {
        if (s == null)
            return false;
        
        if (s.isEmpty())
            return true;
            
        char[] chars = s.toLowerCase().toCharArray();
        int indexL = 0;
        int indexR = chars.length - 1;
        
        while (indexL < indexR)
        {
            char cL = chars[indexL];
            if (ignore(cL))
            {
                indexL ++;
                continue;
            }
            
            char cR = chars[indexR];
            if (ignore(cR))
            {
                indexR --;
                continue;
            }
            
            if (cL != cR)
            {
                return false;
            }
            
            indexL ++;
            indexR --;
        }
        return true;
    }
    
    private boolean ignore(char c)
    {
        return !((c >= ‘a‘ && c <= ‘z‘) || (c >= ‘0‘ && c <= ‘9‘));
    }
}


[LeetCode]125 Valid Palindrome

标签:leetcode

原文地址:http://7371901.blog.51cto.com/7361901/1600196

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