力扣第 189 场周赛

5412. 在既定时间做作业的学生人数

class Solution {
    public int busyStudent(int[] startTime, int[] endTime, int queryTime) {
        int res = 0;
        for(int i = 0;i<endTime.length;i++){
            if(startTime[i]<=queryTime && endTime[i]>=queryTime){
                res++;
            }
        }
        return res;
    }
}

5413. 重新排列句子中的单词

class Solution {
    public String arrangeWords(String text) {
        String[] str = text.toLowerCase().split(" ");
        Arrays.sort(str, (a, b) -> a.length() - b.length());
        int i = 0;
        String res = "";
        for(String s:str){
            if(i==0){
                res = s.substring(0,1).toUpperCase() + s.substring(1);
                i++;
                continue;
            }
            res += " " + s;
        }
        return res;
    }
}

5414. 收藏清单

class Solution {
    public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
        List<Integer> res = new ArrayList<>();
        List<Set<String>> list = new ArrayList<>();
        //用hashset结构来重构原有的数组,便于后续查找
        for(List<String> ListStr:favoriteCompanies){
            Set<String> set = new HashSet<>();
            for(String s:ListStr){
                set.add(s);
            }
            list.add(set);
        }

        for(int i = 0;i<favoriteCompanies.size();i++){
            Set<String> set = list.get(i);
            boolean flag = true;
            for(int j = 0;j<favoriteCompanies.size();j++){
                //当其他的收藏长度比当前长度长时,就需要判断当前的是否为其他的子收藏
                if(list.get(j).size()>=set.size()&&i!=j){
                    if(list.get(j).containsAll(set)){
                        flag = false;
                        //一旦发现是其他的子收藏就跳出,不用再查找
                        break;
                    }
                }
            }
            if(flag) res.add(i);
        }
        return res;
    }
}

5415. 圆形靶内的最大飞镖数量

  • 大佬写得代码,没怎么看懂…
class Solution {
    public double get_distance(double x1,double x2,double y1,double y2){
        double s=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
        return s;
    }
    public int numPoints(int[][] points, int r) {
        if(points.length<2)return points.length;
        int res=1;
        double d,x,y,center_x,center_y,h,x1,x2,y1,y2;
        for(int i=0;i<points.length;i++){
            for(int j=0;j<points.length;j++){
                if(i==j)continue;
                x1=points[i][0];
                y1=points[i][1];
                x2=points[j][0];
                y2=points[j][1];
                d=Math.sqrt(get_distance(x1,x2,y1,y2));
                if(d>2*r)continue;
                else{
                    x=(x1+x2)/2;
                    y=(y1+y2)/2;
                    if(x1==x2){
                       center_x=x;
                       center_y=y;
                    }
                    else{
                        h=Math.sqrt(r*r-(d/2)*(d/2));
                        center_x=h*(y2-y1)/d+x;
                        center_y=-h*(x2-x1)/d+y;
                    }   
                }
                System.out.println(center_x+" "+center_y);
                int count=0;
                for(int k=0;k<points.length;k++){
                    if(get_distance(points[k][0],center_x,points[k][1],center_y)-r*r<=0.00001){
                        count++;    
                    }   
                }
                res=Math.max(count,res);
            }
        }
        return res;
    }
}

你知道的越多,你不知道的越多。

你可能感兴趣的:(数据结构与算法)