医院网站怎么做,表单网站怎么做seo,网站竞价推广哪个好,dz论坛怎么做视频网站文章目录 一、题目二、C# 题解 一、题目 给定一个整数数组#xff0c;编写一个函数#xff0c;找出索引m和n#xff0c;只要将索引区间[m,n]的元素排好序#xff0c;整个数组就是有序的。注意#xff1a;n-m尽量最小#xff0c;也就是说#xff0c;找出符合条件的最短序… 文章目录 一、题目二、C# 题解 一、题目 给定一个整数数组编写一个函数找出索引m和n只要将索引区间[m,n]的元素排好序整个数组就是有序的。注意n-m尽量最小也就是说找出符合条件的最短序列。函数返回值为[m,n]若不存在这样的m和n例如整个数组是有序的请返回[-1,-1]。
示例 输入 [1,2,4,7,10,11,7,12,6,7,16,18,19] 输出 [3,9] 提示
0 len(array) 1000000 点击此处跳转题目。
二、C# 题解 笨蛋方法就是先排序然后一个一个比较。
public class Solution {public int[] SubSort(int[] array) {int[] sorted new int[array.Length], ans { -1, -1 };Array.Copy(array, sorted, array.Length);Array.Sort(sorted);for (var i 0; i array.Length; i) {if (ans[0] -1 array[i] ! sorted[i]) ans[0] i;if (ans[1] -1 array[^(i 1)] ! sorted[^(i 1)]) ans[1] array.Length - i - 1;}return ans;}
}时间204 ms击败 40.00% 使用 C# 的用户内存54.22 MB击败 40.00% 使用 C# 的用户 一次遍历也可以实现原理是乱序的左端 右边的最小值乱序的右端 左边的最大值。 从左向右遍历寻找乱序的右端从右向左遍历寻找乱序的左端。 ⟵ ⟵ ⟵ ⟵ ⟵ ⏞ m i n 6 { 1 2 4 ⋮ 7 ‾ 10 11 7 12 6 7 ‾ ⋮ 16 18 19 } ⏟ m a x 12 ⟶ ⟶ ⟶ ⟶ ⟶ \begin{array}{l} \longleftarrow \hspace{1em} \longleftarrow \hspace{1em} \longleftarrow \hspace{1em} \longleftarrow \hspace{1em} \longleftarrow \hspace{0.8em} \overbrace{\hspace{26.5em}}^{min6}\\ \{\hspace{0.78em} 1 \hspace{2.2em} 2 \hspace{2.25em} 4 \hspace{1.4em} \vdots \hspace{1.4em} \underline{\bold{7}} \hspace{2.1em} 10 \hspace{2.25em} 11 \hspace{2.25em} 7 \hspace{2.1em} 12 \hspace{2.25em} 6 \hspace{2.4em} \underline{\bold{7}} \hspace{1.4em} \vdots \hspace{1.4em} 16 \hspace{2.25em} 18 \hspace{2.25em} 19 \} \\ \hspace{1.2em} \underbrace{\hspace{24.8em} }_{max12} \hspace{1em} \longrightarrow \hspace{1em} \longrightarrow \hspace{1em} \longrightarrow \hspace{1em} \longrightarrow \hspace{1em} \longrightarrow\\ \end{array} ⟵⟵⟵⟵⟵ min6{124⋮7101171267⋮161819}max12 ⟶⟶⟶⟶⟶
时间184 ms击败 100.00% 使用 C# 的用户内存53.18 MB击败 80.00% 使用 C# 的用户