389. Find the Difference 难度:easy

来源:互联网 发布:中国阶级 知乎 编辑:程序博客网 时间:2024/05/18 01:35

题目:

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:eExplanation:'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;            }};


0 0
原创粉丝点击