Redian新闻
>
[合集] 请问开broker account一定要有US address么?
avatar
[合集] 请问开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(ArrayLister>> 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, HashSetger>> 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."
请大家帮忙指点一下,是我理解题意错了么?还是解法错了?
谢谢
avatar
L*g
2
看要求说 MI 州的永久职位PP的寄送地址是NSC,但是我是博后,是临时职位,可以寄
到TSC吗?
在线等,一会就去邮局了。
谢谢。
avatar
b*y
3
☆─────────────────────────────────────☆
IjiaU (坚持梦想) 于 (Fri Nov 6 09:02:46 2009, 美东) 提到:
我有SSN、美国银行账户,但近期不在美国工作,没有固定地址,也不想麻烦朋友
请问有办法开户么?哪个broker不用美国地址?
谢谢
avatar
T*e
4
1. 用HashSet> 是题目要求还是你自己决定的? 感觉如果用
HashSet了对应的set都被算成hash值了,最后即使返回去也不能把对应的set从hashset
里面抠出来吧?
2.dfs(input,result, 0, N-1, tempList);
这句里应该是N而不是N-1吧,试想假如N = 1,此时有一个arraylist但由于一旦调用
dfs就会有size == depth,导致把一个空arraylist加进去
avatar
s*s
5
pp instruction上写得应该很清楚啊。仔细看看。看看你是哪个辖区的啊。

【在 L*********g 的大作中提到】
: 看要求说 MI 州的永久职位PP的寄送地址是NSC,但是我是博后,是临时职位,可以寄
: 到TSC吗?
: 在线等,一会就去邮局了。
: 谢谢。

avatar
l*A
6
啊,你说的第二点很对.大意了,忘了检查一下...
你说的第一点我不太理解,用HashSet是为了避免加入同样的ArrayList,返回一
个set,然后可以用set的API来读任何一个元素啊
大不了再造一个ArrayList> result,然后result.addAll(set),
return result.

hashset

【在 T******e 的大作中提到】
: 1. 用HashSet> 是题目要求还是你自己决定的? 感觉如果用
: HashSet了对应的set都被算成hash值了,最后即使返回去也不能把对应的set从hashset
: 里面抠出来吧?
: 2.dfs(input,result, 0, N-1, tempList);
: 这句里应该是N而不是N-1吧,试想假如N = 1,此时有一个arraylist但由于一旦调用
: dfs就会有size == depth,导致把一个空arraylist加进去

avatar
L*g
7
是看了,可是写的是永久职位要PP的,是寄到NSC。
我是博后,使临时的职位呀,难道寄到哪都行?

【在 s**********s 的大作中提到】
: pp instruction上写得应该很清楚啊。仔细看看。看看你是哪个辖区的啊。
avatar
b*5
8
你这个duplicate, 我不大懂。 你这个每个input 里面会有duplicate element么?
但你这个HashSet>不大对吧。你说利用HashSet的API,就认为两个
ArrayList相同, 但ArrayList是个reference,不是element-wise
comparison。

回一

【在 l*******A 的大作中提到】
: 啊,你说的第二点很对.大意了,忘了检查一下...
: 你说的第一点我不太理解,用HashSet是为了避免加入同样的ArrayList,返回一
: 个set,然后可以用set的API来读任何一个元素啊
: 大不了再造一个ArrayList> result,然后result.addAll(set),
: return result.
:
: hashset

avatar
j*3
9
1a or 1b?
avatar
T*e
10
我对java不是太了解,但个人感觉你用hashset只能检查对于某个set它在不在hashset
里,这个检查靠hashcode()来实现,但你能不能从hashset里取出原来的set我就不清
楚了,我的理解是hashset把每一种set对应的hashcode的值存进去了,而不是存的set

回一

【在 l*******A 的大作中提到】
: 啊,你说的第二点很对.大意了,忘了检查一下...
: 你说的第一点我不太理解,用HashSet是为了避免加入同样的ArrayList,返回一
: 个set,然后可以用set的API来读任何一个元素啊
: 大不了再造一个ArrayList> result,然后result.addAll(set),
: return result.
:
: hashset

avatar
L*g
11
EB1A

【在 j*******3 的大作中提到】
: 1a or 1b?
avatar
b*5
12
。。。

hashset
set

【在 T******e 的大作中提到】
: 我对java不是太了解,但个人感觉你用hashset只能检查对于某个set它在不在hashset
: 里,这个检查靠hashcode()来实现,但你能不能从hashset里取出原来的set我就不清
: 楚了,我的理解是hashset把每一种set对应的hashcode的值存进去了,而不是存的set
:
: 回一

avatar
l*u
13
就算按照你的思维方式,得出的结论应该是
你应该有TSC辖区的永久职位才能寄到TSC吧
也不能得出你随便寄到哪里的结论吧

【在 L*********g 的大作中提到】
: 是看了,可是写的是永久职位要PP的,是寄到NSC。
: 我是博后,使临时的职位呀,难道寄到哪都行?

avatar
l*A
14
good point - 我明天写几个input测试一下应该就知道了
谢谢!

wise

【在 b**********5 的大作中提到】
: 你这个duplicate, 我不大懂。 你这个每个input 里面会有duplicate element么?
: 但你这个HashSet>不大对吧。你说利用HashSet的API,就认为两个
: ArrayList相同, 但ArrayList是个reference,不是element-wise
: comparison。
:
: 回一

avatar
s*s
15
我是NSC辖区的,所以我邮寄到NSC.即时你邮到TSC问题也不大,他们会给你转寄给NSC的.

【在 L*********g 的大作中提到】
: 是看了,可是写的是永久职位要PP的,是寄到NSC。
: 我是博后,使临时的职位呀,难道寄到哪都行?

avatar
T*e
16
刚才看了下HashSet的代码,感觉就是楼上说的处理duplicates的问题,
[1,2,3] 和 [3,2,1] 的hashcode估计不一样吧,加入到hashset里之前可以先排个序
avatar
k*n
17
NSC
avatar
l*n
18
从你这个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.size()) {
List copy = new ArrayList(curr);
Collections.sort(copy);
String sig = Arrays.toString(copy.toArray());
if (!memo.contains(sig)) {
ol.add(copy);
memo.add(sig);
}
return;
}
int[] arr = arrs.get(i);
for (int j = 0; j < arr.length; j++) {
curr.add(arr[j]);
dfs(arrs, i + 1, curr, ol, memo);
curr.remove(curr.size() - 1);
}
}

