# 先用 zip() 处理部分字符串,然后处理较长字符串的未处理部分,简单来说还是暴力循环 classSolution: defmergeAlternately(self, word1: str, word2: str) -> str: ans = [] l1, l2 = len(word1), len(word2) for w1, w2 inzip(word1, word2): ans.append(w1) ans.append(w2) if l1 > l2: for i inrange(l2, l1): ans.append(word1[i]) elif l1 < l2: for i inrange(l1, l2): ans.append(word2[i]) return''.join(ans)
时间复杂度:$O(n)$,空间复杂度:$O(n)$
评论区解法
这个方法与我的差不多,但是处理的方式明显比我要方便许多——采用字符串相加的方式而非列表转字符串、像遍历列表那样遍历字符串而非使用 for 循环遍历:
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defmergeAlternately(self, word1: str, word2: str) -> str: res = '' m = len(word1) n = len(word2) for i, j inzip(word1, word2): res += i + j if m <= n: res += word2[m:] else: res += word1[n:] return res
from itertools import zip_longest classSolution: defmergeAlternately(self, word1: str, word2: str) -> str: ans = [] for x, y in zip_longest(word1, word2): if x: ans.append(x) if y: ans.append(y) return''.join(ans)
classSolution: defmergeAlternately(self, word1: str, word2: str) -> str: index = 0 res = '' while index < len(word1) or index < len(word2): if index < len(word1): res += word1[index] if index < len(word2): res += word2[index] index += 1 return res
找不同
题目
给定两个字符串 s 和 t ,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
示例 1:
输入:s = “abcd”, t = “abcde” 输出:”e”
示例 2:
输入:s = “”, t = “y” 输出:”y”
解法
用哈希表(字典)统计两个字符串中所有字母出现的次数,不相同则输出该字母:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: deffindTheDifference(self, s: str, t: str) -> str: s_dict = dict[str, int]() t_dict = dict[str, int]() for i inrange(len(t)): if i < len(s): ifnot (s[i] in s_dict.keys()): s_dict[s[i]] = 0 s_dict[s[i]] += 1 ifnot (t[i] in t_dict.keys()): t_dict[t[i]] = 0 t_dict[t[i]] += 1 for key in t_dict.keys(): if key in s_dict.keys(): if s_dict[key] != t_dict[key]: return key else: return key
时间复杂度:$O(n)$,空间复杂度:$O(n)$
评论区解法
将字符转为ASCII码相加,然后二者相减就是多出来的那个字母的ASCII码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: deffindTheDifference(self, s: str, t: str) -> str: t_sum = 0 s_sum = 0 for i inrange(len(t)): if i < len(s): s_sum += ord(s[i]) t_sum += ord(t[i]) ans = t_sum - s_sum returnchr(ans) # 一样的算法,但是实现方式使用了map函数,很简洁 classSolution: deffindTheDifference(self, s: str, t: str) -> str: returnchr(sum(map(ord, t)) - sum(map(ord, s)))
classSolution: deffindTheDifference(self, s: str, t: str) -> str: dictTemp = {} # 遍历字符串s,用字典统计词频 for c in s: dictTemp[c] = dictTemp.get(c, 0) + 1# 字典取值操作的时间复杂度是常数阶! # 遍历字符串t,字符串t中的字符出现一次,就让字典对应的词频数量减1,最后词频不为0的那个字符就是不同的字符 for c in t: dictTemp[c] = dictTemp.get(c, 0) - 1 for i in dictTemp: if dictTemp[i] != 0: return i
剩下这几种方法都借助了字符串内置的 count() 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 将两个字符串拼接,统计次数,返回奇数次数的字母 classSolution: deffindTheDifference(self, s: str, t: str) -> str: m = s + t for i in m: if m.count(i) % 2 == 1: return i
# 直接数,出现次数不同就输出 classSolution: deffindTheDifference(self, s: str, t: str) -> str: for c in t: if t.count(c) != s.count(c): return c # 直接数的简洁实现 classSolution: deffindTheDifference(self, s: str, t: str) -> str: return [x for x in t if t.count(x)-s.count(x)==1][0]