zoukankan      html  css  js  c++  java
  • Valid Palindrome

    Valid Palindrome

     Total Accepted: 22728 Total Submissions: 99271My Submissions

    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.


    public class Solution {
        public boolean isPalindrome(String s) {
            if (s == null) {
                return false;
            }
            if (s.length() == 0 || s.length() == 1) {
                return true;
            }
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < s.length();i++){
                Character ch = s.charAt(i);
    	            if(Character.isLetter(ch) || Character.isDigit(ch)){
                    sb.append(Character.toUpperCase(ch));
    
                } else {
                    continue;
                }
            }
            int start = 0 ;
            int end = sb.length() - 1;
            while ( start < end) {
                if (sb.charAt(start) == sb.charAt(end)) {
                    start ++;
                    end --;
                } else {
                    return false; 
                }
            }
            return true;
         }
    }

    这个题目的问题在于刚開始的时候没有考虑到可能是数字的情况。题目本身没有难度。
  • 相关阅读:
    LeetCode 169. Majority Element
    Object-c block块
    Object-c动态特性
    Object-c基础(2)
    Object-c基础
    LeetCode171:Excel Sheet Column Number
    LeetCode242:Valid Anagram
    LeetCood8:String to Integer
    理解qsort 中的 cmp
    google goble cache
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7015635.html
Copyright © 2011-2022 走看看