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 ...
随机推荐
- Python—面向对象06 内置方法
一 .isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(object ...
- UGUI防止点击穿透
if (!IsPointerOverGameObject(Input.mousePosition)) { } public static bool IsPointerOverGameObject(Ve ...
- oracle-02 用户管理
一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 identified by 密码; ...
- BigDecimal运算(加、减、乘、除)
public class BigDecimalOperation { private BigDecimalOperation(){ } public static BigDecimal add(dou ...
- CF605A Sorting Railway Cars(递推)
题目描述 An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers ...
- 关于parseInt的看法
前面在看题目的时候 偶然看到 使用parseInt 来进行整数判断 但是这里的parseInt是错误示范 之后了解了一下 发现这和函数 很有研究 先看看 w3c怎么说这个的 parseInt() ...
- Nginx反向代理 Laravel获取真实IP地址(PHP)
使用VUE前后端分离开发 后端使用Laravel 想要获取到用户的真实IP地址 因为分离开发不同源跨域问题 所以只能进行前端Nginx反向代理 location /api { rewrite ^/a ...
- 关于JUnit4无法支持多线程测试的解决方法
转自:https://segmentfault.com/a/1190000003762719 其实junit是将test作为参数传递给了TestRunner的main函数.并通过main函数进行执行. ...
- js获取浏览器内容宽高(小计)
<SCRIPT LANGUAGE="JavaScript">var s = "";s += "\r\n网页可见区域宽:"+ d ...
- HTML5 + JS 调取摄像头拍照下载
<video id="video" width="640" height="480" autoplay></video&g ...