LeetCode TOP

266 阅读5分钟
题目次数
53. 最大子序和7
215. 数组中的第K个最大元素6
236. 二叉树的最近公共祖先6
206. 反转链表6
21. 合并两个有序链表6
25. K 个一组翻转链表5
232. 用两个栈实现队列5
146. LRU缓存机制5
144. 二叉树的前序遍历4
344. 反转字符串4
543. 二叉树的直径4
160. 相交链表4
142. 环形链表 II3
104. 二叉树的最大深度3
394. 字符串解码3
145. 二叉树的后序遍历3
102. 二叉树的层序遍历3
3. 无重复字符的最长子串3
151. 翻转字符串里的单词3
958. 二叉树的完全性检验3
1. 两数之和3
94. 二叉树的中序遍历2
240. 搜索二维矩阵 II2
33. 搜索旋转排序数组2
121.买卖股票2
190. 颠倒二进制位2
199. 二叉树的右视图2
460. LFU缓存2
41. 缺失的第一个正数2
103. 二叉树的锯齿形层次遍历2
300. 最长上升子序列2
70. 爬楼梯2
54. 螺旋矩阵2
62. 不同路径2
189. 旋转数组2
48. 旋转图像2
27. 二叉树的镜像2
113. 路径总和 II2
25. K 个一组翻转链表9
3. 无重复字符的最长子串8
102. 二叉树的层序遍历6
124. 二叉树中的最大路径和6
206. 反转链表6
15. 三数之和6
146. LRU缓存机制6
215. 数组中的第K个最大元素5
2. 两数相加5
141. 环形链表5
234. 回文链表5
155. 最小栈4
105. 从前序与中序遍历序列构造二叉树4
33. 搜索旋转排序数组4
160. 相交链表4
128. 最长连续序列4
103. 二叉树的锯齿形层次遍历4
113. 路径总和 II4
121.买卖股票3
108. 将有序数组转换为二叉搜索树3
199.二叉树的右视图3
88. 合并两个有序数组3
162. 寻找峰值3
56. 合并区间3
110. 平衡二叉树3
236. 二叉树的最近公共祖先3
322. 零钱兑换3
101. 对称二叉树3
199. 二叉树的右视图3
20. 有效的括号3
143. 重排链表3
54. 螺旋矩阵2
221. 最大正方形2
23. 合并K个排序链表2
83. 删除排序链表中的重复元素2
70. 爬楼梯2
剑指 Offer 61. 扑克牌中的顺子2
32. 最长有效括号2
剑指 Offer 42. 连续子数组的最大和2
415. 字符串相加2
41. 缺失的第一个正数2
704. 二分查找2
958. 二叉树的完全性检验2
69. x 的平方根2
114. 二叉树展开为链表2

104. 二叉树的最大深度

    public int maxDepth(TreeNode root) {
        if (root == null)
            return 0;
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

142. 环形链表 II


    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null)
            return null;
        ListNode fast = head, slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast != slow)
            return null;
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

160. 相交链表

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode ptrA = headA;
        ListNode ptrB = headB;
        while (ptrA != ptrB) {
            ptrA = ptrA == null ? headB : ptrA.next;
            ptrB = ptrB == null ? headA : ptrB.next;
        }
        return ptrA;
    }

543. 二叉树的直径

class Solution {
    int length = 0 ; 
    public int diameterOfBinaryTree(TreeNode root) {
        dfsLength(root);
        return length;
    }

    private int dfsLength(TreeNode root) {
        if(root == null){
            return 0 ;
        }
        int lHeight = dfsLength(root.left);
        int rHeight = dfsLength(root.right);
        length = Math.max(length,lHeight+rHeight);
        return Math.max(lHeight,rHeight)+1;
    }
    
}

344. 反转字符串

class Solution {
    public void reverseString(char[] s) {
        if(s == null || s.length == 1){
            return ;
        }
        int l = 0 ,r = s.length-1;
        while(l < r){
            swap(l , r , s);
            l++;
            r--;
        }                                                                                                       
    }
    public void swap(int l ,int r , char[] s){
        char temp = s[l];
        s[l] = s[r];
        s[r] = temp ; 
    }
}

144. 二叉树的前序遍历


    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) return result;
        Stack<TreeNode> stack = new Stack();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode cur = stack.pop();
            result.add(cur.val);
            if (cur.right != null)
                stack.add(cur.right);
            if (cur.left != null)
                stack.add(cur.left);
        }
        return result;
    }

