标签:ring int nbsp 运行 load static span tle while
public class BSAwesome { public static int getLessIndex(int[] arr){ if (arr==null || arr.length ==0){ return -1; } if (arr[0] < arr[1]){ return 0; } if (arr[arr.length-1] < arr[arr.length-2]){ return arr.length-1; } int left = 1; int right = arr.length - 2; int mid = 0; while (left < right){ mid = (left + right) / 2; if (arr[mid] > arr[mid - 1]){ right = mid - 1; }else if (arr[mid] > arr[mid + 1]){ left = mid + 1; }else return mid; } return left; } public static void main(String[] args) { int[] arr = {1,0,3,5,6,8}; System.out.println("数组中某一个局部最小值的位置:"+ getLessIndex(arr)); } }
用例运行结果:
局部最小值问题:在一个相邻位置的数不相等的数组中寻找任意一个局部最小值
标签:ring int nbsp 运行 load static span tle while
原文地址:https://www.cnblogs.com/pxy-1999/p/13259953.html