amex家的卡不能在同一个online账户下管理么?# Money - 海外理财
s*9
1 楼
用了DP,同时用了HashSet防止重复结果,运行时间无法通过Large Test. HashSet的查
找不是应该O(1)时间吗?怎样才能更快呢?请帮忙看一下:
import java.util.*;
public class Solution {
public ArrayList> threeSum(int[] num) {
HashSet> final_set = new HashSet Integer>>();
ArrayList current_list = new ArrayList();
threeSumRecur(num, 0, current_list, final_set);
ArrayList> final_lists = new ArrayList Integer>>();
for(ArrayList element : final_set) {
final_lists.add(element);
}
return final_lists;
}
public void threeSumRecur(int[] num, int index, ArrayList
current_list, HashSet> final_set) {
if(index >= num.length) {
return;
}
current_list.add(num[index]);
if(current_list.size() >= 3) {
if(current_list.get(0) + current_list.get(1) + current_list.get(
2) == 0) {
ArrayList new_list = new ArrayList();
new_list.addAll(current_list);
Collections.sort(new_list);
if(!final_set.contains(new_list)) {
final_set.add(new_list);
}
}
}
else {
threeSumRecur(num, index + 1, current_list, final_set);
}
current_list.remove(current_list.size() - 1);
threeSumRecur(num, index + 1, current_list, final_set);
}
}
找不是应该O(1)时间吗?怎样才能更快呢?请帮忙看一下:
import java.util.*;
public class Solution {
public ArrayList
HashSet
ArrayList
threeSumRecur(num, 0, current_list, final_set);
ArrayList
for(ArrayList
final_lists.add(element);
}
return final_lists;
}
public void threeSumRecur(int[] num, int index, ArrayList
current_list, HashSet
if(index >= num.length) {
return;
}
current_list.add(num[index]);
if(current_list.size() >= 3) {
if(current_list.get(0) + current_list.get(1) + current_list.get(
2) == 0) {
ArrayList
new_list.addAll(current_list);
Collections.sort(new_list);
if(!final_set.contains(new_list)) {
final_set.add(new_list);
}
}
}
else {
threeSumRecur(num, index + 1, current_list, final_set);
}
current_list.remove(current_list.size() - 1);
threeSumRecur(num, index + 1, current_list, final_set);
}
}