avatar
文章
590
标签
104
分类
17

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

byu_rself

LC.P481[神奇字符串]
发表于2023-07-31|更新于2023-07-31|LeetCode|字符串•模拟
LC.P481[神奇字符串] 方法一:模拟12345678910111213141516class Solution { public int magicalString(int n) { char[] s = new char[n + 2]; s[0] = 1; s[1] = s[2] = 2; char c = 2; for (int i = 2, j = 3; j < n; ++i) { c ^= 3; // 1^3=2, 2^3=1 s[j++] = c; if (s[i] == 2) s[j++] = c; } int ans = 0; for (int i = 0; i < n; ++i) ans += 2 - s[i]; return ans; }} 时间复杂度:$O(n)$ 空间复杂度:$O(n ...
LC.P382[链表随机节点]
发表于2023-07-31|更新于2023-07-31|LeetCode|链表•模拟•蓄水池抽样
LC.P382[链表随机节点] 方法一:模拟12345678910111213141516171819202122232425262728293031323334/** * 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 { List<Integer> list; Random random; public Solution(ListNode head) { list = new A ...
LC.P1503[所有蚂蚁掉下来前的最后一刻]
发表于2023-07-31|更新于2023-07-31|LeetCode|模拟
LC.P1503[所有蚂蚁掉下来前的最后一刻] 方法一:模拟12345678class Solution { public int getLastMoment(int n, int[] left, int[] right) { int ans = 0; for (int x : left) ans = Math.max(ans, x); for (int x : right) ans = Math.max(ans, n - x); return ans; }} 时间复杂度:$O(n)$ 空间复杂度:$O(1)$
LC.P143[重排链表]
发表于2023-07-31|更新于2023-07-31|LeetCode|链表•双指针
LC.P143[重排链表] 方法一:线性表+双指针1234567891011121314151617181920212223242526272829/** * 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 void reorderList(ListNode head) { List<ListNode> list = new ArrayList<>(); ...
LC.P142[环形链表II]
发表于2023-07-30|更新于2023-07-31|LeetCode|哈希表•链表•双指针
LC.P142[环形链表II] 方法一:哈希表12345678910111213141516171819202122/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode detectCycle(ListNode head) { Set<ListNode> set = new HashSet<>(); ListNode p = head; while (p != null) { if (!set.add(p)) return p; p ...
LC.P1894[找到需要补充粉笔的学生编号]
发表于2023-07-29|更新于2023-07-31|LeetCode|模拟
LC.P1894[找到需要补充粉笔的学生编号] 方法一:模拟12345678910111213class Solution { public int chalkReplacer(int[] chalk, int k) { int n = chalk.length; long sum = 0; for (int c : chalk) sum += c; k = (int) (k % sum); for (int i = 0; i < n; ++i) { k -= chalk[i]; if (k < 0) return i; } return -1; }} 时间复杂度:$O(n)$ 空间复杂度:$O(1)$
LC.P141[环形链表]
发表于2023-07-29|更新于2023-07-31|LeetCode|链表•双指针
LC.P141[环形链表] 方法一:快慢指针12345678910111213141516171819202122/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle(ListNode head) { ListNode fast = head, slow = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; ...
LC.P335[路径交叉]
发表于2023-07-28|更新于2023-07-28|LeetCode|数学
LC.P335[路径交叉] 方法一:找规律1234567891011121314class Solution { public boolean isSelfCrossing(int[] distance) { int n = distance.length; if (n < 4) return false; for (int i = 3; i < n; ++i) { if (distance[i] >= distance[i - 2] && distance[i - 1] <= distance[i - 3]) return true; if (i >= 4 && distance[i - 1] == distance[i - 3] && distance[i] + distance[i - 4] >= distance[i - 2]) return t ...
LC.P1494[并行课程II]
发表于2023-07-28|更新于2023-07-28|DFS•记忆化搜索•状态压缩•集合•位运算
LC.P1494[并行课程II] 方法一:记忆化搜索+状态压缩123456789101112131415161718192021222324252627282930313233343536373839404142class Solution { int[] pre1, cache; int k, u; public int minNumberOfSemesters(int n, int[][] relations, int k) { this.k = k; pre1 = new int[n]; for (int[] e : relations) { // 先修课程集合 pre1[e[1] - 1] |= 1 << (e[0] - 1); } u = (1 << n) - 1; // 全集 cache = new int[1 << n]; Arra ...
LC.P2050[并行课程III]
发表于2023-07-28|更新于2023-07-28|LeetCode|动态规划•图•BFS•拓扑排序
LC.P2050[并行课程III] 方法一:拓扑排序+动态规划1234567891011121314151617181920212223242526272829303132333435class Solution { public int minimumTime(int n, int[][] relations, int[] time) { List<Integer>[] g = new List[n]; Arrays.setAll(g, k -> new ArrayList<>()); int[] in = new int[n]; for (int[] e : relations) { int a = e[0] - 1, b = e[1] - 1; g[a].add(b); ++in[b]; } Deque<Integer> queue = new ...
1…282930…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!
搜索
数据库加载中