389.Find the Difference

    xiaoxiao2021-03-25  80

    给两个字符串 s 和 t ,里面都是小写的字符

    字符串t 是根据字符串s随机打乱顺序,然后在随机的位置添加一个字符生成的。

    让找到t字符串中添加的那一个字符

    例如:

    Input: s = "abcd" t = "abcde" Output: e Explanation: 'e' is the letter that was added. 利用位运算的异或。两个相同的数异或结果为0 ,0与一个数异或的结果为该数本身。找到字符串中每个字符对应的ASCII码, 把字符作为整数来处理。将得到的结果在转为字符串就可以了。 chr(x )                 将一个整数转换为一个字符     ord(x )                 将一个字符转换为它的整数值     str(x )                 将对象 x 转换为字符串   这里不能直接将result 转为字符串,因为result为int型。 class Solution(object):     def findTheDifference(self, s, t):         result = 0         for i in range(len(s)):             result = ord(s[i]) ^ result         for k in range(len(t)):             result = ord(t[k]) ^ result         return str(chr(result))

    转载请注明原文地址: https://ju.6miu.com/read-38673.html

    最新回复(0)