不难 只是细节很多 map list等的细节还是需要背啊
public class Solution {
public List<List<String>> groupStrings(String[] strings) {
HashMap<List<Integer>, List<String>> map = new HashMap<List<Integer>, List<String>>();
for ( int i = 0; i < strings.length; i ++ ){
List<Integer> pos = new ArrayList<Integer>();
for ( int j = 0; j < strings[i].length(); j ++ ){
int po = strings[i].charAt(j) - strings[i].charAt(0);
pos.add ( po > 0 ? po : po + 26 );
}
if ( map.containsKey ( pos ) )
map.get ( pos ).add ( strings[i] );
else{
List<String> strs = new ArrayList<String>();
strs.add ( strings[ i ] );
map.put( pos, strs );
}
}
for ( List<String> list : map.values() )
Collections.sort ( list );
List<List<String>> res = new ArrayList<List<String>>();
res.addAll ( map.values() );
return res;
}
}
这里key用一个string就更好了 判断containskey应该会快一些