这个庸题目,看了半天才发现这么简单…
求对比排序后和排序前位置不一样的个数
class Solution {
public int heightChecker(int[] heights) {
int count = 0;
int[] temp = heights.clone();
Arrays.sort(temp);
for(int i = 0; i < heights.length; i++){
if(heights[i] != temp[i]) ++count;
}
return count;
}
}
本文介绍了一个简单的算法问题:计算一个整数数组经过排序前后元素位置发生变化的数量。通过复制原始数组并进行排序,然后逐个比较两个数组对应位置的元素来实现。

被折叠的 条评论
为什么被折叠?



