力扣题解
weixin_42604028
这个作者很懒,什么都没留下…
展开
-
并查集(399,990,547、1631)
如果一个问题具有传递性,则考虑使用并查集!399.除法求值399-除法求值class Solution { public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) { //并查集!! //如果一个问题具有传递性,则考虑使用并查集!! int equa原创 2021-10-13 21:58:46 · 108 阅读 · 0 评论 -
560-和为k的子数组(前缀和)
class Solution { public int subarraySum(int[] nums, int k) { //前缀和 Map<Integer, Integer> map = new HashMap<>(); map.put(0, 1);//map保存前i项和的值以及出现的次数 int ans = 0, pre = 0; for(int i = 0;i < nums.lengt原创 2021-10-10 14:31:05 · 67 阅读 · 0 评论 -
581-最短无序连续子数组
判断数组元素是否无序的方法!class Solution { public int findUnsortedSubarray(int[] nums) { /*思想:从左向右遍历,若当前数比前面的最大值小则表明该数需要重排,以此可以找到需要重排的最右端 同理,从右向左遍历,若当前数比之前的最小值大则表明该数需要重排,以此可以找到需要重排的最左端 */ int n = nums.length; int ma原创 2021-10-06 15:40:32 · 59 阅读 · 0 评论 -
01背包问题(416、494、474、1049)
01背包问题---->动态规划class Solution { public boolean canPartition(int[] nums) { //动态规划(二维!) 01背包问题! int sum = 0, n = nums.length; for(int num : nums){ sum += num; } if(sum % 2 == 1) return f原创 2021-09-17 11:50:12 · 72 阅读 · 0 评论 -
240-搜索二维矩阵Ⅱ
将矩阵看作二叉搜索树!!class Solution { public boolean searchMatrix(int[][] matrix, int target) { //将矩阵看作二叉搜索树 int m = matrix.length, n = matrix[0].length; //从右上角开始搜索(也可以从左下角开始) int row = 0, col = n-1; while(row < m &am原创 2021-09-10 10:55:40 · 50 阅读 · 0 评论 -
85-最大矩形
单调栈class Solution { public int maximalRectangle(char[][] matrix) { //单调栈 结合第84题! int row = matrix.length, col = 0; if(row > 0) col = matrix[0].length; int[][] dp = new int[row][col]; //dp[i][j]记录第原创 2021-09-04 10:48:08 · 55 阅读 · 0 评论 -
338-比特位计数
动态规划class Solution { public int[] countBits(int n) { //动态规划 找做高有效位 int[] dp = new int[n+1]; dp[0] = 0; int highBit = 0;//记录最近的2的整数幂 for(int i = 1;i < n+1;i++){ if((i & (i-1)) == 0){//满足则说明i是2的原创 2021-09-03 10:52:37 · 54 阅读 · 0 评论 -
72-编辑距离
1.自顶向下的递归超时了,因为递归时进行了很多重复计算class Solution { public int minDistance(String word1, String word2) { /* 操作可以简化为以下3种: 1.对A进行插入 2.对A进行删除 3.对A进行替换 */ //自顶向下的递归 超时。。 //每次比较最后一个字符 if(word原创 2021-09-01 15:47:23 · 49 阅读 · 0 评论 -
买股票(Ⅰ,Ⅱ,Ⅲ,Ⅳ,冷冻期,手续费)
121.买卖股票的最佳时机class Solution { public int maxProfit(int[] prices) { //一次遍历 int minPrice = prices[0]; int maxProfit = 0; for(int i = 1;i < prices.length;i++){ if(prices[i] < minPrice) min原创 2021-08-26 13:50:11 · 68 阅读 · 0 评论 -
子集(Ⅰ,Ⅱ)
78.子集回溯法class Solution { List<List<Integer>> ans = new ArrayList<>(); List<Integer> ls = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { //回溯法 f(nums, 0); r原创 2021-08-24 12:24:01 · 66 阅读 · 0 评论 -
84-柱状图的最大矩形
单调栈class Solution { public int largestRectangleArea(int[] heights) { //核心思想:以h[i]为高的矩形的宽等于i左右两边第一个小于h[i]之间的距离 //单调栈 int len = heights.length, area = 0; Deque<Integer> st = new LinkedList<>(); //添加哨兵原创 2021-08-23 12:05:20 · 38 阅读 · 0 评论 -
区间合并(56,57)
56.合并区间class Solution { public int[][] merge(int[][] intervals) { //先排序! Arrays.sort(intervals,new Comparator<int[]>(){//按intervals[i][0]从小到大排! public int compare(int[] interval1, int[] interval2){ ret原创 2021-08-20 15:15:19 · 42 阅读 · 0 评论 -
41-缺失的第一个正数
要求:时间O(n) 空间O(1)1.置换class Solution { public int firstMissingPositive(int[] nums) { //置换 将x换到x-1的位置上 [1,2,3,4,...] int n = nums.length; for(int i = 0;i < n;i++){ while(nums[i] > 0 && nums[i] < n+1 &原创 2021-08-17 15:33:46 · 44 阅读 · 0 评论 -
滑动窗口(3, 438,76)
滑动窗口(双指针)class Solution { public int lengthOfLongestSubstring(String s) { //滑动窗口(双指针!) int n = s.length(); Map<Character, Integer> map = new HashMap<>(); if(n == 0) return 0; int left = 0, ans = 1;原创 2021-08-13 12:37:46 · 46 阅读 · 0 评论 -
94-二叉树中序遍历、538-反向中序遍历、后序遍历(非递归!)
1.递归class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> ans = new ArrayList<>(); dfs(ans, root); return ans; } public void dfs(List<Integer> ans, TreeNode root){原创 2021-08-10 16:14:19 · 201 阅读 · 0 评论 -
组合总数(Ⅰ、Ⅱ、Ⅲ、Ⅳ)
组合总数Ⅰ组合总数Ⅱ组合总数Ⅲ回溯法class Solution { //回溯法 List<List<Integer>> ans = new ArrayList<>(); List<Integer> ls = new ArrayList<>(); public List<List<Integer>> combinationSum3(int k, int n) { f(1原创 2021-08-09 11:36:55 · 52 阅读 · 0 评论 -
137-只出现一次的数字Ⅱ
二进制!!class Solution { public int singleNumber(int[] nums) { //二进制yyds!! //对于每一位的二进制的1和0累加起来必然是3N或者3N+1,为3N代表目标值在这一位没贡献,3N+1代表目标值在这一位有贡献(=1),然后将所有有贡献的位|起来就是结果 int ans = 0; for(int i = 0;i < 32;i++){//把每个数拆成32位逐位分析原创 2021-08-06 20:37:57 · 47 阅读 · 0 评论 -
91-解码方法
动态规划class Solution { public int numDecodings(String s) { //动规 int n = s.length(); int[] f = new int[n]; f[0] = s.charAt(0) == '0' ? 0 : 1; for(int i = 1;i < n;i++){ if(s.charAt(i) == '0'){原创 2021-08-01 21:46:55 · 86 阅读 · 0 评论 -
238-除自身以外数组的乘积
左右乘积列表class Solution { public int[] productExceptSelf(int[] nums) { //O(1)空间,将L用output代替,R使用动态数组即可 int n = nums.length; int[] output = new int[n]; output[0] = 1; int R = 1; for(int i = 1;i < n;i++){原创 2021-07-28 11:15:43 · 38 阅读 · 0 评论 -
347-前k个高频元素
对字典按value排序class Solution { public int[] topKFrequent(int[] nums, int k) { //对map按value排序 Map<Integer, Integer> map = new HashMap<>(); int[] ans = new int[k]; for(int key : nums){ int val = map.g原创 2021-07-26 19:40:59 · 33 阅读 · 0 评论 -
309-买卖股票带冷冻期
动态规划class Solution { public int maxProfit(int[] prices) { int n = prices.length; int dp0 = 0;//dp[i][0]不持有股票且不处于冷冻状态时的收益 int dp1 = -prices[0];//dp[i][1]持有股票时的收益 int dp2 = 0;//dp[i][2]表示处于冷冻状态,即当天卖出时的收益 for(int i原创 2021-07-23 09:33:26 · 38 阅读 · 0 评论 -
337-打家劫舍3
自己的解法不会。。。递归(超时)递归思想(不要深入递归函数体,只需知道递归函数的功能,以及找到跳出递归的边界条件)思路: 能盗取的最高金额为 抢劫该节点+抢劫该节点的左孩子的左右子树+抢劫该节点的右孩子的左右子树 与 抢劫该节点的左子树+抢劫该节点的右子树的和 的最大值class Solution { public int rob(TreeNode root) { //递归! if(root == null) re原创 2021-07-20 09:25:19 · 34 阅读 · 0 评论 -
322-零钱兑换
自己的解法不会动态规划class Solution { public int coinChange(int[] coins, int amount) { //动规!! int max = amount + 1, n = coins.length; int[] f = new int[max]; Arrays.fill(f, max);//令f全为max f[0] = 0; for(int i = 1原创 2021-07-14 10:54:01 · 41 阅读 · 0 评论