2021.03.06 LeetCode双周赛题目
(AC两题超时一题,心态不行第四题没做)
第一题:
示例1:
输入:x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
输出:2
解释:所有点中,[3,1],[2,4] 和 [4,4] 是有效点。有效点中,[2,4] 和 [4,4] 距离你当前位置的曼哈顿距离最小,都为 1 。[2,4] 的下标最小,所以返回 2 。
示例2:
输入:x = 3, y = 4, points = [[3,4]]
输出:0
提示:答案可以与你当前所在位置坐标相同。
示例3:
输入:x = 3, y = 4, points = [[2,3]]
输出:-1
解释:没有有效点。
提示:
1 <= points.length <= 104
points[i].length == 2
1 <= x, y, ai, bi <= 104
我是暴力A的,代码如下:
class Solution {
public int nearestValidPoint(int x, int y, int[][] points) {
int[] num = new int[points.length];
int j = 0;
int min = -1;
int ans = -1;
for(int i = 0 ; i < points.length ; i++) {
if(x == points[i][0] && y == points[i][1]) return i;
if(x == points[i][0] || y == points[i][1]) {
num[i] = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);
if(j == 0) {
min = num[i];
ans = i;
j++;
}
}
//System.out.println(min+" "+num[i]);
if(num[i] != 0 && num[i] < min) {
min = num[i];
ans = i;
}
}
return ans;
}
}
第二题:
占了个小便宜,依旧暴力A的,代码如下:
class Solution {
public boolean checkPowersOfThree(int n) {
if(n >= Math.pow(3,14)) n -= Math.pow(3,14);
if(n >= Math.pow(3,13)) n -= Math.pow(3,13);
if(n >= Math.pow(3,12)) n -= Math.pow(3,12);
if(n >= Math.pow(3,11)) n -= Math.pow(3,11);
if(n >= Math.pow(3,10)) n -= Math.pow(3,10);
if(n >= Math.pow(3,9)) n -= Math.pow(3,9);
if(n >= Math.pow(3,8)) n -= Math.pow(3,8);
if(n >= Math.pow(3,7)) n -= Math.pow(3,7);
if(n >= Math.pow(3,6)) n -= Math.pow(3,6);
if(n >= Math.pow(3,5)) n -= Math.pow(3,5);
if(n >= Math.pow(3,4)) n -= Math.pow(3,4);
if(n >= Math.pow(3,3)) n -= Math.pow(3,3);
if(n >= Math.pow(3,2)) n -= Math.pow(3,2);
if(n >= Math.pow(3,1)) n -= Math.pow(3,1);
if(n >= Math.pow(3,0)) n -= Math.pow(3,0);
if(n == 0) return true;
return false;
}
}
题目三:
这题也想暴力A,但是超时了,一时间没想到合适的方法,然后这么久了第一次做周赛心态不太好,下次好好调整,代码先贴进来,有时间了一定来改好:
class Solution {
public int beautySum(String s) {
int ans = 0;
for(int i = 0 ; i < s.length() ; i++) {
for(int j = i+2 ; j < s.length() ; j++) {
int[] zimu = new int[27];
int min = 0 , max = 0 , y = 0;
for(int x = i ; x <= j ; x++) {
if(s.charAt(x) == 'a') zimu[1]++;
if(s.charAt(x) == 'b') zimu[2]++;
if(s.charAt(x) == 'c') zimu[3]++;
if(s.charAt(x) == 'd') zimu[4]++;
if(s.charAt(x) == 'e') zimu[5]++;
if(s.charAt(x) == 'f') zimu[6]++;
if(s.charAt(x) == 'g') zimu[7]++;
if(s.charAt(x) == 'h') zimu[8]++;
if(s.charAt(x) == 'i') zimu[9]++;
if(s.charAt(x) == 'j') zimu[10]++;
if(s.charAt(x) == 'k') zimu[11]++;
if(s.charAt(x) == 'l') zimu[12]++;
if(s.charAt(x) == 'm') zimu[13]++;
if(s.charAt(x) == 'n') zimu[14]++;
if(s.charAt(x) == 'o') zimu[15]++;
if(s.charAt(x) == 'p') zimu[16]++;
if(s.charAt(x) == 'q') zimu[17]++;
if(s.charAt(x) == 'r') zimu[18]++;
if(s.charAt(x) == 's') zimu[19]++;
if(s.charAt(x) == 't') zimu[20]++;
if(s.charAt(x) == 'u') zimu[21]++;
if(s.charAt(x) == 'v') zimu[22]++;
if(s.charAt(x) == 'w') zimu[23]++;
if(s.charAt(x) == 'x') zimu[24]++;
if(s.charAt(x) == 'y') zimu[25]++;
if(s.charAt(x) == 'z') zimu[26]++;
}
for(int x = 1 ; x <= 26 ; x++) {
if(zimu[x] != 0) {
if(y == 0) {
max = zimu[x];
min = zimu[x];
y++;
}
if(zimu[x] < min) min = zimu[x];
if(zimu[x] > max) max = zimu[x];
}
}
//System.out.println(i+" "+j+" "+ans+" "+max+" "+min);
ans += (max - min);
}
}
return ans;
}
}
题目四(有时间了来补上):
输入:n = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3]
输出:[6,5]
解释:每个点对中,与至少一个点相连的边的数目如上图所示。
示例2:
输入:n = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5]
输出:[10,10,9,8,6]
提示:
2 <= n <= 2 * 104
1 <= edges.length <= 105
1 <= ui, vi <= n
ui != vi
1 <= queries.length <= 20
0 <= queries[j] < edges.length