LeetCode——Find the Difference
LeetCode——Find the Difference
Question
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.
Solution
用hash table求解,时间复杂度为O(2n)
用异或求解,这道题和single number 的问题是一个意思,两个字符串中,找出那个单个的字符。时间复杂度为O(n)。
Answer
class Solution {
public:
char findTheDifference(string s, string t) {
map<char, int> dict;
for (char c : s)
dict[c]++;
map<char, int> dict2;
for (char c : t) {
dict2[c]++;
if (dict2[c] > dict[c])
return c;
}
}
};
用异或求解。
class Solution {
public:
char findTheDifference(string s, string t) {
char r=0;
for(char c:s) r ^=c;
for(char c:t) r ^=c;
return r;
}
};
LeetCode——Find the Difference的更多相关文章
- [LeetCode] Minimum Time Difference 最短时间差
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode Minimum Time Difference
原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/ 题目: Given a list of 24-ho ...
- LeetCode Minimum Absolute Difference in BST
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...
- LeetCode 1026. Maximum Difference Between Node and Ancestor
原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...
- [LeetCode] Find the Difference 寻找不同
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- [LeetCode] Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- LeetCode:Find the Difference_389
LeetCode:Find the Difference [问题再现] Given two strings s and t which consist of only lowercase letter ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
随机推荐
- SSM框架---详细整合教程
http://blog.csdn.net/zhshulin/article/details/37956105/
- git base 简单命令行
记录几个简单的命令 1:克隆-把线上的文件复制到本地 git clone 线上地址 2:检查状态 git status 3:放入待仓储 git add.文件名 git add * (全部文件,简单粗暴 ...
- 【BZOJ4317】Atm的树 动态树分治+二分+线段树
[BZOJ4317]Atm的树 Description Atm有一段时间在虐qtree的题目,于是,他满脑子都是tree,tree,tree…… 于是,一天晚上他梦到自己被关在了一个有根树中,每条路径 ...
- vue+node+mongoDB 火车票H5(二)---vux和less的配置
vue基本环境配置好之后,就可以开始开发页面了 开发页面之前先了解一下项目结构,原始的目录结构为: config是配置文件,环境配置好了开发阶段可以不再去修改了,node_modules文件夹是打包的 ...
- 通过脚本同时运行几个spider
# 通过脚本同时运行几个spider目录结构: 1.在命令行能通过的情况下创建两个spider如TestSpiderTest2Spider 2.在items.py的同级目录创建run.py文件,有三种 ...
- crash处理core文件
(一时心血来潮总结的,供大家参考,时间仓促,不足之处勿拍砖,欢迎讨论~)Crash工具用于解析Vmcore文件,Vmcore文件为通过kdump等手段收集的操作系统core dump信息,在不采用压缩 ...
- 巨蟒python全栈开发-第13天 内置函数 匿名函数lambda
一.今日内容总览 1.内置函数(1):并不是每一个内置函数都是那么常用 上菜:内置函数部分//思维导图:https://www.processon.com/view/link/5b4ee15be4b0 ...
- 服务器1M带宽同时能承受多少人在线
最近网站的流量一直在增长,这个肯定是好事.不过也有个麻烦的问题,目前本站用的的虚拟空间,每月流量30G,虽然95%的图片都已外链,但流量还是很吃紧,日均2000ip,4月份流量34g左右,单JS的响应 ...
- C++ 常见字符处理 收录
1.string字符串删除 字符串中 指定字符 std::string& HTTPRequestHandlerImpl::replace_all_distinct(std::string&am ...
- python16_day13【css、js】
一.部局 1.pg-header(title) .pg-header{ height: 48px; background-color: cadetblue; line-height: 48px; mi ...