There is an array related problem, the requirement is that time complexity is O(n) and space complexity is O(1).
If I use Arrays.sort(arr)
, and use a for
loop to one pass loop, for example:
public static int hello(int[]A){
Arrays.sort(A);
for(int i=0;i<A.length;i++){
....................
}
return ....;
}
So the loop will cost O(n) time. My question is: will Arrays.sort()
cost more time? If I use Arrays.sort()
, will this time complexity still be O(n)? And will Arrays.sort()
cost more space?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…