LeetCode第303场周赛(20220724)

6124. 第一个出现两次的字母

在这里插入图片描述
添加链接描述

解题思路:

使用一个set, 当某个字符添加失败时,说明之前已经添加过一次,该字符就是首次出现两次的字符

class Solution {
    public char repeatedCharacter(String s) {
        HashSet<Character> set=new HashSet<>();
        for(int i=0;i<s.length();i++){
            if(!set.add(s.charAt(i))){
                return s.charAt(i);
            }
        }
        return ' ';
    }
}

6125. 相等行列对

在这里插入图片描述
https://leetcode.cn/problems/equal-row-and-column-pairs/

解题思路:

先判断第0行与第0列、第1列、第2列…是否相同,如果与某一列相同,计数加一;然后再判断第1行与第0列、第1列、第2列…是否相同;
利用三重for循环,完成每一行与每一列的比较

class Solution {
    public int equalPairs(int[][] grid) {
        int m=grid.length,n=grid[0].length;
        int cnt=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                boolean same=true;
                for(int k=0;k<n&&same;k++){
                    if(grid[i][k]!=grid[k][j]){
                        same=false;
                    }
                }
                if(same){
                    cnt++;
                }
            }
        }
        return cnt;
    }
}

6126. 设计食物评分系统

在这里插入图片描述
https://leetcode.cn/problems/design-a-food-rating-system/

解题思路:

模拟题,使用3个hashmap建立映射,其中以烹饪方式为键的map的value是一个treeset类型,按照题目的规则进行排序

class FoodRatings {

    HashMap<String,String> food2Cuisine;//食物-->烹饪方式映射
    HashMap<String,Integer> food2Rate;//食物-->评分映射
    HashMap<String,TreeSet<Node>> cuisine2Node;//烹饪方式-->Node(食物名字,分数)映射
    public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
        food2Cuisine=new HashMap<>();
        food2Rate=new HashMap<>();
        cuisine2Node=new HashMap<>();
        for(int i=0;i<foods.length;i++){
            String name=foods[i];
            String cuisine=cuisines[i];
            int rate=ratings[i];
            food2Cuisine.put(name,cuisine);
            food2Rate.put(name,rate);
            //当前cuisine键为空 创建一个TreeSet 先按rating降序
            //rating相同时再按字典序排序
            if(!cuisine2Node.containsKey(cuisine)){
                cuisine2Node.put(cuisine,new TreeSet<>(
                    (a,b)->{
                        if(a.rating==b.rating){
                            return a.foodName.compareTo(b.foodName);
                        }
                        return b.rating-a.rating;
                    }
                )
                );
            }
            cuisine2Node.get(cuisine).add(new Node(name,rate));
            

        }
    }
    
    public void changeRating(String food, int newRating) {
         String cuisine=food2Cuisine.get(food);
         Node tmp=new Node(food,food2Rate.get(food));
         cuisine2Node.get(cuisine).remove(tmp);//删除原来的旧节点
         food2Rate.put(food,newRating);
         tmp.rating=newRating;
         cuisine2Node.get(cuisine).add(tmp);//加入新的节点
    }
    
    public String highestRated(String cuisine) {
        //取出cuisine对应的TreeSet中的第一个节点
        Node node=cuisine2Node.get(cuisine).first();
        return node.foodName;
    }   
}
class Node{
    String foodName;
    int rating;
    public Node(String foodName,int rating){
        this.foodName=foodName;
        this.rating=rating;
    }
}

6127. 优质数对的数目

在这里插入图片描述
在这里插入图片描述

解题思路:

num1 OR num2 和 num1 AND num2 的二进制表示中值为 1 的位数之和大于等于 k这句话实际上就等价于num1和num2二进制形式中的1的个数加起来大于等于k,因此可以统计二进制中1的个数(0-31)对应的不同数字有多少个,比如1个1的有3个,2个1的有4个,k=2, 则由这种组合形成的优质数对的个数为3*4=12个

class Solution {
    public long countExcellentPairs(int[] nums, int k) {
        int[] cnt=new int[32];
        HashSet<Integer> set=new HashSet<>();
        for(int num:nums){
            if(set.add(num)){
                cnt[Integer.bitCount(num)]++;//bitCount: 计算二进制中1的个数
            }
        }
        long ans=0L;
        for(int i=0;i<=31;i++){
            for(int j=0;j<=31;j++){
                if(i+j>=k){
                    ans+=(cnt[i]*cnt[j]);
                }
            }
        }
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodePanda@GPF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值