[LeetCode]17 Letter Combinations of a Phone Number
2015-01-02 13:42
435 查看
https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
http://fisherlei.blogspot.com/2012/12/leetcode-letter-combinations-of-phone.html
http://fisherlei.blogspot.com/2012/12/leetcode-letter-combinations-of-phone.html
public class Solution { public List<String> letterCombinations(String digits) { if (digits == null) return null; char[] chars = digits.toCharArray(); List<List<String>> allMappings = new ArrayList<>(); for (char c : chars) { List<String> mapping = getMapping(c); if (!mapping.isEmpty()) allMappings.add(mapping); } // Use allMappings to generate results. Set<String> toReturn = new HashSet<>(); toReturn.add(""); for (List<String> mapping : allMappings) { Set<String> newSet = new HashSet<>(); for (String oldstr : toReturn) { for (String s : mapping) { newSet.add(oldstr + s); } } toReturn = newSet; } return new ArrayList<String>(toReturn); } private List<String> getMapping(char c) { List<String> toReturn = new ArrayList<>(); if (c >= '2' && c <= '6') { for (int i = 0 ; i < 3 ; i ++) { char ch = (char)((c - '2') * 3 + 'a' + i); toReturn.add(String.valueOf(ch)); } } else if (c == '7') { toReturn.add("p"); toReturn.add("q"); toReturn.add("r"); toReturn.add("s"); } else if (c == '8') { toReturn.add("t"); toReturn.add("u"); toReturn.add("v"); } else if (c == '9') { toReturn.add("w"); toReturn.add("x"); toReturn.add("y"); toReturn.add("z"); } return toReturn; } }
相关文章推荐
- 17. Letter Combinations of a Phone Number Leetcode Python
- leetcode 17 Letter Combinations of a Phone Number
- [Java]LeetCode17 Letter Combinations of a Phone Number
- LeetCode(17) Letter Combinations of a Phone Number
- [leetcode 17]Letter Combinations of a Phone Number
- leetcode 17: Letter Combinations of a Phone Number
- [leetcode-17]Letter Combinations of a Phone Number(java)
- [Leetcode 47] 17 Letter Combinations of a Phone Number
- LeetCode 17: Letter Combinations of a Phone Number
- 【小熊刷题】Letter Combinations of a Phone Number <Leetcode 17, Java>
- [LeetCode 17] Letter Combinations of a Phone Number
- LeetCode 17 - Letter Combinations of a Phone Number
- LeetCode 17 Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- LeetCode | #17 Letter Combinations of a Phone Number
- [*leetcode 17] Letter Combinations of a Phone Number
- Leetcode 17 Letter Combinations of a Phone Number
- LeetCode 17 Letter Combinations of a Phone Number(C,C++,Java,Python)
- [Leetcode]#17 Letter Combinations of a Phone Number
- leetcode 17 -- Letter Combinations of a Phone Number