`
frank-liu
  • 浏览: 1666032 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

原问题链接:https://leetcode.com/problems/letter-combinations-of-a-phone-number/

 

问题分析

   求给定数字对应的字符串组合,按照前面的示例,基本上是一个数字对应一个字符串。像数字1,对应的是空字符串,而数字0对应的是一个空格。因为是求一个给定数字串的组合。我们可以这样来考虑,对于若干个数字组成的串来说,首先取第一个数字,那么它就有它对应的那几个字符那几种组合。比如数字2对应的字符有"a", "b", "c" 3种。如果后面有多的字符的话,再取下一个,比如数字3,那么对应数字3的字符有“d”, "e", "f"。按照组合的关系,数字1和2对应的所有字符组合将有3乘以3= 9种。

    按照刚才的讨论,如果我们将每次组合后的结果保存起来,然后作为下一步组合拼接的起始集合,这样就可以得到若干个数字构成字符的所有组合。

    有了上述的讨论基础,我们来看怎么实现。首先一个就是要有每个数字所和所对应的字符串的关系。这里可以用一个数组来表示。数组的索引表示该数字,而对应的值表示映射的字符串。这种方式比较简单高效。在每次遍历digits里面的一个字符时,我们需要把这个字符对应的所有字符都加入到原有组合的每个元素里去。所以在这一步应该是一个二重循环,每次取一个对应的字符和原有的组合拼接起来,放到一个地方。然后再下一个,直到这几个字符组合遍历完。这些所有生成的新的结果将作为后面一个digits字符的构造基数。

    这样可以得到如下的代码:

 

public class Solution {
    public List<String> letterCombinations(String digits) {
        String[] data = new String[] { " ", "", "abc", "def", "ghi", "jkl",
            "mno", "pqrs", "tuv", "wxyz" };
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < digits.length(); i++) {
            char[] c = data[digits.charAt(i) - '0'].toCharArray();
            List<String> temp = new ArrayList<String>();
            for (int j = 0; j < c.length; j++) {
                if (result.isEmpty()) result.add("");
                for (String s : result) temp.add(s + c[j]);
            }
            result = temp;
        }
        return result;
    }
}

    总的来说,这里是一个3重循环。因为每个字符对应的字符组合是固定的若干个。所以它的总体时间复杂度是在O()N * N)的范围。

 

总结

    这里的要点是要对数字和对应的元素建立好映射。同时要想到一个合理的办法将每次循环的结果都保存起来作为下一次的起始点。另外,在开始还要避免集合是空的情况。

 

  • 大小: 24.8 KB
分享到:
评论

相关推荐

    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 possible letter combinations that the number could ...

    Leetcode扑克-leetcode:Leetcode算法实践

    Leetcode扑克 Leetcode Starting from 2020/08/02 Leetcode practice with Python (for now) Daily Challenge + Selected Questions From Using Leetcode Plugin for Solutions ID English Title 中文题目名称 ...

    leetcode338-LeetCode:LeetCode刷题总结

    LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....

    leetcode2sumc-leetcode:JavaScript版本leetcode中文版代码

    leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 英文题目 题目描述 JavaScript 难度 1 Two Sum 简单 2 Add Two Numbers 中等 3 Longest Substring Without Repeating... 中等 5...

    程序员面试宝典LeetCode刷题手册

    17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap ...

    leetcode怎么计算空间复杂度是指-LeetCode-Solution:我的第一个LeetCode解决方案

    leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 already!) 我经常在递归的结束地方忘记return!!! 题型一:经典暴力递归:(里面涉及到重复不重复的...

    leetcode怎么销号-LeetCode-Solutions:我自己的LeetCode解决方案

    leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone Number Medium 回溯、暴力 0034...

    javalruleetcode-leetcode:leetcode问题的解决方案

    java lru leetcode Leetcode 问题的解决方案 问题 解决方案 0001_Two_Sum 0002_Add_Two_Numbers 0003_Longest_Substring_Without_Repeating_Characters ...0004_Median_of_Two_...0017_Letter_Combinations_of_a_Phone_N

    leetcode分类-LeetCode-Java-Accepted:这是我的Java学习之旅

    leetcode 分类LeetCode-Java-接受 这是 Leetcode 问题的 Java 解决方案。 细节 标题和答案格式 /* * 17. Letter Combinations of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, ...

    leetcode二维数组搜索-LeetCode-Review:重写LeetCode问题

    leetcode二维数组搜索tags: Leetcode [toc] Leetcode list 一、DataStructure Array Linked_list Stack Queue BT BST Set 二、brute search 穷举(DFS) 17.Letter Combinations of a Phone Number backtracking int ...

    leetcode530-algorithm:算法

    leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** Leetcode 题目列表 005 Longest Palindromic Substring 006 ZigZag Conversion 007 Reverse Integer 008 ...

    Coding Interview In Java

    leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 ...242 Letter Combinations of a Phone Number 587 243 Restore IP Addresses 589 244 Reverse Integer 591 245 Palindrome Number 593

    leetcode怎么销号-leetcodeAns:leetcode问题的答案python

    Number: 这题是要找到号码对应字符串的所有组合。用字典来表示数字到字符串的组合。然后遍历数字串。使其对应的字母list与前面已有的组合进行 连接即可。 19.Remove Nth Node From End of List: 本题要求移除倒数第n...

    gasstationleetcode-leetcode-rust:莱特代码休息

    加油站 leetcode ...17-letter-combinations-of-a-phone-number 20 cargo run --bin 20-valid-parentheses 21 cargo run --bin 21-merge-two-sorted-lists 27 cargo run --bin 27-remove-element 28

    LeetCode 电话号码的字母组合

    题目来源:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number 题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 给出数字到字母的映射如下(与电话按键相同)。注意 1 ...

    cpp-算法精粹

    Letter Combinations of a Phone Number 广度优先搜索 Word Ladder Word Ladder II Surrounded Regions 总结 深度优先搜索 Additive Number Palindrome Partitioning Unique Paths Unique Paths II N-Queens N-...

    Dir-For-LeetCode

    017_Letter_Combinations_of_a_Phone_Number 018_4总和 019_Remove_Nth_Node_From_End_of_List 020_Valid_Parentheses 021_Merge_Two_Sorted_Lists 022_Generate_Parentheses 023_Merge_k_Sorted_Lists 024_...

Global site tag (gtag.js) - Google Analytics