1. 程式人生 > >Lintcode415-Valid Palindrome-Medium

Lintcode415-Valid Palindrome-Medium

ice put pos exception efi lower stat sample fine

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

Lintcode415-Valid Palindrome-Medium