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 ...
随机推荐
- iOS 更改uitextfield placeholder颜色
[passwordField setValue:TPColor forKeyPath:@"_placeholderLabel.textColor"];
- border-radius 原理分析
border-radius 想必大家都有所了解,比较常见的用法就像下面一样: 注意左边的盒子 border-radius: 100px; 右边的为0哦,所以右边的实际上没有设置圆角边框属性:咱们比较 ...
- MySQL安装和Navicat安装、破解
1)mysql下载 地址:https://dev.mysql.com/downloads/mysql/ 2)一路next安装,安装好后文件目录如下(不包括data文件夹,my.ini文件) 3)新建文 ...
- python3连接Mongodb
前提条件,安装过Mongondb,并且装一下Robomongo(为了更加直观地看到测试时数据的变化 ) 1.安装PyMySQL pip install pymongo 2.测试 import pymo ...
- Go语言性能优化
原文:http://bravenewgeek.com/so-you-wanna-go-fast/ 我曾经和很多聪明的人一起工作.我们很多人都对性能问题很痴迷,我们之前所做的是尝试逼近能够预期的(性能) ...
- kafka_2.11-0.10.1.1集群搭建安装配置
在搭建kafka集群之前,请保证zookeeper已安装. 1.下载 官网下载链接:http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/0.10.1.1/ ...
- 【转】spring和springMVC的面试问题总结
1.Spring中AOP的应用场景.Aop原理.好处? 答:AOP--Aspect Oriented Programming面向切面编程:用来封装横切关注点,具体可以在下面的场景中使用: Authen ...
- 设计4个线程,其中2个对num进行加操作,另两个对num进行减操作
/** * 设计4个线程,其中2个对num进行加操作,另两个对num进行减操作 */ public class ThreadTest { private int j; public static vo ...
- 流畅的python 14章可迭代的对象、迭代器 和生成器
可迭代的对象.迭代器和生成器 迭代是数据处理的基石.扫描内存中放不下的数据集时,我们要找到一种惰性获取数据项的方式,即按需一次获取一个数据项.这就是迭代器模式(Iterator pattern). 迭 ...
- Django - ModelForm组件
一.ModelForm组件 这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来,先来一个简单的例子来看一下这个东西怎么用:比如我们的数据库中有这样一张学生表, ...