题目:

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)的更多相关文章

  1. LeetCode 389. Find the Difference (找到不同)

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  2. LeetCode 389. Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  3. 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 ...

  4. LeetCode 389 Find the Difference 解题报告

    题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...

  5. 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 ...

  6. 【LeetCode】389. Find the Difference 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...

  7. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  8. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  9. LeetCode之389. Find the Difference

    -------------------------------------------------- 先计算每个字母的出现次数然后减去,最后剩下的那一个就是后来添加的了. AC代码: public c ...

随机推荐

  1. restlet验证

    1 restlet有无认证对比 无认证: 客户端发起请求 -----> 服务器路由 -----> 访问服务端资源 有认证: 客户端发起请求 -----> 认证 ----->服务 ...

  2. about gnu bash shell

    1 定义字符串不需要引号 var=NONE echo $var ==>NONE 2 支持基本的整数计算 a=1 b=2 echo $((a+b)) ==>3 必须用$(()),双括号的形式 ...

  3. 如果数据需要被多个应用程序消费的话,推荐使用 Kafka,如果数据只是面向 Hadoop 的,可以使用 Flume

    https://www.ibm.com/developerworks/cn/opensource/os-cn-kafka/index.html Kafka 与 Flume 很多功能确实是重复的.以下是 ...

  4. Struts2常见面试点

    01.  三层和MVC的区别 http://blog.csdn.net/csh624366188/article/details/7183872 http://www.cnblogs.com/zdxs ...

  5. linux rsyncserver文件同步

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zqtsx/article/details/24254651 [root@zqtsx]# rpm -q ...

  6. 支付宝异步通知notify_url接收不了问题解决(转)

    此处return_url可以成功跳转回网站页面但notify_url却接收不到支付宝的异步通知.已保证notify_url是一个外网可以访问的网址 1.网站用的是ssh框架,当支付宝发通知到我这个ac ...

  7. [容易] A + B 问题

    题目来源:http://www.lintcode.com/zh-cn/problem/a-b-problem/

  8. Map总结--HashMap/HashTable/TreeMap/WeakHashMap使用场景分析(转)

    首先看下Map的框架图 1.Map概述 1.Map是键值对映射的抽象接口 2.AbstractMap实现了Map中绝大部分的函数接口,它减少了“Map实现类”的重复编码 3.SortedMap有序的“ ...

  9. 【linux】crontab的环境变量问题

    今天遇到一个奇怪的问题,同样一个脚本,手动执行没问题,加入到crontab中,就出现无法运行的情况,第一反应是环境变量问题 环境说明: 操作系统:centos 用户:test用户通过sudo su切换 ...

  10. ubuntu 搭建 tomcat

    一.下载tomcat 先下载到本地,然后ftp上传到服务器 官方 Apache Tomcat 的下载 2 二.解压安装 先解压 tar zxvf apache-tomcat-7.0.64.tar.gz ...