题解 | #牛棚品种分类#
牛棚品种分类
https://www.nowcoder.com/practice/0b6068f804b9426aa737ea8606e8d5c3
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param strs string字符串一维数组
* @return string字符串一维数组
*/
private HashMap<String, String> map = new HashMap();
public String[] groupAnagrams (String[] strs) {
for (String s : strs) {
char[] arr = s.toCharArray();
Arrays.sort(arr);
String si = new String(arr);
if (map.get(si) == null) map.put(si, s);
else map.put(si, map.get(si) + "," + s);
}
String[] ans = new String[map.size()];
int idx = 0;
for (String val : map.values()) {
ans[idx++] = val;
}
Arrays.sort(ans);
return ans;
}
}

查看19道真题和解析