zoukankan      html  css  js  c++  java
  • is_sorted

    leetcode打卡

    题面

    class Solution {
    public:
        bool checkPossibility(vector<int> &nums) {
            int n = nums.size();
            for (int i = 0; i < n - 1; ++i) {
                int x = nums[i], y = nums[i + 1];
                if (x > y) {
                    nums[i] = y;
                    if (is_sorted(nums.begin(), nums.end())) {
                        return true;
                    }
                    nums[i] = x; // 复原
                    nums[i + 1] = x;
                    return is_sorted(nums.begin(), nums.end());
                }
            }
            return true;
        }
    };
    
    
    

    只能改两个地方:i,i+1,如果要改i的话自然是改成i+1处的值最好,如果要改i+1的话自然是改成i的值最好

    不过这里有一个函数
    is_sorted

    • 头文件:#include <algorithm> ;
    • 作用:检查数组或者向量是否被排序好;
    • 默认检查方式:升序;
    is_sorted(v.begin(),v.end());//默认升序检查
    
    
    is_sorted(v.begin(),v.end(),greater<int>());//降序检查
    
    
    bool cmp(const int & a, const int & b){
      return a%10>b%10; 
    } 
    
    is_sorted(a,a+5,cmp);
    is_sorted(v.begin(),v.end(),cmp);//自定义排序规则
    
    为了自己,和那些爱你的人
  • 相关阅读:
    本周总结
    本周总结
    本周总结
    本周总结
    性能分析(4)
    大型网站高性能架构
    第二天大数据清洗
    性能分析(2)
    六大质量属性——性能分析(1)
    java设计模式9-代理模式
  • 原文地址:https://www.cnblogs.com/zhmlzhml/p/14383849.html
Copyright © 2011-2022 走看看