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. 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇四:关于OneNote入库处理以及审核

    篇一:WPF常用知识以及本项目设计总结:http://www.cnblogs.com/baiboy/p/wpf.html 篇二:基于OneNote难点突破和批量识别:http://www.cnblog ...

  2. Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化

    本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解 ...

  3. dicom网络通讯入门(1)

    看标准 越看越糊,根本原因:dicom抽象得非常严重,是“专家”弄的.没办法. 又是什么服务类 又是什么sop,相信你把dicom标准看到头大 都不知如何下手. 不就是 socket么 这有何难. 首 ...

  4. ArcGIS属性选择器筛选

    以前我也会过,后来忘得干干净净.还是老话,学习新东西不难,难的是不断总结和提升.重新学习ArcGIS的属性筛选功能,记录如下. 要求: 1. 查找,删除. 例:根据属性当中相同字段,选择符合要求的数据 ...

  5. 细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型

    在Slick官方文档中描述:连接后台数据库后,需要通过定义Projection,即def * 来进行具体库表列column的选择和排序.通过Projection我们可以选择库表中部分列.也可以增加一些 ...

  6. ArrayList LinkedList源码解析

    在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...

  7. MySQL大数据优化

    我们考虑的情况是在你的数据量很大的情况下,千万级别的数据量.不要当我们的请求响应时间已经让我无法忍受的时候,再来想起来优化,可能有点迟了.因为可能会丢失很多潜在的价值客户.所以,在我们当初设计表,或者 ...

  8. javascript操作系统检测

    function detectOS() { var sUserAgent = navigator.userAgent;console.log(sUserAgent); var isWin = (nav ...

  9. Vector Tile

    Mapbox Vector Tile Specification A specification for encoding tiled vector data. <?XML:NAMESPACE ...

  10. 最新GHOST XP系统下载旗舰增强版 V2016年

    系统来自:系统妈:http://www.xitongma.com 深度技术GHOST xp系统旗舰增强版 V2016年3月 系统概述 深度技术ghost xp系统旗舰增强版集合微软JAVA虚拟机IE插 ...