首页 > 其他 > 详细

回文数

时间:2019-02-01 16:55:52      阅读:161      评论:0      收藏:0      [点我收藏+]

题目:

Given a string, determine if it is a palindrome, considering only alphanumeric characters
and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
Example Questions Candidate Might Ask:
Q: What about an empty string? Is it a valid palindrome?
A: For the purpose of this problem, we define empty string as valid palindrome.

 

解答:

使用两个游标

public boolean isPalindrome(String s) {
    int i = 0;
    int j = s.length()-1;
    while(i < j) {
        while(i < j && !Character.isLetterOrDigit(s.charAt(i))) {
            i++;
        }

        while(i < j && !Character.isLetterOrDigit(s.charAt(j))) {
            j--;
        }

        if(Character.toLowerCase(s.charAt(j) != Character.toLowerCase(s.charAt(i))) {
            return false;
        }

        i++;
        j--;
    }

    return true;
}

 

回文数

原文:https://www.cnblogs.com/wylwyl/p/10346245.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!