389. Find the Difference
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:
e
Explanation:
'e' is the letter that was added.
解1:
找出不同的字符串,和前面的找单身狗的差不多,两个字符串相加后两个字符串中都出现过的字符应该是偶数,所以使用亦或运算。
class Solution(object):
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
return chr(reduce(operator.xor, map(ord, s + t)))
工具
map(),这个不多说
reduce()
这个是向后计算
ord()
ord() 参数是一个ascii字符,返回值是对应的十进制整数
chr() 参数是十进制放回的是ascii字符
后面xor()异或运算必须是数值类型的变量,chr型的不可以。
使用了operator.xor(),operator模块提供了运算符模块。
a = [1,2,3]
b = [5,6,7]
c = map(operator.mul, a, b)
c的值就为[5, 12, 21]
abs(...)
abs(a) -- Same as abs(a).
add(...)
add(a, b) -- Same as a + b.
and_(...)
and_(a, b) -- Same as a & b.
concat(...)
concat(a, b) -- Same as a + b, for a and b sequences.
contains(...)
contains(a, b) -- Same as b in a (note reversed operands).
countOf(...)
countOf(a, b) -- Return the number of times b occurs in a.
delitem(...)
delitem(a, b) -- Same as del a[b].
delslice(...)
delslice(a, b, c) -- Same as del a[b:c].
div(...)
div(a, b) -- Same as a / b when __future__.division is not in effect.
eq(...)
eq(a, b) -- Same as a==b.
floordiv(...)
floordiv(a, b) -- Same as a // b.
ge(...)
ge(a, b) -- Same as a>=b.
getitem(...)
getitem(a, b) -- Same as a[b].
getslice(...)
getslice(a, b, c) -- Same as a[b:c].
gt(...)
gt(a, b) -- Same as a>b.
iadd(...)
iadd(a, b) -- Same as a += b.
iand(...)
iand(a, b) -- Same as a &= b.
iconcat(...)
iconcat(a, b) -- Same as a += b, for a and b sequences.
idiv(...)
idiv(a, b) -- Same as a /= b when __future__.division is not in effect.
ifloordiv(...)
ifloordiv(a, b) -- Same as a //= b.
ilshift(...)
ilshift(a, b) -- Same as a <<= b.
imod(...)
imod(a, b) -- Same as a %= b.
imul(...)
imul(a, b) -- Same as a *= b.
index(...)
index(a) -- Same as a.__index__()
indexOf(...)
indexOf(a, b) -- Return the first index of b in a.
inv(...)
inv(a) -- Same as ~a.
invert(...)
invert(a) -- Same as ~a.
ior(...)
ior(a, b) -- Same as a |= b.
ipow(...)
ipow(a, b) -- Same as a **= b.
irepeat(...)
irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.
irshift(...)
irshift(a, b) -- Same as a >>= b.
isCallable(...)
isCallable(a) -- Same as callable(a).
isMappingType(...)
isMappingType(a) -- Return True if a has a mapping type, False otherwise.
isNumberType(...)
isNumberType(a) -- Return True if a has a numeric type, False otherwise.
isSequenceType(...)
isSequenceType(a) -- Return True if a has a sequence type, False otherwise.
is_(...)
is_(a, b) -- Same as a is b.
is_not(...)
is_not(a, b) -- Same as a is not b.
isub(...)
isub(a, b) -- Same as a -= b.
itruediv(...)
itruediv(a, b) -- Same as a /= b when __future__.division is in effect.
ixor(...)
ixor(a, b) -- Same as a ^= b.
le(...)
le(a, b) -- Same as a<=b.
lshift(...)
lshift(a, b) -- Same as a << b.
lt(...)
lt(a, b) -- Same as a<b.
mod(...)
mod(a, b) -- Same as a % b.
mul(...)
mul(a, b) -- Same as a * b.
ne(...)
ne(a, b) -- Same as a!=b.
neg(...)
neg(a) -- Same as -a.
not_(...)
not_(a) -- Same as not a.
or_(...)
or_(a, b) -- Same as a | b.
pos(...)
pos(a) -- Same as +a.
pow(...)
pow(a, b) -- Same as a ** b.
repeat(...)
repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.
rshift(...)
rshift(a, b) -- Same as a >> b.
sequenceIncludes(...)
sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).
setitem(...)
setitem(a, b, c) -- Same as a[b] = c.
setslice(...)
setslice(a, b, c, d) -- Same as a[b:c] = d.
sub(...)
sub(a, b) -- Same as a - b.
truediv(...)
truediv(a, b) -- Same as a / b when __future__.division is in effect.
truth(...)
truth(a) -- Return True if a is true, False otherwise.
xor(...)
xor(a, b) -- Same as a ^ b.