zoukankan      html  css  js  c++  java
  • Lintcode415-Valid Palindrome-Medium

    Given a string, determine if it is a palindrome, considering only alphanumeric(字母和数字) characters and ignoring cases.

    Example

    Example 1:

    Input: "A man, a plan, a canal: Panama"
    Output: true
    Explanation: "amanaplanacanalpanama"
    

    Example 2:

    Input: "race a car"
    Output: false
    Explanation: "raceacar"
    

    Challenge

    O(n) time without extra memory.

    Notice

    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.

    思路:

    字符串中只考虑字母和数字是否构成一个回文串,no extra space可以定义两个指针。

    注意:

    1. 两个指针命名,front 和 end,不要用i, j
    2. 同类中:方法与方法之间直接用方法名来互相调用。Java方法的调用。
    3. 多个if并列 和 else if 区别:If you have used multiple if statements then if the condition is true all will be executed. If you have used if and else if combination only one will be executed where first comes the true value 
      所以,如果把line 10 12 14 变成并列if,output会出错。因为如果连续出现两个或以上的非字母数字字符的话,就会输出false。比如 String s = a'+ba; 
    4. boolean方法命名以is开头。
    5. line 9, 不能写成 while (front != end) , 举例 s = "aa", 指针front, end 交叉后会outOfIndexException.

    代码:

     1 public class Solution {
     2     
     3     public boolean isPalindrome(String s) {
     4         if (s == null || s.length() == 0)
     5             return true;
     6             
     7         int front = 0;
     8         int end = s.length() - 1;
     9         while (front < end) {
    10             if (!isValid(s.charAt(front)))
    11                 front++;
    12             else if (!isValid(s.charAt(end)))
    13                 end--;
    14             else {
    15                 if (isValidCase(s.charAt(front), s.charAt(end))){
    16                     front++;
    17                     end--;
    18                 } else {
    19                     return false;
    20                 }
    21             }
    22             
    23         }
    24         return true;
    25     }
    26     
    27     public boolean isValid(char c) {
    28         return Character.isLetter(c) || Character.isDigit(c);
    29     }
    30     public boolean isValidCase(char a, char b) {
    31         if (a == b)
    32             return true;
    33         else if (Character.toUpperCase(a) == Character.toUpperCase(b))
    34             return true;
    35         else
    36         return false;
    37     }
    38 }

    代码优化:

    public class Solution {
        public boolean isPalindrome(String s) {
            if (s == null || s.length() == 0) {
                return true;
            }
    
            int front = 0;
            int end = s.length() - 1;
            while (front < end) {
                while (front < s.length() && !isvalid(s.charAt(front))) { // nead to check range of a/b
                    front++;
                }
    
                if (front == s.length()) { // for empty string “.,,,”     
                    return true; 
                }           
    
                while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b
                    end--;
                }
    
                if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) {
                    return false;
                } else {
                    front++;
                    end--;
                }
            }
    
            return true; 
        }
    
        private boolean isvalid (char c) {
            return Character.isLetter(c) || Character.isDigit(c);
        }
    }
  • 相关阅读:
    C# dynamic使用
    Linq简介二
    遇事处理方式
    为什么select关键字没有放到前面而是放到了后面
    decimal、float、double区别
    ViewBag、ViewData、TempData区别
    CommandBehavior.CloseConnection的使用
    LINQ简介一
    ViewBag、ViewData使用
    SQL Server 使用WriteText 存储大容量数据
  • 原文地址:https://www.cnblogs.com/Jessiezyr/p/10643865.html
Copyright © 2011-2022 走看看