java刷lc
天天向上的菜鸡杰!!
生活明朗 万物可爱 人间值得 全力以赴
展开
-
leetcode31. 下一个排列
【代码】leetcode31. 下一个排列。原创 2022-08-28 19:53:08 · 236 阅读 · 1 评论 -
leetcode22. 括号生成(回溯)
【代码】leetcode22. 括号生成(回溯)原创 2022-08-28 19:51:11 · 173 阅读 · 0 评论 -
leetcode11. 盛最多水的容器
【代码】leetcode11. 盛最多水的容器。原创 2022-08-28 17:36:32 · 448 阅读 · 0 评论 -
leetcode5. 最长回文子串
【代码】leetcode5. 最长回文子串。原创 2022-08-28 16:37:54 · 161 阅读 · 0 评论 -
leetcode647. 回文子串(动态规划)
【代码】leetcode647. 回文子串(动态规划)原创 2022-08-28 16:36:23 · 377 阅读 · 0 评论 -
leetcode hot100.2. 两数相加(详解)
这题很狗 笨办法做不出来 最后还是看了题解 rue了。原创 2022-08-27 20:45:36 · 418 阅读 · 0 评论 -
543. 二叉树的直径(详解)
【代码】543. 二叉树的直径(详解)原创 2022-08-25 18:18:08 · 363 阅读 · 0 评论 -
461. 汉明距离(详解)
【代码】461. 汉明距离(详解)原创 2022-08-25 18:15:40 · 361 阅读 · 0 评论 -
234. 回文链表(hot100)
【代码】234. 回文链表(hot100)原创 2022-08-24 17:20:34 · 201 阅读 · 0 评论 -
160. 相交链表(hot100)
【代码】160. 相交链表(hot100)原创 2022-08-24 17:04:39 · 199 阅读 · 0 评论 -
141. 环形链表(hot100)
【代码】141. 环形链表(hot100)原创 2022-08-23 18:25:10 · 126 阅读 · 0 评论 -
136. 只出现一次的数字(hot100)
【代码】136. 只出现一次的数字(hot100)原创 2022-08-23 17:05:53 · 167 阅读 · 0 评论 -
121. 买卖股票的最佳时机
【代码】121. 买卖股票的最佳时机。原创 2022-08-23 16:42:35 · 99 阅读 · 0 评论 -
3. 无重复字符的最长子串(滑动窗口详解版)
hxd们 俺菜鸡杰又回来了 3 2 1 奏曲…原创 2022-08-16 18:06:06 · 168 阅读 · 0 评论 -
leetcode周赛6075. 装满石头的背包的最大数量
一:题目二:上码class Solution {public: /** 思路:求差,将结果放到一个容器当中,然后的话,排序, */ int maximumBags(vector<int>& capacity, vector<int>& rocks, int additionalRocks) { vector<int&g原创 2022-05-22 16:46:18 · 236 阅读 · 1 评论 -
leetcode周赛6074. 字母在字符串中的百分比
这个简单题也比较狗 上午做的时候一直有个测试点不过 但是题目不给出什么测试点不过 原来是100这个答案 给跪了一:题目二:上码class Solution {public: int percentageLetter(string s, char letter) { if (s.size() == 0) return 0; int count = 0; for (int i = 0; i &原创 2022-05-22 16:44:06 · 927 阅读 · 0 评论 -
leetcode周赛6076. 表示一个折线图的最少线段数
气死爹了 用C++过不去 换成Java 过了一:题目二:上码class Solution { public int minimumLines(int[][] stockPrices) { Arrays.sort(stockPrices,(o1,o2)->o1[0]-o2[0]); int n = stockPrices.length; if (n == 1) return 0; int ans = 1;原创 2022-05-22 16:39:41 · 957 阅读 · 0 评论 -
leetcode45. 跳跃游戏 II(java详解)
一:题目二:上码class Solution { public int jump(int[] nums) { int ans = 0; int curIndex = 0;//当前统计出来的可以移动的最远距离的下标 int nextIndex = 0;//在到达 当前最远距离下标的这段距离内 我们统计出的可以达到的最远距离 //如果在统计的过程中 其覆盖范围已经大于数组下标了,那么原创 2022-05-20 18:53:44 · 537 阅读 · 0 评论 -
leetcode55. 跳跃游戏
一:题目二:上码class Solution { /** 最大覆盖范围是否可以可以覆盖终点 */ public boolean canJump(int[] nums) { int coverIndex = 0; for (int i = 0; i <= coverIndex; i++) { coverIndex = Math.max(i+nums[i],coverIndex原创 2022-05-20 17:15:08 · 237 阅读 · 0 评论 -
leetcode122. 买卖股票的最佳时机 II
一:题目二:上码class Solution { /** 思路:1.局部最优:我们买入当前股票等哪天遇见最大值的时候买出 赚最大利润 2.全局最优:局部最优推出全局最优 3.这个利润是可以被分解的 7 1 5 10 利润: -6 4 5 那么最大利润是4+5=9 其实就是1买入10卖出,但是我们可以原创 2022-05-20 16:39:09 · 159 阅读 · 0 评论 -
leetcode53. 最大子数组和
一:题目二:上码class Solution { /** 思路: 1.局部最优:两数之和大于0 2.整体最优:相加的和如果大于0的话,我们继续相加, */ public int maxSubArray(int[] nums) { int sum = Integer.MIN_VALUE; int count = 0; for (int i = 0; i < nums原创 2022-05-20 15:49:16 · 121 阅读 · 0 评论 -
leetcode376. 摆动序列
一:题目二:上码class Solution { /** 思路: 1.局部最优:我们删除单调坡度上的结点(不包括单调坡度两端的结点) 不包括两端的结点 整体最优:整个序列有最多的局部峰值,从而达到最长摆动序列。 */ public int wiggleMaxLength(int[] nums) { if (nums.length == 1) return 1; int curDiff =原创 2022-05-19 19:08:59 · 150 阅读 · 0 评论 -
leetcode455. 分发饼干
一:题目二:上码class Solution { public int findContentChildren(int[] g, int[] s) { int ans = 0; int gIndex = 0; int sIndex = 0; Arrays.sort(g); Arrays.sort(s); while (gIndex < g.length &&原创 2022-05-17 19:34:18 · 218 阅读 · 0 评论 -
leetcode51. N 皇后(java详解)
一:题目二:上码class Solution { /** 思路:1.先说我们选用的数据结构;我们是选取的是List<List<string>> ans 来存每次的结果; 我们在创建这个二维矩阵的时候用的是char的二维数组,那么的话等到我们得到一种 可行解的时候 将char的二维数组每一行转换成 String 并存入 ans 中 2:判断可行性原创 2022-05-17 17:50:11 · 286 阅读 · 0 评论 -
leetcode47. 全排列 II
一:题目二:上码class Solution { private List<List<Integer>> ans = new ArrayList<>(); private List<Integer> path = new ArrayList<>(); private boolean[] used; public void getAns(int[] nums,boolean[] used) {原创 2022-05-16 19:06:43 · 133 阅读 · 0 评论 -
leetcode46. 全排列
一:题目二:上码class Solution { /** 1.全排列问题,我们的横向都是从0开始不再是st了 因为我们用到前面已经使用过的元素 单是在纵向递归当中我们不能使用重复的元素,这里我们用used来去重。 */ private List<List<Integer>> ans = new ArrayList<>(); private List<Integer> path = new原创 2022-05-16 18:10:28 · 138 阅读 · 0 评论 -
leetcode491. 递增子序列(树层去重魔改版)
一:题目二:上码class Solution { /** 思路:1.这里已经指明了不允许有重复的,那么树层去重 + 树枝去重(以前用过的元素不再使用) 2.坑:这里不能用 传统的树层去重了 因为我们无法排序了 3.这里给出的解决办法是 是利用 hashmap,对每个数出现的频率进行统计 如果其频率大于1的话那就 continue 跳过该数 */ List<List<Integer>&g原创 2022-05-16 17:12:30 · 148 阅读 · 0 评论 -
leetcode90. 子集 II(树层去重)
一:题目二:上码class Solution { /** - 先排序 - 树层去重:那么直接在从横向循环中去重即可 */ List<List<Integer>> ans = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public void getAns(int[] nums,int st) {原创 2022-05-16 16:26:34 · 169 阅读 · 0 评论 -
leetcode78. 子集
一:题目二:上码class Solution { /** 横向递归的是:我们子集的长度 纵向递归的是:我们子集的中元素的更改 */ List<List<Integer>> ans = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public void getAns(int[] nums,原创 2022-05-16 16:02:35 · 158 阅读 · 0 评论 -
leetcode93. 复原 IP 地址(思路+详解)
一:题目二:上码class Solution { /** 思路: 1.跟分割字符串差不多,但是我们需要记录添加的'.'的个数 当其个数等于3的时候 我们就已经切割成4段了。 2.判断字符是否合法 1>:以0开头的数字不合法 2>:遇见非数字字符不合法 3>:大于255不合法 */ private L原创 2022-05-14 17:13:23 · 769 阅读 · 0 评论 -
leetcode131. 分割回文串
一:题目二:上码class Solution { /** 思路: 1.纵向递归的话 我们递归深度是 我们的 切割线切到了最后 2.横向for 我们的宽度就是字符串宽度 3.在哪截取的话 那就是在哪个坐标的后面 */ List<List<String>> ans = new ArrayList<>(); List<String> pa原创 2022-05-13 19:12:36 · 730 阅读 · 0 评论 -
leetcode40. 组合总和 II(树层去重)
一:题目二:上码class Solution { /** 注意这里的去重:1.我们分为树层去重 比如[1,1 2,5] target = 8,那么如果我们用树枝去重的话,那么就会出现[1,2,5],[1,2,5] 那么的话我们就需要树层去重 就是横向遇见相同的元素跳过。 2.树枝去重的原创 2022-05-13 17:47:57 · 113 阅读 · 0 评论 -
leetcode39. 组合总和
一:题目二:上码class Solution { private List<List<Integer>> ans = new ArrayList<>(); private List<Integer> path = new ArrayList<>(); public int getSum(List<Integer> list) { int sum = 0; for (Int原创 2022-05-13 16:38:28 · 167 阅读 · 0 评论 -
leetcode17. 电话号码的字母组合
一:题目二:上码class Solution { private List<String> ans = new ArrayList<>(); private List<Character> path = new ArrayList<>(); /** 递归的深度由我们给出的字符串中有几个字符决定的,宽度就是由我们字符对应的字符串 */ public String getStr(c原创 2022-05-12 18:55:44 · 144 阅读 · 0 评论 -
leetcode216. 组合总和 III
一:题目二:上码class Solution { /** 递归深度根据k值确定,宽度的话那就[1,9] 因为递归不允许重复,那么的话我们需要每次在横向遍历的起始位置+1 */ private List<List<Integer>> ans = new ArrayList<>(); private List<Integer> path = new ArrayList<>();原创 2022-05-12 18:19:32 · 100 阅读 · 0 评论 -
leetcode77. 组合
一:题目二:上码class Solution { /** 注意是组合 那么的话 [1,2] 和 [2,1] 是等价的。 */ private List<List<Integer>> ans = new ArrayList<>(); private List<Integer> path = new ArrayList<>(); public void getAns(int st, i原创 2022-05-12 18:02:38 · 86 阅读 · 0 评论 -
leetcode105. 从前序与中序遍历序列构造二叉树
一:题目二:上码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode原创 2022-05-12 17:05:21 · 115 阅读 · 0 评论 -
leetcode106. 从中序与后序遍历序列构造二叉树(java详解版)
一:题目二:上码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode原创 2022-05-12 16:53:15 · 397 阅读 · 0 评论 -
leetcode654. 最大二叉树
一:上码二:上码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNod原创 2022-05-11 19:03:51 · 120 阅读 · 0 评论 -
leetcode113. 路径总和 II
一:题目二:上码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode原创 2022-05-11 17:56:32 · 110 阅读 · 0 评论