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 ...
随机推荐
- centos6.5下安装samba服务器与配置
转自:http://www.centoscn.com/CentosServer/ftp/2014/1023/3989.html http://www.cnblogs.com/x_wukong/p/56 ...
- pocket
Pocket是一个离线阅读服务软件. Pocket的主要功能就是将你要阅读或者一时没有读完的网页标记下来,接着同步到服务器端,然后你就可以在不同的设备上阅读.如果你在电脑上网的时间不多,一些东西又来不 ...
- 3162 抄书问题(划分dp)
3162 抄书问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 现在要把M本有顺序的书分给K个人复制( ...
- 【BZOJ4513】[Sdoi2016]储能表 数位DP
[BZOJ4513][Sdoi2016]储能表 Description 有一个 n 行 m 列的表格,行从 0 到 n−1 编号,列从 0 到 m−1 编号.每个格子都储存着能量.最初,第 i 行第 ...
- Entity Framework查询生成大量的子查询,如何避免?求救
最近使用Entity Framework做一个中型的项目,一张表含有千万条数据,并没有使用很复杂的查询,只是程序上使用了DTO进行帅选数据,且使用了分页,效果很不理想.经过跟踪sql,我发现很多简单的 ...
- JavaScript学习笔记-Js操控HTML5 <progress> 标签
Js操控----HTML5 <progress> 标签 简单模拟下下载进度跑条 <h4>加载进度</h4> <input type="button& ...
- 在Sql Server中使用证书加密数据
IF NOT EXISTS () CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'QWE23987zxJKL569&agf1$94467GRkjg5k3 ...
- curl post CURLOPT_POSTFIELDS
PHP: curl_setopt - Manual http://php.net/manual/en/function.curl-setopt.php CURLOPT_POST TRUE to do ...
- js对象转成用&拼接的请求参数(转)
var parseParam=function(param, key){ var paramStr=""; if(param instanceof String||param in ...
- python基础之练习题(一)
1.执行 Python 脚本的两种方式 python test.py chmod +x test.py && ./test.py 2.简述位.字节的关系 二进制位(bit)是计算机存储 ...