LeetCode刷题实战389:找不同
You are given two strings s and t.
String t is generated by random shuffling string s and then add one more letter at a random position.
Return the letter that was added to t.
示例
示例 1:
输入:s = "abcd", t = "abcde"
输出:"e"
解释:'e' 是那个被添加的字母。
示例 2:
输入:s = "", t = "y"
输出:"y"
示例 3:
输入:s = "a", t = "aa"
输出:"a"
示例 4:
输入:s = "ae", t = "aea"
输出:"a"
解题
class Solution {
public:
char findTheDifference(string s, string t) {
sort(s.begin(),s.end());
sort(t.begin(),t.end());
int i=0;
while(s[i]==t[i]){
i++;
}
return t[i];
}
};
LeetCode刷题实战381:O(1) 时间插入、删除和获取随机元素
评论