标签:for val spl nbsp style arraylist java hash表 hashmap
用Hash表存放String的个数,先遍历一遍全部的String,记录个数
再遍历一遍Hash表找到个数是1的就是唯一的单词
class Solution { public String[] uncommonFromSentences(String A, String B) { List<String> list = new ArrayList<>(); String[] split = A.split(" "); String[] split2 =B.split(" "); HashMap<String,Integer> map = new HashMap<>(); for(String s:split) { map.put(s, map.getOrDefault(s, 0)+1); } for(String s2:split2) { map.put(s2,map.getOrDefault(s2, 0)+1); } for(Map.Entry<String, Integer> entry :map.entrySet()) { if(entry.getValue() == 1) { list.add(entry.getKey()); } } return list.toArray(new String[list.size()]); } }
标签:for val spl nbsp style arraylist java hash表 hashmap
原文地址:https://www.cnblogs.com/cocobear9/p/12945130.html