zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    Linux 重新挂载分区的方法
    SQL复习三(子查询)
    SQL复习四(完整性约束)
    SQL 复习二(数据查询语言)
    SQL复习一(基础知识)
    在windos 环境下安装
    tt程序分析(一)
    单例模式Singleton
    用命令行使用soot反编译生成jimple
    在win10环境下安装eclipse mars版本
  • 原文地址:https://www.cnblogs.com/23lalala/p/3506847.html
Copyright © 2011-2022 走看看