我正在编写一个程序来计算小于1,000,000的3和5的倍数。我有一个函数可以正确地返回3的倍数和2个单独的链表中的5的倍数。现在我想将这两个链接列表组合成一个排序的非重复链接列表。我尝试了以下代码,但由于我使用的是链接列表库,因此“next”不是我的定义元素。我不能把运行时炸成除linearhemic以外的任何东西--O(n log n)。
private static LinkedList<Integer> mergeLists(LinkedList<Integer> ll1, LinkedList<Integer> ll2) {
LinkedList<Integer> finalList = new LinkedList();
if (ll1 == null)
return ll2;
if (ll2 == null)
return ll1;
if (ll1.get(0) < ll2.get(0)) {
ll1.next = MergeLists(ll1.next, ll2);
return ll1;
}
else {
ll2.next = MergeLists(ll2.next, ll1);
return ll2;
}
}
对于所有可能关心的人,这就是我最终要做的事情:
import java.util.*;
public class Multiples {
private static LinkedList<Integer> calculate_multiples(int factor, int limit) {
LinkedList<Integer> muls = new LinkedList<Integer>();
boolean result_not_maxed = true;
int counter = 1;
while (result_not_maxed) {
int local_result = factor*counter;
if (local_result < limit) {
muls.add(local_result);
counter++;
}
else
result_not_maxed = false;
}
return muls;
}
private static LinkedList<Integer> mergeLists(LinkedList<Integer> ll1, LinkedList<Integer> ll2) {
LinkedList<Integer> finalList;
Set<Integer> finalSet = new HashSet<>();
finalSet.addAll(ll1);
finalSet.addAll(ll2);
finalList = new LinkedList<Integer>(finalSet);
return finalList;
}
private static int sum(LinkedList<Integer> ll) {
int sum = 0;
for(int i=0; i<ll.size(); i++) {
sum += ll.get(i);
}
return sum;
}
public static void main(String [] args) {
LinkedList<Integer> ll_3s = Multiples.calculate_multiples(3, 1000000);
LinkedList<Integer> ll_5s = Multiples.calculate_multiples(5, 1000000);
LinkedList<Integer> finalList = new LinkedList<Integer>();
finalList = Multiples.mergeLists(ll_3s, ll_5s);
int result = sum(finalList);
System.out.print("Sum is: " + result);
}
}
答案 0 :(得分:2)
当OP请求时,递归算法合并两个已排序的LinkedList
并跳过重复项。这在O(n)中运行,其中n是两个列表中元素的总数。
请注意,此(递归)对于您所声明的所有3和5的倍数都不到100万的用例是不切实际的。
public static void main(String[] args) {
LinkedList<Integer> list1 = Lists.newLinkedList(
Arrays.asList(3, 6, 9, 12, 15, 18, 21, 24, 27, 30));
LinkedList<Integer> list2 = Lists.newLinkedList(
Arrays.asList(5, 10, 15, 20, 25, 30));
LinkedList<Integer> combined = combine(list1, list2);
System.out.println(combined);
}
private static LinkedList<Integer> combine(LinkedList<Integer> list1,
LinkedList<Integer> list2) {
LinkedList<Integer> combined = new LinkedList<>();
combine(list1, list2, combined);
return combined;
}
private static void combine(LinkedList<Integer> list1,
LinkedList<Integer> list2, LinkedList<Integer> combined) {
if (list1.size() > 0 && list2.size() > 0) {
if (list1.peek() == list2.peek()) {
list1.remove();
} else if (list1.peek() < list2.peek()) {
combined.add(list1.remove());
} else {
combined.add(list2.remove());
}
} else if (list1.size() > 0 && list2.size() == 0) {
combined.add(list1.remove());
} else if (list1.size() == 0 && list2.size() > 0) {
combined.add(list2.remove());
} else {
return;
}
combine(list1, list2, combined);
}
答案 1 :(得分:1)
如果您可以访问Java 8,那么使用流可以更有效地完成这项工作:
Set<Integer> mySet= IntStream.range(0, 1000000)
.filter(n -> n % 3 == 0 || n % 5 == 0)
.collect(Collectors.toSet());
这比创建单独的列表然后合并要快得多。
如果您确实要合并两个列表:
Set<Integer> mySet = Streams.concat(list1.stream(), list2.stream())
.collect(Collectors.toSet());
如果您没有Java 8,那么可能只需将两个列表添加到Set
。 TreeSet
是SortedSet
,因此您无需担心订购:
Set<Integer> final = new TreeSet<>();
if (list1 != null)
final.addAll(list1);
if (list2 != null)
final.addAll(list2);
return final;
答案 2 :(得分:1)
Java 7非流版本:
private static LinkedList<Integer> mergeLists(LinkedList<Integer> ll1, LinkedList<Integer> ll2) {
TreeSet<Integer> set = new TreeSet<>();
set.addAll(ll1);
set.addAll(ll2);
return new LinkedList<Integer>(set);
}
答案 3 :(得分:0)
您无需为此目的合并两个列表。 插入3&amp;阵列中有5个。保持三个辅助变量1.divisible_by_five 2. divisible_by_three和3.max 最初,divisible_by_five = 5,divisible_by_three = 3,max = 5
do{
if(divisible_by_three+3 < divisible_by_five+5){
divisible_by_three += 3;
max = divisible_by_three;
}
else if(divisible_by_three+3 > divisible_by_five+5){
divisible_by_five += 5;
max = divisible_by_five;
}
else{
divisible_by_3 +=3;
max = divisible_by_five = divisible_by_3;
}
insertIntoList(max);
}while(max<100000);