• Valid Palindrome


    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.

    Note:
    Have you consider that the string might be empty? This is a good question to ask during an interview.

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

    public class Solution {
        public boolean isPalindrome(String s) {
            if (s==null||s.length()==0) {
                return true;
            }
            char a = 'a';
            char z = 'z';
            int begin = 0,end = s.length()-1;
            //这里只能用一次不能再循环里调用 不然会TLE
            char[] chars = s.toCharArray();
            while (begin<=end) {
                char charBegin = chars[begin];
                //处理大小写转换问题,统一转小写
                if (charBegin>='A'&&charBegin<='Z') {
                    charBegin = (char) (charBegin + 32);
                }
                char charEnd = chars[end];
                if (charEnd>='A'&&charEnd<='Z') {
                    charEnd = (char) (charEnd + 32);
                }
                //不是可用的字符跳过 注意边界没有 =
                if (charBegin < a || charBegin > z) {
                    if (charBegin<'0' || charBegin>'9') {
                        begin++;
                        continue;
                    }
                }
                if (charEnd < a || charBegin > z) {
                    if (charEnd<'0' || charEnd>'9') {
                        end--;
                        continue;
                    }
                }
                if (charBegin != charEnd) {
                    return false;
                }
                begin++;end--;
            }
            return true;
        }
    }
  • 相关阅读:
    ruby安装方法
    移动端适配问题px->rem方法
    判断元素阶段类型
    domReady
    电梯导航
    判断浏览器的版本以及浏览器内核( 转发 )
    AWS
    Amazon S3
    Pipeline 模型
    RESTful API
  • 原文地址:https://www.cnblogs.com/23lalala/p/3506847.html
Copyright © 2020-2023  润新知