leetcode 017 —— Letter Combinations of a Phone Number

原创 2015年07月09日 10:33:20

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.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

思路:简单的回溯法

class Solution {
public:
	vector<string> letterCombinations(string digits) {
		vector<string> table={"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
		vector<string> res;
		if (digits.empty()) return res;
		scan(0, "", digits, table, res);
		return res;

	}
	void scan(int level, string path, string &digits, vector<string> &table,vector<string> &res){
		if (level == digits.size()){
			res.push_back(path);
			return;
		}
		for (int i = 0; i < table[digits[level] - '0'].size(); i++){
			path.push_back(table[digits[level] - '0'][i]);
			scan(level + 1, path, digits, table, res);
			path.pop_back();
		}
	}
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode-面试算法经典-Java实现】【017-Letter Combinations of a Phone Number (电话号码上的单词组合)】

【017-Letter Combinations of a Phone Number (电话号码上的单词组合)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given ...

[LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)

[LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 15:47
  • 2619

leetcode之路017 Letter Combinations of a Phone Number

题目大意:给定一个数字的字符串,返回所有的有数字代表的字母组合。 例: Input:Digit string "23" Output: ["ad", "ae", "af", "bd", &quo...

LeetCode 017. Letter Combinations of a Phone Number

Letter Combinations of a Phone Number  Total Accepted: 12594 Total Submissions: 48534My Submissions...

LeetCode017 Letter Combinations of a Phone Number

详细见:leetcode.com/problems/letter-combinations-of-a-phone-number/ Java Solution: github packa...

LeetCode刷题(C++)——Letter Combinations of a Phone Number(Medium)

Given a digit string, return all possible letter combinations that the number could represent. A ...

Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路

本系列博文中有很多两种思路的,其实是因为第一遍刷题的时候有一个想法,第二遍刷题的时候已经忘掉之前的思路了,又有新的想法了。 同时大部分代码我也同时PO到leetcode的对应题目的问答中去了,所以如...

Letter Combinations of a Phone Number - Leetcode

题意:Given a digit string, return all possible letter combinations that the number could represent. ...

[leetcode 18] Letter Combinations of a Phone Number

题目: Given a digit string, return all possible letter combinations that the number could represe...

LeetCode Letter Combinations of a Phone Number

题目: Given a digit string, return all possible letter combinations that the number could represe...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)