您的位置:首页 > Web前端

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:

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;

}
};

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: