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

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

遞迴思想,字串逐步減短

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

string A[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        return helper(res,digits);
    }
    
    
    vector<string> helper(vector<string> V, string s)
    {
        if(s=="")
        {
            return V;
        }
        else 
        {
            vector<string> ret;
            int num = s[0] - '0';
            string s1 = A[num];
			if(V.size()==0)
			{
				for(int i = 0;i<s1.size();i++)
				{
					string s = "";
					s.append(1,s1[i]);
					ret.push_back(s);
				}
			}
			else
			{
				for(int i = 0;i<V.size();i++)
				{
					for(int j = 0;j<s1.size();j++)
					{
						string t_s = V[i];
						t_s.append(1,s1[j]);
						ret.push_back(t_s);
					}
				}
			}
			return helper(ret, s.substr(1,s.size()-1));
        }
    }
    
};

相關推薦

每日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

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

轉載自部落格 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