zoukankan      html  css  js  c++  java
  • [LeetCode] Valid Palindrome

    Question:

    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.

    1、题型分类:

    2、思路:采用双指针,一个从前一个从后。

    3、时间复杂度:

    4、代码:

        public boolean isPalindrome(String s) {
            if(s.length()<=1) return true;
            int begin=0,end=s.length()-1;
            while(begin<=end)
            {
                char b=s.charAt(begin);
                char e=s.charAt(end);
                if(!isCharacterNummeric(b))
                {
                    begin++;
                    continue;
                }
                if(!isCharacterNummeric(e))
                {
                    end--;
                    continue;
                }
                if(intoLowwerCase(b)!=intoLowwerCase(e))
                    return false;
                else { begin++;end--;}
            }
            return true;
        }
        public boolean isCharacterNummeric(char c)
        {
            return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9');
        }
        public char intoLowwerCase(char c)
        {
            if(c>='a' && c<='z') return (char)(c-32);
            return c;
        }

    5、优化:

    6、扩展:

  • 相关阅读:
    java基础
    mysql入门
    基础整理
    遍历列表的两种方式
    oracle常用操作
    DIV+CSS网页布局技巧实例1:设置网页整体居中的代码
    html+css 淘宝首页静态页面案例
    WEB前端开发规范文档+CSS命名规范
    day05-苏宁易购导航html
    day04-html
  • 原文地址:https://www.cnblogs.com/maydow/p/4644575.html
Copyright © 2011-2022 走看看