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 ...
随机推荐
- Git Cheat Sheet 中文版
Git Cheat Sheet 中文版 索引 配置 配置文件 创建 本地修改 搜索 提交历史 分支与标签 更新与发布 合并与重置 撤销 Git Flow 配置 列出当前配置: $ git config ...
- readdir() 获取文件类型
readdir()获取文件类型 //// 字符设备文件 type =2, filename207=tty0 crw-rw---- 1 root root 4, 0 04-10 16:28 ...
- 使用HttpClient调用第三方接口
最近项目中需要调用第三方的Http接口,这里我用到了HttpClient. 首先我们要搞明白第三方接口中需要我们传递哪些参数.数据,搞明白参数以后我们就可以使用HttpClient调用接口了. 1.调 ...
- 我的arcgis培训照片13
来自:http://www.cioiot.com/successview-535-1.html
- 使用gdb调试python程序
参考文章:https://mozillazg.com/2017/07/debug-running-python-process-with-gdb.html https://blog.alswl.com ...
- Bundle格式文件的安装
安装VMware Workstation for Linux,文件是Bundle格式,安裝如下: 1 su要先取得root權限2hmod +x VMware-Workstation-Full-7.1. ...
- BUILD FAILED D:\build.xml:2: 前言中不同意有内容。
1.错误描写叙述 Microsoft Windows [版本号 6.1.7601] 版权全部 (c) 2009 Microsoft Corporation. 保留全部权利. C:\Users\Admi ...
- Android之——多线程下载演示样例
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46883927 一.概述 说到Android中的文件下载.Android API中明 ...
- 我和nupt集训队的故事
纯水文,如有不适请ctrl+w撤离 亚洲赛刚结束.看了不少巨巨的退役贴以及岛娘在知乎上的那篇感天动地的人生经历.多少有点夜深忽梦少年事的错觉.作为一个两年前就打出gg的高龄选手,之后又强行以1次队员和 ...
- Java中的字节输入出流和字符输入输出流
Java中的字节输入出流和字符输入输出流 以下哪个流类属于面向字符的输入流( ) A BufferedWriter B FileInputStream C ObjectInputStream D In ...