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;    
        }
    }
    

      

  • 相关阅读:
    Oracle基础操作
    Linux的常用命令
    Javascript 上课笔记
    Linux操作指令
    css样式分类
    DW
    标准sql执行顺序
    Mysql数据库
    模拟课----需求文本
    php 发送邮件
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3783159.html
Copyright © 2011-2022 走看看