avatar
文章
590
标签
104
分类
17

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

byu_rself

LC.P2670[找出不同元素数目差数组]
发表于2024-01-31|更新于2024-01-31|LeetCode|数组•哈希表
LC.P2670[找出不同元素数目差数组] 方法一:暴力123456789101112131415161718192021222324class Solution { public int[] distinctDifferenceArray(int[] nums) { Set<Integer> set = new HashSet<>(); int n = nums.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { if (!set.contains(nums[j])) { set.add(nums[j]); ans[i]++; } ...
LC.P2859[计算K置位下标对应元素的和]
发表于2024-01-25|更新于2024-01-25|LeetCode|数组•位运算
LC.P2859[计算K置位下标对应元素的和] 方法一:模拟(调包)1234567891011121314151617181920class Solution { public int sumIndicesWithKSetBits(List<Integer> nums, int k) { int ans = 0; for (int i = 0; i < nums.size(); ++i) { if (bicCount(i) == k) { ans += nums.get(i); } } return ans; } private int bicCount(int x) { int cnt = 0; while (x > 0) { x &= x - 1; ...
LC.P2865[美丽塔I]
发表于2024-01-24|更新于2024-01-24|LeetCode|数组•枚举
LC.P2865[美丽塔I] 方法一:枚举123456789101112131415161718192021class Solution { public long maximumSumOfHeights(List<Integer> maxHeights) { long ans = 0; int n = maxHeights.size(); for (int i = 0; i < n; ++i) { int height = maxHeights.get(i); long t = height; for (int j = i - 1; j >= 0; --j) { height = Math.min(height, maxHeights.get(j)); t += height; } height ...
LC.P2765[最长交替子数组]
发表于2024-01-23|更新于2024-01-23|LeetCode|数组
LC.P2765[最长交替子数组] 方法一:枚举123456789101112131415class Solution { public int alternatingSubarray(int[] nums) { int ans = -1, n = nums.length; for (int i = 0; i < n; ++i) { int k = 1, j = i; for ( ; j + 1 < n && nums[j + 1] - nums[j] == k; ++j) { k *= -1; } if (j - i + 1 > 1) { ans = Math.max(ans, j - i + 1); } } return ans; &# ...
LC.P2171[拿出最少数目的魔法豆]
发表于2024-01-18|更新于2024-01-18|LeetCode|数学•数组
LC.P2171[拿出最少数目的魔法豆] 方法一:排序+枚举12345678910111213class Solution { public long minimumRemoval(int[] beans) { Arrays.sort(beans); long sum = 0; for (int x : beans) sum += x; long ans = sum; int n = beans.length; for (int i = 0; i < n; ++i) { ans = Math.min(ans, sum - (long) beans[i] * (n - i)); } return ans; }} 时间复杂度:$O(nlogn)$ 空间复杂度:$O(logn)$
LC.P2744[最大字符串配对数目]
发表于2024-01-17|更新于2024-01-17|LeetCode|字符串•哈希表
LC.P2744[最大字符串配对数目] 方法一:哈希表123456789101112131415class Solution { public int maximumNumberOfStringPairs(String[] words) { int ans = 0; boolean[][] visited = new boolean[26][26]; for (String word : words) { int x = word.charAt(0) - 'a', y = word.charAt(1) - 'a'; if (visited[y][x]){ ++ans; } else { visited[x][y] = true; } } retur ...
LC.P2085[统计出现过一次的公共字符串]
发表于2024-01-12|更新于2024-01-17|LeetCode|字符串•哈希表
LC.P2085[统计出现过一次的公共字符串] 方法一:哈希表1234567891011121314class Solution { public int countWords(String[] words1, String[] words2) { Map<String, Integer> map1 = new HashMap<>(), map2 = new HashMap<>(); for (String s : words1) map1.merge(s, 1, Integer::sum); for (String s : words2) map2.merge(s, 1, Integer::sum); int ans = 0; for (Map.Entry<String, Integer> e : map1.entrySet()) { if (e.getValue() == 1 && map2. ...
LC.P2645[构造有效字符串的最少插入数]
发表于2024-01-11|更新于2024-01-11|LeetCode|字符串•双指针•贪心
LC.P2645[构造有效字符串的最少插入数] 方法一:贪心+双指针1234567891011121314151617class Solution { public int addMinimum(String word) { String s = "abc"; int ans = 0, n = word.length(); for (int i = 0, j = 0; j < n; i = (i + 1) % 3) { if (word.charAt(j) != s.charAt(i)) { ++ans; } else { ++j; } } if (word.charAt(n - 1) != 'c') { ans += word.c ...
LC.P2696[删除子串后的字符串最小长度]
发表于2024-01-10|更新于2024-01-11|LeetCode|字符串•栈
LC.P2696[删除子串后的字符串最小长度] 方法一:栈123456789101112131415class Solution { public int minLength(String s) { Deque<Character> stack = new ArrayDeque<>(); stack.push(' '); for (int i = 0; i < s.length(); ++i) { char c = s.charAt(i); if ((c == 'B' && stack.peek() == 'A') || (c == 'D' && stack.peek() == 'C')) { stack.pop(); } else & ...
LC.P2707[字符串中的额外字符]
发表于2024-01-09|更新于2024-01-09|LeetCode|动态规划•字符串•哈希表•前缀树•字典树•Trie
LC.P2707[字符串中的额外字符] 方法一:哈希表+动态规划1234567891011121314151617181920class Solution { public int minExtraChar(String s, String[] dictionary) { Set<String> set = new HashSet<>(); for (String w : dictionary) { set.add(w); } int n = s.length(); int[] f = new int[n + 1]; f[0] = 0; for (int i = 1; i <= n; ++i) { f[i] = f[i - 1] + 1; for (int j = 0; j < i; ++j) { ...
1…345…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!
搜索
数据库加载中