【发布时间】:2020-11-25 19:04:45
【问题描述】:
我想让这个方法尽可能快地运行。
int[] p = new int[] {1,4,4,5,1,3,6};
int[] q = new int[] {2,1,3,1,6,4,3};
int k =4.
void nestLoop(){
for(int i = 0; i < q.length; i++){
for(int j = 0; j < p.length; j++){
//code continues
// inside here...
//This approach
// takes a longer
//time to finish
// executing. I need a better algorithm
}
}
}
为了更清楚;在q[0]处,p = 1,由于1小于q[0],所以选1,p[4]也更小,选1;因此当它是 q[0] 时的答案是 1 和 1 :但是因为 1 和 1 是两个整数,所以它们不等于 k = 4,然后返回 0。如果答案是 1, 1,1,1 那么数字是4,等于k。 [1,1,1,1] 将被返回。其他q也一样
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: java algorithm nested-loops brute-force