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

125. Valid Palindrome

时间:2019-03-10 13:43:21      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:ring   fine   cte   false   col   note   span   git   eterm   

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false
class Solution {
    public boolean isPalindrome(String s) {
        s = s.toLowerCase();
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            if (!Character.isLetterOrDigit(s.charAt(left))) ++left;
            else if (!Character.isLetterOrDigit(s.charAt(right))) --right;
            else if (s.charAt(left) != s.charAt(right)) return false;
            else { ++left; --right; }
        }
        return true;
    }
}

nmdrz题

125. Valid Palindrome

标签:ring   fine   cte   false   col   note   span   git   eterm   

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10505019.html

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