【Python3】【力扣题】389. 找不同

【力扣题】题目描述:

【Python3】代码:

1、解题思路:使用计数器分别统计字符串中的元素和出现次数,两个计数器相减,结果就是新添加的元素。

知识点:collections.Counter(...):字典子类,计数器,统计各元素及出现次数。

              list(...):转为列表。

              列表[索引]:获取列表中索引号对应的元素。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        from collections import Counter
        tdict, sdict = Counter(t), Counter(s)
        res = tdict - sdict
        return list(res)[0]

2、解题思路:分别将两个字符串转为列表并排序,依次比对元素是否相同,不同则t中的该元素为新添加的元素。

知识点:列表.sort():在原列表的基础上将元素按从小到大排序,不生成新列表。

              列表[-1]:获取列表中最后一个元素。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        n = len(s)
        slist, tlist = list(s), list(t)
        slist.sort()
        tlist.sort()
        for i in range(n):
            if tlist[i] != slist[i]:
                return tlist[i]
        return tlist[-1]

3、(1)解题思路:使用一个长度为26的列表,依次记录26个字母在两个字符串中出现次数。字符串s中字母增加列表中对应计数,字符串t中字母减少列表中对应计数。最终计数为-1的为t中新添加的元素。

知识点:len(...):获取序列的长度,即有多少个元素。

              ord(...):将字符转为ASCII或Unicode数值。

              列表.index(...):获取某元素在列表中的索引号。

              chr(...):将ASCII或Unicode数值转为字符串。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        alist = [0] * 26
        n = len(s)
        for i in range(n):
            alist[ord(s[i]) - ord("a")] += 1
            alist[ord(t[i]) - ord("a")] -= 1
        alist[ord(t[-1]) - ord("a")] -= 1
        res = alist.index(-1) + ord("a")
        return chr(res)

也可先统计字符串s中各字母的个数,再遍历字符串t,字符串t中字母减少列表中对应计数,一旦计数小于0即为t中新添加的元素。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        alist = [0] * 26
        for x in s:
            alist[ord(x) - ord("a")] += 1
        for y in t:
            alist[ord(y) - ord("a")] -= 1
            if alist[ord(y) - ord("a")] < 0:
                return y

(2)解题思路:使用一个字典记录各元素及其个数,字符串s中字母增加字典中对应的值,字符串t中字母减少字典中对应的值。最终值为-1的为t中新添加的元素。

知识点:collections.defaultdict(...):字典子类,参数为工厂函数,若某键不存在,则调用工厂函数返回默认值。例如:collections.defaultdict(int),若某键不存在,则值默认为0。

              字典[键]:获取字典中某键对应的值,或修改键对应的值:字典[键]=值。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        from collections import defaultdict
        idict = defaultdict(int)
        n = len(s)
        for i in range(n):
            idict[s[i]] += 1
            idict[t[i]] -= 1 
        idict[t[-1]] -= 1
        for x in idict:
            if idict[x] == -1:
                return x

也可先统计字符串s中各字母的个数,再遍历字符串t,字符串t中字母减少字典中对应的值,一旦计数小于0即为t中新添加的元素。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        from collections import defaultdict
        idict = defaultdict(int)
        for x in s:
            idict[x] += 1
        for y in t:
            idict[y] -= 1
            if idict[y] < 0:
                return y

也可使用计数器直接获得字符串s中字母及其个数,再遍历字符串t,字符串t中字母减少计数器中对应的值,一旦计数小于0即为t中新添加的元素。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        from collections import Counter
        adict = Counter(s)
        for x in t:
            adict[x] -= 1
            if adict[x] < 0:
                return x

4、解题思路:分别获取两个字符串中元素的ASCII或Unicode数值的和,再相减,即为新添加的元素。

知识点:sum(...):求和。

              ord(...):将字符转为ASCII或Unicode数值。

              chr(...):将ASCII或Unicode数值转为字符串。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        ssum = sum(ord(x) for x in s)
        tsum = sum(ord(y) for y in t)
        return chr(tsum - ssum)

 也可以依次按照字符串中对应位置(相同索引号),加上字符串t中字母的ASCII或Unicode数值,并减去字符串s中字母的ASCII或Unicode数值,最终的数值即为新添加的元素对应的数值。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        res = ord(t[-1])
        n = len(s)
        for i in range(n):
            res += ord(t[i]) - ord(s[i])
        return chr(res)

5、(1)解题思路:将两个字符串拼接起来,依次将字母转为ASCII或Unicode数值,再进行位运算。

知识点:字符串+字符串:将两个字符串拼接成1个字符串。

              a ^ b: a与b进行异或运算(相同为0,不同为1)。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        for x in s+t:
            res ^= ord(x)
        return chr(res)

也可以两个字符串拼接返回迭代器,再依次进行位运算。

知识点:itertools.chain(可迭代对象1, 可迭代对象2, ...):返回一个迭代器,包含所有可迭代对象的内容。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        import itertools
        res = 0
        for x in itertools.chain(s,t):
            res ^= ord(x)
        return chr(res)

可进一步使用Python内置函数完成。

知识点:map(函数可迭代对象):对可迭代对象中元素进行映射,返回一个迭代器。

              functools.reduce(函数可迭代对象):函数带有两个参数。对可迭代对象中元素依次进行累积操作,获得一个计算结果。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        from functools import reduce
        res = reduce(lambda x,y:x^y,map(ord,s+t))
        return chr(res)

6、解题思路:遍历字符串s,将字符串s中的字母依次从字符串t中去除,剩余的即为新添加的元素。

知识点:字符串.replace(旧值, 新值, 替换最大次数):字符串中某字符替换成新的字符。字符串为不可变类型,若要获得替换后的字符串需赋值。

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        n = len(s)
        for i in range(n):
            t = t.replace(s[i],"",1)
        return t

相关推荐

  1. 389. 不同

    2024-01-18 20:38:05       22 阅读
  2. (leetcode)第383赎金信(Python

    2024-01-18 20:38:05       60 阅读
  3. 每日一383赎金信

    2024-01-18 20:38:05       53 阅读
  4. 383 赎金信 记录

    2024-01-18 20:38:05       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 20:38:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 20:38:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 20:38:05       82 阅读
  4. Python语言-面向对象

    2024-01-18 20:38:05       91 阅读

热门阅读

  1. C#将货币金额数字转大写汉字

    2024-01-18 20:38:05       52 阅读
  2. MySQL8.0.26-Linux版安装

    2024-01-18 20:38:05       63 阅读
  3. 网络工程师:计算机基础知识面试题(四)

    2024-01-18 20:38:05       47 阅读
  4. tinyxml2

    tinyxml2

    2024-01-18 20:38:05      46 阅读
  5. 设计模式——中介者模式

    2024-01-18 20:38:05       55 阅读
  6. DEJA_VU3D - Cesium功能集 之 119-三维热力图

    2024-01-18 20:38:05       63 阅读
  7. 【webpack5】高级优化

    2024-01-18 20:38:05       46 阅读