题目来源【Leetcode】
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:
eExplanation:
‘e’ is the letter that was added.
这道题就是找出插入的那个字母。我是算出两个字符串的每个字母的和再相减,所得的数再转化为字母输出
class Solution {
public:
char findTheDifference(string s, string t) {
int n = 0;
int m = 0;
for(int i = 0; i < s.length() ; i ++){
n += s[i];
}
for(int i = 0; i < t.length() ; i ++){
m += t[i];
}
cout<<n<<" "<<m<<endl;
return (char)(m-n);
}
};
本文介绍了一种通过计算两个字符串中字母的ASCII值之和来找出被添加到第二个字符串中的额外字母的方法。此方法适用于由小写字母组成的字符串,且第二个字符串是由第一个字符串随机打乱并添加一个字母得到。

205

被折叠的 条评论
为什么被折叠?



