leetcode第17题

简介: 假如是 "23" ,那么第 1 次 for 循环结束后变为 a, b, c;第 2 次 for 循环的第 1 次 while 循环 a 出队,分别加上 d e f 然后入队,就变成 b c ad ae af第 2 次 for 循环的第 2 次 while 循环 b 出队,分别加上 d e f 然后入队,就变成 c ad ae af bd be bf第 2 次 for 循环的第 3 次 while 循环 c 出队,分别加上 d e f 然后入队,就变成 ad ae af bd be bf cd ce cf这样的话队列的元素长度再也没有等于 1 的了就出了 while 循环。

image.png

top17

给一串数字,每个数可以代表数字键下的几个字母,返回这些数字下的字母的所有组成可能。

解法一 定义相乘

自己想了用迭代,用递归,都理不清楚,灵机一动,想出了这个算法。

把字符串 "23" 看成 ["a","b",c] * ["d","e","f"] ,而相乘就用两个 for 循环实现即可,看代码应该就明白了。

publicList<String>letterCombinations(Stringdigits) {
List<String>ans=newArrayList<String>();
for (inti=0; i<digits.length(); i++) {
ans=mul(ans, getList(digits.charAt(i) -'0'));
    }
returnans;
}
publicList<String>getList(intdigit) {
StringdigitLetter[] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
List<String>ans=newArrayList<String>();
for (inti=0; i<digitLetter[digit].length(); i++) {
ans.add(digitLetter[digit].charAt(i) +"");
        }
returnans;
    }
//定义成两个 List 相乘publicList<String>mul(List<String>l1, List<String>l2) {
if (l1.size() !=0&&l2.size() ==0) {
returnl1;
    }
if (l1.size() ==0&&l2.size() !=0) {
returnl2;
    }
List<String>ans=newArrayList<String>();
for (inti=0; i<l1.size(); i++)
for (intj=0; j<l2.size(); j++) {
ans.add(l1.get(i) +l2.get(j));
        }
returnans;
}

解法二 队列迭代

参考这里,果然有人用迭代写了出来。主要用到了队列。


publicList<String>letterCombinations(Stringdigits) {
LinkedList<String>ans=newLinkedList<String>();
if(digits.isEmpty()) returnans;
String[] mapping=newString[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ans.add("");
for(inti=0; i<digits.length();i++){
intx=Character.getNumericValue(digits.charAt(i));
while(ans.peek().length()==i){ //查看队首元素Stringt=ans.remove(); //队首元素出队for(chars : mapping[x].toCharArray())
ans.add(t+s);
            }
        }
returnans;
    }

假如是 "23" ,那么

第 1 次 for 循环结束后变为 a, b, c;

第 2 次 for 循环的第 1 次 while 循环 a 出队,分别加上 d e f 然后入队,就变成 b c ad ae af

第 2 次 for 循环的第 2 次 while 循环 b 出队,分别加上 d e f 然后入队,就变成 c ad ae af bd be bf

第 2 次 for 循环的第 3 次 while 循环 c 出队,分别加上 d e f 然后入队,就变成 ad ae af bd be bf cd ce cf

这样的话队列的元素长度再也没有等于 1 的了就出了 while 循环。


这种题的时间复杂度和空间复杂度自己理的不太清楚就没有写了。

相关文章
|
4月前
|
C++ Python
leetcode-283:移动零
leetcode-283:移动零
26 0
|
4月前
|
算法
leetcode:389. 找不同
leetcode:389. 找不同
21 0
|
存储 Python
LeetCode 66. Plus One
给定表示非负整数的非空数字数组,加上整数的1。 存储数字使得最高有效数字位于列表的开头,并且数组中的每个元素包含单个数字。 您可以假设整数不包含任何前导零,除了数字0本身。
82 0
LeetCode 66. Plus One
leetcode 283 移动零
leetcode 283 移动零
50 0
|
C++ Python
LeetCode 771. Jewels and Stones
LeetCode 771. Jewels and Stones
72 0
|
算法
leetcode第42题
也就是红色区域中的水, 数组是 height = [ 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 ] 。 原则是高度小于 2,temp ++,高度大于等于 2,ans = ans + temp,temp = 0。 temp 初始化为 0 ,ans 此时等于 2。 height [ 0 ] 等于 0 < 2,不更新。 height [ 1 ] 等于 1 < 2,不更新。 height [ 2 ] 等于 0 < 2, 不更新。 height [ 3 ] 等于 2 >= 2, 开始更新 height [ 4 ] 等于 1 < 2,temp = temp + 1 = 1。 h
leetcode第42题
|
人工智能 算法
leetcode第41题
对于这种要求空间复杂度的,我们可以先考虑如果有一个等大的空间,我们可以怎么做。然后再考虑如果直接用原数组怎么做,主要是要保证数组的信息不要丢失。目前遇到的,主要有两种方法就是交换和取相反数。
leetcode第41题
leetcode第53题
解法一和解法二的动态规划,只是在定义的时候一个表示以 i 开头的子数组,一个表示以 i 结尾的子数组,却造成了时间复杂度的差异。问题就是解法一中求出了太多的没必要的和,不如解法二直接,只保存最大的和。
leetcode第53题
leetcode第39题
对回溯法又有了更深的了解,一般的架构就是一个大的 for 循环,然后先 add,接着利用递归进行向前遍历,然后再 remove ,继续循环。而解法二的动态规划就是一定要找到递进的规则,开始的时候就想偏了,导致迟迟想不出来。
leetcode第39题