avatar
文章
590
标签
104
分类
17

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

byu_rself

LC.P1006[笨阶乘]
发表于2023-12-25|更新于2023-12-25|LeetCode|栈
LC.P1006[笨阶乘] 方法一:栈12345678910111213141516171819202122232425class Solution { public int clumsy(int n) { Deque<Integer> stack = new ArrayDeque<>(); stack.push(n--); int index = 0; // * / + - while (n > 0) { if (index % 4 == 0) { stack.push(stack.pop() * n); } else if (index % 4 == 1) { stack.push(stack.pop() / n); } else if (index % 4 == 2) { ...
LC.P1276[不浪费原料的汉堡制作方案]
发表于2023-12-25|更新于2023-12-25|LeetCode|数学
LC.P1276[不浪费原料的汉堡制作方案] 方法一:数学求解二元一次方程即可 1234567891011class Solution { public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) { if (tomatoSlices % 2 != 0 || tomatoSlices < cheeseSlices * 2 || cheeseSlices * 4 < tomatoSlices) { return new ArrayList<>(); } List<Integer> ans = new ArrayList<>(); ans.add(tomatoSlices / 2 - cheeseSlices); ans.add(cheeseSlices * 2 - tomatoSlices / 2); ...
LC.P662[二叉树最大宽度]
发表于2023-12-21|更新于2023-12-21|LeetCode|DFS•BFS•树•二叉树
LC.P662[二叉树最大宽度] 方法一:BFS123456789101112131415161718192021222324252627282930313233343536373839/** * 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 { ...
LC.P2866[美丽塔II]
发表于2023-12-21|更新于2023-12-21|LeetCode|动态规划•数组•单调栈
LC.P2866[美丽塔II] 方法一:单调栈+动态规划123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354class Solution {public long maximumSumOfHeights(List<Integer> maxHeights) { int n = maxHeights.size(); Deque<Integer> stack = new ArrayDeque<>(); int[] left = new int[n], right = new int[n]; Arrays.fill(left, -1); Arrays.fill(right, n); for (int i = 0; i < n; ++i) { int x = maxHeight ...
LC.P357[统计各位数字都不同的数字个数]
发表于2023-12-20|更新于2023-12-21|LeetCode|动态规划•数学•数位DP
LC.P357[统计各位数字都不同的数字个数] 方法一:乘法原理123456789101112class Solution { public int countNumbersWithUniqueDigits(int n) { if (n == 0) return 1; int ans = 10; for (int i = 2, last = 9; i <= n; ++i) { int cur = last * (10 - i + 1); ans += cur; last = cur; } return ans; }} 时间复杂度:$O(n)$ 空间复杂度:$O(1)$ 方法二:数位DP12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849class ...
LC.P2828[判别首字母缩略词]
发表于2023-12-20|更新于2023-12-21|LeetCode|字符串•模拟
LC.P2828[判别首字母缩略词] 方法一:模拟12345678910111213class Solution { public boolean isAcronym(List<String> words, String s) { if (words.size() != s.length()) { return false; } for (int i = 0; i < s.length(); ++i) { if (words.get(i).charAt(0) != s.charAt(i)) { return false; } } return true; }} 时间复杂度:$O(n)$,$n$为$words$的长度 空间复杂度:$O(1)$
LC.P470[用Rand7()实现Rand10()]
发表于2023-12-19|更新于2023-12-19|LeetCode|数学
LC.P470[用Rand7()实现Rand10()] 方法一:数学12345678910111213/** * The rand7() API is already defined in the parent class SolBase. * public int rand7(); * @return a random integer in the range 1 to 7 */class Solution extends SolBase { public int rand10() { int first, second; while ((first = rand7()) > 6); // 拒绝7,对[1,6]采样,把奇数和偶数作为采样结果,每种概率为0.5 while ((second = rand7()) > 5); // 拒绝6,7,对[1,5]采样,有五种结果,每种概率为0.2 return (first & 1) == 1 ? second : 5 + second; ...
LC.P1901[寻找峰值II]
发表于2023-12-19|更新于2023-12-19|LeetCode|数组•二分查找
LC.P1901[寻找峰值II] 方法一:二分查找1234567891011121314151617181920212223class Solution { public int[] findPeakGrid(int[][] mat) { int m = mat.length; int left = 0, right = m - 1; while (left < right) { int i = left + right >> 1; int j = maxPos(mat[i]); if (mat[i][j] > mat[i + 1][j]) right = i; else left = i + 1; } return new int[]{left, maxPos(mat[left])}; } private int ma ...
LC.P319[灯泡开关]
发表于2023-12-18|更新于2023-12-19|LeetCode|数学
LC.P319[灯泡开关] 方法一:数学12345class Solution { public int bulbSwitch(int n) { return (int) Math.sqrt(n); }} 时间复杂度:$O(1)$ 空间复杂度:$O(1)$
LC.P172[阶乘后的零]
发表于2023-12-15|更新于2023-12-15|LeetCode|数学
LC.P172[阶乘后的零] 方法一:数学12345678910class Solution { public int trailingZeroes(int n) { int ans = 0; while (n > 0) { ans += n / 5; n = n / 5; } return ans; }} 时间复杂度:$O(logn)$ 空间复杂度:$O(1)$
1…567…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!
搜索
数据库加载中