zoukankan      html  css  js  c++  java
  • leetcode--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) {
            boolean isPalindrome = true;
    		s = s.toLowerCase();
    		int length = s.length();
    		int left = 0, right= length - 1;
    		while(left <= right){
    			if((s.charAt(left) < 97 || s.charAt(left) > 122) && (s.charAt(left) < 48 || s.charAt(left) > 57))
    				++left;
    			else if((s.charAt(right) < 97 || s.charAt(right) > 122) && (s.charAt(right) < 48 || s.charAt(right) > 57))
    				--right;
    			else{
    				isPalindrome &= (s.charAt(left) == s.charAt(right));
    				if(!isPalindrome)
    					break;
    				++left;
    				--right;
    			}
    		}
    		return isPalindrome;    
        }
    }
    

      

  • 相关阅读:
    MINA源码阅读之ACP
    高性能考量
    Intel项目Java小记
    Java NIO之Selector
    中广核需求分析心得
    Excel下拉框选项切换行颜色切换
    推理与证明习题
    常用逻辑用语习题
    统计章节的几个难点
    正态分布
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3783159.html
Copyright © 2011-2022 走看看