C++ easy and short solution


  • 0
    M
        int findUnsortedSubarray(vector<int>& nums) {
            vector<int> temp=nums;
            sort(temp.begin(), temp.end());
            int l=0, r=nums.size()-1;
            for(l;l<nums.size();l++) {
                if(nums[l]!=temp[l]) break;
            }
            for(r;r<nums.size();r--) {
                if(nums[r]!=temp[r]) break;
            }
            if(l>=r) return 0;
            return r-l+1;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.