LeetCode: 389 Find the Difference(easy)
题目:
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.
代码:
自己的:
class Solution {
public:
char findTheDifference(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
auto r = mismatch(s.begin(), s.end(), t.begin());
return (*r.second);
}
};
别人的(1):
class Solution {
public:
char findTheDifference(string s, string t) {
// Similar to single number. Can do using hashmap...
int charmap[] = {};
for(char c: t) {
charmap[c-'a']++;
}
// Iterate over s, one letter will have count 1 left
for(char c: s) {
charmap[c-'a']--;
}
for(int i=;i<;i++) {
if (charmap[i] == )
return 'a'+i;
}
return -;
}
};
别人的(2):
class Solution {
public:
char findTheDifference(string s, string t) {
char res = ;
for(auto c: s)
res^=c;
for(auto c: t)
res^=c;
return res;
}
};
LeetCode: 389 Find the Difference(easy)的更多相关文章
- 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 random s ...
- 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 ...
- 【LeetCode】389. Find the Difference 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- LeetCode之389. Find the Difference
-------------------------------------------------- 先计算每个字母的出现次数然后减去,最后剩下的那一个就是后来添加的了. AC代码: public c ...
随机推荐
- 九度OJ 1080:进制转换 (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4583 解决:1076 题目描述: 将M进制的数X转换为N进制的数输出. 输入: 输入的第一行包括两个整数:M和N(2<=M,N< ...
- 九度OJ 1029:魔咒词典 (排序)
时间限制:5 秒 内存限制:32 兆 特殊判题:否 提交:4574 解决:1318 题目描述: 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部 ...
- 关于gcc
1 the architecture of gcc 2 自己编译gcc时的 --build --host --target选项的含义和用法 <1> --build 执行本次的gcc编译的主 ...
- 性能测试--初识Jmeter
初识Jmeter Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动 ...
- Android之ProgressBar读取文件进度解析
ProgressBar进度条, 分为旋转进度条和水平进度条,进度条的样式根据需要自定义,之前一直不明白进度条如何在实际项目中使用,网上演示进度条的案例大多都是通过Button点 击增加.减少进度值,使 ...
- RANDOM 的用法
random 用法 1.利用RANDOM取随机数 shell有一个环境变量RANDOM,范围是0--32767 如果我们想要产生0-25范围内的数:$(($RANDOM%26),在$(()) 是可以省 ...
- Django 模型层--单表
ORM 简介 MTV或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这可以大大的减少了开 ...
- EASYARM-IMX283 烧写uboot和linux系统
新入手一台EASYARM-IMX283开发板(以下简称IMX823),价格比较便宜,配置也不错. 开发板默认安装了WINCE,我还是决定重新烧写uboot和linux内核. 开发板配套光盘里面有不少烧 ...
- python正则表达提取文本好文
摘自: http://www.cnblogs.com/rj81/p/5933838.html
- zabbix性能优化等
摘自: http://blog.sina.com.cn/s/blog_4cbf97060101fcfw.html 非常好的一篇,值得有用