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.

这道题给了我们两个字符串s和t,t是在s的任意一个地方加上了一个字符,让我们找出新加上的那个字符。这道题确实不是一道难题,首先第一反应的方法就是用哈希表来建立字符和个数之间的映射,如果在遍历t的时候某个映射值小于0了,那么返回该字符即可,参见代码如下:

解法一:

class Solution {
public:
char findTheDifference(string s, string t) {
unordered_map<char, int> m;
for (char c : s) ++m[c];
for (char c : t) {
if (--m[c] < ) return c;
}
return ;
}
};

我们也可以使用位操作Bit Manipulation来做,利用异或的性质,相同位返回0,这样相同的字符都抵消了,剩下的就是后加的那个字符,参见代码如下:

解法二:

class Solution {
public:
char findTheDifference(string s, string t) {
char res = ;
for (char c : s) res ^= c;
for (char c : t) res ^= c;
return res;
}
};

我们也可以直接用加和减,相同的字符一减一加也抵消了,剩下的就是后加的那个字符,参见代码如下:

解法三:

class Solution {
public:
char findTheDifference(string s, string t) {
char res = ;
for (char c : s) res -= c;
for (char c : t) res += c;
return res;
}
};

下面这种方法是史蒂芬大神提出来的,利用了STL的accumulate函数,实际上是上面解法二的改写,一行就写完了真是丧心病狂啊,参见代码如下:

解法四:

class Solution {
public:
char findTheDifference(string s, string t) {
return accumulate(begin(s), end(s += t), , bit_xor<int>());
}
};

参考资料:

https://discuss.leetcode.com/topic/55987/java-c-1-liner

https://discuss.leetcode.com/topic/55960/two-java-solutions-using-xor-sum

https://discuss.leetcode.com/topic/55912/java-solution-using-bit-manipulation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Find the Difference 寻找不同的更多相关文章

  1. LeetCode——Find the Difference

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

  2. Leetcode(4)寻找两个有序数组的中位数

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...

  3. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

  4. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素2

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素2 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

  5. [LeetCode] Find the Celebrity 寻找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  6. [LeetCode] Find Duplicate Subtrees 寻找重复树

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

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

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

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

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

  9. LeetCode Minimum Time Difference

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

随机推荐

  1. Vertica删除历史分区数据

    假设test用户下创建的t_jingyu表 vsql -Utest -wtestpwd create table t_jingyu( col1 int, col2 varchar, col3 time ...

  2. Effective前端4:尽可能地使用伪元素

    伪元素是一个好东西,但是很多人都没怎么用,因为他们觉得伪元素太诡异了.其实使用伪元素有很多好处,最大的好处是它可以简化页面的html标签,同时用起来也很方便,善于使用伪元素可以让你的页面更加地简洁优雅 ...

  3. EntityFramework中的DbContext使用疑点说明

    1.DbContext怎么在Asp.mvc中使用? public class Repository { //实例化EF容器:有弊端.一个线程里可能会创建多个DbContext //DbContext ...

  4. Entity Framework 教程——概述

    Entity Framework 基础 本教材将手把手教你使用entity framework,我们将使用entity framework 6.0和visual studio 2012. 以下表格是e ...

  5. C# 之 EXCEL导入导出

    以下方式是本人总结的一些经验,肯定有很多种方法,在此先记下,留待以后补充... 希望朋友们一起来探讨相关想法,请在下方留言. A-1:EXCEL模板导出 非常简单,将EXCEL模板上传到项目中后,将其 ...

  6. Visual Studio (VSIX,项目模板 )制作

    下载Vsiual Studio 2012 SDK 下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=30668 提示:一定要注意 ...

  7. Android Service

    一.在MainAcitivity界面启动Service  : public class MyService extends Service intent = new Intent(MainActivi ...

  8. JavaWeb_day01_HTTP_Servlet

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! JavaWeb_day01 HTTP协议 HTTP(H ...

  9. Struts2入门(四)——数据输入验证

    一.前言 1.1.什么是输入验证?为什么需要输入验证? 在上一篇文章中,我们学习了数据类型转换,我们提到了表示层数据处理的两个方法,也提到了用户输入数据需要进行类型转换才能得到我们想要的数据,那么,我 ...

  10. ASP.Net MVC数据传递

    今天做了个项目,涉及到离线下载HTML,没有前后台交互,没有Ajax,JavaScript,只有第一次从控制器带参数进入,一次读取到页面所需要的全部数据,使用Razor语法绑定到前台页面,在做这个项目 ...