回文字符串,回文链表

leetcode中有一些与回文相关的题,下面我们来看几道。

1.判断字符串是否回文串

判断一个字符串是不是回文串,只需要使用双指针的方式,从字符串两端向中间靠拢即可。

public class Huiwen {

    public static boolean huiwenStr(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }

        int i=0, j=s.length()-1;
        while(i < j) {
            if (s.charAt(i) == s.charAt(j)) {
                i++;j--;
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String s = "abeba";
        boolean result = huiwenStr(s);
        System.out.println(result);
    }
}

2.判断一个链表是不是回文链表

回文链表是指正序遍历与逆序遍历的结果一样。因为单向链表的性质,无法直接逆序遍历,所以最直接的办法是将链表倒序存一条新的链表,然后与原链表进行对比。

public class ListNode {

    public int data;
    public ListNode next;

    public ListNode(int data) {
        this.data = data;
    }
    
}
public class HuiwenListV1 {

    public static ListNode init() {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(2);
        ListNode n5 = new ListNode(2);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        return n1;
    }

    public static ListNode reverse(ListNode root) {
        ListNode cur = root, pre = null;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public static boolean isHuiWen(ListNode a) {
        if (a == null) return false;
        ListNode b = reverse(a);
        while (a != null) {
            if (a.data == b.data) {
                a = a.next;
                b = b.next;
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode root = init();
        boolean result = isHuiWen(root);
        System.out.println(result);
    }
}

如果我们不采用上面这种解法,使用类似递归的解法,也可以达到同样的效果。

public class HuiwenListV2 {

    public static ListNode init() {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(2);
        ListNode n5 = new ListNode(1);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        return n1;
    }

    private static ListNode left = init();

    public static boolean ishuiwen(ListNode right) {
        if (right == null) return true;
        boolean result = ishuiwen(right.next);
        result = result && (right.data == left.data);
        left = left.next;
        return result;
    }

    public static void main(String[] args) {
        ListNode right = init();
        boolean result = ishuiwen(right);
        System.out.println(result);
    }
}

上面的解法思路,借鉴了链表的递归常用套路

public void traverse(ListNode head) {
    // 正序遍历逻辑
    traverse(head.next);
    // 逆序遍历代码
}

上面的代码核心思想,先初始化right为root,然后利用函数递归不停将right移动到底,再将right与left进行对比,就达到了类似双指针对比的目的。

猜你喜欢

转载自blog.csdn.net/bitcarmanlee/article/details/114210789