题目链接: Leetcode Weekly Contest 257
1、1995. Count Special Quadruplets
难度:Easy
思路:
暴力。
代码:
class Solution {
public int countQuadruplets(int[] nums) {
int n=nums.length;
int res=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j+1;k<n;k++){
for(int l=k+1;l<n;l++){
if(nums[i]+nums[j]+nums[k]==nums[l]){
res++;
}
}
}
}
}
return res;
}
}
2、1996. The Number of Weak Characters in the Game
难度:Medium
思路:
排序,排序的方式比较巧妙。
class Solution {
public int numberOfWeakCharacters(int[][] nums) {
Arrays.sort(nums,(a,b)->a[0]!=b[0]?b[0]-a[0]:a[1]-b[1]);
int res=0,max=0;
for(int[] num:nums){
if(num[1]<max){
res++;
}
max=Math.max(max,num[1]);
}
return res;
}
}
3、1997. First Day Where You Have Been in All the Rooms
难度:Medium
思路:
动态规划
代码
class Solution {
public int firstDayBeenInAllRooms(int[] A) {
int n=A.length;
int mod=1000_000_007;
long[] dp=new long[n];//dp[i]表示第一次到达房间i的时间
//第一次到达房间i,前面的房间都访问了偶数次
for(int i=1;i<n;i++){
dp[i]=(2*dp[i-1]-dp[A[i-1]]+2+mod)%mod;
}
return (int)dp[n-1] ;
}
}
4、1998. GCD Sort of an Array
难度:Hard
思路
需要用到并查集,暂时不会。
代码
//