Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the letter combination of phone numbers in C++

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how C++ realizes the letter combination of phone numbers, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that after reading this C++ article on how to achieve the letter combination of phone numbers, we will gain something. Let's take a look.

Letter combination of Letter Combinations of a Phone Number phone number

Given a string containing digits from 2-9inclusive, 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".

Note:

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

This question allows us to find the letter combination of telephone numbers, that is, each number in the numbers 2 to 9 can represent several letters, and then give a string of numbers to find all possible combinations. Here you can use recursive Recursion to solve the problem, you need to set up a dictionary to store the string represented by each number, and then you need a variable level to record the number of characters in the currently generated string, which is very similar to the above problems. In the recursive function, the level is judged first, and if the number of numbers in the digits is equal, the current combination is added to the result res, and then returned. We extract the string from the dict through the numbers in digits, then iterate through the extracted string, add each character to the current combination, and call the recursive function, as shown in the code below:

Solution 1:

Class Solution {public: vector letterCombinations (string digits) {if (digits.empty ()) return {}; vector res; vector dict {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; letterCombinationsDFS (digits, dict, 0, ", res); return res } void letterCombinationsDFS (string& digits, vector& dict, int level, string out, vector& res) {if (level = = digits.size ()) {res.push_back (out); return;} string str = digitals [level]-"0"]; for (int I = 0; I < str.size (); + + I) {letterCombinationsDFS (digits, dict, level + 1, out + str [I], res) }}}

This problem can also be solved by iterative Iterative. When traversing all the numbers in digits, first establish a temporary string array t, and then, as in the above solution, extract the string str from the dict through the number, then traverse all the characters in the string, and then traverse each string in the current result res, add the characters to the end, and add the temporary string array t. After traversing the extracted string str, assign the temporary string array to the result res. For more information, please see the following code:

Solution 2:

Class Solution {public: vector letterCombinations (string digits) {if (digits.empty ()) return {}; vector res {""}; vector dict {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; for (int I = 0; I < digits.size (); + + I) {vector t String str = str [I]-"0"]; for (int j = 0; j < str.size (); + + j) {for (string s: res) t.push_back (s + str [j]);} res = t;} return res;}} This is the end of the article on "how to realize the letter combination of phone numbers in C++". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve the letter combination of phone numbers in C++". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 288

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report