Search for a Range & Search Insert Position leetcode

八股文背诵
• 阅读 1685

Search for a Range

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].

二分法

说明

用两次二分查找分别找出左右边界

复杂度

时间 O(logN) 空间 O(1)

代码

public int[] searchRange(int[] nums, int target) {
    int[] res = {-1, -1};
    if (nums == null || nums.length == 0) {
        return res;
    }
    int left = 0;
    int right = nums.length - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    int start = 0;
    int end = nums.length - 1;
    while(start <= end) {
        int mid = start + (end - start) / 2;
        if (nums[mid] > target) {
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    if (left <= end) {
        res[0] = left;
        res[1] = end;
    }
    return res;
}

Search Insert Position

Given a sorted array and a target value, return the index if the
target is found. If not, return the index where it would be if it were
inserted in order.

You may assume no duplicates in the array.

Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2 → 1 [1,3,5,6], 7
→ 4 [1,3,5,6], 0 → 0

二分法

复杂度

时间O(logn),空间复杂度O(1)

代码

public int searchInsert(int[] nums, int target) {
    if (nums == null || nums.length == 0) {
        return 0;
    }
    int left = 0, right = nums.length - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return left;
}
点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
待兔 待兔
1年前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Stella981 Stella981
4年前
Search Insert Position
\toc\题目链接SearchInsertPositionLeetCode(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fproblems%2Fsearchinsertposition%2F)注意点
Stella981 Stella981
4年前
Postman 使用方法详细介绍
1,下载安装:https://www.getpostman.com/apps2,打开Postman,如图所示:!(https://oscimg.oschina.net/oscnet/00f434cd831f2f74fea6f6d7b86bc46a751.png)3,创建一个接口项目!(https://oscimg.oschina.
Stella981 Stella981
4年前
35. Search Insert Position
Category:BinarySearch&ArrayGivenasortedarrayandatargetvalue,returntheindexifthetargetisfound.Ifnot,returntheindexwhereit
Stella981 Stella981
4年前
Elasticsearch Search API之(Request Body Search 查询主体)
preference查询选择副本分片的倾向性(即在一个复制组中选择副本的分片值。默认情况下,es以未指定的顺序从可用的碎片副本中进行选择,副本之间的路由将在集群章节更加详细的介绍。可以通过该字段指定分片倾向与选择哪个副本。preference可选值:\_primary只在节点上执行,在6.1.0版本后废弃,将在7.x版
Stella981 Stella981
4年前
HTML5新增input标签属性
一.inputtype属性1<formaction""2邮箱<inputtype"email"name""id""<inputtype"submit"value"提交"<br/<br/3手机号码<inputtype"tel"name
Stella981 Stella981
4年前
Nginx反向代理upstream模块介绍
!(https://oscimg.oschina.net/oscnet/1e67c46e359a4d6c8f36b590a372961f.gif)!(https://oscimg.oschina.net/oscnet/819eda5e7de54c23b54b04cfc00d3206.jpg)1.Nginx反
Stella981 Stella981
4年前
ElasticSearch5.0之后的改变
ES5的变化1.search\_typecount和scan都移除了2.count可以用size0代替GET/my_index/_search{"size":0,"aggs":{"my_terms":{"terms":{
Stella981 Stella981
4年前
Elasticsearch Search API 概述与URI Search
本节开始,将详细介绍SearchAPI的使用。SearchAPI概述详细API如下:publicfinalSearchResponsesearch(SearchRequestsearchRequest,RequestOptionsoptions)throwsIOExceptionpubl
八股文背诵
八股文背诵
Lv1
四张机,鸳鸯织就欲双飞,可怜未老头先白;春波碧草,晓寒深处,相对浴红衣。
文章
3
粉丝
0
获赞
0