栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

日志-2022.1.30

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

日志-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的各种类库好久没用了,此处就参考了官方的题解,还在回顾中。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/719675.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号