class Solution { public int heightChecker(int[] heights) { //设置一个桶数组arr长度为101
int[] arr = new int[101];
for(int height:heights){//增强for循环,可理解为遍历heights中的数值,height从0开始计数
arr[height]++; //遍历每个桶里面有几个元素,如1 1 2 3 4 1 则arr[1]=3,arr[2]=1,
}
int count= 0;
for(int i=0,j=0;i<arr.length;i++){//依次用heights和arr中的下标对比
while(arr[i]-->0){ if(heights[j++]!=i)
count++;
}
}
return count;
}
}
leetcode刷题笔记,高度检查器
最新推荐文章于 2021-08-12 06:45:08 发布