GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/GroupShiftedStrings.java at master · Tulpen/Leetcode-Java Expand file tree/ Copy pathGroupShiftedStrings.javaMore file actions 39 lines (31 loc) · 1022 Bytes / Copy pathGroupShiftedStrings.javaFile metadata and controls39 lines (31 loc) · 1022 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import java.util.*; public class GroupShiftedStrings { /** * 这里核心是getTag,不论怎么shift,都回滚到a开头 */ public List<List<String>> groupStrings(String[] strings) { Map<String, List<String>> map = new HashMap<>(); for (String s : strings) { String key = getTag(s); List<String> value = map.get(key); if (value == null) { value = new ArrayList<>(); map.put(key, value); } value.add(s); } List<List<String>> ret = new ArrayList<>(); for (List<String> lst : map.values()) { Collections.sort(lst); // dont forget to sort. ret.add(lst); } return ret; } String getTag(String s) { int diff = (int) s.charAt(0) - (int) 'a'; StringBuilder sb = new StringBuilder(); for (char c : s.toCharArray()) sb.append((c + 26 - diff) % 26); return sb.toString(); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |