389. Find the Difference
2017-12-28 10:46
302 查看
char findTheDifference(string s, string t) { unordered_map<char,int> m; for(auto a:s) m[a]++; for(auto a:t) if(m[a]--<1) return a; return 0; }
利用异或,相同的抵消,结果为添加的字符
char findTheDifference(string s, string t) { char res=0; for(auto a:s) res^=a; for(auto a:t) res^=a; return res; }
也可以用加减,剩下的为加上的
char findTheDifference(string s, string t) { char res=0; for(auto a:s) res-=a; for(auto a:t) res+=a; return res; }
char findTheDifference(string s, string t) { return accumulate(begin(s),end(s+=t),0,bit_xor<int>()); }
c++中的accumulate
相关文章推荐
- 389. Find the Difference
- leetcode 389. Find the Difference
- 【leetcode】389. Find the Difference
- Leetcode 389 Find the Difference
- Leetcode 389. Find the Difference
- LeetCode 389. Find the Difference (字符串比较)
- 389. Find the Difference
- 389. Find the Difference。
- 389.Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- LeetCode | 389. Find the Difference 水题
- LeetCode-389. Find the Difference
- LeetCode 简单操作 | 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- [leetcode] 389. Find the Difference 解题报告
- 389. Find the Difference
- LeetCode 389. Find the Difference
- Leetcode 389 Find the Difference