[LeetCode]389. Find the Difference
2017-03-19 22:35
316 查看
[LeetCode]389. Find the Difference
题目描述
思路
1 使用map,将第一个字符串中出现的字母存储+1,之后遍历第二个字符串,对于每个字符,若map中有,则对应-1,若无,则返回该字符2 位运算,异或, 相同的字符异或结果为0
代码
1.使用mapclass Solution { public: char findTheDifference(string s, string t) { unordered_map<char, int> m; for (char &p : s) { ++m[p]; } for (char &p : t) { if (m[p]) --m[p]; else return p; } } };
2 位运算
class Solution { public: char findTheDifference(string s, string t) { char r = 0; for(char &p : s) r ^= p; for(char &p : t) r ^= p; return r; } };
相关文章推荐
- LeetCode389. Find the Difference简单
- LeetCode—389. Find the Difference
- [leetcode]389. Find the Difference -- JavaScript 代码
- 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
- Leetcode 389 Find the Difference
- Leetcode 389. Find the Difference
- leetcode:bits:Find the Difference(389)
- [leetcode] 389. Find the Difference 解题报告
- 【leetcode】389. Find the Difference【E】
- 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 C++
- 【leetcode】389. Find the Difference
- leetcode之389. Find the Difference(C++解法)