LeetCode: Valid Palindrome 解题报告

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.

SOLUTION 1:

左右指针往中间判断。注意函数: toLowerCase

 1 /*
 2     SOLUTION 1: Iterator.
 3     */
 4     public boolean isPalindrome1(String s) {
 5         if (s == null) {
 6             return false;
 7         }
 8         
 9         int len = s.length();
10         
11         boolean ret = true;
12         
13         int left = 0;
14         int right = len - 1;
15         
16         String sNew = s.toLowerCase();
17         
18         while (left < right) {
19             // bug 1: forget a )
20             while (left < right && !isNumChar(sNew.charAt(left))) {
21                 left++;
22             }
23             
24             while (left < right && !isNumChar(sNew.charAt(right))) {
25                 right--;
26             }
27             
28             if (sNew.charAt(left) != sNew.charAt(right)) {
29                 return false;
30             }
31             
32             left++;
33             right--;
34         }
35         
36         return true;
37     }
38     
39     public boolean isNumChar(char c) {
40         if (c <= '9' && c >= '0' || c <= 'z' && c >= 'a' || c <= 'Z' && c >= 'A') {
41             return true;
42         }
43         
44         return false;
45     }
View Code

 

SOLUTION 2:

引自http://blog.csdn.net/fightforyourdream/article/details/12860445 的解答,会简单一点儿。不用判断边界。

左右指针往中间判断。新技能GET: isLetterOrDigit 

 1 /*
 2     SOLUTION 2: Iterator2.
 3     */
 4     public boolean isPalindrome(String s) {
 5         if (s == null) {
 6             return false;
 7         }
 8         
 9         int len = s.length();
10         
11         boolean ret = true;
12         
13         int left = 0;
14         int right = len - 1;
15         
16         String sNew = s.toLowerCase();
17         
18         while (left < right) {
19             // bug 1: forget a )
20             if (!Character.isLetterOrDigit(sNew.charAt(left))) {
21                 left++;
22             // bug 2: Line 67: error: cannot find symbol: method isLetterOrDigital(char)    
23             } else if (!Character.isLetterOrDigit(sNew.charAt(right))) {
24                 right--;
25             } else if (sNew.charAt(left) != sNew.charAt(right)) {
26                 return false;
27             } else {
28                 left++;
29                 right--;
30             }
31         }
32         
33         return true;
34     }
View Code

 

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/IsPalindrome_2014_1229.java

posted on 2014-12-30 20:16  Yu's Garden  阅读(617)  评论(0编辑  收藏  举报

导航