zoukankan      html  css  js  c++  java
  • 581. Shortest Unsorted Continuous Subarray(LeetCode)

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

    You need to find the shortest such subarray and output its length.

    Example 1:

    Input: [2, 6, 4, 8, 10, 9, 15]
    Output: 5
    Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
    

    Note:

    1. Then length of the input array is in range [1, 10,000].
    2. The input array may contain duplicates, so ascending order here means <=.
       1 class Solution {
       2 public:
       3     int findUnsortedSubarray(vector<int>& nums) {
       4         int len = nums.size();
       5         vector<int> vet;
       6         vector<int> vet1;
       7         vet = nums;
       8         sort(vet.begin(), vet.end());
       9         for (int i = 0; i < len; i++)
      10         {
      11             if (vet[i] != nums[i])
      12             {
      13                 vet1.push_back(i);
      14             }
      15         }
      16         if (vet1.size() == 0)
      17             return 0;
      18         else
      19         {
      20             sort(vet1.begin(), vet1.end());
      21             return vet1.back() - vet1.front() + 1;
      22         }
      23     }
      24 };
  • 相关阅读:
    Kibana之配置文件
    Elasticsearch之集群,本地搭建集群
    支付宝支付流程
    AJAX应用的五个步骤
    位运算
    微信小程序页面传值详解
    面向对象三大基本特性,五大基本原则
    rem样板
    js 数组 随机排序
    按位异或运算符
  • 原文地址:https://www.cnblogs.com/wujufengyun/p/7197359.html
Copyright © 2011-2022 走看看