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: e
Explanation: ‘e’ is the letter that was added.
給定兩個字符串s和t只由小寫字母組成。
字符串t由隨機字符串s生成,然后在隨機位置添加一個字母。
找到在t中添加的字母。
注意:兩個字符串,且中間有一個字符不同,這讓我立刻想到了剛剛看過的http://blog.csdn.net/zzlcsdn2017/article/details/59697212異或的妙用。 只要讓兩個字符串加起來,然后依次異或就可以得到不同的那個字母。
我的解法在循環方面做的不夠簡練,在網上找到了這個
class Solution { public: char findTheDifference(string s, string t) { s += t; int ch =0; for(auto val: s) ch ^= val; return ch; } };原文:http://blog.csdn.net/QQ508618087/article/details/52352635,他還有hash表解法,但是這個我不會,我要學學再來更新
新聞熱點
疑難解答