每日随机

简单

1732. 找到最高海拔

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int largestAltitude(int[] gain) {
int result = 0;
result = Math.max(result, gain[0]);
for (int i = 1; i < gain.length; i++) {
gain[i] += gain[i - 1];
result = Math.max(result, gain[i]);
}
return result;
}
}

2089. 找出数组排序后的目标下标

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
// 直接系统函数排序了
Arrays.sort(nums);
List<Integer> reuslt = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
reuslt.add(i);
}
}
return reuslt;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
// 按照题目给定的范围,范围很小
List<Integer> reuslt = new ArrayList<>();
int[] counts = new int[101];
for (int num : nums) {
counts[num]++;
}
int t = 0;
for (int i = 1; i <= 100; i++) {
if (counts[i] != 0) {
if (i == target) {
for (int j = 0; j < counts[i]; j++) {
reuslt.add(t);
t++;
}
break;
} else {
t += counts[i];
}

}
}
return reuslt;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
// 不要局限在排序思想,题目增加复杂度的,统计比target小的数量和等于target的数量就行
List<Integer> reuslt = new ArrayList<>();
int count1 = 0, count2 = 0;
for (int num : nums) {
if (num < target) {
count1++;
} else if (num == target) {
count2++;
}
}
if (count1 < nums.length) {
for (int i = 0; i < count2; i++) {
reuslt.add(count1++);
}
}
return reuslt;
}
}

2778. 特殊元素平方和

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int sumOfSquares(int[] nums) {
int sum = 0;
int n = nums.length;
for (int i = 0; i < nums.length; i++) {
// 题目说的是长度能被i,i从1开始的,但是在数组里面从0下标开始的
if (n % (i + 1) == 0) {
sum += nums[i] * nums[i];
}
}
return sum;
}
}

中等

2958. 最多 K 个重复元素的最长子数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public int maxSubarrayLength(int[] nums, int k) {
// 双指针+哈希
HashMap<Integer, Integer> map = new HashMap<>();
int l = 0, r = 0, result = 0;
while (r < nums.length) {
// 超过k就移动l直到满足条件,不超过k就没问题,可以直接算长度更新结果,因为r继续往后也是超的,只能移动l
int t = map.getOrDefault(nums[r], 0) + 1;
if (t > k) {
while (l <= r) {
if (nums[l] == nums[r]) {
l++;
break;
} else {
map.put(nums[l], map.get(nums[l]) - 1);
l++;
}
}
// 一开始超过k的,就多一个,这里出来一定变成k的
map.put(nums[r], k);
} else {
map.put(nums[r], t);
}
result = Math.max(r - l + 1, result);
r++;
}
return result;
}
}

3612. 用特殊操作处理字符串 I

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public String processStr(String s) {
char[] result = new char[0];
for (char c : s.toCharArray()) {
if (c == '*') {
result = del(result);
} else if (c == '#') {
result = merge(result, result);
} else if (c == '%') {
result = reverse(result);
} else {
result = merge(result, new char[] { c });
}
}
return new String(result);
}

public char[] del(char[] c1) {
if (c1.length == 0) {
return c1;
}
char[] result = new char[c1.length - 1];
for (int i = 0; i < result.length; i++) {
result[i] = c1[i];
}
return result;
}

public char[] merge(char[] c1, char[] c2) {
char[] result = new char[c1.length + c2.length];
int t = 0;
for (char c : c1) {
result[t++] = c;
}
for (char c : c2) {
result[t++] = c;
}
return result;
}

public char[] reverse(char[] c) {
int l = 0, r = c.length - 1;
while (l < r) {
char t = c[l];
c[l] = c[r];
c[r] = t;
l++;
r--;
}
return c;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 直接系统api
public class Solution {
public String processStr(String s) {
StringBuilder result = new StringBuilder();
for (char ch : s.toCharArray()) {
if (ch == '*') {
if (result.length() > 0)
result.deleteCharAt(result.length() - 1);
} else if (ch == '#') {
result.append(result.toString());
} else if (ch == '%') {
result.reverse();
} else {
result.append(ch);
}
}
return result.toString();
}
}