難度等級:中等
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
返回的每行都是可以anagrams ,并且按照字典排序的。每行的單詞都是排好序的。
代碼:
參考代碼
解題思路:把所有滿足anagrams的字符串放到一個map的multiset里;然后遍歷map里的每個multiset取出所有的字符串賦值給vector<string>。在這里注意的是multiset自動將里邊的字符串排好序了。
知識拓展
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
從一個數組里挑出符合angrams字符串的群組。
代碼:
拓展代碼
思路:排序字符串,映射到map里,然后賦值給ret。vector<string> 大于 1 的時候,才能說明是找到兩個以上的字符串了,否則不符合條件,if起到篩選的作用。
引用自博客:博客園