![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
力扣
个人力扣做题记录
BJERGSEN97ai
这个作者很懒,什么都没留下…
展开
-
<动态规划>完全背包(最大价值,恰好装满最大价值)
完全背包_牛客题霸_牛客网 (nowcoder.com)(10条消息) 01背包 完全背包是否恰好装满问题_Simple的博客-CSDN博客 public ArrayList<Integer> knapsack (int v, int n, ArrayList<ArrayList<Integer>> nums) { int[] dp = new int [v+1]; int[] dp1 = new int[v+..原创 2022-02-25 11:42:30 · 1552 阅读 · 0 评论 -
力扣404. 左叶子之和
404. 左叶子之和 - 力扣(LeetCode) (leetcode-cn.com)class Solution { int ans = 0; public int sumOfLeftLeaves(TreeNode root) { if(root==null)return 0; dfs(root); return ans; } public void dfs(TreeNode root){ if(r.原创 2022-01-04 12:43:03 · 327 阅读 · 0 评论 -
力扣1005. K 次取反后最大化的数组和(自定义排序)
1005. K 次取反后最大化的数组和自定义根据绝对值排序class Solution { public int largestSumAfterKNegations(int[] nums, int k) { List<Integer> arr = new ArrayList<>(); for(int num:nums){ arr.add(num); } Collection..原创 2021-12-21 13:58:04 · 79 阅读 · 0 评论 -
力扣347.前 K 个高频元素(优先队列)
347. 前 K 个高频元素本题的关键点是对map中的value进行排序。题目中涉及前几个,考虑优先队列优先队列要通过lamda表达式写清楚是大数在顶还是小数在顶逆序:PriorityQueue<Integer> queue = new PriorityQueue<>(((o1, o2) -> o2-o1));顺序:PriorityQueue<Map.Entry<Integer, Integer>> queue = ..原创 2021-12-17 15:54:09 · 386 阅读 · 0 评论 -
力扣239. 滑动窗口最大值(自定义排序队列)
239. 滑动窗口最大值//实现功能:peek()取得队列的最大值,比最大值先添加进来的删除,后添加进来的保留class Mydeque{ Deque<Integer> deque; public Mydeque() { deque = new LinkedList<>(); } public void add(int val){ while(!deque.isEmpty() && val&g.原创 2021-12-17 14:35:06 · 83 阅读 · 0 评论 -
力扣851.喧闹和富有(DFS)
851. 喧闹和富有class Solution { public int[] loudAndRich(int[][] richer, int[] quiet) { int n = quiet.length; List<Integer>[] g = new List[n]; for (int i = 0; i < n; ++i) { g[i] = new ArrayList<Inte...原创 2021-12-15 13:07:50 · 904 阅读 · 0 评论 -
力扣18.四数之和
18. 四数之和 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { if(nums.length<4)return new ArrayList<>(); List<List<Integer>> res = new ArrayL.原创 2021-12-14 13:58:02 · 643 阅读 · 0 评论 -
力扣19. 删除链表的倒数第 N 个结点
19. 删除链表的倒数第 N 个结点思路一定要清晰,就是想办法找到要删除节点的前一个节点方法就是双指针,具体细节需要自己codingclass Solution { public ListNode removeNthFromEnd(ListNode head, int n) { if(head==null)return head; ListNode dummy = new ListNode(0,head); ListNode ...原创 2021-11-25 14:02:10 · 238 阅读 · 0 评论 -
力扣786. 第 K 个最小的素数分数(自定义排序方式)?
786. 第 K 个最小的素数分数 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int[] kthSmallestPrimeFraction(int[] arr, int k) { int n = arr.length; ArrayList<int[]> score_lst = new ArrayList<>(); for (int i = 0; i .原创 2021-11-29 13:21:46 · 263 阅读 · 0 评论 -
力扣200岛屿数量(DFS/BFS)
200. 岛屿数量 - 力扣(LeetCode) (leetcode-cn.com)1.DFSDFS的思路是,首先遍历整个表,找到1结果就加一,之后通过DFS将找到的1连通区域内的所有1变为0,代码如下 public int numIslands(char[][] grid) { if(grid==null || grid.length==0){ return 0; } int rowlen = grid.le..原创 2021-12-07 14:07:48 · 1868 阅读 · 0 评论 -
※力扣242. 有效的字母异位词(数组)
对于类似26个字母问题,由于固定长度,所以可以用数组代替哈希表进行设计首先是hash表的处理方式class Solution { public boolean isAnagram(String s, String t) { Map<Character,Integer> map = new HashMap<>(); int lens = s.length(); int lent = t.length();原创 2021-11-29 14:00:30 · 107 阅读 · 0 评论 -
力扣206. 反转链表(我的错误)
206. 反转链表 - 力扣(LeetCode) (leetcode-cn.com)在弄懂原理后我写出了这样的代码class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; ListNode temp = null; while(cur!=null){ .原创 2021-11-24 17:25:17 · 284 阅读 · 0 评论 -
力扣318. 最大单词长度乘积(位运算标记单词)
318. 最大单词长度乘积 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int maxProduct(String[] words) { int length = words.length; int[] masks = new int[length]; for (int i = 0;i<length;i++){ for(int j = 0;..原创 2021-11-17 13:44:47 · 268 阅读 · 0 评论 -
力扣319. 灯泡开关
319. 灯泡开关 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int bulbSwitch(int n) { return (int)Math.sqrt(n); }}n轮n个灯泡中亮着的都是完全平方数原创 2021-11-15 12:34:30 · 202 阅读 · 0 评论 -
力扣268. 丢失的数字
268. 丢失的数字 - 力扣(LeetCode) (leetcode-cn.com)方法一,求和相减class Solution { public int missingNumber(int[] nums) { int n = nums.length; int sum = (1+n)*n/2; int sum2 = 0; for (int num:nums){ sum2+=num; .原创 2021-11-06 10:16:47 · 73 阅读 · 0 评论 -
力扣633. 平方数之和(双指针)
633. 平方数之和 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public boolean judgeSquareSum(int c) { int sq = (int)Math.sqrt(c); int min = 0; int max = sq; while(min<=max){ long res =(long)min*min+(long.原创 2021-11-17 13:39:30 · 355 阅读 · 0 评论 -
力扣136、260只出现一次的数字等(异或)
136. 只出现一次的数字 - 力扣(LeetCode) (leetcode-cn.com)260. 只出现一次的数字 III - 力扣(LeetCode) (leetcode-cn.com)题目可以推广到数字出现偶数次里面找1个或两个出现 奇数次的数字class Solution { public int singleNumber(int[] nums) { int eor = 0; for(int num:nums){ ..原创 2021-10-31 19:59:53 · 2683 阅读 · 0 评论 -
力扣46. 全排列和力扣47. 全排列 II(回溯)
46. 全排列 - 力扣(LeetCode) (leetcode-cn.com)47. 全排列 II - 力扣(LeetCode) (leetcode-cn.com)46题为回溯的经典模板,回溯问题可以理解为输出不同的排列方式,需要注意的是不同的回溯问题添加条件与选择条件不同,需要自己根据不同问题进行coding,这也是回溯问题的困难所在。class Solution { List<List<Integer>> res = new ArrayList...原创 2021-10-28 15:09:32 · 473 阅读 · 0 评论 -
力扣476. 数字的补数
476. 数字的补数 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int findComplement(int num) { int s = -1; for (int i = 30; i >= 0; i--) { if (((num >> i) & 1) != 0) { s = i; ..原创 2021-10-27 17:13:23 · 251 阅读 · 0 评论 -
力扣301. 删除无效的括号
301. 删除无效的括号 - 力扣(LeetCode) (leetcode-cn.com)class Solution { int n; int max; int len; Set<String> set = new HashSet<>(); public List<String> removeInvalidParentheses(String s) { n = s.length(); in.原创 2021-10-27 12:09:12 · 88 阅读 · 0 评论 -
力扣240. 搜索二维矩阵 II
240. 搜索二维矩阵 II - 力扣(LeetCode) (leetcode-cn.com)class Solution { public boolean searchMatrix(int[][] matrix, int target) { int m = matrix.length, n = matrix[0].length; int r = 0, c = n - 1; while (r < m && c &g..原创 2021-10-25 18:24:51 · 98 阅读 · 0 评论 -
力扣229. 求众数 II(hashmap/摩尔投票法)
229. 求众数 II - 力扣(LeetCode) (leetcode-cn.com)hashmapclass Solution { public List<Integer> majorityElement(int[] nums) { HashMap<Integer,Integer> hashmap = new HashMap<>(); int n = nums.length; int num = ..原创 2021-10-22 14:31:04 · 218 阅读 · 0 评论 -
力扣66. 加一
66. 加一 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int[] plusOne(int[] digits) { for (int i = digits.length - 1; i >= 0; i--) { digits[i]++; digits[i] = digits[i] % 10; if (digits[i] != 0).原创 2021-10-21 13:31:52 · 69 阅读 · 0 评论 -
力扣453. 最小操作次数使数组元素相等
453. 最小操作次数使数组元素相等 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int minMoves(int[] nums) { int n = nums.length; int res = 0; int min_nums = Integer.MAX_VALUE; for(int i:nums){ min_nums = Math..原创 2021-10-20 20:35:32 · 241 阅读 · 0 评论 -
力扣42. 接雨水(单调栈)
42. 接雨水 - 力扣(LeetCode) (leetcode-cn.com) public int trap(int[] height) { Deque<Integer> stack = new LinkedList<>(); int n = height.length; int ans = 0; for (int i = 0; i < n; i++) { while(!.原创 2021-10-11 15:03:50 · 1080 阅读 · 0 评论 -
力扣739. 每日温度
739. 每日温度 - 力扣(LeetCode) (leetcode-cn.com)暴力class Solution { public int[] dailyTemperatures(int[] temperatures) { int[] res = new int[temperatures.length]; for (int i = 0; i < temperatures.length; i++) { int len ..原创 2021-10-11 15:46:33 · 45 阅读 · 0 评论 -
力扣441. 排列硬币
441. 排列硬币 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int arrangeCoins(int n) { long l = 1, r = n; while (l < r) { long mid = l + r + 1 >> 1; if (mid * (mid + 1) / 2 <= n) l = mid;..原创 2021-10-10 21:07:16 · 55 阅读 · 0 评论 -
力扣352. 将数据流变为多个不相交区间
352. 将数据流变为多个不相交区间 - 力扣(LeetCode) (leetcode-cn.com)/*@可爱抱抱呀执行用时:310 ms, 在所有 Java 提交中击败了5.47%的用户内存消耗:43.9 MB, 在所有 Java 提交中击败了40.62%的用户2021年10月6日 23:12*/class SummaryRanges { List<Integer> list; public SummaryRanges() { list=.原创 2021-10-09 17:45:13 · 162 阅读 · 0 评论 -
力扣187. 重复的DNA序列
187. 重复的DNA序列 - 力扣(LeetCode) (leetcode-cn.com) public List<String> findRepeatedDnaSequences(String s) { List<String> res = new ArrayList<>(); Set<String> set = new HashSet<>(); int i = 0; .原创 2021-10-08 11:36:33 · 45 阅读 · 0 评论 -
力扣482. 密钥格式化
482. 密钥格式化 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public String licenseKeyFormatting(String s, int k) { StringBuilder ans = new StringBuilder(); int cnt = 0; for (int i = s.length() - 1; i >= 0; i--) { .原创 2021-10-05 21:01:32 · 1377 阅读 · 0 评论 -
力扣145. 二叉树的后序遍历(递归方式)
145. 二叉树的后序遍历 - 力扣(LeetCode) (leetcode-cn.com)class Solution { List<Integer> res = new ArrayList<>(); public List<Integer> postorderTraversal(TreeNode root) { lastorder(root); return res; } public vo..原创 2021-10-01 12:23:33 · 49 阅读 · 0 评论 -
力扣144. 二叉树的前序遍历(递归方式)
144. 二叉树的前序遍历 - 力扣(LeetCode) (leetcode-cn.com)class Solution { List<Integer> res = new ArrayList<>(); public List<Integer> preorderTraversal(TreeNode root) { inorder(root); return res; } publ..原创 2021-10-01 12:18:58 · 55 阅读 · 0 评论 -
力扣1436. 旅行终点站
class Solution { public String destCity(List<List<String>> paths) { Set<String> hashSet = new HashSet<>(); for (List<String> city :paths){ hashSet.add(city.get(0)); } for (List&.原创 2021-10-01 10:51:28 · 88 阅读 · 0 评论 -
力扣208.实现前缀树(重要)
public class Trie { private Node1 root; public Trie() { root = new Node1(); } public void insert(String word) { if(word==null)return; char[] chs = word.toCharArray(); Node1 node = root; node.pass++.原创 2021-09-30 10:38:32 · 782 阅读 · 0 评论 -
力扣223. 矩形面积
class Solution { boolean flag = false; public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { int area1 = (ax2-ax1)*(ay2-ay1); int area2 = (bx2-bx1)*(by2-by1); int[] weight = both(a.原创 2021-09-30 09:41:20 · 169 阅读 · 0 评论 -
力扣24. 两两交换链表中的节点
我的代码,维护一个长度为2的栈写的巨垃圾 public ListNode swapPairs(ListNode head) { if(head==null||head.next==null)return head; Stack<ListNode> stack = new Stack<>(); ListNode res=null; ListNode temp= null; while(hea原创 2021-09-29 16:20:18 · 52 阅读 · 0 评论 -
力扣128. 最长连续序列
128. 最长连续序列 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int longestConsecutive(int[] nums) { int n = nums.length; int length = 0; Set<Integer> hashset = new HashSet<>(); for (int i : nums)..原创 2021-09-29 11:10:07 · 123 阅读 · 0 评论 -
力扣517. 超级洗衣机
517. 超级洗衣机 - 力扣(LeetCode) (leetcode-cn.com)class Solution { public int findMinMoves(int[] machines) { int sum = 0; int avg = 0; int n = machines.length; for (int machine : machines) { sum += machine; ..原创 2021-09-29 10:37:14 · 2307 阅读 · 0 评论 -
力扣113. 路径总和 II(回溯)
113. 路径总和 II - 力扣(LeetCode) (leetcode-cn.com)整个树的遍历过程符合回溯的遍历节点过程,所以选择回溯作为框架代码如下class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> temp = new ArrayList<>(); public List<List<..原创 2021-09-28 22:45:41 · 76 阅读 · 0 评论 -
力扣112. 路径总和
112. 路径总和 - 力扣(LeetCode) (leetcode-cn.com)class Solution { boolean res; public boolean hasPathSum(TreeNode root, int targetSum) { if(root==null)return false; rootsum(root,targetSum); return res; } public void ro.原创 2021-09-28 14:44:33 · 52 阅读 · 0 评论