
找重复,所以我用了hashmap,切割字符串后,遍历两个字符串,然后再hashmap中存入他们所出现的次数,最后遍历一遍key找出次数为1的。
代码:class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
HashMap hash=new HashMap();
String[] ss1=s1.split(" ");
String[] ss2=s2.split(" ");
List ans = new ArrayList();
for(int i=0;i keys=hash.keySet();
for(String s:keys){
if(hash.get(s)==1){
ans.add(s);
}
}
return ans.toArray(new String[0]);
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)