avatar
文章
590
标签
104
分类
17

首页
归档
标签
分类
友链
日志
byu_rself
搜索
首页
归档
标签
分类
友链
日志

byu_rself

LC.P2003[每棵子树内缺失的最小基因值]
发表于2023-10-31|更新于2023-11-02|LeetCode|DFS•哈希表•树•二叉树
LC.P2003[每棵子树内缺失的最小基因值] 方法一:DFS12345678910111213141516171819202122232425262728293031323334353637383940class Solution { public int[] smallestMissingValueSubtree(int[] parents, int[] nums) { int n = parents.length, node = -1; int[] ans = new int[n]; Arrays.fill(ans, 1); for (int i = 0; i < n; ++i) { if (nums[i] == 1) { node = i; // 出发点 break; } } if (node == -1) return ...
LC.P871[最低加油次数]
发表于2023-10-27|更新于2023-10-27|LeetCode|数组•贪心•优先队列•大根堆
LC.P871[最低加油次数] 方法一:贪心+优先队列1234567891011121314151617181920class Solution { public int minRefuelStops(int target, int startFuel, int[][] stations) { int ans = 0, remain = startFuel, x = 0, n = stations.length, i = 0; PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a); while (x < target) { if (remain == 0) { if (!q.isEmpty()) { remain += q.poll(); ++ans; ...
LC.P1465[切割后面积最大的蛋糕]
发表于2023-10-27|更新于2023-10-27|LeetCode|贪心•排序
LC.P1465[切割后面积最大的蛋糕] 方法一:贪心+排序12345678910111213141516171819class Solution { private static final int MOD = (int) 1e9 + 7; public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) { Arrays.sort(horizontalCuts); Arrays.sort(verticalCuts); int m = horizontalCuts.length, n = verticalCuts.length; int x = Math.max(horizontalCuts[0], h - horizontalCuts[m - 1]); int y = Math.max(verticalCuts[0], w - verticalCuts[n - 1]); for ...
LC.P2520[统计能整除数字的位数]
发表于2023-10-26|更新于2023-10-26|LeetCode|模拟
LC.P2520[统计能整除数字的位数] 方法一:模拟1234567891011class Solution { public int countDigits(int num) { int x = num, ans = 0; while (x > 0) { int val = x % 10; if (num % val == 0) ++ans; x /= 10; } return ans; }} 时间复杂度:$O(lognum)$ 空间复杂度:$O(1)$
LC.P650[只有两个键的键盘]
发表于2023-10-25|更新于2023-10-25|LeetCode|数学
LC.P650[只有两个键的键盘] 方法一:数学123456789101112131415class Solution { public int minSteps(int n) { int ans = 0; for (int i = 2; i * i <= n; ++i) { while (n % i == 0) { n /= i; ans += i; } } if (n > 1) { ans += n; } return ans; }} 时间复杂度:$O(\sqrt{n})$ 空间复杂度:$O(1)$
LC.P2698[求一个整数的惩罚数]
发表于2023-10-25|更新于2023-10-25|LeetCode|DFS
LC.P2698[求一个整数的惩罚数] 方法一:枚举+DFS12345678910111213141516171819202122class Solution { public int punishmentNumber(int n) { int ans = 0; for (int i = 1; i <= n; ++i) { int x = i * i; if (check(x + "", 0, i)) ans += x; } return ans; } private boolean check(String s, int i, int target) { int m = s.length(); if (i >= m) return target == 0; int x = 0; for (int j = i; j < ...
LC.P965[单值二叉树]
发表于2023-10-24|更新于2023-10-25|LeetCode|DFS•树•二叉树
LC.P965[单值二叉树] 方法一:DFS1234567891011121314151617181920212223242526/** * 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 right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution { int val = -1; publi ...
LC.P1155[掷骰子等于目标和的方法数]
发表于2023-10-24|更新于2023-10-25|LeetCode|DFS•动态规划•记忆化搜索
LC.P1155[掷骰子等于目标和的方法数] 方法一:记忆化搜索123456789101112131415161718192021222324252627class Solution { private static final int MOD = (int) 1e9 + 7; int[][] cache; public int numRollsToTarget(int n, int k, int target) { if (target < n || target > n * k) return 0; cache = new int[n + 1][target - n + 1]; for (int[] c : cache) { Arrays.fill(c, -1); } return dfs(n, target - n, k); } private int dfs(int i, int j, int k ...
LC.P368[最大整除子集]
发表于2023-10-20|更新于2023-10-20|LeetCode|动态规划
LC.P368[最大整除子集] 方法一:动态规划123456789101112131415161718192021222324252627282930313233class Solution { public List<Integer> largestDivisibleSubset(int[] nums) { Arrays.sort(nums); int n = nums.length; int[] f = new int[n], g = new int[n]; for (int i = 0; i < n; ++i) { int len = 1, prev = i; for (int j = 0; j < i; ++j) { if (nums[i] % nums[j] == 0) { if (f[j] + 1 > len) { ...
LC.P2525[根据规则将箱子分类]
发表于2023-10-20|更新于2023-10-20|LeetCode|模拟
LC.P2525[根据规则将箱子分类] 方法一:模拟123456789101112class Solution { private static final String[] d = {"Neither", "Bulky", "Heavy", "Both"}; public String categorizeBox(int length, int width, int height, int mass) { long v = (long) length * width * height; int bulky = length >= 10000 || width >= 10000 || height >= 10000 || v >= 1000000000 ? 1 : 0; int heavy = mass >= 100 ? 1 : 0; int i = he ...
1…121314…59
avatar
byu_rself
努力努力!
文章
590
标签
104
分类
17
Follow Me
最新文章
LC.P416[分割等和子集]2025-04-07
LC.P2874[有序三元组中的最大值II]2025-04-02
LC.P3128[直角三角形]2024-08-02
LCP.P40[心算挑战]2024-08-01
LC.P3115[质数的最大距离]2024-07-02
分类
  • LeetCode546
    • LCP4
    • LCR13
    • 剑指Offer13
    • 面试题2
  • Linux3
  • 后端9
    • CompletableFuture1
标签
GitDFSGolang动态规划记忆化搜索字符串栈数学数组哈希表滑动窗口链表递归图BFS多源BFS双指针树子数组前缀和前缀树字典树Trie子序列区间DP递推模拟枚举字符串哈希二分查找贪心排序负二进制回溯二叉树状态压缩子串迭代随机化后缀和
归档
  • 四月 20252
  • 八月 20242
  • 七月 20241
  • 五月 20243
  • 四月 20242
  • 三月 202410
  • 二月 202410
  • 一月 202414
网站资讯
文章数目 :
590
已运行时间 :
本站总字数 :
289.3k
本站访客数 :
本站总访问量 :
最后更新时间 :
©2023 - 2025 By byu_rself
框架 Hexo|主题 Butterfly
Hi, welcome to my blog!
搜索
数据库加载中