zoukankan      html  css  js  c++  java
  • LeetCode_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"].
    

      水题: DFS

    int counts[] = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4};  
    char letter[] = {'0', '0', 'a', 'd', 'g', 'j', 'm', 'p', 't', 'w'};  
    
    class Solution {
    public:
         void    DFS( string & digit,int i, string s)
         {
            if(i == size){
                result.push_back(s);
                return ;
            }
            int index = digit[i] - '0' ;
        
            for(int j = 0; j < counts[index] ; j++)
            {                        
                            
                char c = letter[index]+j ;
                          
                DFS(digit, i+1, s+c);    
                                               
            }
    
        }
        vector<string> letterCombinations(string digits) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            result.clear();
            size = digits.size();
    
            
            string s = "";
            DFS(digits, 0 , s);
            
            return result ;
            
        }
    private :
    vector<string> result ;
    int size ;
    };
  • 相关阅读:
    JavaSE-面向对象
    JavaSE-数组
    JavaSE-方法
    JavaSE-流程控制
    JavaSE-基础语法
    Java-初识Java
    PTH的几种食用姿势
    CVE-2020-1472 Zerologon
    OpenGL 术语
    使用IDEA创建SpringMVC项目
  • 原文地址:https://www.cnblogs.com/graph/p/3213036.html
Copyright © 2011-2022 走看看