本文共 812 字,大约阅读时间需要 2 分钟。
题目:
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input:s = "abcd"t = "abcde"Output:eExplanation:'e' is the letter that was added.
方法一:性能46ms
class Solution(object): def findTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ res = ord(t[-1]) for i in range(len(s)): res += ord(t[i]) - ord(s[i]) return chr(res)
方法二:性能39ms
class Solution(object): def findTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ a=ord(t[0]) for i in s+t[1:]: a=a^ord(i) return chr(a)
转载地址:http://cgcrj.baihongyu.com/