回顾前面刷过的算法(1)

之前刷了几十道简单的算法题,现在进行回顾,主要是为了巩固对每一道算法的理解,每次回顾都可能有新的收获,我回顾的方式主要就是再次理解一遍算法,然后重新手敲一遍,手敲是必须的,光看不写是没用的,记录展示如下

package com.example.flow.algorithm;

import java.util.Deque;
import java.util.LinkedList;

public class Test {
    class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    //相交链表
    //思路: 定义两指针分别循环遍历自身,遍历到结尾时再将指针指向对方头结点继续遍历,直到两个指针相等
    private ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode p = headA, q = headB;
        while (p != q) {
            if (p.next != null) {
                p = p.next;
            } else {
                p = headB;
            }
            if (q.next != null) {
                q = q.next;
            } else {
                q = headA;
            }
        }
        return p;
    }

    //二叉树的最近公共祖先
    //思路: 通过递归来遍历二叉树左右子树,如果返回的结点不为null则说明找到了
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == p || root == q || root == null) {
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left != null && right != null) {
            return root;
        } else if (left != null) {
            return left;
        } else {
            return right;
        }
    }

    //回文链表
    //思路: 利用快慢指针找中间结点,把链表从中间分为两份,翻转后半部分链表,判断回文后再恢复链表
    public ListNode reverseList(ListNode head) {
        ListNode temp = null;
        ListNode p = head;
        ListNode q = p.next;
        while (q != null) {
            p.next = temp;
            temp = p;
            p = q;
            q = q.next;
        }
        p.next = temp;
        return p;
    }

    public boolean isPalindrome(ListNode head) {
        ListNode p = head, q = head;
        while (q.next != null && q.next.next != null) {
            p = p.next;
            q = q.next;
            q = q.next;
        }
        ListNode end = p;
        end.next = null;
        ListNode h1 = head, h2 = p.next;
        h2 = reverseList(h2);
        //判断是否是回文
        p = h1;
        q = h2;
        while (p != null && q != null) {
            if (p.val != q.val) {
                end.next = reverseList(h2);
                return false;
            } else {
                p = p.next;
                q = q.next;
            }
        }
        end.next = reverseList(h2);
        return true;
    }

    //每日温度
    //思路: 利用单调栈,栈存储元素索引,当出现新入栈的比栈内元素大时,则将可出栈元素出栈并记录其索引差,再将这个新元素入栈
    public int[] dailyTemperatures(int[] temperatures) {
        int length = temperatures.length;
        Deque<Integer> stack = new LinkedList<Integer>();
        int[] ans = new int[length];
        for (int i = 0; i < length; i++) {
            int temperature = temperatures[i];
            if (!stack.isEmpty() && temperature > temperatures[stack.peek()]) {
                int index = stack.pop();
                ans[index] = i - index;
            }
            stack.push(i);
        }
        return ans;
    }


    //翻转二叉树
    //思路: 递归,先写左右子树只有一个元素的二叉树的翻转,然后得出递推关系式
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return null;
        TreeNode temp = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(temp);
        return root;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值