Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路

本系列博文中有很多两种思路的,其实是因为第一遍刷题的时候有一个想法,第二遍刷题的时候已经忘掉之前的思路了,又有新的想法了。

同时大部分代码我也同时PO到leetcode的对应题目的问答中去了,所以如果你也查看问题讨论的话会发现有和我一模一样的代码,其实就是我PO的:)

书接正文,基于循环的两种思路如下:

第一种思路

比如“234”这个字符串,我可以先将0...1的所有排列找到-->{"a", "b", "c"}

再进一步将0...2的所有排列找到-->{"ad", "ae","af", "bd", "be", "bf", "cd", "ce", "cf"}

如此循环...直到字符串末尾。实现如下

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;
    }

第二种思路

先生成"234"的第一个可行解"adg",再在可行解上衍生出其他解

类似数数的时候遇到9就变成0并进位

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);
			//寻找可行解
			bool find = false;
			for (int i = digits.size() - 1; i >= -1 && ! find; i--)
			{
				if (i == -1)	return res;//遍历结束
				string chars = charmap[digits[i] - '0'];
				if (alpha[i] == chars[chars.size() - 1])//遍历第i个数字的最后一个可行解,重置并寻找第i+1个数字的可行解
				{
					alpha[i] = chars[0];
					continue;
				}
				for (int c = 0; c < chars.size() && ! find; c++)//遍历第i个数字的其他可行解
				{
				    if (alpha[i] == chars[c])
				    {
				        alpha[i] = chars[c+1];
				        find = true;
				    }
				}
			}
		}
	}
时间: 2024-10-21 12:02:37

Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路的相关文章

Letter Combinations of a Phone Number leetcode java

题目: 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"

[LeetCode] Maximum Product Subarray的两种思路

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这题和之前的一题Maximum Subarray非常类似,一个是求最大和,而这个是

Letter Combinations of a Phone Number -- leetcode

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", &q

Letter Combinations of a Phone Number——LeetCode

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", &q

Letter Combinations of a Phone Number(带for循环的DFS,递归总结)

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", &q

Submission Details [leetcode] ---- inplace 线性时间 的两种思路

两种思路都利用了输入的数组A,若A中存在i,则给A[i]作为标记. 因为A中的n个元素存在>n和<=0的,所以第一个缺失的正整数一定在[1-n+1]之间. 第一种思路是将标记设为一个特定的数.因为改变数值会影响该位置原来存的值,所以需要在一个循环里依次处理所有"原来的值". 例如数组为{2,3,4,1}.对第一个数2,我们将位置(2-1)=1标记为-MAX_INT,数组变为{2,-MAX_INT,4,1},丢失了3,所以应记录下数组原来的值,并继续将位置(3-1)=2标记为

LeetCode: Letter Combinations of a Phone Number [018]

[题目] 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"

【Leetcode长征系列】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"

[LeetCode][JavaScript]Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 40709 Total Submissions: 157759My Submissions Question Solution Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just l