怒刷 LeetCode 100 道 (49)
08-16
怒刷 LeetCode 100 道 (49)
來自專欄編程人生
題目
Description:
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]]
解法
class Solution: def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] """ ret = [] tmp = [] for e in strs: t = sorted(e) if t not in tmp: tmp.append(t) ret.append([e]) else: ret[tmp.index(t)].append(e) return ret
推薦閱讀:
※數控車如何用UG自動編程
※The Nature of Code(譯) — 前言(內含教學大綱)
※雜文筆記: 「堅持」的背後
※四步處理一個數據或元素的拖放