34. Search for a Range

 
<p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">Given a sorted array of integers, find the starting and ending position of a given target value.</p><p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">Your algorithm's runtime complexity must be in the order of <span style="box-sizing: border-box;">O</span>(log <span style="box-sizing: border-box;">n</span>).</p><p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">If the target is not found in the array, return <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[-1, -1]</code>.</p><p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">For example,<br style="box-sizing: border-box;" />Given <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[5, 7, 7, 8, 8, 10]</code> and target value 8,<br style="box-sizing: border-box;" />return <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[3, 4]</code>.</p>
<p>
</p><p>public class Solution {</p>    public int[] searchRange(int[] A, int target) {
        int start = Solution.firstsearch(A, target);
		if (start == A.length || A[start] != target) {
			return new int[]{-1, -1};
		}
		return new int[]{start, Solution.firstsearch(A, target + 1) - 1};
	}
        public static int firstsearch(int []s,int target){
            int low=0;
            int high=s.length-1;
            while(low<=high){
                int mid=(low+high)/2;
                if(s[mid]<target){
                    low=mid+1;
                }
                else{
                    high=mid;
                }
            }
            return low;   目标值的最小序数是low
        }
}


public class Solution {
    public int[] searchRange(int[] nums, int target) {
         int[] ret= {-1, -1};
         int i=0; int j=nums.length-1;
         int m;

         while (i<j) {
             m=i+((j-i-1)>>1);
             if(nums[m] < target) {
                 i=m+1;
             } else {
                 j=m; //j是找到的目标值的最前的位置
             }
         }
         if(nums[j]==target) {
             ret[0] = j;    //如果有j==target 那就是目标值的第一个 如果不是那就没有目标值
             
             i=j; j=nums.length-1;
             while (i<j) {
                 m=i+((j-i+1)>>1);
                 if(nums[m] > target) {
                     j=m-1;
                 } else {
                     i=m;
                 }
             }
             ret[1] = i;
         }

         return ret;
    }
}

没写完。。







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值