lcp336

统计范围内的元音字符串数

方法一:HashSet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int vowelStrings(String[] words, int left, int right) {
int res = 0;
Set<Character> set = new HashSet<>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');
for (int i = left; i <= right; ++i) {
String str = words[i];
if (set.contains(str.charAt(0)) && set.contains(str.charAt(str.length() - 1)))
++res;
}
return res;
}
}

重排数组以得到最大前缀分数

方法一:贪心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int maxScore(int[] nums) {
int n = nums.length, res = 0;
Arrays.sort(nums);
long[] preSum = new long[n + 1];
for (int i = n - 1; i >= 0; --i) {
preSum[i] = preSum[i + 1] + nums[i];
if (preSum[i] > 0)
++res;
else
break;
}
return res;
}
}

统计美丽子数组数目

方法一:求异或和等于0 的子数组个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public long beautifulSubarrays(int[] nums) {
int n = nums.length;
long res = 0;
int[] s = new int[n + 1];
for (int i = 0; i < n; ++i)
s[i + 1] = s[i] ^ nums[i];
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : s) {
res += cnt.getOrDefault(x, 0);
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
}
return res;
}
}

完成所有任务的最少时间

方法一:

1


lcp336
https://leopol1d.github.io/2023/08/19/lcp336/
作者
Leopold
发布于
2023年8月19日
许可协议