题目:

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. (转)js中__proto__和prototype的区别和关系

    作者:doris链接:https://www.zhihu.com/question/34183746/answer/58155878来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  2. Spark与缓存

    预期成果 1.1   当前问题 当前以图搜图应用存在的问题: 当前使用spark RDD方案无法达到数据实时加载(每10分钟加载一次,虽然可配,但太短可能会有问题) Spark RDD内存会被分为两部 ...

  3. BZOJ 3362 Navigation Nightmare

    一道带权并查集题目. 带权并查集的重点是信息的合并. 这类题出现得并不多,练习一下. #include<bits/stdc++.h> using namespace std; #defin ...

  4. 玩转Google开源C++单元测试框架Google Test系列(gtest)(总)

    原文地址:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Goo ...

  5. 算法(Algorithms)第4版 练习 1.3.15

    Queue: package com.qiusongde; import java.util.Iterator; import java.util.NoSuchElementException; im ...

  6. Linux学习之路(四)帮助命令

    帮助命令man .man 命令 #获取指定命令的帮助 .man ls #查看ls的帮助 man的级别 1 查看命令的帮助 2 查看可被内核调用的函数的帮助 3 查看函数的函数库的帮助 4 查看特殊文件 ...

  7. PHP相关安全配置【转】

    PHP是广泛使用的开源服务端脚本语言.通过HTTP或HTTPS协议,Apache Web服务允许用户访问文件或内容.服务端脚本语言的错误配置会导致各种问题.因此,PHP应该小心使用.以下是为系统管理员 ...

  8. 转载h5问题总结

    判断微信浏览器 function isWeixin(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger ...

  9. hadoop源码剖析--hdfs安全模式

    一.什么是安全模式 hadoop安全模式是name node的一种状态,处于该状态时有种量特性: 1.namenode不接受任何对hfds文件系统的改变操作(即此时整个文件系统处于只读状态): 2.不 ...

  10. codeforces 706A A. Beru-taxi(水题)

    题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...