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.

解题思路:

思路一:

使用DFS算法,JAVA实现如下:

	    static String[] alpha = new String[] {
" ","1", "abc", "def","ghi", "jkl", "mno","pqrs", "tuv", "wxyz"
};
static StringBuilder sb = new StringBuilder();
static void dfs(List<String> list, String digits, int cur) {
if (cur >= digits.length())
list.add(sb.toString());
else {
for (int i = 0; i < alpha[digits.charAt(cur) - '0'].length(); i++) {
sb.append(alpha[digits.charAt(cur) - '0'].charAt(i));
dfs(list, digits, cur + 1);
sb.deleteCharAt(sb.length() - 1);
}
}
}
static public List<String> letterCombinations(String digits) {
List<String> list = new ArrayList<String>();
if (digits.length()==0)
  return list;
dfs(list, digits, 0);
return list;
}

C++:

 class Solution {
public:
const string alpha[] = {" ","", "abc", "def","ghi", "jkl", "mno","pqrs", "tuv", "wxyz"};
void dfs(vector<string> &list, string &digits, int cur,string sb) {
if (cur >= digits.length())
list.push_back(sb);
else {
for (char a : alpha[digits[cur] - '']) {
sb.push_back(a);
dfs(list, digits, cur + ,sb);
sb.pop_back();
}
}
}
vector<string> letterCombinations(string digits) {
vector<string> list;
if (digits.length() == )
return list;
dfs(list, digits, ,"");
return list;
}
};

思路二:

凡是用到递归的地方都能用循环解决,因此可以用循环算法,JAVA实现如下:

static public List<String> letterCombinations(String digits) {
List<String> list = new ArrayList<String>();
String[] alpha = new String[] {
" ","1", "abc", "def","ghi", "jkl", "mno","pqrs", "tuv", "wxyz"
};
if (digits.length()==0)
return list;
int[] number = new int[digits.length()];//存储每次遍历字符位置
int index = 0;
while(index>=0) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<digits.length(); i++)
sb.append(alpha[digits.charAt(i)-'0'].charAt(number[i]));
list.add(sb.toString());
// 每回合需要重置index到末尾
index = digits.length()-1;
while(index>=0) {
if( number[index] < (alpha[digits.charAt(index)-'0'].length()-1) ) {
number[index]++;
break;
} else {
number[index] = 0;
index--;
}
}
}
return list;
}

【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number的更多相关文章

  1. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  7. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  8. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 【HDU 5105】Math Problem

    题意 f(x)=|ax3+bx2+cx+d| 求f(x)在L≤x≤R的最大值. 分析 参数有可能是0,注意分类讨论 1.当a=0时 b=0,f为一次函数(c≠0)或者常数函数(c=0),最大值点在区间 ...

  2. SpringMVC数据库链接池,以及其他相关配置

    1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  3. 通知栏Notification的学习

    转:http://blog.csdn.net/yczz/article/details/28416893 在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理 ...

  4. JS 在线网站

    JS 在线压缩网站 国内压缩js网站http://tool.css-js.com/ uglifyjs 压缩js网站http://lisperator.net/uglifyjs JS在线格式化网站 ht ...

  5. 网络html查看器

    1)演示效果:

  6. _stdcall与_cdecl(了解)

    调用约定(Calling Convention)是指在程序设计语言中为了实现函数调用而建立的一种协议.这种协议规定了该语言的函数中的参数传送方式.参数是否可变和由谁来处理堆栈等问题.不同的语言定义了不 ...

  7. CodeForces 701B Cells Not Under Attack

    题目链接:http://codeforces.com/problemset/problem/701/B 题目大意: 输入一个数n,m, 生成n*n的矩阵,用户输入m个点的位置,该点会影响该行和该列,每 ...

  8. php 非缓冲查询

    最近在开发一个PHP程序时遇到了下面的错误: PHP Fatal error: Allowed memory size of 268 435 456 bytes exhausted 错误信息显示允许的 ...

  9. Javascript输出表格

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. PHP初学者必须掌握的10个知识点

    这里总结了PHP初学者容易感到困惑的10个问题,供大家参考.1.页面之间无法传递变量get,post,session在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用1 ...