389 Find the Difference 找不同
给定两个字符串 s 和 t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
示例:
输入:
s = "abcd"
t = "abcde"
输出:
e
解释:
'e' 是那个被添加的字母。
详见:https://leetcode.com/problems/find-the-difference/description/
C++:
方法一:
class Solution {
public:
char findTheDifference(string s, string t) {
char res=0;
for(char c:s)
{
res^=c;
}
for(char c:t)
{
res^=c;
}
return res;
}
};
方法二:
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] < 0)
{
return c;
}
}
return 0;
}
};
389 Find the Difference 找不同的更多相关文章
- 389. Find the Difference 找出两个字符串中多余的一个字符
[抄题]: Given two strings s and t which consist of only lowercase letters. String t is generated by ra ...
- 【LeetCode】389 Find the Difference(java)
原题 Given two strings s and t which consist of only lowercase letters. String t is generated by rando ...
- 9. leetcode 389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- LeetCode 389 Find the Difference 解题报告
题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...
- LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告
1.题目大意 Given two strings s and t which consist of only lowercase letters. String t is generated by r ...
- 【leetcode389】389. Find the Difference
异或 找不同 —.— public class Solution { public char findTheDifference(String s, String t) { char temp = 0 ...
- LeetCode389Find the Difference找不同
给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...
- 【LeetCode】389. Find the Difference 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...
- LeetCode 389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
随机推荐
- nyoj_176_整数划分(二)_201404261715
整数划分(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 把一个正整数m分成n个正整数的和,有多少种分法? 例:把5分成3个正正数的和,有两种分法: 1 1 3 ...
- MongoDB小结21 - find【游标】
数据库使用游标来控制find的执行结果. 客户端对游标的实现通常能够对最终结果进行有效控制. 可以限制结果的数量,略过部分结果,对任意方向任意键的组合对结果进行排序,或者去执行一些功能强大的操作. 我 ...
- Python学习系列之装饰器
装饰器的作用 装饰器用于装饰某个函数.方法或者类,它可以让这个函数执行之前或者执行之后做一些操作 手工实现一个装饰器 def outer(some_func): #装饰器 $1 def inner() ...
- android账号与同步之账号管理
在android提供的sdk中,samples文件夹下有一个叫SampleSyncAdapter的演示样例,它是一个账号与同步的实例,比方Google原始的android手机能够使用Google账号进 ...
- C#程序如何把窗体文件从从一个项目中复制到另一个项目
一个窗体有三个文件,全部拷贝到新的项目中 在新的项目中点击显示所有文件,然后右击导入的文件,点击包括在项目中,会自动修改颜色(此时还没有被识别为窗体) 重启这个项目,三个文件已经被识别出来了 ...
- 用C# (.NET Core) 实现抽象工厂设计模式
用C# (.NET Core) 实现抽象工厂设计模式 本文的概念性内容来自深入浅出设计模式一书. 上一篇文章讲了简单工厂和工厂方法设计模式 http://www.cnblogs.com/cgzl/ ...
- HDOJ 5387 Clock 水+模拟
Clock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- spring 的核心接口
spring有两个核心接口,BeanFactory 和ApplicationContext ,其中ApplicationContext 是BeanFactory的子接口.他们代表了Spring容器. ...
- 解决jQuery uploadify在非IE核心浏览器下无法上传
之前上传了一个通过Flash实现多文件上传,但是在IE正常运行,FireFox 不能正常上传.经过反复研究学习,之所以firefox和360浏览器无法正常运行,是因为FireFox.chrome.36 ...
- android删除短信
代码如下: //删除短信 getContentResolver().delete(Uri.parse("content://sms/#"),"address=?" ...