class Solution {
public char findTheDifference(String s, String t) {
HashMap<Character,Integer> map=new HashMap<>();
for(char c:s.toCharArray()){
map.put(c,map.getOrDefault(c,0)+1);
}
for(char c:t.toCharArray()){
if(map.getOrDefault(c,0)==0)
return c;
else map.put(c,map.get(c)-1);
}
return 'a';//按照题目 第二个for循环里必定跳出,因此这里随便返回一个值
}
}
/* 评论中的优质方法:1、将所给的字符串转换为字符数组,求字符数组的int和,作差,再转回char,返回(3ms,击败100%)
2、用异或,最后剩下的字符就是结果*/
leetcode No.389 找字符串不同
最新推荐文章于 2023-12-30 21:05:04 发布