Leetcode_34_Search for a Range

97 篇文章 0 订阅
94 篇文章 18 订阅

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44021767



Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].



思路:

(1)题意为给定一个排好序的int类型数组以及一个整数,求该整数在int数组中出现的范围。

(2)该题主要考察对排序数组中元素的查找。对于已排好序的数组,要查找给定的元素,首先应该想到的是二分查找。本文也是运用二分查找的思想。首先,对于只包含一个元素的数组进行判断并返回相应值;其次,创建一个大小为2的数组,并初始化为[-1,-1],用于存储目标元素在数组中的起始位置和终止位置;最后,使用二分查找算法对目标整数进行查找,如果没有查到目标整数,则返回[-1, -1];如果查找到了目标整数,由于目标函数可能在数组中连续出现了多次,所以需要从目标函数所在位置开始分别向前、向后进行查找可能存在的目标函数,向前直到数组第一个元素或出现非目标整数时停止,向后直到数组最后一个元素或出现非目标整数时停止,所得到的向前、向后遍历中最后出现的目标函数在数组中的下标,即为起始位置和终止位置,将其存入数组中,即为所得。

(3)详情见下方代码。希望本文对你有所帮助。



算法代码实现如下:

/**
 * 
 * @author liqq
 *
 */
public class Search_for_a_Range {
    public static int[] searchRange(int[] A, int target) {
    	if(A==null || A.length==0) return null;
    	
    	if(A.length==1){
    		int[] pos = new int[2];
    		if(A[0]==target){
    			pos[0]=pos[1]=0;
    			return pos;
    		}else{
    			pos[0]=pos[1]=-1;
    			return pos;
    		}
    	}
    	
    	int[] result = new int[2];
    	result[0] = -1;
    	result[1] = -1;
    	int end = A.length-1;
    	int start = 0;
    	//如果没找到返回默认值 找到了返回找到的位置
    	
    	//首先确定元素起始位置 然后在确定终止位置
    
    	while(start<=end){
    		int mid = start + ((end - start)>>1);
    	
    		if(A[mid]>target){
    			end = mid-1;
    		}else if(A[mid]<target){
    			start = mid +1;
    		}else if(A[mid]==target){
    			//找到了
    			//从该位置分别往前往后寻找
    			
    			//往前寻找
    			int head = 0;
    			int temp = mid;
    			
    			if(temp-1>=0){
        			while(temp-1>=0){
        				if(A[temp-1]==A[temp]){
        					head=temp-1;
        					if(temp-1==0){
        						head = 0;
        						result[0] = 0;
        						break;
        					}
        					temp = temp-1;
        				}else{
        					if(head==0){
        						result[0] =mid;
        						break;
        					}else{
        						result[0] = head < mid ? head : mid;
            					break;
        					}
        					
        				}
        			}
    			}else{
    				result[0]=0;
    			}
    			
    			//往后寻找
    			int last = 0;
    			int temp2 = mid;
    			if(temp2+1<=A.length-1){
    				while(temp2+1<=A.length-1){
    					if(A[temp2] == A[temp2+1]){
        					last = temp2+1;
        					if(temp2+1==A.length-1){
        						result[1] = temp2+1;
        						return result;
        					}
        					temp2 = temp2+1;
        				}else{
        					result[1] = last>temp2?last:temp2;
        					return result;
        				}
    				}
    			}else{
    				result[1] = A.length-1;
					return result;
    			}
    		}
    	}
    	
    	return result;
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值