[合集] 请问开broker account一定要有US address么?# Stock
l*A
1 楼
题目:
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....]
[c1, c2... ]
...
Output: ALL permutations
[a1, b1, c1...],
[a1,b1,c2..]
....
Note: the above example is just an sample of potential input to illustrate
the output. You have to write code to solve for generalized input. Please t
ype in the pad below so we can follow your thought process.Show us your mos
t elegant solution
我的解法:
public HashSet> findPermutation(ArrayList er>> input, int N) {
int inputSize = input.size();
HashSet> result = new HashSet>();
ArrayList tempList = new ArrayList();
if (inputSize!=N) {
System.out.println("Input invliad"); //very simple input checking.
could throw an inputinvalid exception if needed
return result; //be nice to just return an empty set for now. could
do many other things
}
//use depth first search to traverse through the input and return result
dfs(input,result, 0, N-1, tempList);
return result;
}
private void dfs (ArrayList input, HashSet ger>> result, int depth, int size, ArrayList tempList) {
// base condition - when we reach the desired depth(size), add tempList
to result
if (depth==size) {
result.add(new ArrayList(tempList)); //use of set API to r
emove duplicate
return;
}
ArrayList thisList = input.get(depth);
for (Integer item : thisList) {
tempList.add(item);
dfs(input,result,depth+1,size,tempList);
tempList.remove(tempList.size()-1); //remove last element
}
}
把解法发给面试的人以后,他说"you unfortunately missed the solution and did no
t solve the question asked in the exercise."
请大家帮忙指点一下,是我理解题意错了么?还是解法错了?
谢谢
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....]
[c1, c2... ]
...
Output: ALL permutations
[a1, b1, c1...],
[a1,b1,c2..]
....
Note: the above example is just an sample of potential input to illustrate
the output. You have to write code to solve for generalized input. Please t
ype in the pad below so we can follow your thought process.Show us your mos
t elegant solution
我的解法:
public HashSet
int inputSize = input.size();
HashSet
ArrayList
if (inputSize!=N) {
System.out.println("Input invliad"); //very simple input checking.
could throw an inputinvalid exception if needed
return result; //be nice to just return an empty set for now. could
do many other things
}
//use depth first search to traverse through the input and return result
dfs(input,result, 0, N-1, tempList);
return result;
}
private void dfs (ArrayList
// base condition - when we reach the desired depth(size), add tempList
to result
if (depth==size) {
result.add(new ArrayList
emove duplicate
return;
}
ArrayList
for (Integer item : thisList) {
tempList.add(item);
dfs(input,result,depth+1,size,tempList);
tempList.remove(tempList.size()-1); //remove last element
}
}
把解法发给面试的人以后,他说"you unfortunately missed the solution and did no
t solve the question asked in the exercise."
请大家帮忙指点一下,是我理解题意错了么?还是解法错了?
谢谢