我试图利用缓存来提高我的Fibonacci方法的性能.然而,即使斐波那契仍然需要花费大量时间(40).
import java.util.Scanner;
public class FibWithCache {
public static void main(String args[]) {
System.out.println("Enter Fibonacci index: ");
Scanner sc = new Scanner(System.in);
int index = sc.nextInt();
sc.close();
System.out.println(fibonacci(index));
}
private static long fibonacci(int index) {
long result = 0;
long[] fibCache = new long[200];
if(index==0)
return 0;
else if(index == 1)
return 1;
else if(fibCache[index] != 0)
return fibCache[index];
else {
result = fibonacci(index-1) + fibonacci(index-2);
fibCache[index] = result;
return result;
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我无法从缓存中受益?
我正在尝试使用 python 获取 DNS 服务器 IP 地址。要在 Windows 命令提示符下执行此操作,我将使用
ipconfig-全部
如下所示:
我想使用 python 脚本做同样的事情。有什么方法可以提取这些值吗?我成功提取了设备的 IP 地址,但 DNS 服务器 IP 被证明更具挑战性。
我正在尝试使用分而治之技术来实现Quicksort.我在递归调用中收到Stack Overflow错误.这是我的代码:
public static void main(String[] args) {
ArrayList<Integer> unsorted = new ArrayList<Integer>();
unsorted.add(23);
unsorted.add(5);
unsorted.add(1);
unsorted.add(-8);
unsorted.add(101);
unsorted.add(21);
unsorted.add(10);
unsorted.add(10);
unsorted.add(0);
unsorted.add(50);
ArrayList<Integer> sorted = Quicksort(unsorted);
System.out.println(sorted.toString());
}
public static ArrayList<Integer> Quicksort(ArrayList<Integer> unsorted) {
if (unsorted.size() <= 1)
return unsorted;
ArrayList<Integer> less = new ArrayList<Integer>();
ArrayList<Integer> more = new ArrayList<Integer>();
int pivotindex = unsorted.size()/2;
for (int i = 0; i < unsorted.size(); i++) {
if (unsorted.get(i) < unsorted.get(pivotindex))
less.add(unsorted.get(i));
else
more.add(unsorted.get(i)); …Run Code Online (Sandbox Code Playgroud)