面试题38:数字在排序数组中出现的次数

问题说明:

1.给定排序的数组,例如从小到大已经排序。

2.给定某个查找关键字,求出该关键字出现的次数。

3.完全遍历一遍,时间复杂度为O(n),并未充分利用排序的信息。

4.需要借鉴二分查找的思想改进,算法的时间复杂度为O(logn)。

package com.zhang.csdn;
/**
 * @author pizi
 * @date 2014年1月7日 
 */
public class BinarySearchTest {

	/**@author pizi
	 * @param @param args
	 * @return void
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//int[] a = {1};
		//int[] a = {1,2};
		//int[] a = {1,2,3};
        int[] a = {2,4,5,7,11,11,15,16,16,16,17,18,19,19};
        
        System.out.println(countValue(a, 19));
	}
	// find the count of keyValue in sorted array 
	// not find return - 1 
	public static int countValue(int[] sorted, int keyValue){
		int len = 0;
		if(sorted == null ||(len = sorted.length) == 0){
			throw new IllegalArgumentException();
		}
		int firstPos = getFirstKey(sorted, 0, len - 1, keyValue);
		int lastPos = getLastKey(sorted, 0, len - 1, keyValue);
		
		boolean isFound = (firstPos != -1) && (lastPos != -1);
		return isFound? (lastPos - firstPos + 1) : -1;
	}
	private static int getLastKey(int[] sorted, int fromIndex, int toIndex, int keyValue){
		 if(fromIndex > toIndex){
			 return -1;
		 }
		 // fromIndex <= toIndex
		 int midIndex = (fromIndex + toIndex) >> 1;
		 int midValue = sorted[midIndex];
		 if(midValue < keyValue){
			 return getLastKey(sorted, midIndex + 1, toIndex, keyValue);
		 }
		 if(midValue > keyValue){
			 return getLastKey(sorted, fromIndex, midIndex - 1, keyValue);
		 }
		 //midValue == keyValue
		 boolean isLast = (midIndex == toIndex) ||((midIndex < toIndex) && (sorted[midIndex + 1] != keyValue));
		 
		 return isLast? midIndex : getLastKey(sorted, midIndex + 1, toIndex, keyValue);
		 
	}
	private static int getFirstKey(int[] sorted,int fromIndex, int toIndex, int keyValue){
		   if(fromIndex > toIndex){
			   return -1;
		   }
		   // fromIndex <= toIndex
		   int midIndex = (fromIndex + toIndex) >> 1;
		   int midValue = sorted[midIndex];
		   if(midValue < keyValue){
			   return getFirstKey(sorted, midIndex + 1, toIndex, keyValue);
		   }
		   if(midValue > keyValue){
			   return getFirstKey(sorted, fromIndex, midIndex - 1, keyValue);
		   }
		   // midValue == keyValue
		   boolean isFirst = (midIndex == fromIndex) || ((midIndex > fromIndex) && (sorted[midIndex - 1] != keyValue));
		   
		   return isFirst? midIndex : getFirstKey(sorted, fromIndex, midIndex - 1, keyValue);  
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值