
ord()将字符转换成ASCII
chr()将ASCII转换成字符
class Solution(object):
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
# 1.异或 0^N = N , N^N = 0
res = 0
for i in s+t:
res ^= ord(i)
return chr(res)
# 2.用栈
if len(s) == 0:
return t
res = [0] * 26
# s中有就存-1,t中有就存+1,这样就可以使用一个列表来完成任务
for i in s:
i = ord(i) - 97
res[i] = res[i] - 1
for j in t:
j = ord(j) - 97
res[j] = res[j] + 1
for t in res:
if t == 1:
return chr(res.index(t)+97)
1632

被折叠的 条评论
为什么被折叠?



