> For the complete documentation index, see [llms.txt](https://celia-qian.gitbook.io/leetcode-notebook-2020-2021/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://celia-qian.gitbook.io/leetcode-notebook-2020-2021/linear-list/array/exercises-i.md).

# Exercises I

## Remove Element

原题链接：<https://leetcode.com/problems/remove-element/>

难度：easy

参考解法：

```
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        if(nums.empty())return 0;
        int index=0;
        for(int n:nums)
         if(n!=val) nums[index++]=n; 
        return index;
    } 
};
```

## Move Zeroes

原题链接：<https://leetcode.com/problems/move-zeroes/>

难度：easy

参考解法：

```
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int index=0, count=0;
        for(int n:nums)
            if(n!=0){nums[index++]=n;count++;}
        while(count!=nums.size()){
            nums[count]=0;
            count++;
        }
    }
};
```

## Shortest Subarray to be Removed to Make Array Sorted

原题链接：<https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/>

难度：medium

[参考解法](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/830480/C%2B%2B-O\(N\)-Sliding-window-Explanation-with-Illustrations)：

```
class Solution {
public:
    int findLengthOfShortestSubarray(vector<int>& A) {
        int N = A.size(), left = 0, right = N - 1;
        while (left + 1 < N && A[left] <= A[left + 1]) ++left;
        if (left == A.size() - 1) return 0;
        while (right > left && A[right - 1] <= A[right]) --right;
        int ans = min(N - left - 1, right), i = 0, j = right;
        while (i <= left && j < N) {
            if (A[j] >= A[i]) {
                ans = min(ans, j - i - 1);
                ++i;
            } else ++j;
        }
        return ans;
    }
};
```

## Remove Covered Intervals

原题链接：<https://leetcode.com/problems/remove-covered-intervals/>

难度：medium

[参考解法](https://leetcode.com/problems/remove-covered-intervals/discuss/451277/JavaC%2B%2BPython-Sort-Solution-Test-Cases-are-Trash)：

```
class Solution {
public:
    int removeCoveredIntervals(vector<vector<int>>& A) {
        int res = 0, left = -1, right = -1;
        sort(A.begin(), A.end());
        for (auto& v: A) {
            if (v[0] > left && v[1] > right) {
                left = v[0];
                ++res;
            }
            right = max(right, v[1]);
        }
        return res;
    }
};
```
