Guest User

Untitled

a guest
Oct 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. public class LetterCombinationsofaPhoneNumber {
  2.  
  3. /*
  4. 方法一:递归,dfs
  5.  
  6. 全局变量 map
  7.  
  8. 需要传递的变量:
  9. 题目所给的string digits
  10. 当前string 的状态 curr
  11. 处理哪一位数字 index
  12. 返回结果 ans
  13.  
  14. 递归函数的终止条件:
  15. index==digits.length
  16.  
  17. 时间复杂度O(3^n),空间复杂度O(n)
  18. */
  19. final HashMap<Character, char[]> hmap = new HashMap<Character, char[]>();
  20.  
  21. public List<String> letterCombinations1(String digits) {
  22. List<String> ans = new LinkedList<>();
  23. if (digits.length() == 0)
  24. return ans;
  25. hmap.put('1', new char[]{});
  26. hmap.put('2', new char[]{'a', 'b', 'c'});
  27. hmap.put('3', new char[]{'d', 'e', 'f'});
  28. hmap.put('4', new char[]{'g', 'h', 'i'});
  29. hmap.put('5', new char[]{'j', 'k', 'l'});
  30. hmap.put('6', new char[]{'m', 'n', 'o'});
  31. hmap.put('7', new char[]{'p', 'q', 'r', 's'});
  32. hmap.put('8', new char[]{'t', 'u', 'v'});
  33. hmap.put('9', new char[]{'w', 'x', 'y', 'z'});
  34. hmap.put('0', new char[]{' '});
  35.  
  36. helper(digits, "", 0, ans);
  37. return ans;
  38. }
  39.  
  40. public void helper(String digits, String curr, int index, List<String> ans) {
  41. if (index == digits.length()) {
  42. ans.add(curr);
  43. } else {
  44. char[] characters = hmap.get(digits.charAt(index));
  45. for (char ch : characters) {
  46. helper(digits, curr + ch, index + 1, ans);
  47. }
  48. }
  49. }
  50.  
  51. /*
  52. 方法二:queue,bfs
  53. */
  54. public List<String> letterCombinations2(String digits) {
  55. List<String> ans = new LinkedList<>();
  56. if (digits.length() == 0)
  57. return ans;
  58. hmap.put('1', new char[]{});
  59. hmap.put('2', new char[]{'a', 'b', 'c'});
  60. hmap.put('3', new char[]{'d', 'e', 'f'});
  61. hmap.put('4', new char[]{'g', 'h', 'i'});
  62. hmap.put('5', new char[]{'j', 'k', 'l'});
  63. hmap.put('6', new char[]{'m', 'n', 'o'});
  64. hmap.put('7', new char[]{'p', 'q', 'r', 's'});
  65. hmap.put('8', new char[]{'t', 'u', 'v'});
  66. hmap.put('9', new char[]{'w', 'x', 'y', 'z'});
  67. hmap.put('0', new char[]{' '});
  68.  
  69. Queue<String> queue = new LinkedList<>();
  70. char[] digit = digits.toCharArray();
  71. for(char d:digit){
  72. if(queue.isEmpty()){
  73. for(char ch:hmap.get(d)){
  74. queue.add(ch+"");
  75. }
  76. } else {
  77. int size = queue.size();
  78. for(int i=0; i<size; i++){
  79. String curr = queue.poll();
  80. for(char ch:hmap.get(d)){
  81. queue.add(curr+ch);
  82. }
  83. }
  84. }
  85.  
  86. }
  87. while(!queue.isEmpty()){
  88. ans.add(queue.poll());
  89. }
  90. return ans;
  91. }
  92.  
  93. /*
  94. 方法三:queue update->linkedlist
  95. */
  96. public List<String> letterCombinations3(String digits) {
  97. LinkedList<String> ans = new LinkedList<String>();
  98. if (digits.length() == 0)
  99. return ans;
  100. String[] map = new String[]{"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
  101. ans.add("");
  102. for(int i=0; i<digits.length(); i++){
  103. int digit = Character.getNumericValue(digits.charAt(i));
  104. while(ans.peek().length()==i){
  105. String s = ans.remove();
  106. for(char ch:map[digit].toCharArray()){
  107. ans.add(s+ch);
  108. }
  109. }
  110. }
  111. return ans;
  112. }
  113. }
Add Comment
Please, Sign In to add comment