
力扣
Mrrr_Li
时间一直都在向前,你也应该一直向前。
展开
-
283. 移动零(快慢指针)
题目要求算法思想:1.使用快慢指针,初始都设为0,慢指针指向的位置是快指针指向的非0元素的位置。2.最优解法是直接把非0元素赋值到慢指针的位置,每次操作完快指针指向的非0元素后,都要使慢指针+1.然后最后再将数组后面的位置赋值为的0代码实现class Solution { // public void moveZeroes(int[] nums) { // //快慢指针,时间O(n),空间O(1) // if(nums.lengt原创 2021-10-27 09:09:51 · 161 阅读 · 0 评论 -
135. 分发糖果
点此查看官方详细题解class Solution { public boolean canPlaceFlowers(int[] flowerbed, int n) { int count = 0; int m = flowerbed.length; int prev = -1; for (int i = 0; i < m; i++) { if (flowerbed[i] == 1) { ...原创 2021-03-25 21:21:20 · 101 阅读 · 0 评论 -
605. 种花问题
点此查看官方详细题解class Solution { public boolean canPlaceFlowers(int[] flowerbed, int n) { int count = 0; int m = flowerbed.length; int prev = -1; for (int i = 0; i < m; i++) { if (flowerbed[i] == 1) { ...原创 2021-03-25 21:09:40 · 99 阅读 · 0 评论 -
122. 买卖股票的最佳时机 II
解题思想:贪心算法官方详细讲解代码实现class Solution { public int maxProfit(int[] prices) { int ans = 0; int n = prices.length; for (int i = 1; i < n; ++i) { ans += Math.max(0, prices[i] - prices[i - 1]); } .原创 2021-03-25 20:55:08 · 108 阅读 · 0 评论 -
452. 用最少数量的箭引爆气球
class Solution { public int findMinArrowShots(int[][] points) { if(points.length == 0) return 0; Arrays.sort(points, (p1, p2) -> p1[1] < p2[1] ? -1 : 1); int res = 1; int pre = points[0][1]; for (int i =...原创 2021-03-25 20:23:45 · 136 阅读 · 0 评论 -
面试题 08.06. 汉诺塔问题
、class Solution{ public void hanota(List<Integer> A, List<Integer> B, List<Integer> C) { move(A.size(), A, B, C); } void move(int n, List<Integer> a, List<Integer> b, List<Integer> c){ ...原创 2021-03-25 14:05:34 · 133 阅读 · 0 评论 -
1143. 最长公共子序列Java实现
class Solution { public int longestCommonSubsequence(String text1, String text2) { if (text1 == null || text2 == null) return 0; char[] chars1 = text1.toCharArray(); if (chars1.length == 0) return 0; char[] c...原创 2021-03-14 17:14:40 · 192 阅读 · 0 评论 -
70. 爬楼梯
思路◼ 楼梯有 n 阶台阶,上楼可以一步上 1 阶,也可以一步上 2 阶,走完 n 阶台阶共有多少种不同的走法?假设 n 阶台阶有 f(n) 种走法,第 1 步有 2 种走法✓ 如果上 1 阶,那就还剩 n – 1 阶,共 f(n – 1) 种走法✓ 如果上 2 阶,那就还剩 n – 2 阶,共 f(n – 2) 种走法所以 f(n) = f(n – 1) + f(n – 2)Java实现...原创 2021-03-08 09:02:07 · 147 阅读 · 0 评论 -
53. 最大子序和Java实现,几种方法
1.穷举法class Solution { public int maxSubArray(int[] nums) { if (nums == null || nums.length == 0) return 0; int max = Integer.MIN_VALUE; for (int begin = 0; begin < nums.length; begin++){ for (int end = ...原创 2021-03-06 17:09:34 · 173 阅读 · 0 评论 -
90. 子集 II
回溯法class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); Arrays.sort(nums); //排序 getAns(nums, 0, new ArrayList<>(), ans); return ans;.原创 2021-03-04 14:41:57 · 137 阅读 · 1 评论 -
78. 子集
方法一:迭代法实现子集枚举class Solution { List<Integer> t = new ArrayList<Integer>(); List<List<Integer>> ans = new ArrayList<List<Integer>>(); public List<List<Integer>> subsets(int[] nums) { int .原创 2021-03-04 14:38:58 · 142 阅读 · 1 评论 -
40. 组合总和 II
Java实现class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { int len = candidates.length; List<List<Integer>> res = new ArrayList<>(); if (len == 0) { .原创 2021-03-04 14:13:10 · 212 阅读 · 2 评论 -
39. 组合总和
实现class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { int len = candidates.length; List<List<Integer>> res = new ArrayList<>(); if (len == 0) { r.原创 2021-03-04 14:07:05 · 103 阅读 · 1 评论 -
51. N 皇后
Java实现class Solution { /** * 数组索引是行号,数组元素是列号 */ int[] cols; //存储每一行的皇后的位置(即列号) /** * 一共有多少种摆法 */ int ways; List<List<String>> res = new ArrayList<>(); public List<List<String>>..原创 2021-03-03 21:50:34 · 135 阅读 · 1 评论 -
47. 全排列 II
class Solution { public List<List<Integer>> permuteUnique(int[] nums) { int len = nums.length; List<List<Integer>> res = new ArrayList<>(); if (len == 0) return res; Deque<Integer>.原创 2021-03-03 20:34:02 · 123 阅读 · 1 评论 -
LeetCode——46. 全排列
Java实现,回溯法class Solution { public List<List<Integer>> permute(int[] nums) { int len = nums.length; List<List<Integer>> res = new ArrayList<>(); if (len == 0) return res; Deque<Integer&.原创 2021-03-03 19:51:19 · 146 阅读 · 1 评论 -
1640. 能否连接形成数组JavaHashMap解决
class Solution { public boolean canFormArray(int[] arr, int[][] pieces) { // 构造 HashMap O(1) 取值 Map<Integer, int[]> map = new HashMap<>(); for (int[] piece : pieces) { map.put(piece[0], piece); //以.原创 2021-01-02 14:20:35 · 117 阅读 · 0 评论 -
面试必会LeetCode经典,TopK问题
373. 查找和最小的K对数字692. 前K个高频单词347. 前 K 个高频元素703. 数据流中的第 K 大元素(设计类)451. 根据字符出现频率排序378. 有序矩阵中第K小的元素原创 2021-01-02 11:12:22 · 292 阅读 · 0 评论 -
378. 有序矩阵中第K小的元素
class Solution { public int kthSmallest(int[][] matrix, int k) { PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(); for (int[] arr1: matrix){ for(int num: arr1){ priorityQueue.add(num).原创 2021-01-02 11:11:25 · 116 阅读 · 0 评论 -
373. 查找和最小的K对数字
暴力小根堆初始化优先级队列的容量为k,保留k个元素在优先级队列中,这个在代码的for循环里有体现,如果多于k个,就削掉堆顶元素。由于Java官方的PriorityQueue为小根堆实现,所以不改变比较策略的话,队列里的k个元素就是前k大的,其中堆顶是前k大中最小的。将比较策略反过来的话,队列里的k个元素就是前k小的,其中堆顶是前k小中最大的。class Solution { public List<List<Integer>> kSmallestPairs(int[] .原创 2021-01-02 10:58:52 · 133 阅读 · 0 评论 -
692. 前K个高频单词
方法一:排序官方的方法,一堆没见过的方法class Solution { public List<String> topKFrequent(String[] words, int k) { Map<String, Integer> count = new HashMap(); for (String word: words) { count.put(word, count.getOrDefault(word, 0).原创 2021-01-01 23:29:48 · 147 阅读 · 0 评论 -
347. 前 K 个高频元素
就是TopK问题,使用小根堆实现先是自己写的class Solution { public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> hashMap = new HashMap<>(); for (int num: nums) { hashMap.put(num, hashMap.getOrDefault(num,0)+1);原创 2021-01-01 19:18:56 · 159 阅读 · 0 评论 -
703. 数据流中的第 K 大元素
算是利用Java官方的优先级队列解决TopK问题吧k来确定优先级队列的最大容量,然后传入的nums数组表示待加入的数据流,再通过add方法来限制queue存储的元素,最后add方法返回最小堆的堆顶元素。代码实现class KthLargest { private PriorityQueue<Integer> queue; private int limit; public KthLargest(int k, int[] nums) { limi原创 2021-01-01 14:06:45 · 174 阅读 · 0 评论 -
451. 根据字符出现频率排序,HashMap+PriorityQueue+lambda表达式改变比较策略
代码实现class Solution { public String frequencySort(String s) { Map<Character, Integer> hashMap = new HashMap<>(); for (int i = 0; i < s.length(); i++) { hashMap.put(s.charAt(i), hashMap.getOrDefault(s.charAt(i.原创 2021-01-01 10:57:09 · 244 阅读 · 0 评论 -
23. 合并K个升序链表
第一次独立写出hard难度的题,记录一下,虽然确实水(逃/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = .原创 2020-12-31 21:49:05 · 82 阅读 · 0 评论 -
110. 平衡二叉树
题目**给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。**从顶至底(暴力法)就是从根节点开始判断,判断他的左右子树高度差的绝对值是否<=1,然后再递归判断他的左子树是否平衡、右子树是否平衡。代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode原创 2020-12-14 08:55:49 · 110 阅读 · 0 评论 -
889. 根据前序和后序遍历构造二叉树
题目代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode constructFromPrePost(int[] pre, int[]原创 2020-12-13 11:25:45 · 145 阅读 · 0 评论 -
106. 从中序与后序遍历序列构造二叉树
题目思路后序遍历的序列的最后一个元素是根节点。根据这个根节点切割、新建数组。和前序与后序遍历序列构造二叉树类似代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution {原创 2020-12-13 10:47:08 · 130 阅读 · 0 评论 -
105. 从前序与中序遍历序列构造二叉树
代码class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if(preorder.length==0 || inorder.length==0) { return null; } //根据前序数组的第一个元素,就可以确定根节点 TreeNode root = new TreeNode(preorder[0]); for(int i=0;i<preorder.length;+.原创 2020-12-13 10:28:34 · 179 阅读 · 0 评论 -
栈的LeetCode作业
◼ 有效的括号 :https://leetcode-cn.com/problems/valid-parentheses/solution/◼ 括号的分数https://leetcode-cn.com/problems/score-of-parentheses◼ 逆波兰表达式求值https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/◼ 基本计算器https://leetcode-cn.com/problems/原创 2020-12-13 09:18:34 · 105 阅读 · 0 评论 -
链表的LeetCode作业
◼ 删除链表中的节点 :https://leetcode-cn.com/problems/delete-node-in-a-linked-list◼ 反转一个链表:https://leetcode-cn.com/problems/reverse-linked-list◼ 判断一个链表是否有环,(快慢指针 :https://leetcode-cn.com/problems/linked-list-cycle/◼ 移除链表元素https://leetcode-cn.com/problems/remo原创 2020-12-13 09:14:38 · 151 阅读 · 0 评论 -
99. 恢复二叉搜索树
好家伙,第一次独立的完成hard的题(当然莫里斯遍历算法我还是不会,逃。。。)先是自己写的朴素的O(n)代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } *原创 2020-12-13 09:04:46 · 130 阅读 · 1 评论 -
173. 二叉搜索树迭代器
思路根据二叉搜索树中序遍历得到的结果为从小到大依次排列,得出答案。这糟糕的算法代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNo.原创 2020-12-12 20:44:02 · 104 阅读 · 0 评论 -
230. 二叉搜索树中第K小的元素
题目**给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素。说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数。来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。**先是自己写的糟糕的算法:/** * Definition for a binary tree原创 2020-12-12 20:28:48 · 118 阅读 · 0 评论 -
235. 二叉搜索树的最近公共祖先
题目给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。代码递归:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNod原创 2020-12-12 16:44:02 · 165 阅读 · 0 评论 -
938. 二叉搜索树的范围和
题目给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。思路:前序遍历+递归/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val原创 2020-12-12 16:14:48 · 73 阅读 · 0 评论 -
109. 有序链表转换二叉搜索树
与这一题类似:108.有序数组转换为二叉搜索树链表中间结点的查找(快慢指针法)点此进入代码/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode .原创 2020-12-12 15:43:31 · 139 阅读 · 0 评论 -
876. 链表的中间结点
使用快慢指针解法,起初快慢指针都指向head,慢指针每次循环走一步,快指针每次循环走两步,当块指针走完整个链表时,慢支针刚好走到链表的中间位置。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public Li.原创 2020-12-12 15:37:07 · 121 阅读 · 1 评论 -
108. 将有序数组转换为二叉搜索树
一、题目分析题意:根据升序数组,恢复一棵高度平衡的BST????。分析:BST的中序遍历是升序的,因此本题等同于根据中序遍历的序列恢复二叉搜索树。因此我们可以以升序序列中的任一个元素作为根节点,以该元素左边的升序序列构建左子树,以该元素右边的升序序列构建右子树,这样得到的树就是一棵二叉搜索树啦~ 又因为本题要求高度平衡,因此我们需要选择升序序列的中间元素作为根节点奥~/** * Definition for a binary tree node. * public class TreeNode {原创 2020-12-12 14:52:38 · 129 阅读 · 0 评论 -
530. 二叉搜索树的最小绝对差
好家伙思想:二叉搜索树的中序遍历结果是升序的,将遍历结果存到list中,再相邻的值相减,查找最小值/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { Lis.原创 2020-12-12 14:26:48 · 95 阅读 · 0 评论