avatar
文章
590
标签
104
分类
17

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

byu_rself

LC.P15[三数之和]
发表于2023-07-09|更新于2023-08-02|LeetCode|数组•双指针•排序
LC.P15[三数之和] 方法一:排序+双指针1234567891011121314151617181920212223242526class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); int n = nums.length; Arrays.sort(nums); for (int i = 0; i < n - 2; ++i) { if (nums[i] > 0) break; if (i > 0 && nums[i] == nums[i - 1]) continue; int j = i + 1, k = n - 1; while (j < ...
LC.P1244[最大相等频率]
发表于2023-07-08|更新于2023-07-10|LeetCode|数组•模拟
LC.P1244[最大相等频率] 方法一:模拟123456789101112131415161718class Solution { int[] cnt = new int[100010], sum = new int[100010]; public int maxEqualFreq(int[] nums) { int n = nums.length, max = 0, ans = 0; for (int i = 0; i < n; ++i) { int x = nums[i], cur = ++cnt[x], len = i + 1; sum[cur]++; sum[cur - 1]--; max = Math.max(max, cur); if (max == 1) ans = len; if (max * sum[max] + 1 == len) ans = len; ...
LC.P167[两数之和II-输入有序数组]
发表于2023-07-08|更新于2023-07-10|LeetCode|数组•双指针
LC.P167[两数之和II-输入有序数组] 方法一:双指针123456789101112class Solution { public int[] twoSum(int[] numbers, int target) { int l = 0, r = numbers.length - 1; while (l < r) { int sum = numbers[l] + numbers[r]; if (sum < target) ++l; else if (sum > target) --r; else return new int[]{l + 1, r + 1}; } return new int[]{}; }} 时间复杂度:$O(n)$ 空间复杂度:$O(1)$
LC.P526[优美的排列]
发表于2023-07-07|更新于2023-07-07|LeetCode|DFS•动态规划•记忆化搜索•回溯•状态压缩
LC.P526[优美的排列] 方法一:回溯123456789101112131415161718192021222324class Solution { int n; boolean[] visited; public int countArrangement(int n) { this.n = n; visited = new boolean[n + 1]; return dfs(1); } private int dfs(int i) { if (i > n) return 1; int ans = 0; for (int x = 1; x <= n; ++x) { if (!visited[x] && (x % i == 0 || i % x == 0)) { visited[x] = true; ...
LC.P2532[过桥的时间]
发表于2023-07-07|更新于2023-07-07|LeetCode|模拟•堆•优先队列•小根堆•大根堆
LC.P2532[过桥的时间] 方法一:堆+模拟1234567891011121314151617181920212223242526272829303132333435363738394041424344454647class Solution { public int findCrossingTime(int n, int k, int[][] time) { Arrays.sort(time, (a, b) -> a[0] + a[2] - b[0] - b[2]); PriorityQueue<int[]> workL = new PriorityQueue<>((a, b) -> a[1] - b[1]); PriorityQueue<int[]> workR = new PriorityQueue<>((a, b) -> a[1] - b[1]); PriorityQueue<int[]> waitL = new P ...
LC.P224[基本计算器]
发表于2023-07-06|更新于2023-07-22|LeetCode|栈•递归•模拟
LC.P224[基本计算器] 方法一:栈123456789101112131415161718192021222324252627282930313233343536373839404142class Solution { int i, n; char[] cs; public int calculate(String s) { cs = s.toCharArray(); n = cs.length; return cal(); } private int cal() { int num = 0; char sign = '+'; Deque<Integer> stack = new ArrayDeque<>(); for (; i < n; ++i) { char c = cs[i]; if (c == ' ...
LC.P227[基本计算器II]
发表于2023-07-06|更新于2023-07-06|LeetCode|栈•模拟
LC.P227[基本计算器II] 方法一:栈12345678910111213141516171819202122232425262728293031class Solution { public int calculate(String s) { char[] cs = s.toCharArray(); int n = cs.length, num = 0; Deque<Integer> stack = new ArrayDeque<>(); char sign = '+'; for (int i = 0; i < n; ++i) { char c = cs[i]; // 数字 if (c >= '0' && c <= '9') { num = num * 1 ...
LC.P2178[拆分成最多数目的正偶数之和]
发表于2023-07-06|更新于2023-07-06|LeetCode|贪心
LC.P2178[拆分成最多数目的正偶数之和] 方法一:贪心123456789101112class Solution { public List<Long> maximumEvenSplit(long finalSum) { List<Long> ans = new ArrayList<>(); if (finalSum % 2 == 1) return ans; for (long i = 2; i <= finalSum; i += 2) { ans.add(i); finalSum -= i; } ans.add(ans.remove(ans.size() - 1) + finalSum); return ans; }} 时间复杂度:$O(\sqrt {finalSum})$ 空间复杂度:$O(1)$
LC.P535[TinyURL的加密与解密]
发表于2023-07-05|更新于2023-07-05|LeetCode|哈希表•模拟
LC.P535[TinyURL的加密与解密] 方法一:哈希表+模拟123456789101112131415161718192021222324252627282930313233public class Codec { String s = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; int k = 6; Map<String, String> originToTinyMap = new HashMap<>(), tinyToOriginMap = new HashMap<>(); Random random = new Random(); String prefix = "https://byurself.github.io/"; // Encodes a URL to a shortened URL. public String encode(String longU ...
LC.P468[验证IP地址]
发表于2023-07-05|更新于2023-07-05|LeetCode|模拟
LC.P468[验证IP地址] 方法一:模拟123456789101112131415161718192021222324252627282930313233343536373839404142434445class Solution { public String validIPAddress(String queryIP) { if (queryIP.contains(".") && checkIPv4(queryIP)) return "IPv4"; else if (queryIP.contains(":") && checkIPv6(queryIP)) return "IPv6"; else return "Neither"; } private boolean checkIPv4(String ip) { char[] s = ip ...
1…333435…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!
搜索
数据库加载中