Letter combinations of a phone number [leetcode] Two ideas about the cyclic Solution

Source: Internet
Author: User

There are many two ideas in this series of blog posts, because there is an idea when I click questions for the first time, and I forget my previous ideas when I click questions for the second time, there are new ideas.

At the same time, I also Po most of the Code to the Q & A of the corresponding question of leetcode, So if you also look at the problem discussion, you will find the same code as me, actually my PO :)

The following two methods are used to connect books to the text:


First approach

For example, for the string "234", I can first locate all the arrays of 0... 1 --> {"A", "B", "C "}

Then add 0... for all the arrays of 2, go to --> {"ad", "AE", "af", "BD", "be", "BF", "cd", "CE ", "CF "}

So loop... until the end of the string. The implementation is as follows:

vector<string> letterCombinations(string digits) {        vector<string> res;        string charmap[10] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};        res.push_back("");        for (int i = 0; i < digits.size(); i++)        {            vector<string> tempres;            string chars = charmap[digits[i] - '0'];            for (int c = 0; c < chars.size();c++)                for (int j = 0; j < res.size();j++)                    tempres.push_back(res[j]+chars[c]);            res = tempres;        }        return res;    }


Second approach

Mr. Cheng is the first feasible solution "ADG" of "234", and other solutions are derived from the feasible solution.

When the number is similar to the number, 9 is changed to 0 and carry.

Vector <string> lettercombinations (string digits) {string charmap [10] = {"0", "1", "ABC", "def", "Ghi ", "jkl", "MnO", "pqrs", "TUV", "wxyz"}; vector <string> res; string Alpha; For (INT I = 0; I <digits. size (); I ++) Alpha + = charmap [digits [I]-'0'] [0]; while (true) {res. push_back (alpha); // find the feasible solution bool find = false; For (INT I = digits. size ()-1; I >=- 1 &&! Find; I --) {if (I =-1) return res; // The End Of The traversal string chars = charmap [digits [I]-'0']; if (alpha [I] = chars [chars. size ()-1]) // traverses the last feasible solution of the number I, reset and find the feasible solution of the number I + 1 {Alpha [I] = chars [0]; continue;} For (INT C = 0; C <chars. size ()&&! Find; C ++) // traverse other feasible solutions of the number I {If (alpha [I] = chars [c]) {Alpha [I] = chars [C + 1]; find = true ;}}}}}



Letter combinations of a phone number [leetcode] Two ideas about the cyclic Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.