avatar
文章
590
标签
104
分类
17

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

byu_rself

Offer.P32-I[从上到下打印二叉树]
发表于2023-06-07|更新于2023-06-07|LeetCode剑指Offer|BFS•树•二叉树•层序遍历
Offer.P32-I[从上到下打印二叉树] 方法一:层序遍历123456789101112131415161718192021222324252627/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int[] levelOrder(TreeNode root) { List<Integer> list = new ArrayList<>(); Deque<TreeNode> queue = new ArrayDeque<>(); if (root != null) queue.addLast(root); ...
LC.P2611[老鼠和奶酪]
发表于2023-06-07|更新于2023-06-07|LeetCode|数组•贪心•排序
LC.P2611[老鼠和奶酪] 方法一:贪心+排序1234567891011121314class Solution { public int miceAndCheese(int[] reward1, int[] reward2, int k) { int ans = 0, n = reward1.length; for (int i = 0; i < n; ++i) { ans += reward2[i]; reward1[i] -= reward2[i]; } Arrays.sort(reward1); for (int i = 0; i < k; ++i) { ans += reward1[n - i - 1]; } return ans; }} 时间复杂度:$O(nlogn)$ 空间复杂度:$O(n)$
Offer.P35[复杂链表的复制]
发表于2023-06-06|更新于2023-06-06|LeetCode剑指Offer|哈希表•链表
Offer.P35[复杂链表的复制] 方法一:哈希表1234567891011121314151617181920212223242526272829303132/*// Definition for a Node.class Node { int val; Node next; Node random; public Node(int val) { this.val = val; this.next = null; this.random = null; }}*/class Solution { public Node copyRandomList(Node head) { if (head == null) return null; Node p = head; Map<Node, Node> map = new HashMap<>(); while (p != nu ...
Offer.P24[反转链表]
发表于2023-06-06|更新于2023-06-06|LeetCode剑指Offer|链表•递归•双指针•模拟
Offer.P24[反转链表] 方法一:迭代(双指针)1234567891011121314151617181920/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null, cur = head; while (cur != null) { ListNode temp = cur.next; cur.next = pre; pre = cur; cur = temp; } ...
Offer.P22[链表中倒数第k个节点]
发表于2023-06-06|更新于2023-06-06|LeetCode剑指Offer|链表•双指针•模拟
Offer.P22[链表中倒数第k个节点] 方法一:模拟1234567891011121314151617181920212223/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode getKthFromEnd(ListNode head, int k) { ListNode p = head; int n = 0; while (p != null) { ++n; p = p.next; } p = head; for (int i = 1; i <= n - k; ++i ...
LC.P2352[相等行列对]
发表于2023-06-06|更新于2023-06-06|LeetCode|数组•模拟
LC.P2352[相等行列对] 方法一:暴力123456789101112131415161718class Solution { public int equalPairs(int[][] grid) { int n = grid.length, ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (isEqual(i, j, n, grid)) ++ans; } } return ans; } private boolean isEqual(int row, int col, int n, int[][] grid) { for (int i = 0; i < n; ++i) { if (grid[row ...
LC.P472[连接词]
发表于2023-06-05|更新于2023-06-05|LeetCode|DFS•记忆化搜索•字符串•前缀树•字典树•Trie
LC.P472[连接词] 方法一:Trie+记忆化搜索123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657class Solution { Trie trie; public List<String> findAllConcatenatedWordsInADict(String[] words) { trie = new Trie(); List<String> ans = new ArrayList<>(); Arrays.sort(words, (a, b) -> a.length() - b.length()); for (String word : words) { int n = word.length(); if (n == 0) con ...
LC.P2460[对数组执行操作]
发表于2023-06-05|更新于2023-06-05|数组•模拟
LC.P2460[对数组执行操作] 方法一:模拟123456789101112131415161718192021class Solution { public int[] applyOperations(int[] nums) { int n = nums.length; for (int i = 0; i < n - 1; ++i) { if (nums[i] == nums[i + 1]) { nums[i] *= 2; nums[i + 1] = 0; } } int k = 0; for (int i = 0; i < n; ++i) { if (nums[i] != 0) { nums[k++] = nums[i]; } ...
LC.P583[两个字符串的删除操作]
发表于2023-06-04|更新于2023-06-04|LeetCode|动态规划•子序列
LC.P583[两个字符串的删除操作] 方法一:转化为最长公共子序列求将两个字符串删除任意字符后能相同的最短步数,可以等价于求两个字符串的最长公共子序列LCS,最后使得两字符串删除任意字符后能相同的最短步数$= n - lcs + m - lcs$ 123456789101112131415161718class Solution { public int minDistance(String word1, String word2) { int n = word1.length(), m = word2.length(); int[][] f = new int[n + 1][m + 1]; // 求最长公共子序列 (LC.P1143) for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (word1.charAt(i - 1) == ...
LC.P2465[不同的平均值数目]
发表于2023-06-04|更新于2023-06-04|LeetCode|数组•哈希表•排序
LC.P2465[不同的平均值数目] 方法一:排序+哈希表1234567891011class Solution { public int distinctAverages(int[] nums) { Arrays.sort(nums); int n = nums.length; Set<Integer> set = new HashSet<>(); for (int i = 0, j = n - 1; i < j; ++i, --j) { set.add(nums[i] + nums[j]); } return set.size(); }} 时间复杂度:$O(nlogn)$ 空间复杂度:$O(n)$
1…394041…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!
搜索
数据库加载中