Shortest Unsorted Continuous Subarray

原创 2018年04月17日 20:27:16

581. Shortest Unsorted Continuous Subarray

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

You need to find the shortest such subarray and output its length.

Example 1:

Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

Note:

  1. Then length of the input array is in range [1, 10,000].
  2. The input array may contain duplicates, so ascending order here means <=.

先用一个数组temp保存nums,然后对tmp排序,然后用两个变量low和high去找两个数组出现不同之处的第一个位置和最后一个位置,最后返回high-low+1就是要找的数组长度

public static int getShotestArray(int[]nums){
		int low=0;
		int high=nums.length-1;
//		while(low<high){
			while(low<high&&nums[low]<=nums[low+1]){
				low++;
			}
			while(low<high&&nums[high]>=nums[high-1]){
				high--;
			}
			System.out.println("helo");
//		}
		return high-low+1;
	}

581. Shortest Unsorted Continuous Subarray

Given an integer array, you need to find one continuous subarray that if you only sort this subarray...
  • yimi1995
  • yimi1995
  • 2017-05-15 15:36:25
  • 1451

[LeetCode]581. Shortest Unsorted Continuous Subarray

[LeetCode]581. Shortest Unsorted Continuous Subarray题目描述思路找出对应的最大最小值,看是否在对应的位置,如果在就向中间缩小,如果都不在,计算长度 ...
  • Lcharon
  • Lcharon
  • 2017-05-14 17:34:50
  • 1204

LeetCode-581. Shortest Unsorted Continuous Subarray (JAVA)最短未排序数组

LeetCode-581. Shortest Unsorted Continuous Subarray (JAVA)最短未排序数组
  • mine_song
  • mine_song
  • 2017-05-26 17:16:16
  • 876

Shortest Unsorted Continuous Subarray问题及解法

问题描述: Given an integer array, you need to find one continuous subarray that if you only sort this s...
  • u011809767
  • u011809767
  • 2017-05-15 13:53:16
  • 1460

20170606-leetcode-581-Shortest Unsorted Continuous Subarray

1.DescriptionGiven an integer array, you need to find one continuous subarray that if you only sort ...
  • u010105243
  • u010105243
  • 2017-06-06 11:24:09
  • 411

LeetCode 581. Shortest Unsorted Continuous Subarray

581. Shortest Unsorted Continuous Subarray一、问题描述 Given an integer array, you need to find one cont...
  • u010352603
  • u010352603
  • 2017-05-26 16:33:48
  • 716

LeetCode刷题【Array】 Shortest Unsorted Continuous Subarray

【题目】  Given an integer array, you need to find one continuous subarray that if you only sort th...
  • buaalt
  • buaalt
  • 2017-07-18 10:26:59
  • 318

【LeetCode】581. Shortest Unsorted Continuous Subarray

1.问题描述 Given an integer array, you need to find one continuous subarray that if you only sort this ...
  • GSH_Hello_World
  • GSH_Hello_World
  • 2017-05-24 11:37:39
  • 407

LeetCode:581. Shortest Unsorted Continuous Subarray

解题思路:选用两个临时变量left right分别指向数组的左右两边,如果left 和right所指数既不是数组的最大值也不是最小值,结束循环;如果left所指数是整个数组的最小值,将left++;如...
  • u012608701
  • u012608701
  • 2018-03-30 10:51:31
  • 7

[leetcode]581. Shortest Unsorted Continuous Subarray

Given an integer array, you need to find one continuous subarray that if you only sort this subarray...
  • kangbin825
  • kangbin825
  • 2017-05-20 17:21:31
  • 242
收藏助手
不良信息举报
您举报文章:Shortest Unsorted Continuous Subarray
举报原因:
原因补充:

(最多只允许输入30个字)