[LeetCode]17.Letter Combinations of a Phone Number

2024-02-19 02:08

本文主要是介绍[LeetCode]17.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.

【分析】

【代码】

/*********************************
*   日期:2015-01-22
*   作者:SJF0115
*   题目: 17.Letter Combinations of a Phone Number
*   网址:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <vector>
using namespace std;class Solution {
public:vector<string> letterCombinations(string digits) {vector<string> vec;if(digits.length() <= 0){vec.push_back(digits);return vec;}//ifvector<char> number;DFS(vec,digits,number);return vec;}
private:void DFS(vector<string> &vec,string digits,vector<char> &number){string letters[] = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};// 一种组合完成int curLen = number.size();if(curLen == digits.length()){string letter;for(int i = 0;i < curLen;++i){letter += number[i];}//forvec.push_back(letter);return;}//if// 数字int num = digits[curLen] - '0';// 数字所对应的字母长度int len = letters[num].length();for(int i = 0;i < len;++i){number.push_back(letters[num][i]);DFS(vec,digits,number);number.pop_back();}//for}
};int main(){Solution solution;string number = "2";vector<string> result = solution.letterCombinations(number);// 输出for(int i = 0;i < result.size();++i){cout<<result[i]<<endl;}//forreturn 0;
}


【代码二】

class Solution {
public:vector<string> letterCombinations(string digits) {vector<string> result;DFS(digits,0,"",result);return result;}
private:void DFS(string digits,int cur,string path,vector<string> &result){string keyboard[] = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};if(cur == digits.length()){result.push_back(path);return;}//ifint len = keyboard[digits[cur] - '0'].length();for(int i = 0;i < len;++i){char c = keyboard[digits[cur] - '0'][i];DFS(digits,cur + 1,path + c,result);}//for}
};



注意一种特殊情况:



这篇关于[LeetCode]17.Letter Combinations of a Phone Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/723181

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

usaco 1.2 Name That Number(数字字母转化)

巧妙的利用code[b[0]-'A'] 将字符ABC...Z转换为数字 需要注意的是重新开一个数组 c [ ] 存储字符串 应人为的在末尾附上 ‘ \ 0 ’ 详见代码: /*ID: who jayLANG: C++TASK: namenum*/#include<stdio.h>#include<string.h>int main(){FILE *fin = fopen (

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

题目1380:lucky number

题目1380:lucky number 时间限制:3 秒 内存限制:3 兆 特殊判题:否 提交:2839 解决:300 题目描述: 每个人有自己的lucky number,小A也一样。不过他的lucky number定义不一样。他认为一个序列中某些数出现的次数为n的话,都是他的lucky number。但是,现在这个序列很大,他无法快速找到所有lucky number。既然

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

cell phone teardown 手机拆卸

tweezer 镊子 screwdriver 螺丝刀 opening tool 开口工具 repair 修理 battery 电池 rear panel 后盖 front and rear cameras 前后摄像头 volume button board 音量键线路板 headphone jack 耳机孔 a cracked screen 破裂屏 otherwise non-functiona

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param