您的位置:首页 > Web前端

Leetcode 389 Find the Difference

2017-09-24 18:11 375 查看

Leetcode 389 Find the Difference

#include <vector>
#include <string>
using namespace std;
class Solution {
public:
char findTheDifference(string s, string t) {
vector<int> lettercount(26,0);

for(int i = 0;i < s.length();i ++)
{
lettercount[s[i] - 'a'] ++;
}
for(int i = 0; i < t.length();i ++)
{
lettercount[t[i] - 'a'] --;
if(lettercount[t[i] - 'a'] < 0)
return t[i];
}

}
char findTheDifference1(string s, string t) {
char c = 0;
for(int i = 0;i < s.length();i ++)
{
c ^= s[i];
}
for(int i = 0; i < t.length();i ++)
{
c ^= t[i];
}
return c;

}
char findTheDifference2(string s, string t) //to learn
{
char c = 0;

for (auto ch : s) c ^= ch;
for (auto ch : t) c ^= ch;

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