【数据结构与算法】查找波峰或者波谷

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010900754/article/details/78138490

给定一个没有重复数字的数组,找出里面的波峰或者波谷,没有返回-1。如果有,那么波峰波谷的两侧是单调的。

思路是二分查找。返回的条件是找到的中间值比两边都大或者比两边都小。那么如果不满足就需要查找左边或者右边。在判断左右之前,我们需要先判断数组的类型,是包含波峰还是波谷,方法是检测前两个值,如果a[0]>a[1],那么是找波谷,否则找波峰。

下面只看波峰的情况,如果a[m-1] < a[m] < a[m+1],那么位于递增区间,此时应该往右,否则往左。波谷的情况类似。

这是存在波峰或者波谷的情况,假设原来的数组的是单调递增的或者单调递减的,那么就应该返回-1,如果用上面的逻辑就会越界,因为肯定每一次都是往某一个方向,最后就到了最边的那一个元素,此时用a[m-1]或者a[m+1]的时候会越界。那么一旦说m到了最边界,可以说肯定不存在了,只需要返回-1就行,如果存在肯定不会是最两边的元素,在到了最边的元素之前就已经return了,所以检测到到了最边的时候,就返回-1。

public class Main {

	public static void main(String[] args) {
		System.out.println(findPeak(new int[]{-200,-100,1,4,0}));
	}
	
	public static boolean isUp(int[] array, int m){
		return array[m - 1] < array[m] && array[m] < array[m + 1];
	}
	
	public static int findPeak(int[] array){
		if(array == null || array.length <= 2)
			return -1;
		int mark = 0;
		if(array[0] > array[1])
			mark = -1;
		else
			mark = 1;
		int start = 0, end = array.length - 1;
		while(start <= end){
			int m = (start + end) / 2;
			if(m == 0 || m == array.length - 1)
				return -1;
			if((array[m - 1] > array[m] && array[m + 1] > array[m]) || (array[m - 1] < array[m] && array[m + 1] < array[m])){
				return m;
			}
			//往左边
			if((isUp(array, m) && mark < 0) || (!isUp(array, m) && mark > 0)){
				end = m - 1;
			}else{//右边
				start = m + 1;
			}
		}
		return -1;
	}

}



波峰波谷

06-24

