【问题标题】:Compare maps and update one with intersecting keys with lowest value at key比较地图并用相交的键更新一个,键的值最低
【发布时间】:2020-10-28 21:27:29
【问题描述】:

我需要这个解决方案来解决 LeetCode 问题 1002:查找常用字符,但我没有找到合适的解决方案。

我需要的操作是用“a”和“b”之间的相交键更新映射“a”,并将键的值设置为两者中较低的值。

虽然这个用例很少,但有一个解决方案还是不错的。

class Solution {
    public List<String> commonChars(String[] A) {
        List<String> list = new ArrayList<>();
        Map<Character, Integer> a = new HashMap<>();
        Map<Character, Integer> b = new HashMap<>();
        List<Character> removeChars = new ArrayList<>();
        
        //base: populate map a so you can compare with next string in A[]
        
        for(char c : A[0].toCharArray()){
            int count = a.getOrDefault(c, 0);
            a.put(c, count+1);
        }
        
        /*
        compare each character in A[] to keys in map a
        
            if contains
                put character in map b and incremnt count
            else
                remove from map a
            
        update map a with whatever has smaller value for each key
        clear map b
            
        iterate
            
        put all keys in list for how many values it has
        
        */
        for(int i = 1; i < A.length; i++){
            for(char c : A[i].toCharArray()){
                if(a.containsKey(c)){
                    int count = b.getOrDefault(c, 0);
                    b.put(c, count+1);
                } else
                    a.remove(c);
            }


            /*
             Here I need to compare the keys to each map
                 - If they were present in both, take the lower value
                 - If not, then I needed to remove the key from map "a"
            */


            for(char c : removeChars){
                a.remove(c);
            }
            b.clear();
        }
        for(Map.Entry<Character, Integer> entry : a.entrySet()){
            char key = entry.getKey();
            int val = entry.getValue();
            for(int i = 0; i < val; i++){
                list.add(String.valueOf(key));
            }
        }
        return list;
        
    }
}

【问题讨论】:

    标签: java hashmap


    【解决方案1】:
    • 从地图 a 创建条目集
    • 遍历条目
    • 将键和值设置为变量以进行简单比较
      • 如果key之间有交集,用key的较低值覆盖map a
      • 否则将键添加到字符列表中

    您不能在循环期间从条目集中删除键,而是将要删除的键存储在列表中,并在循环发生后将其删除。

    Map<Character, Integer> a = new HashMap<>();
    Map<Character, Integer> b = new HashMap<>();
    List<Character> removeChars = new ArrayList<>();
    
    for(Map.Entry<Character, Integer> entry1 : a.entrySet()){
        char keyA = entry1.getKey();
        int valA = entry1.getValue();
        if(b.containsKey(keyA)){
            int valB = b.get(keyA);
            a.put(keyA, Math.min(valA, valB));
         }else
            removeChars.add(keyA);
    }
    

    一般用途

    Map<Key, Value> mapA = new HashMap<Key, Value>();
    Map<Key, Value> mapB = new HashMap<Key, Value>();
    List<T> remove = new ArrayList<T>();
    
    for(Map.Entry<Ket, value> entry : mapA.entrySet()){
        Key key = entry.getKey();
        Value valA = entry.getValue();
        
        if(mapB.containskey(key)){
            int valB = mapB.get(key);
            mapA.put(key, Math.min(valA, valB);
        } else
            remove.add(keyA);
    }
    

    【讨论】:

    • 但是如果你很邪恶,不喜欢你的初级同事,或者想迷惑没有经验的初学者,那么一句话解决整个事情:mapA.forEach((k,v) -&gt; mapA.put(k , Math.min(mapB.getOrDefault(k, v), v)));
    • @Eritrean 该死的你是对的,为了工作安全,总是首选可读性较低的解决方案。
    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多