您的位置:首页 > Web前端

389. Find the Difference

2017-12-28 10:46 302 查看




char findTheDifference(string s, string t) {
unordered_map<char,int> m;
for(auto a:s) m[a]++;
for(auto a:t)
if(m[a]--<1)
return a;
return 0;
}


利用异或,相同的抵消,结果为添加的字符

char findTheDifference(string s, string t) {
char res=0;
for(auto a:s) res^=a;
for(auto a:t) res^=a;
return res;
}


也可以用加减,剩下的为加上的

char findTheDifference(string s, string t) {
char res=0;
for(auto a:s) res-=a;
for(auto a:t) res+=a;
return res;
}


char findTheDifference(string s, string t) {
return accumulate(begin(s),end(s+=t),0,bit_xor<int>());
}


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