题目如下,要求recursive 和 non recursive解法
write a program that given a 7 digit telephone number, could print all
possible combinations of letters that each number could represent.
second part: if the telephone number changes to 12 digits one
recursive 解法很清晰,建立一个字典,然后递归就好了。
iterative 解法,我就卡住了,google了一下,有一个解法如下:
private static ArrayList processOneNumber(ArrayList input, int[] phoneNum,
int currentNum) {
ArrayList output = new ArrayList();
int numberToProcess = phoneNum[currentNum];
char c;
... 阅读全帖
题目:
Write a method in Java to:
Find all set of permutations from N number of ArrayLists. Each ArrayList has
a different length.
Each permutation is formed by picking one item from each input ArrayList.
You have to exhaust ALL permutations and can't return duplicate permuations.
Each permutation is a Set, so the order of the items does not matter. For
example [a1,b1,c1] is the same permutation as [c1,b1,a1].
Example:
Input: N number of array lists with different length
[a1,a2,a3....]
[b1,b2....... 阅读全帖
从你这个dfs解法倒是学到了点东西:n重循环可以通过dfs来做。:D
解法当中唯一的问题就是set判重错了,ArrayList是没有重定义hash()跟
equals()方法的,所以判重就只看是否是同一个reference。
List> arrayCombinate(List arrs) {
List> ol = new ArrayList>();
if (arrs.size() == 0)
return ol;
dfs(arrs, 0, new ArrayList(), ol, new HashSet());
return ol;
}
void dfs(List arrs, int i, List curr,
List> ol, Set memo) {
if (i == arrs.siz... 阅读全帖
以前版里贴过的一题
void minMSwap(int[] num, int m), return the min array after m swaps, each
swap happens only between two adjacent elements([4,2,1,3], 2 return [1,4,2,3
] )
4,2,1,3
4,1,2,3
1,4,2,3
当时贴的解法是找最小的元素,判断swap它需要的最少交换次数n,如果n<=m,swap过
来,m -= n,递归, 如果n>m?那就找次小的,重复上面的步骤
这个应该是O(n^2)时间复杂度,因为n次循环,每次循环里面找到符合交换次数的没有
用过的最小位又是O(n)的时间。
我觉得每次循环里面用heap优化,有希望把总时间降到O(nlogn), 但检查或update
heap中元素是否符合交换次数这一步还是要耗掉O(n), 这样总时间还是O(n^2)
哪位大牛贴个nlogn的完整解法?
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int sum = 0;
int total = 0;
int j = -1;
for(int i = 0; i < gas.length ; ++i)
{
sum += gas[i]-cost[i];
total += gas[i]-cost[i];
if(sum < 0)
{
j=i; sum = 0;
}
}
if(total<0) return -1;
else return j+1;
}
}
OJ的确是accepted了。但是我觉得是test case不够多,没有测到我认为的case。
... 阅读全帖
开始写了个brute force的,后来看到下面这个得了31个votes的解法,还有人有更简洁
,更好的解法么? https://oj.leetcode.com/discuss/366/better-solution-than-brute-force
class Solution {
private:
vector res;
map cntL;
map cn;
int n ;
public:
vector findSubstring(string S, vector &L)
{ res.clear();
cntL.clear();
cn.clear();
n = S.length();
int e = L.size();
int t = L[0].length();
int k = 0;
for(int i = 0; i < e ; i++)
{ if(cn.count(L[i]) == 0)
... 阅读全帖
【 以下文字转载自 Automobile 讨论区 】
发信人: zhang88 (david), 信区: Automobile
标 题: 三步倒车新解法Easy 3-Step Parallel Parking With 3 Simple M
发信站: BBS 未名空间站 (Wed Jul 20 12:06:02 2016, 美东)
三步道车新解法Easy 3-Step Parallel Parking With 3 Simple Markers http://youtu.be/No-rx8hjDZY
Easy 3-Step Parallel Parking With 3 Simple Markers
For new drivers, parallel parking is considered to be the hardest skill. For
non-new drivers, the problem is not remembering how to do it when it is
needed. Or you may not have the confidence to... 阅读全帖
【 以下文字转载自 Automobile 讨论区 】
发信人: zhang88 (david), 信区: Automobile
标 题: 三步道车新解法Easy 3-Step Parallel Parking With 3 Simple M
发信站: BBS 未名空间站 (Wed Jul 20 12:06:02 2016, 美东)
三步道车新解法Easy 3-Step Parallel Parking With 3 Simple Markers http://youtu.be/No-rx8hjDZY
Easy 3-Step Parallel Parking With 3 Simple Markers
For new drivers, parallel parking is considered to be the hardest skill. For
non-new drivers, the problem is not remembering how to do it when it is
needed. Or you may not have the confidence to... 阅读全帖
【 以下文字转载自 Automobile 讨论区 】
发信人: zhang88 (david), 信区: Automobile
标 题: 三步并行爬车新解法Easy 3-Step Parallel Parking With 3 Simp
发信站: BBS 未名空间站 (Wed Jul 20 12:06:02 2016, 美东)
三步道车新解法Easy 3-Step Parallel Parking With 3 Simple Markers http://youtu.be/No-rx8hjDZY
Easy 3-Step Parallel Parking With 3 Simple Markers
For new drivers, parallel parking is considered to be the hardest skill. For
non-new drivers, the problem is not remembering how to do it when it is
needed. Or you may not have the confidence to d... 阅读全帖
【 以下文字转载自 Computation 讨论区 】
发信人: grasssu (没有昵称), 信区: Computation
标 题: 请问一个基本的minimization problem有没有近似解法?
发信站: BBS 未名空间站 (Tue May 20 04:38:00 2008)
a set of objects (i from 1 to m), each has:
vi: the value of object i;
si: size of object i.
求选取a subset that can minimize the total value, subject to the condition
that total size greater than or equal to B.
请问有没有已知的近似解法?谢谢!