Leetcode - Valid Palindrome

Paste_Image.png

My code:

public class Solution {
    public boolean isPalindrome(String s) {
        if (s == null)
            return false;
        if (s.length() == 0)
            return true;
        int head = 0;
        int tail = s.length() - 1;
        
        while (head < tail) {
            char tempHead = s.charAt(head);
            char tempTail = s.charAt(tail);
            while (tempHead < 48 || (tempHead > 57 && tempHead < 65) 
                    || (tempHead > 90 && tempHead < 97) || tempHead > 122) {
                head++;
                if (head == s.length())
                    return true;
                else
                    tempHead = s.charAt(head);
            }
            while (tempTail < 48 || (tempTail > 57 && tempTail < 65) 
                    || (tempTail > 90 && tempTail < 97) || tempTail > 122) {
                tail--;
                tempTail = s.charAt(tail);
            }
            
            if (tempHead >= 65 && tempHead <= 90) {
                if (tempTail >= 65 && tempTail <= 90) {
                    if (tempHead == tempTail) {
                        head++;
                        tail--;
                    }
                    else
                        return false;
                }
                else if (tempTail > 90) {
                    if (tempHead == tempTail - 32) {
                        head++;
                        tail--;
                    }
                    else
                        return false;
                }
                else
                    return false;
            }
            else if (tempHead > 90) {
                if (tempTail >= 65 && tempTail <= 90) {
                    if (tempHead == tempTail + 32) {
                        head++;
                        tail--;
                    }
                    else
                        return false;
                }
                else if (tempTail > 90) {
                    if (tempHead == tempTail) {
                        head++;
                        tail--;
                    }
                    else
                        return false;
                }
                else
                    return false;
                
            }
            else {
                if (tempHead == tempTail) {
                    head++;
                    tail--;
                }
                else
                    return false;
            }
        }
        return true;
    }
}

My test result:

这道题目比较简单。主要烦的两点。
一个是他是大小写忽略的,要用if语句判断好这些情况。然后原有的字符串还有数字。不能有toLowercase 这些方法,太浪费时间了,每用一次都要遍历一次字符串!
第二个是,忽略其他非字母数字的字符。所以需要里面用两个while过滤一下。
同时,当head跑到字符串尾巴处时,表示该字符串不含数字字母,就直接返回true就行了。
然后在tail里面不需要考虑这个corner case,因为如果真是这样,才head里面已经判断结束返回了。

**
总结: String, traversal
**

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public boolean isPalindrome(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }
        s = s.toLowerCase();
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            while (i < s.length() && !isValid(s.charAt(i))) {
                i++;
            }
            while (j >= 0 && !isValid(s.charAt(j))) {
                j--;
            }
            if (i >= j) {
                break;
            }
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            else {
                i++;
                j--;
            }
        }
        return true;
    }
    
    private boolean isValid(char c) {
        if (c >= '0' && c <= '9') {
            return true;
        }
        else if (c >= 'a' && c <= 'z') {
            return true;
        }
        else {
            return false;
        }
    }
}

感觉这么一年下来,自己写的代码水平还是有些进步的。。。
之前怎么写的那么复杂。。
这道题目最重要的是理解一个单词:
alphanumeric
文字数字的。

所以我单独写了一个函数来检查是否valid

其他就差不多了。

Anyway, Good luck, Good luck, Richardo! -- 09/17/2016

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容