avatar
文章
590
标签
104
分类
17

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

byu_rself

LCR.P30[O(1)时间插入、删除和获取随机元素]
发表于2023-08-11|更新于2023-08-11|LeetCodeLCR|哈希表•模拟
LCR.P30[O(1)时间插入、删除和获取随机元素] 方法一:哈希表+List模拟12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455class RandomizedSet { Map<Integer, Integer> map; List<Integer> list; Random random; /** * Initialize your data structure here. */ public RandomizedSet() { map = new HashMap<>(); list = new ArrayList<>(); random = new Random(); } /** * Inserts a value to the set. R ...
LCR.P29[循环有序列表的插入]
发表于2023-08-11|更新于2023-08-25|LeetCodeLCR|链表•模拟
LCR.P29[循环有序列表的插入] 方法一:模拟12345678910111213141516171819202122232425262728293031323334353637383940414243444546/*// Definition for a Node.class Node { public int val; public Node next; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, Node _next) { val = _val; next = _next; }};*/class Solution { public Node insert(Node head, int insertVal) { Node node = new Node(insertVal ...
LCR.P21[删除链表的倒数第N个结点]
发表于2023-08-11|更新于2023-08-11|LeetCodeLCR|链表•双指针•模拟
LCR.P21[删除链表的倒数第N个结点] 方法一:遍历1234567891011121314151617181920212223242526/** * 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 removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0, head), p = h ...
LC.P1572[矩阵对角线元素的和]
发表于2023-08-11|更新于2023-08-11|LeetCode|数组•模拟
LC.P1572[矩阵对角线元素的和] 方法一:模拟12345678910class Solution { public int diagonalSum(int[][] mat) { int n = mat.length, ans = 0; for (int i = 0, j = n - 1; i < n; ++i, --j) { ans += mat[i][i] + mat[i][j]; if (i == j) ans -= mat[i][i]; } return ans; }} 时间复杂度:$O(n)$ 空间复杂度:$O(1)$
LCR.P91[粉刷房子]
发表于2023-08-10|更新于2023-08-11|LeetCode|动态规划
LCR.P91[粉刷房子] 方法一:动态规划123456789101112131415class Solution { public int minCost(int[][] costs) { int red = 0, blue = 0, green = 0; for (int[] cost : costs) { int r = Math.min(blue, green) + cost[0]; int b = Math.min(red, green) + cost[1]; int g = Math.min(red, blue) + cost[2]; red = r; blue = b; green = g; } return Math.min(red, Math.min(blue, green)); }} 时间复杂度:$O(n ...
LC.P312[戳气球]
发表于2023-08-10|更新于2023-08-11|LeetCode|动态规划
LC.P312[戳气球] 方法一:动态规划123456789101112131415161718class Solution { public int maxCoins(int[] nums) { int n = nums.length; int[] arr = new int[n + 2]; arr[0] = arr[n + 1] = 1; for (int i = 1; i <= n; i++) arr[i] = nums[i - 1]; int[][] dp = new int[n + 2][n + 2]; for (int len = 3; len <= n + 2; ++len) { for (int l = 0; l + len - 1 <= n + 1; ++l) { int r = l + len - 1; for (int k = l + 1 ...
LC.P1289[下降路径最小和II]
发表于2023-08-10|更新于2023-08-11|LeetCode|动态规划•数组
LC.P1289[下降路径最小和II] 方法一:动态规划1234567891011121314151617181920212223242526class Solution { public int minFallingPathSum(int[][] grid) { int n = grid.length; int[][] f = new int[n][n]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { f[i][j] = Integer.MAX_VALUE; } } System.arraycopy(grid[0], 0, f[0], 0, n); for (int i = 1; i < n; ++i) { for (int j = 0; j ...
Offer.P20[表示数值的字符串]
发表于2023-08-09|更新于2023-08-09|LeetCode剑指Offer|字符串•模拟
Offer.P20[表示数值的字符串] 方法一:模拟12345678910111213141516171819202122232425262728class Solution { public boolean isNumber(String s) { boolean isNum = false, isDot = false, iseOrE = false; char[] cs = s.trim().toCharArray(); for (int i = 0; i < cs.length; ++i) { if (cs[i] >= '0' && cs[i] <= '9') { isNum = true; } else if (cs[i] == '.') { // 只能有一个小数点 || 小 ...
LC.P1281[整数的各位积和之差]
发表于2023-08-09|更新于2023-08-09|LeetCode|模拟
LC.P1281[整数的各位积和之差] 方法一:模拟123456789101112class Solution { public int subtractProductAndSum(int n) { int prod = 1, sum = 0; while (n > 0) { int x = n % 10; n /= 10; prod *= x; sum += x; } return prod - sum; }} 时间复杂度:$O(logn)$ 空间复杂度:$O(1)$
LC.P75[颜色分类]
发表于2023-08-08|更新于2023-08-08|LeetCode|数组•哈希表•双指针
LC.P75[颜色分类] 方法一:哈希表12345678910111213class Solution { public void sortColors(int[] nums) { Map<Integer, Integer> map = new HashMap<>(); for (int num : nums) map.merge(num, 1, Integer::sum); int i = 0; for (int key : map.keySet()) { int cnt = map.get(key); while (cnt-- > 0) { nums[i++] = key; } } }} 时间复杂度:$O(n)$ 空间复杂度:$O(n)$ 方法二:双指针12345678910111213141 ...
1…252627…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!
搜索
数据库加载中