return the index of that peek or valey
来源:互联网 发布:中国水污染地图数据库 编辑:程序博客网 时间:2024/06/14 17:35
给定一个array,条件:1. A[i] = A[i-1]+-1; 2. There is only one peek or one valey in the array, return the index of that peek or valey.
public static int find(int[] nums) {if (nums.length == 0)return -1;int len = nums.length;if (Math.abs(nums[len - 1] - nums[0]) == len - 1)return -1;// There is no such a valley or peakreturn helper(nums, 0, len - 1);}public static int helper(int[] nums, int start, int end) {int mid = start + (end - start) / 2;if ((nums[mid - 1] - nums[mid]) * (nums[mid + 1] - nums[mid]) > 0)return nums[mid];int diffOfIndex = mid - start;int diffOfValue = Math.abs(nums[mid] - nums[start]);if (diffOfIndex == diffOfValue)start = mid;elseend = mid;return helper(nums, start, end);}public static void main(String[] args) {int[] nums1 = { 1, 2, 3, 4, 3, 2 };int[] nums2 = {};int[] nums3 = { 1, 2, 3, 4, 5, 6, 7, 8, 7 };int[] nums4 = { 1, 2, 3, 4, 5 };int[] nums5 = { 5, 4, 3, 2, 1, 2, 3 };int[] nums6 = { 9, 8, 7, 6, 7 };System.out.println(find(nums1));System.out.println(find(nums2));System.out.println(find(nums3));System.out.println(find(nums4));System.out.println(find(nums5));System.out.println(find(nums6));}
0 0
- return the index of that peek or valey
- Facebook面试题 find the peek or valley index in an array
- return the index of a 'key' in Python dictionary
- Factory methods are static methods that return an instance of the native class(保护CONSTRUCTOR)
- The return of king
- Return of the Nim
- Return of the Nim
- The parameter is (or has an element that is) the wrong kind of geometry
- The parameter is (or has an element that is) the wrong kind of geometry
- pointer to ref of an array, and to the func that return a ref of an array
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- given an array of integers Find the index of values that satisfy A+B = C + D
- The name or security ID (SID) of the domain specified is inconsistent with the trust information for that domain(转)
- Range or index out of bounds
- 报错:This generally means that another instance of this process was already running or is hung in the
- This generally means that another instance of this process was already running or is hung in the deb
- This generally means that another instance of this process was already running or is hung in the deb
- This generally means that another instance of this process was already running or is hung in the deb
- water 参考程序
- Angry Cows总结_2016.2.28
- 创新班第一到四课的内容【组合数学】(补)
- jquery的ajaxSetup()函数用法:设置全局的ajax默认选项
- 2015信息学夏令营第1、2……天——搜索与递归
- return the index of that peek or valey
- 动态规划
- 宽度搜索
- 位运算
- 2015NOIP总结
- 畅通工程续(最短路)
- 创新班20课3题+总结
- git使用笔记
- LCA