题目:搜索目标范围

难度:Medium

题目内容

Given an array of integers nums sorted in ascending order, 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].

翻译

给定一个按升序排序的整数,找到给定目标值的起始和结束位置。

您的算法的运行时复杂性必须按照O(log n)的顺序。

如果在数组中找不到目标,返回[-1,-1]。

我的思路:数组、有序、查找、lgn————》二分法

    找到后用两个指针向两边移动,直到不等于,就是他的范围。

MyCode

     public int[] searchRange(int[] nums, int target) {
if (nums.length == 0) {
return new int[]{-1,-1};
}
int loc = binaryFind(nums, target);
if (loc == -1) {
return new int[]{-1,-1};
}
int start = loc;
int end = loc;
while (start-1 > -1 && nums[start-1] == nums[loc])
start--;
while (end+1 < nums.length && nums[end+1] == nums[loc])
end++;
return new int[]{start, end};
} static int binaryFind(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = low + (high - low)/2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}

我的复杂度:O(logN)+ O(N) = O(N)

因为后面的双向移动确实有可能将所有元素进行一次遍历,所以达不到O(logN)级别

编码过程中出现问题

1、length 写成了 lengh;

2、high的初始值设置成了length,应该为length-1;

3、 while(start-1 > -1 && nums[start-1] == nums[loc])  注意这个写法,如果将start--放入此处判断,那么最后的值就多减了1;

4、没找到的时候别忘了return [-1,-1]。

答案代码

     public int[] searchRange(int[] A, int target) {
int start = Solution.firstGreaterEqual(A, target);
if (start == A.length || A[start] != target) {
return new int[]{-1, -1};
}
return new int[]{start, Solution.firstGreaterEqual(A, target + 1) - 1};
} private static int firstGreaterEqual(int[] A, int target) {
int low = 0, high = A.length;
while (low < high) {
int mid = low + ((high - low) >> 1);
//A[low] <= target < A[high]
if (A[mid] < target) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}

答案复杂度:O(logN)

答案思路:在二分法的基础上做出了改进,即最后返回大于等于target的第一个数(所有target里最左边的那一个),

所以不仅当target>A[mid],并且target==A[mid]的时候,此时都应该向左边继续搜寻,

但是此时是令high = mid,而不是mid-1,是因为A[mid]<=target的时候,包括了A[mid]==target,此时如果取high = mid-1,则有可能将唯一的一个target给弄到右边去。

最后得到start,然后再将方法的target给+1再传入此方法,那么就能找到target右边的那一个数字的下标。

举个例子:

[1,2,3,5,5,9],target=5

首先定位到下标2——3,因为3<target,要找的点必定在右边:lo=mid+1

再定位下标4——5,因为5>=target,所以也要向左寻找:high=mid

。。。

扩展:我们也可以求的小于等于target的第一个数,即所有target最右边的那个数,那么当target>=A[mid]的时候,此时继续向右边搜索。。。

LeetCode第[34]题(Java):Search for a Range的更多相关文章

  1. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  6. LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

    题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word ...

  7. LeetCode第[33]题(Java):Search in Rotated Sorted Array

    题目:在翻转有序中搜索 难度:Medium 题目内容: Suppose an array sorted in ascending order is rotated at some pivot unkn ...

  8. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  9. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

随机推荐

  1. Python设置默认编码为UTF-8

    1.在Python\Lib\site-packages目录下创建一个sitecustomize.py文件 源代码: import sys sys.setdefaultencoding('utf-8') ...

  2. Eclipse Spring Tool Suite插件安装

    目录 Eclipse Spring Tool Suite插件安装 Eclipse Spring Tool Suite插件安装 1.登录网址:http://spring.io/tools/sts/all ...

  3. 史上最全Vim快捷键键位图 -- 入门到进阶

    文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) 本文原文地址:http://cenalulu.github.io/linux/all-vim-cheatshe ...

  4. delphi---EHlib第三方插件----TDBGridEH,TDBNumberEditEh,TDBComboBoxEh

    一.TDBGridEH 1.多选 行 options->dgMultiSelect 2.列字体改变颜色,OnDrawColumnCell写下方法. if Column.FieldName='价格 ...

  5. SQL 将列转成字符串并用逗号分隔

    SELECT STUFF((SELECT ',' + FieldName FROM TableName FOR XML PATH('')),1,1,'') AS T 其中的逗号可以换成其它字符 转换完 ...

  6. django博客项目2.建立 Django 博客应用

    建立博客应用 我们已经建立了 Django 博客的项目工程,并且成功地运行了它.不过到目前为止这一切都还只是 Django 为我们创建的项目初始内容,Django 不可能为我们初始化生成博客代码,这些 ...

  7. “技术产品”面向市场的表达方法思辨——BY Me

    “技术产品”面向市场的表达方法思辨 首先,我们来看看“技术产品”在面向市场去表达的时候,怎么表述是容易被市场接受和理解的,“目标受众是谁?”.“市场切入点是什么?”,做到有的放矢,打动目标受众.切中其 ...

  8. Open Source VOIP applications, both clients and servers (开源sip server & sip client 和开发库)

    SIP Proxies SBO SIP Proxy Bypass All types of Internet Firewall JAIN-SIP Proxy Mini-SIP-Proxy A very ...

  9. C#设置当前程序通过IE代理服务器上网

    注意:以下设置只在当前程序中有效,对IE浏览器无效,且关闭程序后,自动释放代码. using System; using System.Collections.Generic; using Syste ...

  10. java 程序cpu100%问题

    找到java应用进程 ID即 java_id 找到该 java_id对应的CPU占用比较大的线程 ID即 thread_id 使用jdk自带jstack工具打印跟该线程相关的堆栈信息 [root@pv ...