146. LRU缓存机制


class LRUCache {

    public static final int NOTFOUND = -1;
    int capacity = 0;
    LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();

    public LRUCache(int capacity) {
        this.capacity = capacity;
    }

    public int get(int key) {
        int value = NOTFOUND;
        if (!map.containsKey(key))
            return value;
        value = map.get(key);
        map.remove(key);
        // 保证 访问过的 元素 放在末尾
        map.put(key, value);
        return value;
    }

    public void put(int key, int value) {
        if (map.containsKey(key)) {
            map.remove(key);
        } else if (map.size() == capacity) {
            map.remove(map.keySet().iterator().next());
        }
        map.put(key, value);
    }

}

232. 用栈实现队列


class MyQueue {
    // in 与 out 两个栈 
    // 始终添加到 in 始终从out 出  out为空则从in中取
    private Stack<Integer> in = new Stack<>();
    private Stack<Integer> out = new Stack<>();

    public void push(int x) {
        in.push(x);
    }

    public int pop() {
        in2out();
        return out.pop();
    }

    public int peek() {
        in2out();
        return out.peek();
    }

    private void in2out() {
        if (out.isEmpty()) {
            while (!in.isEmpty()) {
                out.push(in.pop());
            }
        }
    }

    public boolean empty() {
        return in.isEmpty() && out.isEmpty();
    }
}

25. K 个一组翻转链表


    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || k == 0 || k == 1) {
            return head;
        }
        int length = 0;
        ListNode dummyNode = new ListNode(-1);
        ListNode prev = dummyNode, cur = head, next = null;
        dummyNode.next = head;
        while (head != null) {
            head = head.next;
            length++;
        }
        for (int i = 0; i < length / k; i++) {
            for (int j = 1; j < k; j++) {
                // 内循环中 prev 未变过 cur 未变过
                // 只是每次循环 将 cur 后的node 提到 prev后 d123 d213 d321
                next = cur.next;
                cur.next = next.next;
                next.next = prev.next;
                prev.next = next;
            }
            prev = cur;
            cur = prev.next;
        }
        return dummyNode.next;
    }

21. 合并两个有序链表


    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        if (l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }

206. 反转链表


    public ListNode reverseList(ListNode head) {
        if (head == null)
            return head;
        ListNode dummyNode = new ListNode(-1);
        while (head != null) {
            ListNode next = head.next;
            head.next = dummyNode.next;
            dummyNode.next = head;
            head = next;
        }
        return dummyNode.next;
    }

236. 二叉树的最近公共祖先

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == p || root == q || root == null )
            return root;
        TreeNode leftResult = lowestCommonAncestor(root.left,p,q);
        TreeNode rightResult = lowestCommonAncestor(root.right,p,q);
        return leftResult==null ? rightResult :rightResult==null?leftResult:root; 
    }

215. 数组中的第K个最大元素

    public int findKthLargest(int[] nums, int k) {
        if (nums == null)
            return 0;
        PriorityQueue<Integer> queue = new PriorityQueue<>(k);
        for (int ele : nums) {
            if (queue.size() < k) {
                queue.add(ele);
                continue;
            }
            if (queue.peek() < ele) {
                queue.poll();
                queue.add(ele);
            }
        }
        return queue.peek();
    }

53. 最大子序和.


    public int maxSubArray(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        // dp[i] --> 以 i 结尾的最大长度
        int[] dp = new int[nums.length];
        int maxValue;
        dp[0] = maxValue = nums[0];

        for (int i = 1; i < nums.length; i++) {
            dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
            if (dp[i] > maxValue) {
                maxValue = dp[i];
            }
        }
        return maxValue;
    }

103. 二叉树的锯齿形层次遍历

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        if (root == null)
            return new ArrayList();
        List<List<Integer>> result = new ArrayList<>();

        int flag = 0;
        Deque<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> path = new ArrayList();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();

                if (node.left != null)
                    queue.add(node.left);
                if (node.right != null)
                    queue.add(node.right);

                path.add(node.val);
            }

            if (flag % 2 == 1) {
                Collections.reverse(path);
            }
            flag++;
            if (path.size() != 0) {
                result.add(path);
            }
        }
        return result;
    }