您的位置:首页 > Web前端

LeetCode | 389. Find the Difference 水题

2018-01-07 15:54 453 查看
Giventwo strings s and t which
consist of only lowercase letters.

String t isgenerated by random shuffling string s andthen
add one more letter at a random position.

Findthe letter that was added in t.

Example:

Input:

s = "abcd"

t = "abcde"

 

Output:

e

 

Explanation:

'e' is the letter that was added.

水题,用一个数组存储a~z的数量,一个数组存储s,一个数组存储 t,然后比较t数组哪一位比s数组数量大一,然后输出大一的那位对应的字母

class Solution {
public:
char findTheDifference(string s, string t) {
int numS[26]={0}; int i;
int numT[26]={0};
for(char u : s) numS[u-'a']+=1;
for(char u : t)
{
i=u-'a';
numT[i]+=1;
if(numT[i]>numS[i]) return u;
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: