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. 用php生成数据字典

    <?php header("Content-type: text/html; charset=utf-8"); $dbserver = "localhost&quo ...

  2. bzoj 1193 贪心

    如果两点的曼哈顿距离在一定范围内时我们直接暴力搜索就可以得到答案,那么开始贪心的跳,判断两点横纵坐标的差值,差值大的方向条2,小的条1,不断做,直到曼哈顿距离较小时可以暴力求解. 备注:开始想的是确定 ...

  3. web.xml中/与/*的区别

    1.拦截"/",可以实现现在很流行的REST风格.很多互联网类型的应用很喜欢这种风格的URL.为了实现REST风格,拦截了所有的请求.同时对*.js,*.jpg等静态文件的访问也就 ...

  4. 【poj1201】 Intervals

    http://poj.org/problem?id=1201 (题目链接) 题意 给出n个区间${[ai,bi]}$,要求选出尽可能少的数,使得每个区间i中至少存在${c[i]}$个数. Soluti ...

  5. MySQL的表分区详解

    这篇文章主要介绍了MySQL的表分区,例如什么是表分区.为什么要对表进行分区.表分区的4种类型详解等,需要的朋友可以参考下 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysq ...

  6. 深入了解A*

    一.前言 在这里我将对A*算法的实际应用进行一定的探讨,并且举一个有关A*算法在最短路径搜索的例子.值得注意的是这里并不对A*的基本的概念作介绍,如果你还对A*算法不清楚的话,请看姊妹篇<初识A ...

  7. Linux下SVN安装配置和使用中遇到的问题

    两个命令: svn info :显示版本库信息,svn的下载url等. svn co https://xxxxx/xxx   wodemulu   (通过我的目录制定co的文件夹) svn st:显示 ...

  8. Mongodb for C# 分组查询

    #region 排序获取集合 static List<BsonDocument> GetPagerWithGroup(string connectionString, string dat ...

  9. 在Sublime Text3 开发Node.js遇到的一个小问题

    原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 以前的Sublime Text 2包管理出现问题了,不能安装新包,让人开发很捉急,今天装了个3,这个问题解决了 那我们就 ...

  10. cf.295.B Two Buttons (bfs)

     Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...