输入:heights = [1,1,4,2,1,3]
输出:3
解释:
当前数组:[1,1,4,2,1,3]
目标数组:[1,1,1,2,3,4]
在下标 2 处(从 0 开始计数)出现 4 vs 1 ,所以我们必须移动这名学生。
在下标 4 处(从 0 开始计数)出现 1 vs 3 ,所以我们必须移动这名学生。
在下标 5 处(从 0 开始计数)出现 3 vs 4 ,所以我们必须移动这名学生。
public class Solution
{
public int HeightChecker(int[] heights)
{//实际就是计算原数组与排序后数组的元素位置变化个数
int count = 0;
//克隆数组的方法
int[] temp = new int[heights.Length];
Array.Copy(heights,0,temp,0,heights.Length);
//对数组从小到大排序方法
Array.Sort(temp);
//Array.Reverse(temp);//先sort再Reverse,从大到小排序
for(int i=0;i<heights.Length;i++)
{
if(heights[i]!=temp[i])
{
count++;
}
}
return count;
}
}