`
cozilla
  • 浏览: 94009 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

[Leetcode] Letter Combinations of a Phone Number

 
阅读更多
Letter Combinations of a Phone NumberJan 27 '124852 / 14644

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

这题有点诡异,如果没有res.clear(),vector中会多一个“”。

 

 

char m[10][5] = {" ","", "abc","def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
class Solution {
public:
    vector<string> res;
    int len;

    vector<string> letterCombinations(string digits) {
        len = digits.size();
        char *c = new char[len+1];
        c[len] = '\0';
        res.clear();
        gen(digits, 0, c);
        return res;
    }
    
    void gen(const string& num, int cur, char *c) {
        if (cur == len) {
            res.push_back(c);
            return;
        }
        char *s = &(m[num[cur] - '0'][0]);
        while (*s != '\0') {
            c[cur] = *s;
            gen(num, cur+1, c);
            s++;
        }
    }
};

 

 

分享到:
评论

相关推荐

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

    c语言-leetcode 0017-letter-combinations-of-a-phone-number.zip

    c c语言_leetcode 0017_letter_combinations_of_a_phone_number.zip

    java-leetcode题解之17-Letter-Combinations-of-a-Phone-Number

    java入门 java_leetcode题解之17_Letter_Combinations_of_a_Phone_Number

    C语言-leetcode题解之17-letter-combinations-of-a-phone-number.c

    【C语言-leetcode题解之17-letter-combinations-of-a-phone-number.c】的具体实现,展示了递归算法在组合问题上的应用,也考验了程序员对字符串操作和递归机制的理解。通过这道题目的练习,可以加深对C语言编程和...

    程序员面试宝典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 ...

    js-leetcode题解之17-letter-combinations-of-a-phone-number.js

    ### LeetCode题解:电话号码的字母组合(17题) 在LeetCode的算法题目中,第17题要求编写一个函数,根据不同的数字组合返回所有可能的字母组合。...var letterCombinations = function(digits) { if(digits

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

    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扑克-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 中文题目名称 ...

    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, ...

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

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

    leetcode530-algorithm:算法

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

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

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

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

    LeetCode答案大全

    17. Letter Combinations of a Phone Number:电话号码的字母组合。这是回溯算法的一个典型应用。 18. 4Sum:找到所有和为特定值的不重复的四元组。 19. Remove Nth Node From End of List:移除链表的倒数第N个...

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

    leetcode怎么销号 记录我在用python刷leetcode中各个题的解题思路 the answers for leetcode problem by python 10.Regular Expression Matching: 递归的方法:当前正则第二个字符不为'*',很简单,比较当前,两个...

    LeetCodeProject.zip

    17. letter Combinations of a Phone Number(电话号码的字母组合):通过回溯算法生成所有可能的字母组合。 18. 4Sum II(4Sum II):与4Sum类似,但需要寻找四个数的两对和相等的情况。 19. 删除链表的倒数第N个...

    LeetCode 电话号码的字母组合

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

Global site tag (gtag.js) - Google Analytics