Topic
需要排序的最短子数组长度
Java code
Package com.lizhouwei.chapter8;/** * @Description: Minimum sub-array length to sort * @Author: Lizhouwei * @CreateDate: 2018/4/29 8:03 * @Modi FY by: * @ModifyDate: */public class Chapter8_5 {public int getminlength (int[] arr) {if (arr = = NULL | | arr.le Ngth < 2) {return 0; } int minindex =-1; int min = arr[arr.length-1]; for (int i = arr.length-2; i >-1; i--) {if (Arr[i] > min) {minindex = i; } else {min = math.min (min, arr[i]); }} if (Minindex = =-1) {return 0; } int maxindex =-1; int max = arr[0]; for (int i = 1; i < arr.length; i++) {if (Arr[i] < max) {maxindex = i; } else {max = Math.max (max, arr[i]); }} return maxindex-minindex + 1; }//test public static void main (string[] args) {chapter8_5 chapter = new Chapter8_5 (); Int[] arr = {1, 5, 3, 4, 2, 6, 7}; int res = chapter.getminlength (arr); System.out.print ("{1, 5, 3, 4, 2, 6, 7} are required to sort the maximum length:" + res); }}
Results
Programmer Code Interview guide eighth array and matrix problems need to sort the shortest sub-array length