网站外链怎么发布,哪家建站公司好,外部调用wordpress 热门文章,快速赚钱的软件【力扣题】题目描述#xff1a; 【Python3】代码#xff1a;
1、解题思路#xff1a;使用计数器分别统计字符串中的元素和出现次数#xff0c;两个计数器相减#xff0c;结果就是新添加的元素。
知识点#xff1a;collections.Counter(...)#xff1a;字典子类#x…【力扣题】题目描述 【Python3】代码
1、解题思路使用计数器分别统计字符串中的元素和出现次数两个计数器相减结果就是新添加的元素。
知识点collections.Counter(...)字典子类计数器统计各元素及出现次数。 list(...)转为列表。 列表[索引]获取列表中索引号对应的元素。
class Solution:def findTheDifference(self, s: str, t: str) - str:from collections import Countertdict, sdict Counter(t), Counter(s)res tdict - sdictreturn list(res)[0] 2、解题思路分别将两个字符串转为列表并排序依次比对元素是否相同不同则t中的该元素为新添加的元素。
知识点列表.sort()在原列表的基础上将元素按从小到大排序不生成新列表。 列表[-1]获取列表中最后一个元素。
class Solution:def findTheDifference(self, s: str, t: str) - str:n len(s)slist, tlist list(s), list(t)slist.sort()tlist.sort()for i in range(n):if tlist[i] ! slist[i]:return tlist[i]return tlist[-1] 3、1解题思路使用一个长度为26的列表依次记录26个字母在两个字符串中出现次数。字符串s中字母增加列表中对应计数字符串t中字母减少列表中对应计数。最终计数为-1的为t中新添加的元素。
知识点len(...)获取序列的长度即有多少个元素。 ord(...)将字符转为ASCII或Unicode数值。 列表.index(...)获取某元素在列表中的索引号。 chr(...)将ASCII或Unicode数值转为字符串。
class Solution:def findTheDifference(self, s: str, t: str) - str:alist [0] * 26n len(s)for i in range(n):alist[ord(s[i]) - ord(a)] 1alist[ord(t[i]) - ord(a)] - 1alist[ord(t[-1]) - ord(a)] - 1res alist.index(-1) ord(a)return chr(res)也可先统计字符串s中各字母的个数再遍历字符串t字符串t中字母减少列表中对应计数一旦计数小于0即为t中新添加的元素。
class Solution:def findTheDifference(self, s: str, t: str) - str:alist [0] * 26for x in s:alist[ord(x) - ord(a)] 1for y in t:alist[ord(y) - ord(a)] - 1if alist[ord(y) - ord(a)] 0:return y
2解题思路使用一个字典记录各元素及其个数字符串s中字母增加字典中对应的值字符串t中字母减少字典中对应的值。最终值为-1的为t中新添加的元素。
知识点collections.defaultdict(...)字典子类参数为工厂函数若某键不存在则调用工厂函数返回默认值。例如collections.defaultdict(int)若某键不存在则值默认为0。 字典[键]获取字典中某键对应的值或修改键对应的值字典[键]值。
class Solution:def findTheDifference(self, s: str, t: str) - str:from collections import defaultdictidict defaultdict(int)n len(s)for i in range(n):idict[s[i]] 1idict[t[i]] - 1 idict[t[-1]] - 1for x in idict:if idict[x] -1:return x
也可先统计字符串s中各字母的个数再遍历字符串t字符串t中字母减少字典中对应的值一旦计数小于0即为t中新添加的元素。
class Solution:def findTheDifference(self, s: str, t: str) - str:from collections import defaultdictidict defaultdict(int)for x in s:idict[x] 1for y in t:idict[y] - 1if idict[y] 0:return y
也可使用计数器直接获得字符串s中字母及其个数再遍历字符串t字符串t中字母减少计数器中对应的值一旦计数小于0即为t中新添加的元素。
class Solution:def findTheDifference(self, s: str, t: str) - str:from collections import Counteradict Counter(s)for x in t:adict[x] - 1if adict[x] 0:return x 4、解题思路分别获取两个字符串中元素的ASCII或Unicode数值的和再相减即为新添加的元素。
知识点sum(...)求和。 ord(...)将字符转为ASCII或Unicode数值。 chr(...)将ASCII或Unicode数值转为字符串。
class Solution:def findTheDifference(self, s: str, t: str) - str:ssum sum(ord(x) for x in s)tsum sum(ord(y) for y in t)return chr(tsum - ssum) 也可以依次按照字符串中对应位置相同索引号加上字符串t中字母的ASCII或Unicode数值并减去字符串s中字母的ASCII或Unicode数值最终的数值即为新添加的元素对应的数值。
class Solution:def findTheDifference(self, s: str, t: str) - str:res ord(t[-1])n len(s)for i in range(n):res ord(t[i]) - ord(s[i])return chr(res) 5、1解题思路将两个字符串拼接起来依次将字母转为ASCII或Unicode数值再进行位运算。
知识点字符串字符串将两个字符串拼接成1个字符串。 a ^ b a与b进行异或运算相同为0不同为1。
class Solution:def findTheDifference(self, s: str, t: str) - str:for x in st:res ^ ord(x)return chr(res)
也可以两个字符串拼接返回迭代器再依次进行位运算。
知识点itertools.chain(可迭代对象1, 可迭代对象2, ...)返回一个迭代器包含所有可迭代对象的内容。
class Solution:def findTheDifference(self, s: str, t: str) - str:import itertoolsres 0for x in itertools.chain(s,t):res ^ ord(x)return chr(res)
可进一步使用Python内置函数完成。
知识点map(函数, 可迭代对象)对可迭代对象中元素进行映射返回一个迭代器。 functools.reduce(函数, 可迭代对象)函数带有两个参数。对可迭代对象中元素依次进行累积操作获得一个计算结果。
class Solution:def findTheDifference(self, s: str, t: str) - str:from functools import reduceres reduce(lambda x,y:x^y,map(ord,st))return chr(res) 6、解题思路遍历字符串s将字符串s中的字母依次从字符串t中去除剩余的即为新添加的元素。
知识点字符串.replace(旧值, 新值, 替换最大次数)字符串中某字符替换成新的字符。字符串为不可变类型若要获得替换后的字符串需赋值。
class Solution:def findTheDifference(self, s: str, t: str) - str:n len(s)for i in range(n):t t.replace(s[i],,1)return t