LeetCode 第 225 场周赛
第一题: 5661. 替换隐藏数字得到的最晚时间
模拟
AC Code
class Solution {
public String maximumTime(String time) {
char[] cs = time.toCharArray();
char[] map = new char[]{
'2','3', ':', '5','9'};
int len = cs.length;
for(int i = 0; i < len; i++) {
if(cs[i] == '?') {
if(i == 0) {
if(cs[i + 1] > '3' && cs[i + 1] != '?') cs[i] = '1';
else cs[i] = '2';
} else {
if(i == 1 && cs[i - 1] != '2') cs[i] = '9';
else cs[i] = map[i];
}
}
}
return new String(cs);
}
}
题型: 牛客编程巅峰赛S2第6场 - 青铜&白银&黄金 第二题
一下子确实没想到.
枚举 a 的最大字符,26个字母,枚举26次, a 种的字符全部小于这个最大字符, b 中的字符大于等于这个最大字符
同理 枚举 b 的最大字符, a、b 相同的字符
AC Code
class Solution {
public int minCharacters(String a, String b) {
int alen = a.length(), blen = b.length();
char[] csa = a.toCharArray(), csb = b.toCharArray();
int ans = Integer.MAX_VALUE;
for(int i = 0; i < 26; i++) {
char cc = (char)('a' + i);
int c = 0, d = 0, e = 0;
for(int j = 0; j < alen; j++) {
if (csa[j] < cc) c++;
if (csa[j] > cc) d++;
if (csa[j] != cc) e++;
}
for(int j = 0; j < blen; j++) {
if (csb[j] >= cc) c++;
if (csb[j] <= cc) d++;
if (csb[j] != cc) e++;
}
ans = Math.min(ans, c);
ans = Math.min(ans, d);
ans = Math.min(ans, e);
}
return ans;
}
}
第三题: 5663. 找出第 K 大的异或坐标值
记录前缀和
二维 -> 一维 pre 前缀和数组
思路:
初始化第一行
然后当前行当前列的异或结果,等于(上一行异或结果 ^ 当前行到该列的异或结果)
然后排序(从小到大), 输出 len - k 就是第 k 大的异或结果
AC Code
class Solution {
public int kthLargestValue(int[][] m, int k) {
int row = m.length, col = m[0].length;
int[] pre = new int[row * col];
// 初始化
pre[0] = m[0][0];
// 列
for(int i = 1; i < col; i++) pre[i] = pre[i - 1] ^ m[0][i];
for(int i = 1; i < row; i++) {
int tmp = 0;
for(int j = 0; j < col; j++) {
tmp ^= m[i][j];
pre[i * col + j] = pre[(i - 1) * col + j] ^ tmp;
}
}
Arrays.sort(pre);
// System.out.println(Arrays.toString(pre));
int idx = row * col - k;
return pre[idx];
}
}
第四题: 5664. 放置盒子
找规律
不会, 待补
AC Code