|
5 | 5 | ## 题目描述
|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
| 8 | + |
8 | 9 | <p>给定一个整数数组,编写一个函数,找出索引<code>m</code>和<code>n</code>,只要将索引区间<code>[m,n]</code>的元素排好序,整个数组就是有序的。注意:<code>n-m</code>尽量最小,也就是说,找出符合条件的最短序列。函数返回值为<code>[m,n]</code>,若不存在这样的<code>m</code>和<code>n</code>(例如整个数组是有序的),请返回<code>[-1,-1]</code>。</p>
|
9 | 10 | <p><strong>示例:</strong></p>
|
10 | 11 | <pre><strong>输入:</strong> [1,2,4,7,10,11,7,12,6,7,16,18,19]
|
|
18 | 19 | ## 解法
|
19 | 20 |
|
20 | 21 | <!-- 这里可写通用的实现逻辑 -->
|
| 22 | + |
| 23 | +**方法一:两次遍历** |
| 24 | + |
| 25 | +我们先从左到右遍历数组 $array$,用 $mx$ 记录遍历过的最大值,如果当前值 $x$ 小于 $mx$,则说明 $x$ 需要被排序,我们将 $x$ 的下标 $i$ 记录为 $right$;否则更新 $mx$。 |
| 26 | + |
| 27 | +同理,我们再从右到左遍历数组 $array$,用 $mi$ 记录遍历过的最小值,如果当前值 $x$ 大于 $mi$,则说明 $x$ 需要被排序,我们将 $x$ 的下标 $i$ 记录为 $left$;否则更新 $mi$。 |
| 28 | + |
| 29 | +最后返回 $[left, right]$ 即可。 |
| 30 | + |
| 31 | +时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 |
| 32 | + |
21 | 33 | <!-- tabs:start -->
|
22 | 34 |
|
23 | 35 | ### **Python3**
|
24 | 36 |
|
25 | 37 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
26 | 38 |
|
27 | 39 | ```python
|
28 |
| - |
| 40 | +class Solution: |
| 41 | + def subSort(self, array: List[int]) -> List[int]: |
| 42 | + n = len(array) |
| 43 | + mi, mx = inf, -inf |
| 44 | + left = right = -1 |
| 45 | + for i, x in enumerate(array): |
| 46 | + if x < mx: |
| 47 | + right = i |
| 48 | + else: |
| 49 | + mx = x |
| 50 | + for i in range(n - 1, -1, -1): |
| 51 | + if array[i] > mi: |
| 52 | + left = i |
| 53 | + else: |
| 54 | + mi = array[i] |
| 55 | + return [left, right] |
29 | 56 | ```
|
30 | 57 |
|
31 | 58 | ### **Java**
|
32 | 59 |
|
33 | 60 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
34 | 61 |
|
35 | 62 | ```java
|
| 63 | +class Solution { |
| 64 | + public int[] subSort(int[] array) { |
| 65 | + int n = array.length; |
| 66 | + int mi = Integer.MAX_VALUE, mx = Integer.MIN_VALUE; |
| 67 | + int left = -1, right = -1; |
| 68 | + for (int i = 0; i < n; ++i) { |
| 69 | + if (array[i] < mx) { |
| 70 | + right = i; |
| 71 | + } else { |
| 72 | + mx = array[i]; |
| 73 | + } |
| 74 | + } |
| 75 | + for (int i = n - 1; i >= 0; --i) { |
| 76 | + if (array[i] > mi) { |
| 77 | + left = i; |
| 78 | + } else { |
| 79 | + mi = array[i]; |
| 80 | + } |
| 81 | + } |
| 82 | + return new int[] {left, right}; |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### **C++** |
| 88 | + |
| 89 | +```cpp |
| 90 | +class Solution { |
| 91 | +public: |
| 92 | + vector<int> subSort(vector<int>& array) { |
| 93 | + int n = array.size(); |
| 94 | + int mi = INT_MAX, mx = INT_MIN; |
| 95 | + int left = -1, right = -1; |
| 96 | + for (int i = 0; i < n; ++i) { |
| 97 | + if (array[i] < mx) { |
| 98 | + right = i; |
| 99 | + } else { |
| 100 | + mx = array[i]; |
| 101 | + } |
| 102 | + } |
| 103 | + for (int i = n - 1; ~i; --i) { |
| 104 | + if (array[i] > mi) { |
| 105 | + left = i; |
| 106 | + } else { |
| 107 | + mi = array[i]; |
| 108 | + } |
| 109 | + } |
| 110 | + return {left, right}; |
| 111 | + } |
| 112 | +}; |
| 113 | +``` |
| 114 | +
|
| 115 | +### **Go** |
| 116 | +
|
| 117 | +```go |
| 118 | +func subSort(array []int) []int { |
| 119 | + n := len(array) |
| 120 | + mi, mx := math.MaxInt32, math.MinInt32 |
| 121 | + left, right := -1, -1 |
| 122 | + for i, x := range array { |
| 123 | + if x < mx { |
| 124 | + right = i |
| 125 | + } else { |
| 126 | + mx = x |
| 127 | + } |
| 128 | + } |
| 129 | + for i := n - 1; i >= 0; i-- { |
| 130 | + if array[i] > mi { |
| 131 | + left = i |
| 132 | + } else { |
| 133 | + mi = array[i] |
| 134 | + } |
| 135 | + } |
| 136 | + return []int{left, right} |
| 137 | +} |
| 138 | +``` |
36 | 139 |
|
| 140 | +### **TypeScript** |
| 141 | + |
| 142 | +```ts |
| 143 | +function subSort(array: number[]): number[] { |
| 144 | + const n = array.length; |
| 145 | + let [mi, mx] = [Infinity, -Infinity]; |
| 146 | + let [left, right] = [-1, -1]; |
| 147 | + for (let i = 0; i < n; ++i) { |
| 148 | + if (array[i] < mx) { |
| 149 | + right = i; |
| 150 | + } else { |
| 151 | + mx = array[i]; |
| 152 | + } |
| 153 | + } |
| 154 | + for (let i = n - 1; ~i; --i) { |
| 155 | + if (array[i] > mi) { |
| 156 | + left = i; |
| 157 | + } else { |
| 158 | + mi = array[i]; |
| 159 | + } |
| 160 | + } |
| 161 | + return [left, right]; |
| 162 | +} |
37 | 163 | ```
|
38 | 164 |
|
39 | 165 | ### **...**
|
|
0 commit comments