1. 程式人生 > >LeetCode第17題 電話號碼的字母組合

LeetCode第17題 電話號碼的字母組合

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string>ans;
        if(digits.size() == 0)return ans;
        int len = digits.size();
        string str[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        ans.push_back("");
        for(int i=0; i<digits.size(); i++){
            int len1 = str[digits[i] - '2'].size();
            vector<string>temp;
            for(int j=0;j<len1;j++){
                for(int k=0;k<ans.size();k++){
                    temp.push_back(ans[k] + str[digits[i] - '2'].at(j));
                }
            }
            ans = temp;
        }
        return ans;
    }
};

相關推薦

LeetCode17 電話號碼字母組合

class Solution { public:     vector<string> letterCombinations(string digits) {         vector<string>ans;         if(digits.s

LeetCode 17電話號碼字母組合

i++ color link 17. ear else return void arr /*17. 電話號碼的字母組合給定一個僅包含數字 2-9 的字符串,返回所有它能表示的字母組合。給出數字到字母的映射如下(與電話按鍵相同)。註意 1 不對應任何字母。輸入:"23"輸出:

LeetCode17)—— 電話號碼字母組合

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

Leetcode演算法——17電話號碼的字元組合

題目 給定一個字串 digits,包含了2~9的整數,要求返回所有可能的數字對應的字母組合。 每一個數字都對應一些字母,和電話撥號器相對應,如下: 1 2abc 3def 4ghi 5jkl 6mno 7pqrs 8tuv 9wxyz

leetcode17——**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 li

LeetCode 17電話號碼字母組合 [DFS入門:暴力列舉所有情況]

17. 電話號碼的字母組合 給定一個僅包含數字 2-9 的字串,返回所有它能表示的字母組合。 給出數字到字母的對映如下(與電話按鍵相同)。注意 1 不對應任何字母。 示例: 輸入:“23” 輸出:[“

LeetCode演算法17電話號碼字母組合解析

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

Leetcode 17電話號碼字母組合(最詳細的解法!!!)

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

LeetCode 49 字母異位詞分組

兩個 add arraylist 等於 說明 順序 外部 字符串排序 sort (一) 題目描述     給定一個字符串數組,將字母異位詞組合在一起。字母異位詞指字母相同,但排列不同的字符串。   示例:   輸入: ["eat", "tea", "tan", "ate",

leetcode39組合綜合

ret sum solver append else end 包含 color 排序 給定一個無重復元素的數組 candidates 和一個目標數 target ,找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無

LeetCode 3 (Longest Substring Without Repeating Characters)

sts mod while find his 代碼 key 不存在 簡單 LeetCode 第 3 題(Longest Substring Without Repeating Characters) Given a string, find th

LeetCode 342 (Power of Four)

-m clas script pri ack ber 要求 case ret LeetCode 第 342 題(Power of Four) Given an integer (signed 32 bits), write a function

LeetCode 231 (Power of Two)

i++ ret write data mark hid article views pop LeetCode 第 231 題 (Power of Two) Given an integer, write a function to determi

小米2017秋招真——電話號碼分身問題(Java版)

imp spa ray 描述 mov lose port next can 原題描述如下: 通過對各個數字對應的英文單詞的分析,可以發現一些規律: 字母Z為0獨占,字母W為2獨占,字母U為4獨占,字母X為6獨占,字母G為8獨占; 在過濾一遍0、2、4、6、8後,字母O為1

Leetcode110 思路分析及C++實現

mine long -c 一行 而且 特殊 頭部 根據 shard 筆者按照目錄刷題,對於每一道題,力爭使用效率最高(時間復雜度最低)的算法,並全部通過C++代碼實現AC。(文中計算的復雜度都是最壞情況復雜度) 因為考慮到大部分讀者已經在Leetcode瀏覽過題目了,所以

leetcode32:最長有效括號(遇到一個奇葩的錯誤)

問題描述: 給一個只包含 '(' 和 ')' 的字串,找出最長的有效(正確關閉)括號子串的長度。 對於 "(()",最長有效括號子串為 "()" ,它的長度是 2。 另一個例子 ")()())",最長有效括號子

合併兩個有序的連結串列(LeetCode21

方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None clas

有效的括號(Leetcode20

方法一: class Solution: def isValid(self, s): """ :type s: str :rtype: bool """ if (len(s) % 2

LeetCode448 Find All Numbers Disappeared in an Array解決辦法

    題目 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear

leetcode

採用動態規劃的方法去做,核心思想如下: "滑動視窗"      比方說 abcabccc 當你右邊掃描到abca的時候你得把第一個a刪掉得到bca,     然後"視窗"繼續向右滑動,每當加到一個