zoukankan      html  css  js  c++  java
  • LeetCode

    题目:

    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.

    思路:递归。

    package recursion;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class LetterCombinationsOfAPhoneNumber {
    
        public List<String> letterCombinations(String digits) {
            String[] map = { " ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
            List<String> res = new ArrayList<String>();
            if (digits == null || digits.length() == 0) return res;
            res.add("");
            return DFS(map, res, digits, 0);
        }
        
        private List<String> DFS(String[] map, List<String> subRes, String digits, int pos) {
            if (pos == digits.length()) return subRes;
            int index = digits.charAt(pos) - '0';
            List<String> res = new ArrayList<String>();
            for (String s : subRes) {
                for (int j = 0; j < map[index].length(); ++j) {
                    res.add(s + map[index].charAt(j));
                }
            }
            
            return DFS(map, res, digits, ++pos);
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            LetterCombinationsOfAPhoneNumber l = new LetterCombinationsOfAPhoneNumber();
            System.out.println(l.letterCombinations("23"));
        }
    
    }
  • 相关阅读:
    边框的各种样式
    内容溢出显示省略号
    UNIAPP开发注意事项
    css文本的三条线 删除线 下划线 上划线
    防抖截流
    浏览器窗口改变触发的函数
    ES5数组方法
    弹性布局
    ubuntu16.04 安装adb
    git clone
  • 原文地址:https://www.cnblogs.com/null00/p/5048536.html
Copyright © 2011-2022 走看看