LeetCode 389——找不同
1. 题目

2. 解答
2.1. 方法一
将 s 和 t 转化为 Python 的列表,然后遍历列表 s 的元素,将它们从列表 t 中删除,最后列表 t 中会余下一个元素,即为所求。
class Solution:
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
s_list = list(s)
t_list = list(t)
for i in s_list:
t_list.remove(i)
return t_list[0]
2.2. 方法二
将 t 转化为 Python 的集合,由于集合元素的互异性,这个过程会去掉重复元素,然后再将其转化为列表 r,此时 r 包含 t 中的所有字符。
遍历 r 中的字符,然后分别计数其在 s 和 t 中出现的次数,如果二者不相等,则当前字符即为所求。
class Solution:
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
r = list(set(t))
result = ' '
for i in r:
if s.count(i) != t.count(i):
result = i
return result
2.3. 方法三
将 t 中所有字符的 ASCII 码之和减去 s 中所有字符的 ASCII 码之和,最后的差对应的字符即为所求。
class Solution {
public:
char findTheDifference(string s, string t) {
int count = 0;
string::iterator i1 = s.begin(), i2 = t.begin();
for (; i1 != s.end() && i2 != t.end(); i1++, i2++)
{
count += *i2 - *i1;
}
count = count + *i2;
return char(count);
}
};
2.4. 方法四
利用按位异或运算。假设有两个数 a, b,按位异或可以实现两个数的交换。
a = a ^ b
b = a ^ b = a ^ b ^ b = a ^ 0 = a
a = a ^ b = a ^ b ^ a = b
因此,我们可以将 s 和 t 中的元素按位异或,相同的元素按位异或之后都会变成零,最后的结果即为所求。
class Solution {
public:
char findTheDifference(string s, string t) {
int count = 0;
string::iterator i1 = s.begin(), i2 = t.begin();
for (; i1 != s.end() && i2 != t.end(); i1++, i2++)
{
count = *i2 ^ *i1 ^ count;
}
count = count ^ *i2;
return char(count);
}
};
获取更多精彩,请关注「seniusen」!

LeetCode 389——找不同的更多相关文章
- Java实现 LeetCode 389 找不同
389. 找不同 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = " ...
- 【LeetCode】389.找不同
389.找不同 知识点:哈希表.抵消思想: 题目描述 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. ...
- 力扣(LeetCode)389. 找不同
给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...
- LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)
513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...
- LeetCode.1002-寻找共有字符(Find Common Characters)
这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都 ...
- 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 ...
- 位运算 leecode.389. 找不同
//给定两个字符串 s 和 t,它们只包含小写字母. //字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. //请找出在 t 中被添加的字母 char findTheDifferenc ...
- LeetCode 389 Find the Difference 解题报告
题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...
- leetcode ex3 找出穿过最多点的直线 Max Points on a Line
题目 https://oj.leetcode.com/problems/max-points-on-a-line/ 答案与分析 http://www.aiweibang.com/yuedu/18326 ...
随机推荐
- 怎么得到scrollTop
我们学习一个事件 : 页面滚动效果 window.onscroll = function() { 页面滚动语句 } 谷歌浏览器 和没有声明 DTD <DOCTYPE > : ...
- FFMPEG系列一:Mac下FFMPEG编译安装配置及使用例子
系统环境:10.13以前系统版本,没有升级到macOS High Sierra.正常情况是直接输入brew install ffmpeg即可安装ffmpeg,但是该过程还是有一些坑需要填. 一.mac ...
- Java并发编程:线程封闭和ThreadLocal详解
转载请标明出处: http://blog.csdn.net/forezp/article/details/77620769 本文出自方志朋的博客 什么是线程封闭 当访问共享变量时,往往需要加锁来保证数 ...
- linux 执行程序时,提示not found问题分析
sh: ./test: not found 通常可以通过readelf查看该进程文件所以依赖的运行环境,检查相关路径是否存在对应的文件. 比如如下: 1. 检查/lib目录,发现ld-X.XX.so为 ...
- C++指针数组,二级指针和函数指针的练习
1.编一程序,将字符串“Hello,C++!”赋给一个字符数组, 然后从第一个字母开始间隔地输出该串(请用指针完成). 代码如下 #include<iostream> #include&l ...
- 【TOJ 1449】Area of Circles II(求不同位置的两圆面积之和)
描述 There are two circles on the plane. Now you must to calculate the area which they cover the plane ...
- 大专生自学c++到找到工作的前前后后
先做个自我介绍,我13年考上一所很烂专科民办的学校,学的是生物专业,具体的学校名称我就不说出来献丑了.13年我就辍学了,我在那样的学校,一年学费要1万多,但是根本没有人学习,我实在看不到希望,我就退学 ...
- 爬虫——Selenium与PhantomJS
Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同的是Selenium可以直接运行在浏览器上, ...
- 微信小程序数据传递基本
1.从后台服务器获取数据,然后存在JS,通过数据绑定显示在页面 后台获取数据: getUser: function () { var that = this; //function 里面已经不是thi ...
- Python的virtualenv你用过吗?
1. 为什么要有virtualenv 在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾 ...