389. Find the Difference 难度:easy
2016-10-13 20:24
423 查看
题目:
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
思路:
字符串t在字符串s的基础上多加了一个字符,要求我们找出那个字符。将s和t当作一个整体,除了一个字符其余字符都出现了两次,即可对s和t的每个字符进行异或操作,最后的结果就是单独的那个字符。
程序:
class Solution {
public:
char findTheDifference(string s, string t) {
char res = 0x00;
for(int i = 0;i < s.size();i++)
res ^= s[i];
for(int i = 0;i < t.size();i++)
res ^= t[i];
return res;
}
};
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input: s = "abcd" t = "abcde" Output: e Explanation: 'e' is the letter that was added.
思路:
字符串t在字符串s的基础上多加了一个字符,要求我们找出那个字符。将s和t当作一个整体,除了一个字符其余字符都出现了两次,即可对s和t的每个字符进行异或操作,最后的结果就是单独的那个字符。
程序:
class Solution {
public:
char findTheDifference(string s, string t) {
char res = 0x00;
for(int i = 0;i < s.size();i++)
res ^= s[i];
for(int i = 0;i < t.size();i++)
res ^= t[i];
return res;
}
};
相关文章推荐
- Easy 389题 Find the Difference
- leetcode389[easy]---Find the Difference
- 【LeetCode】(C#)389. Find the Difference(Easy)
- 【leetcode】【Easy】【389. Find the Difference】【string】【bit manipulation】
- 389. Find the Difference [easy] (Python)
- Leetcode 389. Find the Difference (Easy) (cpp)
- 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 oj 389. Find the Difference(Hash)
- 389. Find the Difference
- 389. Find the Difference-hash map/XOR
- leetcode 389. Find the Difference
- 389. Find the Difference
- LeetCode 389. Find the Difference (字符串比较)
- 389. Find the Difference
- 389. Find the Difference