• 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"].

    思路:知道要加字母,但是不知道具体的怎么加。

    String candidate = candidates.get(sb.length());把一个字母里的元素拿出来是关键。当前长度,可以取出最后一位,好吧。

    
    

    其实回溯法的backtrace函数里面不用写cc了,主函数写了就行了。backtrace函数里面只写退出函数就行了,这道题是sb的长度等于 

    if (digits == null || digits.length() == 0) {
    return new ArrayList();
    }
    cc直接返回一个新的就行了。先不用新建,新建会指定成string的类型。

    sb的删除要用:deleteCharAt

    backtrace里面也是sb,因为已经append了

    Your input
    "23"
    stdout
    candidates = [abc, def]
    candidate = abc
      
    candidate.charAt(i) = a
    candidates = [abc, def]
    candidate = def
      
    candidate.charAt(i) = d
    candidate.charAt(i) = e
    candidate.charAt(i) = f
    candidate.charAt(i) = b
    candidates = [abc, def]
    candidate = def
      
    candidate.charAt(i) = d
    candidate.charAt(i) = e
    candidate.charAt(i) = f
    candidate.charAt(i) = c
    candidates = [abc, def]
    candidate = def
      
    candidate.charAt(i) = d
    candidate.charAt(i) = e
    candidate.charAt(i) = f
    
    Output
    ["ad","ae","af","bd","be","bf","cd","ce","cf"]
    Expected
    ["ad","ae","af","bd","be","bf","cd","ce","cf"]
    class Solution {
        private String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        
        public List<String> letterCombinations(String digits) {
            //cc
            List<String> candidates = new ArrayList<>();
            if (digits == null || digits.length() == 0) {
                return new ArrayList();
            }
            
            for (int i = 0; i < digits.length(); i++) {
                candidates.add(KEYS[digits.charAt(i) - '0']);
            }
            
            List<String> results = new ArrayList<>();
            backtrace(candidates, new StringBuilder(), results);
            
            return results;
        }
        
        public void backtrace(List<String> candidates, StringBuilder sb, List<String> results) {
            //exit
            if (sb.length() == candidates.size()) {
                results.add(sb.toString());
                return ;
            }
            
            String candidate = candidates.get(sb.length());
            for (int i = 0; i < candidate.length(); i++) {
                sb.append(candidate.charAt(i));
                backtrace(candidates, sb, results);
                sb.deleteCharAt(sb.length() - 1);
            }
        }
    }
    View Code
  • 相关阅读:
    分割回文串(力扣第131题)
    子集 II(力扣第91题)
    子集(力扣第78题)
    组合总和 III(力扣第216题)
    JavaWeb部分 (前序)
    JavaSE部分 (IO流下)
    JavaSE部分 (IO流上)
    JavaSE部分 (File类)
    Leetcode 08.04 幂集 回溯与位图
    Leetcode 1405 最长快乐字符串 穷举与贪心
  • 原文地址:https://www.cnblogs.com/immiao0319/p/13334460.html
Copyright © 2020-2023  润新知