• 1452. People Whose List of Favorite Companies Is Not a Subset of Another List


    Given the array favoriteCompanies where favoriteCompanies[i] is the list of favorites companies for the ith person (indexed from 0).

    Return the indices of people whose list of favorite companies is not a subset of any other list of favorites companies. You must return the indices in increasing order.

    Example 1:

    Input: favoriteCompanies = [["leetcode","google","facebook"],["google","microsoft"],["google","facebook"],["google"],["amazon"]]
    Output: [0,1,4] 
    Explanation: 
    Person with index=2 has favoriteCompanies[2]=["google","facebook"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] corresponding to the person with index 0. 
    Person with index=3 has favoriteCompanies[3]=["google"] which is a subset of favoriteCompanies[0]=["leetcode","google","facebook"] and favoriteCompanies[1]=["google","microsoft"]. 
    Other lists of favorite companies are not a subset of another list, therefore, the answer is [0,1,4].
    

    Example 2:

    Input: favoriteCompanies = [["leetcode","google","facebook"],["leetcode","amazon"],["facebook","google"]]
    Output: [0,1] 
    Explanation: In this case favoriteCompanies[2]=["facebook","google"] is a subset of favoriteCompanies[0]=["leetcode","google","facebook"], therefore, the answer is [0,1].
    

    Example 3:

    Input: favoriteCompanies = [["leetcode"],["google"],["facebook"],["amazon"]]
    Output: [0,1,2,3]
    

    Constraints:

    • 1 <= favoriteCompanies.length <= 100
    • 1 <= favoriteCompanies[i].length <= 500
    • 1 <= favoriteCompanies[i][j].length <= 20
    • All strings in favoriteCompanies[i] are distinct.
    • All lists of favorite companies are distinct, that is, If we sort alphabetically each list then favoriteCompanies[i] != favoriteCompanies[j].
    • All strings consist of lowercase English letters only.
    class Solution {
        public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
            Set<String>[] sets = new HashSet[favoriteCompanies.size()];
            for(int i = 0; i < sets.length; i++) sets[i] = new HashSet(favoriteCompanies.get(i));
            List<Integer> res = new ArrayList();
            
            for(int i = 0; i < sets.length; i++){
                boolean flag = true;
                for(int j = 0; j < sets.length; j++){
                    if(j != i && sets[i].size() < sets[j].size() && sets[j].containsAll(sets[i])){
                        flag = false; 
                        break;
                    }
                }
                if(flag == true) res.add(i);
            }
            return res;
        }
    }

    利用了set的containsAll method,检查setA是否包含了所有setB的元素。

    或者

    class Solution {
         public List<Integer> peopleIndexes(List<List<String>> favoriteCompanies) {
                Set<String>[] set = new Set[favoriteCompanies.size()];
                for (int i = 0; i < set.length; ++i)
                    set[i] = new HashSet<>(favoriteCompanies.get(i));
                List<Integer> res = new ArrayList<>();
                outer: 
                for (int i = 0; i < set.length; ++i) {
                    for (int j = 0; j < set.length; ++j)
                        if (i != j && set[j].containsAll(set[i]))
                            continue outer;
                    res.add(i);
                }
                return res;        
            }
    }

    outer关键词可以帮助跳出外循环,具体用法如下

    https://blog.csdn.net/qq_41668547/article/details/80527969?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

  • 相关阅读:
    My SqL 常用命令
    RGB颜色查询对照表
    Android 对话框(Dialog)大全
    【转】Android一些知识点汇总
    Google Maps API v2密钥申请
    蓝牙中文API文档
    IOS 电商类app 第一版架构 改进
    ios 开发小记 (四)
    ios 深度复制 copy & mutablecopy
    CCS绘制成的三角形箭头
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12908303.html
Copyright © 2020-2023  润新知