LeetCode--389--找不同

问题描述:

给定两个字符串 st,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例:

输入:
s = "abcd"
t = "abcde"

输出:
e

解释:
'e' 是那个被添加的字母。

方法1:

 1 class Solution(object):
 2     def findTheDifference(self, s, t):
 3         """
 4         :type s: str
 5         :type t: str
 6         :rtype: str
 7         """
 8         for i in t:
 9             if i not in s:
10                 return i
11             elif s.count(i) != t.count(i):
12                 return i

amazing:

 1 class Solution(object):
 2     def findTheDifference(self, s, t):
 3         """
 4         :type s: str
 5         :type t: str
 6         :rtype: str
 7         """
 8         num_s = 0
 9         num_t = 0
10         for i in s:
11             num_s += ord(i)
12         for i in t:
13             num_t += ord(i)
14         return chr(num_t -num_s)

2018-09-29 06:58:05

原文地址:https://www.cnblogs.com/NPC-assange/p/9721743.html