欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1051 LeetCode 高度检查器

程序员文章站 2024-03-15 13:48:23
...

题目描述:
1051 LeetCode 高度检查器

思路:
先从小到大进行排序
然后与原来的数组进行比较

代码如下:

class Solution {
public:
    int heightChecker(vector<int>& heights) {
        vector<int>res=heights;
        int sum=0;
        sort(res.begin(),res.end());
        for(int i=0;i<res.size();i++){
            if(res[i]!=heights[i])
            sum++;
        }
        return sum;
    }
};