如何在这个问题中处理报价问题?
class Solution:
# @param strs: A list of strings
# @return: A list of strings
def anagrams(self, strs):
# write your code here
dic = {}
result = []
for s in strs:
if s == '""':
key ='""'
else:
key = ''.join(sorted(s))
dic[key].add(s)
for key, value in dic.iteritems():
if len(value) > 1:
result.append(value)
return result
输入
["",""]
预期
["",""]
错误消息
Traceback (most recent call last): File "Main.py", line 7, in ans = Solution().anagrams(strs) File "Solution.py", line 14, in anagrams dic[key].add(s) KeyError: '' EXITCODE=1
答案 0 :(得分:1)
当您编写["",""]
时,输入数组的元素是空字符串 - ""
不是字符串内容的一部分。因此s = '""'
将无法与之匹配。你需要写:
if s == '':