几年前我有这个面试问题,但我还没有找到答案.
什么应该是x和y来进行无限循环?
while (x <= y&& x >= y && x != y) {
}
Run Code Online (Sandbox Code Playgroud)
我们试着用Nan,infinity+/-,null
floatVS int.
我isSorted在Scala和Java中都有功能.当我测量两个函数的执行时间时,我发现Scala版本非常慢.它运行大约3.2秒为10000 Int但Java版本只运行大约10ms!如何让我的scala版本更快?
这些是实施:
斯卡拉:
def main(args: Array[String]) ={
println(isSorted(getArray, (x:Int,y:Int) => x<y ))
}
def isSorted[A](items : Array[A], cond: (A,A) => Boolean) : Boolean = items match{
case Array(_) => true
case Array(x,y) =>cond(x,y)
case Array(_*) => cond(items(0),items(1)) && isSorted(items tail,cond)
}
Run Code Online (Sandbox Code Playgroud)
Java的:
public static void main(String... args){
Sorter<Integer> sorter=new Sorter<Integer>();
System.out.println(sorter.isSorted(sorter.getList(),new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
}));
}
public …Run Code Online (Sandbox Code Playgroud)