题目
思路
//本质是与升序排序对比有多少元素不在应在的位置上
//sort排序数组得到新数组
//同时遍历新旧数组,相同index,不同value,count++;
class Solution {
public int heightChecker(int[] heights) {
int count=0;
int[] copyHeights=new int[heights.length];
System.arraycopy(heights, 0, copyHeights, 0, heights.length);
Arrays.sort(copyHeights);
for(int i=0;i<heights.length;i++) {
if(heights[i]!=copyHeights[i]) {
count++;
}
}
return count;
}
}
测试结果