0076 判断回文串

方法一:从中间向两头比较

	public static boolean isPalindrome(String str){
        if (str == "" || str == null) {
            return false;
        }
        char[] c = str.toCharArray();
        int maxIndex = c.length - 1;
        boolean isPalindrome=true;
        for (int i=(maxIndex-1)>>1;i>=0;i--) {
            if (c[i] != c[maxIndex - i]) {
                isPalindrome=false;
                break;
            }
        }
        return isPalindrome;
    }

方法二:从两头向中间比较

	public static boolean isPalindrome(String str) {
        if (str == "" || str == null) {
            return false;
        }

        char[] c = str.toCharArray();
        int head = 0, tail = c.length - 1;
        while (head < tail) {
            if (c[head] != c[tail]) {
                return false;
            }
            head++;
            tail--;
        }
        return true;
    }
posted @ 2017-07-28 09:50  sonng  阅读(175)  评论(0编辑  收藏  举报