[img=http://img.bbs.csdn.net/upload/201306/24/1372071142_126818.jpg][/img]rnrn问题描述:rn对于任意给出的一组随机数列,求出离给出点和指定区间(即给出点往前多少个单位)最近的上波浪或下波浪,结果表示为两个点的坐标。rn如给出的点在B与C之间,给出的指定区间为30,则答案为(3,5.15)和(24,3.3),即A、B两个点的坐标。rn给出的点在E、F之间,给定的区间为30,则答案为(54,3.3)和(62,4.55),即D、E两点的坐标rnrn大家能帮忙想一下吗?rnrn数据:rn(1,4.6)(2,4.7)(3,5.15)(4,5.05)(5,5.1)(6,4.8)(7,4.7)(8,4.65)(9,4.7)(10,4.4)(11,4.0)(12,3.85)(13,3.65)(14,3.7)(15,3.85)(16,4.05)(17,3.8)(18,3.95)(19,4.0)(20,3.9)(21,3.65)(22,3.85)(23,3.5)(24,3.3)(25,3.3)(26,3.35)(27,3.35)(28,3.55)(29,3.45)(30,3.75)(31,4.15)(32,4.0)(33,4.05)(34,4.25)(35,4.1)(36,3.85)(37,3.95)(38,4.15)(39,4.0)(40,4.0)(41,3.75)(42,3.35)(43,3.25)(44,3.9)(45,4.0)(46,4.0)(47,4.05)(48,3.85)(49,3.85)(50,3.45)(51,3.4)(52,3.45)(53,3.4)(54,3.3)(55,3.7)(56,4.0)(57,4.15)(58,3.95)(59,4.2)(60,4.3)(61,4.45)(62,4.55)(63,4.5)(64,3.95)(65,3.8)(66,3.75)(67,3.7)(68,3.75)(69,3.75)(70,3.8)(71,3.75)(72,3.7)(73,3.75)(74,3.6)(75,3.2)(76,3.25)(77,3.2)(78,3.05)(79,2.8)(80,2.9)(81,2.75)(82,2.85)(83,2.85)(84,2.85)(85,2.85)(86,3.15)(87,3.6)(88,3.9)(89,3.95)(90,4.05)(91,3.75)(92,3.75)(93,4.15)(94,4.15)(95,4.35)(96,4.1)(97,4.0)(98,4.1)(99,4.4)(100,4.25)(101,4.35)(102,4.15)(103,4.15)(104,4.1)(105,4.2)(106,4.0)(107,3.9)(108,3.65)(109,3.85)(110,3.7)(111,3.75)(112,3.8)(113,3.35)(114,3.55)(115,3.35)(116,3.3)(117,3.5)(118,3.6)(119,3.35)(120,3.3)(121,3.55)(122,3.75)(123,3.75)(124,4.0)(125,4.0)(126,3.95)(127,3.7)(128,3.6)(129,3.4)(130,3.65)(131,3.6)(132,3.55)(133,3.6)(134,3.3)(135,3.55)(136,3.55)(137,3.2)(138,3.1)(139,3.1)(140,3.2)(141,3.35)(142,3.2)(143,3.45)(144,3.2)(145,3.15)(146,3.25)(147,3.2)(148,3.5)(149,3.5)(150,3.4)(151,3.5)(152,4.1)(153,4.3)(154,4.35)(155,4.1)(156,4.0)(157,4.0)(158,4.15)(159,4.2)(160,4.2)(161,3.75)(162,3.8)(163,3.55)(164,3.7)(165,3.75)(166,3.6)(167,3.55)(168,3.35)(169,3.35)(170,3.3)(171,3.3)(172,2.7)(173,2.6)(174,2.6)(175,2.85)(176,3.05)(177,3.35)(178,3.1)(179,3.0)(180,3.05)(181,3.55)(182,3.7)(183,3.7)(184,4.2)(185,4.1)(186,4.2)(187,4.2)(188,4.1)(189,4.3)(190,4.25)(191,4.35)(192,4.45)(193,4.6)(194,4.6)(195,4.35)(196,4.4)(197,4.45)(198,4.45)(199,4.7)(200,4.75)(201,4.8)(202,4.6)(203,4.65)(204,4.05)(205,4.1)(206,4.05)(207,4.25)(208,4.3)(209,4.45)(210,4.55)(211,4.5)(212,4.65)(213,4.3)(214,4.75)(215,4.9)(216,4.6)(217,4.25)(218,4.5)(219,4.25)(220,4.35)(221,3.9)(222,4.15)(223,4.45)(224,4.5)(225,4.45)(226,4.35)(227,4.4)(228,4.35)(229,4.0)(230,3.9)(231,3.75)(232,3.7)(233,3.75)(234,3.45)(235,3.5)(236,3.5)(237,4.2)(238,4.0)(239,4.1)(240,3.85)(241,3.7)(242,3.4)(243,3.15)(244,3.1)(245,3.1)(246,3.4)(247,3.6)(248,3.7)(249,3.8)(250,3.85)(251,3.85)(252,3.7)(253,3.7)(254,3.65)(255,3.75)(256,4.0)(257,3.45)(258,3.55)(259,3.8)(260,3.95)(261,3.95)(262,4.0)(263,4.15)(264,4.2)(265,4.25)(266,3.95)(267,3.6)(268,3.5)(269,3.55)(270,3.45)(271,3.65)(272,3.9)(273,3.9)(274,4.2)(275,3.95)(1,4.6)(2,4.7)(3,5.15)(4,5.05)(5,5.1)(6,4.8)(7,4.7)(8,4.65)(9,4.7)(10,4.4)(11,4.0)(12,3.85)(13,3.65)(14,3.7)(15,3.85)(16,4.05)(17,3.8)(18,3.95)(19,4.0)(20,3.9)(21,3.65)(22,3.85)(23,3.5)(24,3.3)(25,3.3)(26,3.35)(27,3.35)(28,3.55)(29,3.45)(30,3.75)(31,4.15)(32,4.0)(33,4.05)(34,4.25)(35,4.1)(36,3.85)(37,3.95)(38,4.15)(39,4.0)(40,4.0)(41,3.75)(42,3.35)(43,3.25)(44,3.9)(45,4.0)(46,4.0)(47,4.05)(48,3.85)(49,3.85)(50,3.45)(51,3.4)(52,3.45)(53,3.4)(54,3.3)(55,3.7)(56,4.0)(57,4.15)(58,3.95)(59,4.2)(60,4.3)(61,4.45)(62,4.55)(63,4.5)(64,3.95)(65,3.8)(66,3.75)(67,3.7)(68,3.75)(69,3.75)(70,3.8)(71,3.75)(72,3.7)(73,3.75)(74,3.6)(75,3.2)(76,3.25)(77,3.2)(78,3.05)(79,2.8)(80,2.9)(81,2.75)(82,2.85)(83,2.85)(84,2.85)(85,2.85)(86,3.15)(87,3.6)(88,3.9)(89,3.95)(90,4.05)(91,3.75)(92,3.75)(93,4.15)(94,4.15)(95,4.35)(96,4.1)(97,4.0)(98,4.1)(99,4.4)(100,4.25)(101,4.35)(102,4.15)(103,4.15)(104,4.1)(105,4.2)(106,4.0)(107,3.9)(108,3.65)(109,3.85)(110,3.7)(111,3.75)(112,3.8)(113,3.35)(114,3.55)(115,3.35)(116,3.3)(117,3.5)(118,3.6)(119,3.35)(120,3.3)(121,3.55)(122,3.75)(123,3.75)(124,4.0)(125,4.0)(126,3.95)(127,3.7)(128,3.6)(129,3.4)(130,3.65)(131,3.6)(132,3.55)(133,3.6)(134,3.3)(135,3.55)(136,3.55)(137,3.2)(138,3.1)(139,3.1)(140,3.2)(141,3.35)(142,3.2)(143,3.45)(144,3.2)(145,3.15)(146,3.25)(147,3.2)(148,3.5)(149,3.5)(150,3.4)(151,3.5)(152,4.1)(153,4.3)(154,4.35)(155,4.1)(156,4.0)(157,4.0)(158,4.15)(159,4.2)(160,4.2)(161,3.75)(162,3.8)(163,3.55)(164,3.7)(165,3.75)(166,3.6)(167,3.55)(168,3.35)(169,3.35)(170,3.3)(171,3.3)(172,2.7)(173,2.6)(174,2.6)(175,2.85)(176,3.05)(177,3.35)(178,3.1)(179,3.0)(180,3.05)(181,3.55)(182,3.7)(183,3.7)(184,4.2)(185,4.1)(186,4.2)(187,4.2)(188,4.1)(189,4.3)(190,4.25)(191,4.35)(192,4.45)(193,4.6)(194,4.6)(195,4.35)(196,4.4)(197,4.45)(198,4.45)(199,4.7)(200,4.75)(201,4.8)(202,4.6)(203,4.65)(204,4.05)(205,4.1)(206,4.05)(207,4.25)(208,4.3)(209,4.45)(210,4.55)(211,4.5)(212,4.65)(213,4.3)(214,4.75)(215,4.9)(216,4.6)(217,4.25)(218,4.5)(219,4.25)(220,4.35)(221,3.9)(222,4.15)(223,4.45)(224,4.5)(225,4.45)(226,4.35)(227,4.4)(228,4.35)(229,4.0)(230,3.9)(231,3.75)(232,3.7)(233,3.75)(234,3.45)(235,3.5)(236,3.5)(237,4.2)(238,4.0)(239,4.1)(240,3.85)(241,3.7)(242,3.4)(243,3.15)(244,3.1)(245,3.1)(246,3.4)(247,3.6)(248,3.7)(249,3.8)(250,3.85)(251,3.85)(252,3.7)(253,3.7)(254,3.65)(255,3.75)(256,4.0)(257,3.45)(258,3.55)(259,3.8)(260,3.95)(261,3.95)(262,4.0)(263,4.15)(264,4.2)(265,4.25)(266,3.95)(267,3.6)(268,3.5)(269,3.55)(270,3.45)(271,3.65)(272,3.9)(273,3.9)(274,4.2)(275,3.95)

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试