资讯专栏INFORMATION COLUMN

LeetCode 389——找不同

mcterry / 1050人阅读

摘要:遍历中的字符,然后分别计数其在和中出现的次数,如果二者不相等,则当前字符即为所求。方法三将中所有字符的码之和减去中所有字符的码之和,最后的差对应的字符即为所求。

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」!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/44900.html

相关文章

  • Leetcode PHP题解--D73 389. Find the Difference

    摘要:题目链接题目分析给定两个字符串,其中一个字符串比另一个字符串在随机位置多一个字符。思路用计算字符串中字符出现的次数,对比两个字符串的字符出现次数。计算差集,返回差异部分即可。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D73 389. Find the Difference 题目链接 389. Find the Difference 题目分析 给定两个字符串,其中一个字符串比另一...

    widuu 评论0 收藏0
  • 力扣(LeetCode)389

    摘要:题目地址题目描述给定两个字符串和,它们只包含小写字母。字符串由字符串随机重排,然后在随机位置添加一个字母。示例输入输出解释是那个被添加的字母。解答这一题可以用两种解法。 题目地址:https://leetcode-cn.com/probl...题目描述:给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被...

    lylwyy2016 评论0 收藏0
  • leetcode389.Find The Difference

    摘要:题目要求假设两个只包含小写字母的字符串和,其中是中字母的乱序,并在某个位置上添加了一个新的字母。最后只需要遍历整数数组检查是否有某个字符计数大于。 题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling strin...

    cyqian 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0

发表评论

0条评论

mcterry

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<