LeetCode | 49.字母异位词分组

输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]

利用字典属性,排序后插入

strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
temp_dict = {}
for i in strs:
    sort_str = "".join(sorted(i))
    if sort_str not in temp_dict:
        temp_dict[sort_str] = [i]
    else:
        temp_dict[sort_str].append(i)

print([i for i in temp_dict.values()])

 

posted @ 2021-08-03 16:49  黄金国的大象  阅读(26)  评论(0编辑  收藏  举报