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. 详解Java中ArrayList、Vector、LinkedList三者的异同点

    转载:https://my.oschina.net/zzw922cn/blog/491631 一.ArrayList ArrayList是一个可以处理变长数组的类型,这里不局限于"数&quo ...

  2. Apple的App Analytics统计平台你必须知道的Q&A整理与翻译

    Apple的App Analytics统计平台你必须知道的Q&A整理与翻译 Apple最近在iTunesConnect里最新发布了App Analytics统计平台,提供了现有友盟统计平台和自 ...

  3. FOJProblem 2214 Knapsack problem(01背包+变性思维)

    http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4    Submit: 6Time Limit: 3000 mSec    Memory Lim ...

  4. java中抽象类与接口中方法访问修饰符问题 (

    1.抽象类中的抽象方法(其前有abstract修饰)不能用private.static.synchronized.native访问修饰符修饰.原 因如下:抽象方法没有方法体,是用来被继承的,所以不能用 ...

  5. CSS小记

    1.元素居中 (1)水平居中:指定宽度,然后margin auto 即可 .middle{ max-width:400px; //width:400px;//当浏览器被缩小,宽度小于元素宽度时,元素会 ...

  6. windows2003安全加固脚本

    @echo off title= Windwos/index.html' target='_blank'>Windows Security echo. echo **************** ...

  7. xss跨站攻击测试代码

    '><script>alert(document.cookie)</script> ='><script>alert(document.cookie)& ...

  8. MySql开启事务

    CREATE PROCEDURE test_sp1( ) BEGIN ; ; START TRANSACTION; INSERT INTO test VALUES(NULL, 'test sql 00 ...

  9. EDdb 是ED数据

    eddb  是ED数据统计汇总软件的简称,用于统计汇总企事业单位的各类信息数据. 采用Excel界面,操作简单. 对各类信息数据,均可以自定义数据格式,通过internet联网,收集各类信息数据,并通 ...

  10. Quartz.net开源作业调度框架使用详解(转)

    前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron-like表达式,集群,数据库.功能性能强大更不 ...