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 };
  • 相关阅读:
    P1891 疯狂LCM
    P2568 GCD
    P1516 青蛙的约会和P2421 [NOI2002]荒岛野人
    P4168 蒲公英
    P5960 差分约束算法模板
    P2024 食物链(种类并查集)
    CF1328E Tree Queries
    CF1328B K-th Beautiful String
    dij-spfa乱搞
    P1993 小K的农场
  • 原文地址:https://www.cnblogs.com/wujufengyun/p/7197359.html
Copyright © 2011-2022 走看看