Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a497e67

Browse files
add q581
1 parent 7a29a75 commit a497e67

File tree

3 files changed

+73
-31
lines changed

3 files changed

+73
-31
lines changed

‎.idea/workspace.xml‎

Lines changed: 40 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* [q54_螺旋矩阵](/src/数组操作/q54_螺旋矩阵)
5656
* [q73_矩阵置零](/src/数组操作/q73_矩阵置零)
5757
* [q78_子集](/src/数组操作/q78_子集)
58+
* [q581_最短无序连续子数组](/src/数组操作/q581_最短无序连续子数组)
5859
* [q945_使数组唯一的最小增量](/src/数组操作/q945_使数组唯一的最小增量)
5960

6061
### 栈相关
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package 数组操作.q581_最短无序连续子数组;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 利用排序 o(n*log(n))
7+
*/
8+
public class Solution {
9+
10+
public int findUnsortedSubarray(int[] nums) {
11+
if (nums == null || nums.length < 1) {
12+
return 0;
13+
}
14+
15+
int[] cloneNums = nums.clone();
16+
Arrays.sort(nums);
17+
18+
int begin = Integer.MAX_VALUE;
19+
int end = 0;
20+
for (int i = 0; i < nums.length; i++) {
21+
if (nums[i] != cloneNums[i]) {
22+
begin = Math.min(begin, i);
23+
end = Math.max(end, i);
24+
}
25+
}
26+
return Math.max(end - begin + 1, 0);
27+
}
28+
29+
public static void main(String[] args) {
30+
new Solution().findUnsortedSubarray(new int[]{2, 6, 4, 8, 10, 9, 15});
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /