LeetCode每日一题: 高度检查器(No.1051)

栏目: 编程工具 · 发布时间: 5年前

学校在拍年度纪念照时,一般要求学生按照 非递减 的高度顺序排列。
请你返回至少有多少个学生没有站在正确位置数量。
该人数指的是:能让所有学生以 非递减 高度排列的必要移动人数。
复制代码

示例:

输入:[1,1,4,2,1,3]
输出:3
解释:
高度为 4、3 和最后一个 1 的学生,没有站在正确的位置。
复制代码

思考:

先将数组排序,再与原数组中元素比较,对应位置元素不相同学生人数+1。
复制代码

实现:

class Solution {
    public int heightChecker(int[] heights) {
        int[] copy = Arrays.copyOf(heights, heights.length);
        Arrays.sort(copy);
        int diff = 0;
        for (int count = 0; count < heights.length; count++) {
            if (heights[count] != copy[count]) {
                diff++;
            }
        }
        return diff;
    }
}复制代码

以上所述就是小编给大家介绍的《LeetCode每日一题: 高度检查器(No.1051)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming the Mobile Web

Programming the Mobile Web

Maximiliano Firtman / O'Reilly Media / 2010-07-23 / $44.99

* Learn how to use your existing skills to move into mobile web development * Discover the new possibilities of mobile web development, and understand its limitations * Get detailed coverage of ......一起来看看 《Programming the Mobile Web》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具