zoukankan      html  css  js  c++  java
  • 17. Letter Combinations of a Phone Number 17.*的字母组合

    Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

    Example:

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

    思路:index不用for循环,直接在内部+1就行了


    currentString + chars[i]这种写法比sb.append更加简洁一些


    退出的条件是由index+1得出来的
    if (index == digits.length())
    
    
    

    也不知道为啥dfs函数一定要返回什么东西……

    
    
    class Solution {
        private final String[] keys = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        
        public List<String> letterCombinations(String digits) {
            //cc
            List<String> results = new ArrayList<String>();
            if (digits == null || digits.length() == 0)
                return results;
            
            //dfs
            dfs(digits, 0, "", results);
            
            //return 
            return results;
        }
        
        public List<String> dfs(String digits, 
                                int index, String currentString,
                                List<String> results) {
            //exit
            if (index == digits.length()) {
                results.add(currentString);
                return results;
            }
            
            //for loop
            char[] chars = keys[digits.charAt(index) - '0'].toCharArray();
            for (int i = 0; i < chars.length; i++) {
                dfs(digits, index + 1, currentString + chars[i], results);
            }
            
            return results;
        }
    }
    View Code
    
    
    


  • 相关阅读:
    Modelsim仿真笔记
    RTP协议
    SHFileOperation使用
    浅谈基于IP网络的H.264关键技术及应用
    jrtplib在windows下的编译步骤
    DEBUG AND RELEASE
    CTree 使用详解 转:
    VC中给树形控件的图标加上工具提示
    MFC六大关键技术之(二)——运行时类型识别(RTTI)
    Linux下编译jrtplib和jthread:
  • 原文地址:https://www.cnblogs.com/immiao0319/p/13594142.html
Copyright © 2011-2022 走看看