美文网首页北美程序员面试干货
LeetCode 389 [Find the Differenc

LeetCode 389 [Find the Differenc

作者: Jason_Yuan | 来源:发表于2016-09-06 09:18 被阅读104次

原题

给出两个字符串,只有一处不同,一个字符串比两一个多一个字符,找出这个字符

样例

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.

解题思路

  • 合并两个字符串,题目转化为寻找单身狗

完整代码

class Solution(object):
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        temp = 0
        for char in s + t:
            temp = temp ^ ord(char)
        return chr(temp)

相关文章

网友评论

    本文标题:LeetCode 389 [Find the Differenc

    本文链接:https://www.haomeiwen.com/subject/mrldettx.html