【发布时间】:2018-03-14 16:13:50
【问题描述】:
需要更高效的方式
结果如下:
示例案例:正确答案
各种小数组:正确答案
sortedArray 包含 lessThan 时的性能测试:时间 超出限制
sortedArray 不包含 lessThan 时的性能测试:超出时间限制
以下是代码:
import java.util.Arrays;
public class SortedSearch {
public static int countNumbers(int[] sortedArray, int lessThan) {
Arrays.sort(sortedArray);
int count =0;
for(int num :sortedArray){
if(num < lessThan)
count++;
}
return count;
}
public static void main(String[] args) {
System.out.println(SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4));
}
}
参考链接:Try here
【问题讨论】:
-
查找“二分查找”
-
将
System.out.println(count)放在你的 for 循环中。我怀疑你的循环没有正确实施。您将能够看到您的循环运行了多少次。 -
为什么要对已排序的数组进行排序??