Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string "23"

Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

这题好无聊, 纯粹的重复性劳动,算法很简单就是DFS。

  1 public class Solution {

  2     ArrayList<String> result = null;

  3     public ArrayList<String> letterCombinations(String digits) {

  4         // Note: The Solution object is instantiated only once and is reused by each test case.

  5         result = new ArrayList<String>();

  6         if(digits == null) return result;

  7         if(digits.length() == 0){

  8             result.add("");

  9             return result;

 10         }

 11         getDigit(digits, 0, new StringBuffer());

 12         return result;

 13     }

 14     public void getDigit(String s,int pos,StringBuffer sb){

 15         if(pos == s.length()){

 16             result.add(sb.toString());

 17             return;

 18         }

 19         char c = s.charAt(pos);

 20         if(c == '2'){

 21             sb.append('a');

 22             getDigit(s,pos + 1, new StringBuffer(sb));

 23             sb.deleteCharAt(sb.length() - 1);

 24             

 25             sb.append('b');

 26             getDigit(s,pos + 1, new StringBuffer(sb));

 27             sb.deleteCharAt(sb.length() - 1);

 28             

 29             sb.append('c');

 30             getDigit(s,pos + 1, new StringBuffer(sb));

 31             sb.deleteCharAt(sb.length() - 1);

 32         }else if(c == '3'){

 33             sb.append('d');

 34             getDigit(s,pos + 1, new StringBuffer(sb));

 35             sb.deleteCharAt(sb.length() - 1);

 36             

 37             sb.append('e');

 38             getDigit(s,pos + 1, new StringBuffer(sb));

 39             sb.deleteCharAt(sb.length() - 1);

 40             

 41             sb.append('f');

 42             getDigit(s,pos + 1, new StringBuffer(sb));

 43             sb.deleteCharAt(sb.length() - 1);

 44         }else if(c == '4'){

 45             sb.append('g');

 46             getDigit(s,pos + 1, new StringBuffer(sb));

 47             sb.deleteCharAt(sb.length() - 1);

 48             

 49             sb.append('h');

 50             getDigit(s,pos + 1, new StringBuffer(sb));

 51             sb.deleteCharAt(sb.length() - 1);

 52             

 53             sb.append('i');

 54             getDigit(s,pos + 1, new StringBuffer(sb));

 55             sb.deleteCharAt(sb.length() - 1);

 56         }else if(c == '5'){

 57             sb.append('j');

 58             getDigit(s,pos + 1, new StringBuffer(sb));

 59             sb.deleteCharAt(sb.length() - 1);

 60             

 61             sb.append('k');

 62             getDigit(s,pos + 1, new StringBuffer(sb));

 63             sb.deleteCharAt(sb.length() - 1);

 64             

 65             sb.append('l');

 66             getDigit(s,pos + 1, new StringBuffer(sb));

 67             sb.deleteCharAt(sb.length() - 1);

 68         }else if(c == '6'){

 69             sb.append('m');

 70             getDigit(s,pos + 1, new StringBuffer(sb));

 71             sb.deleteCharAt(sb.length() - 1);

 72             

 73             sb.append('n');

 74             getDigit(s,pos + 1, new StringBuffer(sb));

 75             sb.deleteCharAt(sb.length() - 1);

 76             

 77             sb.append('o');

 78             getDigit(s,pos + 1, new StringBuffer(sb));

 79             sb.deleteCharAt(sb.length() - 1);

 80         }else if(c == '7'){

 81             sb.append('p');

 82             getDigit(s,pos + 1, new StringBuffer(sb));

 83             sb.deleteCharAt(sb.length() - 1);

 84             

 85             sb.append('q');

 86             getDigit(s,pos + 1, new StringBuffer(sb));

 87             sb.deleteCharAt(sb.length() - 1);

 88             

 89             sb.append('r');

 90             getDigit(s,pos + 1, new StringBuffer(sb));

 91             sb.deleteCharAt(sb.length() - 1);

 92             

 93             sb.append('s');

 94             getDigit(s,pos + 1, new StringBuffer(sb));

 95             sb.deleteCharAt(sb.length() - 1);

 96         }else if(c == '8'){

 97             sb.append('t');

 98             getDigit(s,pos + 1, new StringBuffer(sb));

 99             sb.deleteCharAt(sb.length() - 1);

100             

101             sb.append('u');

102             getDigit(s,pos + 1, new StringBuffer(sb));

103             sb.deleteCharAt(sb.length() - 1);

104             

105             sb.append('v');

106             getDigit(s,pos + 1, new StringBuffer(sb));

107             sb.deleteCharAt(sb.length() - 1);

108         }else if(c == '9'){

109             sb.append('w');

110             getDigit(s,pos + 1, new StringBuffer(sb));

111             sb.deleteCharAt(sb.length() - 1);

112             

113             sb.append('x');

114             getDigit(s,pos + 1, new StringBuffer(sb));

115             sb.deleteCharAt(sb.length() - 1);

116 

117             sb.append('y');

118             getDigit(s,pos + 1, new StringBuffer(sb));

119             sb.deleteCharAt(sb.length() - 1);

120             

121             sb.append('z');

122             getDigit(s,pos + 1, new StringBuffer(sb));

123             sb.deleteCharAt(sb.length() - 1);

124         }else if(c == '0'){

125             sb.append(' ');

126             getDigit(s,pos + 1, new StringBuffer(sb));

127             sb.deleteCharAt(sb.length() - 1);

128         }else{

129             return;

130         }

131     }

132 }

 别人的解法:

 1 public class Solution {

 2     public ArrayList<String> letterCombinations(String digits) {

 3         // Start typing your Java solution below

 4         // DO NOT write main() function

 5         ArrayList<String> res = new ArrayList<String> ();

 6         res.add("");

 7         if(digits==null) return res;

 8         String[] table = digitToString();

 9         for(int i=0;i<digits.length();i++){

10             ArrayList<String> cur = new ArrayList<String>();

11             char c = digits.charAt(i);

12             if(c>='2' && c<='9'){

13                 for(String temp:res){

14                     for(int j=0;j<table[c-'2'].length();j++){

15                         cur.add(temp + table[c-'2'].charAt(j));

16                     }

17                 }

18             }

19             res= new ArrayList<String>(cur);

20         }

21         return res;    

22     }

23     public String[] digitToString(){

24       String[] res = new String[8];

25       char start = 'a';

26       

27       for(int i=0;i<8;i++){

28         int count = (i==5||i==7)?4:3;

29         StringBuilder temp = new StringBuilder("");

30         for(int j=0;j<count;j++){

31             temp.append((char)(start+j));

32         }

33         start=(char)(start+count);

34         res[i] = temp.toString(); 

35       }

36       return res;

37     }

38 }

 第二遍:

 1 public class Solution {

 2     ArrayList<String> result = null;

 3     public ArrayList<String> letterCombinations(String digits) {

 4         // Note: The Solution object is instantiated only once and is reused by each test case.

 5          String[] c={   "",

 6                         "",

 7                         "abc",

 8                         "def",

 9                         "ghi",

10                         "jkl",

11                         "mno",

12                         "pqrs",

13                         "tuv",

14                         "wxyz"  };

15         result = new ArrayList<String>();

16         search( digits, 0, new StringBuffer(), c);

17         return result;

18     }

19     public void search(String s, int pos, StringBuffer sb,String[] c){

20         if(pos == s.length()){

21             result.add(sb.toString());

22             return;

23         }

24         int num = s.charAt(pos) - '0';

25         for(int i = 0; i < c[num].length(); i ++){

26             sb.append(c[num].charAt(i));

27             search(s, pos + 1, new StringBuffer(sb), c);

28             sb.deleteCharAt(sb.length() - 1);

29         }

30     }

31 }

 

你可能感兴趣的:(number)