題目: LeetCode - 389. Find the Difference

題目說明

給兩個字串 stts 加上一個字元組成,求該字元為何。

解題思路

由於字元本身為 ascii code,也是一種數字,所以使用 XOR 找出多出來的字元即可,詳細可參考本篇文章:LeetCode - 136 解題紀錄

參考解法

1
2
3
4
5
6
7
8
9
class Solution {
public:
char findTheDifference(string s, string t)
{
char ch{};
for(const auto& c : (s + t)) ch ^= c;
return ch;
}
};