has
permuations.


【在 l*******A 的大作中提到】
: 题目:
: 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

avatar
f*s
19
NSC
avatar
l*n
20
不光[1,2,3]跟[3,2,1]的hashcode不一样,[1,2,3]跟[1,2,3]的也不一样!
java的object(ArrayList就只是个object)如果没有重定义hashcode方法的话
,hashcode就都是对应object的地址而已。

【在 T******e 的大作中提到】
: 刚才看了下HashSet的代码,感觉就是楼上说的处理duplicates的问题,
: [1,2,3] 和 [3,2,1] 的hashcode估计不一样吧,加入到hashset里之前可以先排个序

avatar
l*A
21
cool. you guys are correct. Just tested it. thanks!
avatar
l*7
22
public static ArrayList> getPermutation(ArrayList<
ArrayList> lists){
ArrayList> result = new ArrayListInteger>>();
if(lists != null && lists.size() != 0){
permutation(lists, result, new ArrayList(), 0);
}

return result;
}

public static void permutation(ArrayList> lists,
ArrayList> result, ArrayList current, int index){
if(index == lists.size()){
result.add((ArrayList)current.clone());
return;
}

ArrayList currentList = lists.get(index);
for(int i = 0; i < currentList.size(); i++){
current.add(currentList.get(i));
permutation(lists, result, current, index+1);
current.remove(current.size()-1);
}
}
avatar
T*e
23
你确定吗? 我那天用hashmap试了试,对于两个不同的arraylist,如果都是空的话,
hash值是不一样的,如果里面是1 2 3 且顺序一样,可以用list1找出list2的value

【在 l*n 的大作中提到】
: 不光[1,2,3]跟[3,2,1]的hashcode不一样,[1,2,3]跟[1,2,3]的也不一样!
: java的object(ArrayList就只是个object)如果没有重定义hashcode方法的话
: ,hashcode就都是对应object的地址而已。

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。