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.

思路:用map记录所有char出现的次数。复杂度O(N)。

 class Solution {
public:
char findTheDifference(string s, string t) {
unordered_map<char, int> dict;
for (int i = ; i < s.size(); i++)
if (!dict.count(s[i])) dict.insert(make_pair(s[i], ));
else dict[s[i]]++;
for (int i = ; i < t.size(); i++)
if (!dict.count(t[i]) || !dict[t[i]]) return t[i];
else dict[t[i]]--;
return '\0';
}
};

Find the Difference -- LeetCode的更多相关文章

  1. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  2. [LeetCode] Minimum Time Difference 最短时间差

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  3. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  4. LeetCode——Find the Difference

    LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...

  5. LeetCode Minimum Time Difference

    原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/ 题目: Given a list of 24-ho ...

  6. [Leetcode Week10]Minimum Time Difference

    Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...

  7. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  8. LeetCode 1026. Maximum Difference Between Node and Ancestor

    原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...

  9. 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...

随机推荐

  1. Ubuntu下安装LNMP之nginx的卸载

    我在安装Nginx时,是采用自己从网上down下自己需要的nginx版本进行编译安装的,如果使用过apt库来进行安装的话可以参考这篇文章:ubuntu中彻底删除nginx 假如是编译安装的童鞋,可以按 ...

  2. The 'brew link' step did not complete successfully

    在mac 上更新node时遇到了一系列的问题: 卸载node重新安装之后提示: The 'brew link' step did not complete successfully 其实这里已经给出了 ...

  3. 调整文本输入框placeholder的颜色等样式

    input::-webkit-input-placeholder{     color: white !important;}input:-moz-placeholder{    color: whi ...

  4. 如何根据pom.xml文件下载jar包

    遇到过这种情况:从网上下载了一个项目, 使用的maven, 但是我想要新建一个项目, 但是不需要使用maven. 但是我怎么样才能将他那个项目的所有引用的jar包给下载下载下来呢; 1.下载一个mav ...

  5. viewDidLoad方法的调用顺序

      https://www.evernote.com/shard/s227/sh/01307822-de66-448d-8bac-7954f01ffc64/185b44c2bf5b6e266f4bed ...

  6. swift出师作,史丹佛大学游戏制作案例,计算器,小游戏

    这两个案例得好好弄清楚,感觉在任何方面既然能够作为公开课被提到这所名校的课程里面自然有不得不学习的理由,感觉应该去入手一下,毕竟这种课,价格不匪,难以接触,能看到就当再教育了.

  7. cve-2012-5613 mysql本地提权

    cve-2012-5613  是一个通过FILE权限写Trigger的TRG存储文件(即伪造Trigger),由root触发而导致权限提升的漏洞.不知道为什么这个漏洞一直没修,可能mysql认为这是一 ...

  8. Linux make命令详解

    在linux环境下的工作,免不了需要经常编译C/C++源代码,所以make命令是我们经常都会用到的.当然make工具不一定针对C代码,它也可以维护其他各种代码,详见:man make    在列举其详 ...

  9. POJ2186 (强连通分量缩点后出度为0的分量内点个数)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27820   Accepted: 11208 De ...

  10. rtp包格式

    转载一篇帮助我了解h264 rtp的文档,地址http://www.cppblog.com/czanyou/archive/2009/12/25/67940.html 当packetization-m ...