题目链接: Leetcode Weekly Contest 237
写在前面:
本次周赛难度不大,做出了前两道,后两道道没有想出来,第三道大概的思路有,但是没有写出来,第四道爱猜才会赢,考了一个位运算的数学性质。
1、1832. Check if the Sentence Is Pangram
难度:Easy
题目大意:
字符串中只含有小写字母,判断字符串是否含有所有的小写字母。
思路:
统计每个字符的数量,然后判断即可。
代码
class Solution {
public boolean checkIfPangram(String sentence) {
int[] count=new int[26];
for(int i=0;i<sentence.length();i++){
count[sentence.charAt(i)-'a']++;
}
for(int i=0;i<26;i++){
if(count[i]==0){
return false;
}
}
return true;
}
}
2、1833. Maximum Ice Cream Bars
难度:Medium
题目大意:
给出每件商品的价格和手头拥有的钱,判断最多能买几件商品。
思路:
贪心,优先买便宜的商品。
代码
class Solution {
public int maxIceCream(int[] costs, int coins) {
Arrays.sort(costs);
int sum=0;
int count=0;
for(int i=0;i<costs.length;i++){
if(sum+costs[i]>coins){
break;
}
else{
sum+=costs[i];
count++;
}
}
return count;
}
}
3、1834. Single-Threaded CPU
难度:Medium
题目大意:
详见题目。
思路:
参考高赞回答,用优先队列来求解,优先队列的比较规则写好之后是不能改的,增加额外的条件来限制入队的task。
代码
class Solution {
public int[] getOrder(int[][] tasks) {
Map<int[],Integer> map=new HashMap<>();
int index=0;
int n=tasks.length;
long time=0;
for(int[] task:tasks){//存储每个task的下标
map.put(task,index++);
}
Arrays.sort(tasks,(a,b)->a[0]-b[0]);//按开始时间的先后排序
PriorityQueue<int[]> pq=new PriorityQueue<>((a,b)->a[1]==b[1]?map.get(a)-map.get(b):a[1]-b[1]);
int[] res=new int[n];
index=0;
int ti=0;
while(index<n){
while(ti<n&&tasks[ti][0]<=time){//将所有满足题意的task入队
pq.offer(tasks[ti]);
ti++;
}
if(pq.isEmpty()){//如果没有需要处理的task,则取时间最近的task。
time=tasks[ti][0];
continue;
}
int[] cur=pq.poll();
res[index++]=map.get(cur);
time+=cur[1];
}
return res;
}
}
4、1835. Find XOR Sum of All Pairs Bitwise AND
难度:Hard
题目大意:
将两个数组中的元素两两相与,得到的元素放入一个新的数组中,求新数组中所有元素的异或。
思路
参考高赞回答,知道这个性质的话很快就能写出来。
代码
class Solution {
public int getXORSum(int[] arr1, int[] arr2) {
int a=arr1[0];
for(int i=1;i<arr1.length;i++){
a=a^arr1[i];
}
int b=arr2[0];
for(int i=1;i<arr2.length;i++){
b=b^arr2[i];
}
return a&b;
}
}