日志-2022.1.30

日志-2022.1.30,第1张

日志-2022.1.30

今天出去喝酒了,没怎么学习,进度算是拖了,看了一下今天的力扣每日一题(884. 两句话中的不常见单词),一道很简单的构造哈希表题,就不写题解了,稍微注意一下c++的利用stringstream的字符串分割。

代码 c++:
class Solution {
public:
    vector uncommonFromSentences(string s1, string s2) {
        unordered_map hash;
        stringstream ss;
        ss<> s){
            hash[move(s)]++;
        }
        vector res;
        for(auto&& [s, c]: hash) if(c == 1) res.emplace_back(move(s));
        return res;
    }
};
Java:
class Solution {
    public String[] uncommonFromSentences(String s1, String s2) {
        Map hash = new HashMap();
        insert(s1, hash);
        insert(s2, hash);
        List res = new ArrayList();
        for(Map.Entry temp : hash.entrySet()){
            if(temp.getValue() == 1){
                res.add(temp.getKey());
            }
        } 
        return res.toArray(new String[0]);
    }
    public void insert(String s, Map hash){
        String[] arr = s.split(" ");
        for(String temp : arr){
            hash.put(temp,hash.getOrDefault(temp, 0) + 1);
        }
    }
}

Java的各种类库好久没用了,此处就参考了官方的题解,还在回顾中。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5715990.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存