https://leetcode.com/problems/find-the-difference/discuss/
题目:求新增加的字母
解法一:直接统计
class Solution {
public:
char findTheDifference(string s, string t) {
int diff[26]={0};
for(int x=0;x<s.length();x++)
diff[s[x]-'a']++,diff[t[x]-'a']--;
diff[t[t.length()-1]-'a']--;
for(int x=0;x<26;x++)
if(diff[x])
return 'a'+x;
}
};
解法二: 异或
class Solution {
public:
char findTheDifference(string s, string t) {
int diff=0;
for(int x=0;x<s.length();x++)
diff^=(s[x]-'a'),diff^=(t[x]-'a');
diff^=(t[t.length()-1]-'a');
return 'a'+diff;
}
};
或
class Solution {
public:
char findTheDifference(string s, string t) {
char diff=0;
for(int x=0;x<s.length();x++)
diff^=s[x],diff^=t[x];
diff^=t[t.length()-1];
return diff;
}
};