avatar
文章
590
标签
104
分类
17

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

byu_rself

LC.P235[二叉搜索树的最近公共祖先]
发表于2023-08-25|更新于2023-09-06|LeetCode|DFS•树•二叉树
LC.P235[二叉搜索树的最近公共祖先] 方法一:DFS123456789101112131415161718/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { int x = root.val; if (p.val < x && q.val < x) return lowestCommonAncestor(root.left, p, q); if (p.val > x && ...
LC.P1448[统计二叉树中好节点的数目]
发表于2023-08-25|更新于2023-08-25|LeetCode|DFS•树•二叉树
LC.P1448[统计二叉树中好节点的数目] 方法一:DFS12345678910111213141516171819202122232425262728/** * 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 { public int ...
LC.P664[奇怪的打印机]
发表于2023-08-24|更新于2023-08-24|LeetCode|动态规划•字符串
LC.P664[奇怪的打印机] 方法一:动态规划123456789101112131415161718class Solution { public int strangePrinter(String s) { int n = s.length(); int[][] f = new int[n + 1][n + 1]; for (int len = 1; len <= n; ++len) { for (int l = 0; l + len - 1 < n; ++l) { int r = l + len - 1; f[l][r] = f[l + 1][r] + 1; for (int k = l + 1; k <= r; ++k) { if (s.charAt(l) == s.charAt(k)) { ...
LC.P1267[统计参与通信的服务器]
发表于2023-08-24|更新于2023-08-24|LeetCode|数组•模拟
LC.P1267[统计参与通信的服务器] 方法一:模拟1234567891011121314151617181920212223class Solution { public int countServers(int[][] grid) { int m = grid.length, n = grid[0].length; int[] cntRow = new int[m], cntCol = new int[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) { ++cntRow[i]; ++cntCol[j]; } } } i ...
LC.P375[猜数字大小II]
发表于2023-08-23|更新于2023-08-23|LeetCode|DFS•动态规划•记忆化搜索
LC.P375[猜数字大小II] 方法一:记忆化搜索1234567891011121314151617181920class Solution { int[][] cache; public int getMoneyAmount(int n) { cache = new int[n + 1][n + 1]; return dfs(1, n); } private int dfs(int l, int r) { if (l >= r) return 0; if (cache[l][r] != 0) return cache[l][r]; int ans = Integer.MAX_VALUE; for (int x = l; x <= r; ++x) { int cur = Math.max(dfs(l, x - 1), dfs(x + 1, r)) + x; ans ...
LC.P1782[统计点对的数目]
发表于2023-08-23|更新于2023-08-23|LeetCode|哈希表•双指针•排序
LC.P1782[统计点对的数目] 方法一:排序+双指针+哈希表123456789101112131415161718192021222324252627282930313233343536373839404142434445class Solution { public int[] countPairs(int n, int[][] edges, int[] queries) { // degree[i] 表示与点 i 相连的边的数目 int[] degree = new int[n + 1]; // 节点编号从 1 到 n Map<Integer, Integer> cntE = new HashMap<>(); for (int[] e : edges) { int x = e[0], y = e[1]; if (x > y) { // 交换 x 和 y,因为 1-2 和 2- ...
LC.P849[到最近的人的最大距离]
发表于2023-08-22|更新于2023-08-23|LeetCode|贪心
LC.P849[到最近的人的最大距离] 方法一:贪心1234567891011121314class Solution { public int maxDistToClosest(int[] seats) { int last = -1, d = 0, n = seats.length, ans = 1; for (int i = 0; i < n; ++i) { if (seats[i] == 1) { if (last == -1) ans = Math.max(ans, i); // 坐左边 else ans = Math.max(ans, (i - last) / 2); // 坐中间 last = i; } } ans = Math.max(ans, n - last - 1); // 坐右边 retur ...
LC.P672[灯泡开关II]
发表于2023-08-21|更新于2023-08-21|LeetCode|数学
LC.P672[灯泡开关II] 方法一:找规律123456789101112class Solution { public int flipLights(int n, int presses) { // 不按开关 if (presses == 0) return 1; // 1个灯泡 if (n == 1) return 2; // 2个灯泡 else if (n == 2) return presses == 1 ? 3 : 4; // n >= 3 else return presses == 1 ? 4 : presses == 2 ? 7 : 8; }} 时间复杂度:$O(1)$ 空间复杂度:$O(1)$
LC.P667[优美的排列II]
发表于2023-08-21|更新于2023-08-21|LeetCode|模拟
LC.P667[优美的排列II] 方法一:脑筋急转弯1234567891011121314151617class Solution { public int[] constructArray(int n, int k) { int[] ans = new int[n]; int index = 0; // 前面部分相差为1 for (int i = 1; i < n - k; ++i) { ans[index++] = i; } for (int i = n - k, j = n; i <= j; ++i, --j) { ans[index++] = i; if (i != j) { ans[index++] = j; } } return ans;  ...
LC.P2237[移动片段得到字符串]
发表于2023-08-21|更新于2023-08-21|LeetCode|字符串•双指针
LC.P2237[移动片段得到字符串] 方法一:双指针123456789101112class Solution { public boolean canChange(String start, String target) { int n = start.length(); for (int i = 0, j = 0; ; ++i, ++j) { while (i < n && start.charAt(i) == '_') ++i; while (j < n && target.charAt(j) == '_') ++j; if (i == n && j == n) return true; if (i == n || j == n || start.charAt(i) != target.charAt(j)) return fals ...
1…222324…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!
搜索
数据库加载中