【JAVA、C++】LeetCode 017 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.
解题思路:
思路一:
使用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的更多相关文章
- 【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 ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【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 ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- CSS3媒体查询
随着响应式设计模型的诞生,Web网站又要发生翻天腹地的改革浪潮,可能有些人会觉得在国内IE6用户居高不下的情况下,这些新的技术还不会广泛的蔓延下去,那你就错了,如今淘宝,凡客,携程等等公司都已经在大胆 ...
- Yii2修改默认布局
public $layout = 'layout';//在类中定义一个变量,名为$layout的php文件 <?php echo $content; ?>
- JAVA的整型与字符串相互转换
1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([S ...
- 采用get的方式提交数据到服务器
1 效果演示:
- jquery------提供灵活的方法参数
index.jsp <h1 >再次重逢的世界</h1> my.js $(document).ready(function(){ (function($){ $.fn.shado ...
- 修复VirtualBox "This kernel requires the following features not present on the CPU: pae Unable to boot
问题描述: 1.机器:Linux主机,特别是主机为大内存,比如: 4G内存的使用pae内核的Ubuntu系统的dell电脑. 2.情况:使用VirtualBox安装Linux系统时,比如:通过Virt ...
- mvc 简单笔记
---恢复内容开始--- 入口文件 index.php 唯一的一个让浏览器直接请求的脚本文件 控制器 协调模型和试图 模型 提供数据 保存数据 数据的验证 试图 只负责显示 <?php $c = ...
- Bookmarklet
学习 http://www.ruanyifeng.com/blog/2011/06/a_guide_for_writing_bookmarklet.html
- 检测端口状态的python脚本
#!/usr/bin/env python import os,subprocess,socket,time,sys from urllib import urlencode from socket ...
- mysqli 操作数据库(转)
从php5.0开始增加mysql(i)支持 , 新加的功能都以对象的形式添加 i表示改进的意思 功能多.效率高.稳定 编译时参数: ./configure --with-mysql=/usr/bin/ ...