Written question 30. Leetcode OJ (17)

Source: Internet
Author: User

This topic is interesting, and life close, the meaning of the topic to know, it should be the full array of strings. However, it is important to note that there are few numeric characters that do not have corresponding strings, such as ' 0 ' and ' 1 ', so if they are in our input string, they are filtered out. This question my idea is recursion, because this kind of topic recursive thought clear, to use the circulation, will form the N layer circulation, therefore the circulation thought should be eliminated. I like to do this problem first to solve the "superfluous" things first, such as ' 0 ' and ' 1 ' The two numbers, I first traverse the side digits array, will ' 0 ' and ' 1 ' filter out, so in the back of the recursive can be less consideration of something, since it is desirable, paving the way forward. My code is as follows:

Class Solution {public:vector<string> Lettercombinations (string digits) {//individuals think input should not have non-numeric characters,//Enter a series of numbers, Select a constituent string from each number corresponding to the character,vector<string> last;//remove the numeric characters ' 0 ' and ' 1 ', which is not cumbersome to calculate, string newdig;for (int i = 0; i< Digits.size (); ++i) {if (digits[i] >= ' 0 ' && digits[i] <= ' 9 ' && digits[i]! = ' 0 ' && digits[i]! = ' 1 ') {//even non- Numeric characters, I'll filter him out Newdig.push_back (Digits[i]);}} Store numbers corresponding to the values vector<string> array = {"", "", "abc", "Def", "Ghi", "JKL", "MnO", "PQRS", "TUV", "WXYZ"};int len = newd Ig.size (); if (len = = 0) {return last;} for (int i = 0; i < array[newdig[0]-' 0 '].size (); ++i) {//The first character with a character number is a string tmp;tmp.push_back (array[newdig[0]-' 0 '] [ I]); ADDCH (last, TMP, array, digits, 1, len);}    return last;} recursive void Addch (vector<string> &last, string tmp, vector<string>& arr, const string &newdig, int b Egin, int end) {if (begin = = end) {last.push_back (TMP); return;} else{for (int i = 0; i < Arr[newdig[begin]-' 0 '].size (); ++i) {Tmp.push_bacK (Arr[newdig[begin]-' 0 '][i]) addch (last, TMP, arr, newdig, begin + 1, end); Tmp.pop_back ();}}}; 
The result is as follows, can know is right, but still hope everybody has good problem-solving thought can share with me, please leave your valuable opinion.



Written question 30. Leetcode OJ (17)

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.