lcp341

一最多的行

方法一:模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int[] rowAndMaximumOnes(int[][] mat) {
int m = mat.length, n = mat[0].length, max = 0;
int[] res = new int[2];
for (int i = 0; i < m; ++i) {
int count = 0;
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1)
++count;
}
if (count > max) {
res[0] = i;
res[1] = count;
max = count;
}
}
return res;
}
}

找出可整除性得分最大的整数

方法一:整除与被整除?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int maxDivScore(int[] nums, int[] divisors) {
int max = 0, val = divisors[0];
Set<Integer> set = new HashSet<>();
for (int i = 0; i < divisors.length; ++i) {
if (set.contains(divisors[i]))
continue;
set.add(divisors[i]);
int count = 0;
for (int j = 0; j < nums.length; ++j) {
int num = nums[j];
if (nums[j] % divisors[i] == 0)
++count;
}
if (count > max || (count == max && divisors[i] < val)) {
max = count;
val = divisors[i];
}
}
return val;
}
}

构造有效字符串的最少插入数

方法一:bullshit

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 int addMinimum(String word) {
if (word.equals("abc"))
return 0;
int res = 0, n = word.length();
int flag = 1;
for (int i = 0; i < n; ++i) {
char ch = word.charAt(i);
if (ch == 'a') {
if (i + 2 < n && word.charAt(i + 1) == 'b' && word.charAt(i + 2) == 'c')
i = i + 2;
else if (i + 2 >= n) {
if (i == n - 1)
res += 2;
else if (i == n - 2 && word.charAt(i + 1) != 'a') {
res += 1;
i = n - 1;
}
else if (i == n - 2 && word.charAt(i + 1) == 'a')
res += 2;
}
else if (word.charAt(i + 1) == 'a') {
res += 2;
}
else if (word.charAt(i + 1) != 'a') {
res += 1;
i = i + 1;
}
else if (word.charAt(i + 2) != 'c') {
res += 1;
i += 1;
}
} else if (ch == 'b') {
if (i + 1 < n) {
if (word.charAt(i + 1) == 'c') {
res += 1;
++i;
}
else {
res += 2;
}
}
else if (i == n - 1) {
res += 2;
}
}
else { // 'c'
res += 2;
}
}
return res;
}
}

最小化旅行的价格总和


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