字符串按照zigzag排列,最后按照行输出结果。
public static List<String> letterCombinations(String digits) {
List<String> ans = new ArrayList<String>();
if(digits == null || digits.length() == 0){
return ans;
}
String[] record = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
StringBuffer sb = new StringBuffer();
useme(ans,record,digits,0,sb);
return ans;
}
public static void useme(List<String> ans,
String[] record, String digits, int index,StringBuffer sb){
if(index == digits.length()){
ans.add(new String(sb));
return ;
}
String curStr = record[digits.charAt(index)-'0'];
if(curStr.equals("")){
useme(ans,record,digits,index+1,sb);
}
for(int i =0;i<curStr.length();i++){
char curChar = curStr.charAt(i);
sb.append(curChar);
useme(ans,record,digits,index+1,sb);
sb.deleteCharAt(sb.length()-1);
}
}
要注意 0,1 的时候,可能进入不了循环,所以要加一个判断,直接跳过他们。