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;
}
}
};
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;
}
}
};
相关文章推荐
- LeetCode -- 389. Find the Difference
- leetcode-389. Find the Difference 字典,查找某个元素a不在list中
- LeetCode之389. Find the Difference
- 389.[LeetCode]Find the Difference
- [LeetCode]389. Find the Difference
- LeetCode 389 Find the Difference
- leetcode 389. Find the Difference 解题报告
- leetcode刷题系列-389. Find the Difference
- leetcode之389. Find the Difference(C++解法)
- LeetCode 389. Find the Difference
- LeetCode 389. Find the Difference
- LeetCode—389. Find the Difference
- LeetCode : 389. Find the Difference
- LeetCode 389. Find the Difference
- leetcode 389. Find the Difference 牛人用异或 或者 求和 解决,很简单。
- Leetcode 389 Find the Difference
- LeetCode[389]Find the Difference
- [LeetCode] 389.Find the Difference
- Leetcode-389. Find the Difference
- leetcode 389. Find the Difference