avatar
文章
590
标签
104
分类
17

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

byu_rself

LC.P447[回旋镖的数量]
发表于2024-01-08|更新于2024-01-09|LeetCode|数组•哈希表
LC.P447[回旋镖的数量] 方法一:哈希表+计数12345678910111213141516class Solution { public int numberOfBoomerangs(int[][] points) { int ans = 0; Map<Integer, Integer> cnt = new HashMap<>(); for (int[] p1 : points) { cnt.clear(); for (int[] p2 : points) { int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]); int c = cnt.getOrDefault(d, 0); ans += c * 2; ...
LC.P1944[队列中可以看到的人数]
发表于2024-01-05|更新于2024-01-05|LeetCode|栈•数组•单调栈
LC.P1944[队列中可以看到的人数] 方法一:单调栈123456789101112131415161718class Solution { public int[] canSeePersonsCount(int[] heights) { int n = heights.length; int[] ans = new int[n]; Deque<Integer> stack = new ArrayDeque<>(); for (int i = n - 1; i >= 0; --i) { while (!stack.isEmpty() && stack.peek() < heights[i]) { stack.pop(); ans[i]++; } if (!stack.isEmpty()) { ...
LC.P2397[被列覆盖的最多行数]
发表于2024-01-04|更新于2024-01-05|LeetCode|数组•二进制
LC.P2397[被列覆盖的最多行数] 方法一:二进制枚举12345678910111213141516171819202122232425class Solution { public int maximumRows(int[][] matrix, int numSelect) { int m = matrix.length, n = matrix[0].length; int[] rows = new int[m]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 1) { rows[i] |= 1 << j; } } } int ans = 0; ...
LC.P2487[从链表中移除节点]
发表于2024-01-03|更新于2024-01-03|LeetCode|栈•链表•递归•单调栈
LC.P2487[从链表中移除节点] 方法一:单调栈12345678910111213141516171819202122232425262728/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode removeNodes(ListNode head) { ListNode dummy = new ListNode(0, head); Deque< ...
LC.P576[出界的路径数]
发表于2023-12-29|更新于2023-12-29|LeetCode|DFS•记忆化搜索•数组
LC.P576[出界的路径数] 方法一:记忆化搜索123456789101112131415161718192021222324252627282930313233class Solution { int m, n; int[][][] cache; static int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; static final int MOD = (int) 1e9 + 7; public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) { this.m = m; this.n = n; cache = new int[m][n][maxMove + 1]; for (int i = 0; i < ...
LC.P2706[购买两块巧克力]
发表于2023-12-29|更新于2023-12-29|LeetCode|数组
LC.P2706[购买两块巧克力] 方法一:一次遍历123456789101112131415class Solution { public int buyChoco(int[] prices, int money) { int a = Integer.MAX_VALUE, b = Integer.MAX_VALUE; for (int x : prices) { if (x < a) { b = a; a = x; } else if (x < b) { b = x; } } int cost = a + b; return money < cost ? money : money - cost; }} 时间复杂度:$O(n)$ ...
LC.P816[模糊坐标]
发表于2023-12-28|更新于2023-12-29|LeetCode|字符串•枚举
LC.P816[模糊坐标] 方法一:枚举1234567891011121314151617181920212223242526272829class Solution { String s; public List<String> ambiguousCoordinates(String ss) { s = ss.substring(1, ss.length() - 1); int n = s.length(); List<String> ans = new ArrayList<>(); for (int i = 0; i < n - 1; ++i) { for (String x : search(0, i)) { for (String y : search(i + 1, n - 1)) { ans.add(String.format(& ...
LC.P1775[通过最少操作次数使数组的和相等]
发表于2023-12-28|更新于2023-12-29|LeetCode|数组•哈希表•贪心
LC.P1775[通过最少操作次数使数组的和相等] 方法一:贪心+计数12345678910111213141516171819202122232425262728class Solution { public int minOperations(int[] nums1, int[] nums2) { int m = nums1.length, n = nums2.length; if (6 * m < n || 6 * n < m) return -1; int d = 0; for (int x : nums2) d += x; for (int x : nums1) d -= x; // 交换,统一让 nums1 的数变大,nums2 的数变小 if (d < 0) { d = -d; int[] temp = nums1; nums1 = nums2; ...
LC.P1349[参加考试的最大学生数]
发表于2023-12-26|更新于2023-12-29|LeetCode|DFS•记忆化搜索•状态压缩•位运算
LC.P1349[参加考试的最大学生数] 方法一:记忆化搜索+状态压缩123456789101112131415161718192021222324252627282930313233343536373839class Solution { int[][] cache; public int maxStudents(char[][] seats) { int m = seats.length, n = seats[0].length; int[] a = new int[m]; // a[i] 是第 i 排可用椅子的下标集合 for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (seats[i][j] == '.') { a[i] |= 1 << j; ...
LC.P1447[最简分数]
发表于2023-12-25|更新于2023-12-25|LeetCode|数学
LC.P1447[最简分数] 方法一:数学123456789101112131415class Solution { public List<String> simplifiedFractions(int n) { List<String> ans = new ArrayList<>(); for (int i = 1; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { if (gcd(i, j) == 1) ans.add(i + "/" + j); } } return ans; } private int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); ...
1…456…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!
搜索
数据库加载中