1. 程式人生 > >LeetCode(17)--Letter Combinations of a Phone Number

LeetCode(17)--Letter Combinations of a Phone Number

轉載自部落格

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.

Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

列舉所有情況。

對於每一個輸入數字,對於已有的排列中每一個字串,分別加入該數字所代表的每一個字元。

所有是三重for迴圈。

舉例:
初始化排列{“”}
1、輸入2,代表”abc”
已有排列中只有字串”“,所以得到{“a”,”b”,”c”}
2、輸入3,代表”def”
(1)對於排列中的首元素”a”,刪除”a”,並分別加入’d’,’e’,’f’,得到{“b”,”c”,”ad”,”ae”,”af”}
(2)對於排列中的首元素”b”,刪除”b”,並分別加入’d’,’e’,’f’,得到{“c”,”ad”,”ae”,”af”,”bd”,”be”,”bf”}
(3)對於排列中的首元素”c”,刪除”c”,並分別加入’d’,’e’,’f’,得到{“ad”,”ae”,”af”,”bd”,”be”,”bf”,”cd”,”ce”,”cf”}

注意
(1)每次新增新字母時,應該先取出現有ret當前的size(),而不是每次都在迴圈中呼叫ret.size(),因為ret.size()是不斷增長的。
(2)刪除vector首元素程式碼為:

ret.erase(ret.begin());

實現程式碼如下:

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> ret;
        if(digits == "")
            return
ret; ret.push_back(""); vector<string> dict(10); //0~9 dict[2] = "abc"; dict[3] = "def"; dict[4] = "ghi"; dict[5] = "jkl"; dict[6] = "mno"; dict[7] = "pqrs"; dict[8] = "tuv"; dict[9] = "wxyz"; for(int i = 0; i < digits.size(); i ++) { int size = ret.size(); for(int j = 0; j < size; j ++) { string cur = ret[0]; ret.erase(ret.begin()); for(int k = 0; k < dict[digits[i]-'0'].size(); k ++) { ret.push_back(cur + dict[digits[i]-'0'][k]); } } } return ret; } };

相關推薦

Leetcode 17. Letter Combinations of a Phone number

res bsp self. col join lee num nat leetcode 求給出的數字串,如果按照電話鍵盤的編譯方式,可以給出多少那些對應的數字組合。例如: Input:Digit string "23" Output: ["ad", "ae", "af"

[leetcode]17. Letter Combinations of a Phone Number手機鍵盤的字母組合

poi clu char evel 個數字 The let alt input Given a string containing digits from 2-9 inclusive, return all possible letter combinations that

leetcode 17-Letter Combinations of a Phone Number(medium)

shm put note 下標 維護 nta style == emp Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the

leetcode#17. Letter Combinations of a Phone Number

commons 註意 構建 media ber str http wiki pan 給定一個僅包含數字 2-9 的字符串,返回所有它能表示的字母組合。 給出數字到字母的映射如下(與電話按鍵相同)。註意 1 不對應任何字母。 示例: 輸入:"23" 輸出:["ad", "

LeetCode(17)-Letter Combinations of a Phone Number

17.Letter Combinations of a Phone Number Given a string containing digits from 2-9 inclusive, return all possible letter combinations that

Leetcode -17. Letter Combinations of a Phone Number

這個問題我最開始的想的是使用樹的結構去做,之後用python實現的時候是使用了幾個for迴圈: class Solution: def letterCombinations(self, digits): """ :type digits: str

[leetcode]17. Letter Combinations of a Phone Number

激動人心啊,終於能以這種操作ac了 第一遍submit忘記處理空,第二遍就ac了 Runtime: 2 ms, faster than 91.37% of Java online submissions for Letter Combinations of a Phone Num

LeetCode 17.Letter Combinations of a Phone Number (電話號碼的字母組合)

題目描述: 給定一個僅包含數字 2-9 的字串,返回所有它能表示的字母組合。 給出數字到字母的對映如下(與電話按鍵相同)。注意 1 不對應任何字母。 示例: 輸入:"23" 輸出:["ad", "ae", "af", "bd", "be", "bf", "cd

LeetCode——17. Letter Combinations of a Phone Number

一.題目連結:   https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 二.題目大意:   給定一段數字字串,其中每個數字字元對應瞭如下的字母字元,求出輸入字串對應的所有可能的字母字串集合。      例如,輸入數

Leetcode 17 Letter Combinations of a Phone Number

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping o

#Leetcode# 17. Letter Combinations of a Phone Number

https://leetcode.com/problems/letter-combinations-of-a-phone-number/   Given a string containing digits from 2-9 inclusive, return all pos

回溯Leetcode 17 Letter Combinations of a Phone Number

Leetcode 17 Letter Combinations of a Phone Number Problem Description: 電話上每個數字都有對應的字母,如下圖所示,數字0和數字1沒有對應的字母,數字2對應的字母包括“abc”,現輸入一串數字要求輸出所

演算法練習--LeetCode--17. Letter Combinations of a Phone Number

Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, return all possible letter combinatio

LeetCode 17Letter Combinations of a Phone Number(電話號碼的字母組合)

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A map

每日leetcode--(17) Letter Combinations of a Phone Number

遞迴思想,字串逐步減短 #include <iostream> #include <vector> #include <algorithm> #include <math.h> #include <stdio.h&g

LeetCode(17)--Letter Combinations of a Phone Number

轉載自部落格 Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could

leetCode 17.Letter Combinations of a Phone Number(電話數字對應的字母組合) 解題思路和方法

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A m

leetcode --17. Letter Combinations of a Phone Number

題目:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 程式碼: class Solution { public: vector<string>

LeetCode17. Letter Combinations of a Phone Number(Medium)

class stat def es2017 不同的 進行 先進先出 ati tps 1. 原題鏈接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 題目要

LeetCode17. Letter Combinations of a Phone Number - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given a string containing digits from 2-9 inclusive, return all possible