lcp349

既不是最小值也不是最大值

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int findNonMinOrMax(int[] nums) {
if (nums.length <= 2)
return -1;
Arrays.sort(nums);
int min = nums[0], max = nums[nums.length - 1];
for (int i = 1; i < nums.length - 1; ++i) {
if (nums[i] != min && nums[i] != max)
return nums[i];
}
return -1;
}
}

执行子串操作后的字典序最小字符串

方法一:

至少要操作一次

也就是说如果s = “aaaa”,操作一次后是“aaaz”

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 String smallestString(String s) {
StringBuilder sb = new StringBuilder();
int edit = 0, n = s.length();
for (int i = 0; i < n; ++i) {
char ch = s.charAt(i);
if (ch == 'a') {
if (edit == 0) {
if (i == n - 1)
sb.append('z');
else
sb.append('a');
}
else { // edit > 0
sb.append(s.substring(i));
break;
}
}
else {
sb.append((char) (ch - 1));
++edit;
}
}
return sb.toString();
}
}

收集巧克力

最大和查询


lcp349
https://leopol1d.github.io/2023/07/06/lcp349/
作者
Leopold
发布于
2023年7月6日
许可协议