Group Anagrams
Given an array of strings, group anagrams together.
Example:
1 | Input: ["eat", "tea", "tan", "ate", "nat", "bat"], |
Note:
- All inputs will be in lowercase.
- The order of your output does not matter.
思路:想到通过对String转换成char [] 然后排序,如果两个字符串排序后的结果内容相等的话就说明这两个字符串的是Anagrams。代码如下:算法正确,但是运行超时了。
1 | class Solution { |
这种题目,里边有防止重复的计算量时候,考虑用map结构来回更加有效率,利用map后的代码如下,通过全部测试用例。
1 | class Solution { |