题目链接

题目大意:找出一串升序数组中target值的起始下标和结束下标值,如果不存在则返回{-1,-1}。

解法一:用二分查找,找到数组中的target,然后找其左边和右边的target下标值。代码如下(耗时11ms):

     public int[] searchRange(int[] nums, int target) {
if(nums == null || nums.length == 0) {
int[] r = {-1, -1};
return r;
}
int low = 0, high = nums.length - 1;
int start = -1, end = -1;
while(low <= high) {
int mid = (low + high) / 2;
if(nums[mid] < target) {
low = mid + 1;
}
else if(nums[mid] > target) {
high = mid - 1;
}
else {
//找左边起始下标
for(int i = mid; i >= 0; i--) {
if(nums[i] == target) {
start = i;
}
else {
break;
}
}
//找右边终止下标
for(int i = mid; i < nums.length; i++) {
if(nums[i] == target) {
end = i;
}
else {
break;
}
}
break;
}
}
int[] res = {start, end};
return res;
}

解法二:直接暴力,一次遍历,找重复值。代码如下(耗时10ms):

     public int[] searchRange(int[] nums, int target) {
int start = -1, end = -1;
boolean mark = false;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == target) {
if(mark == false) {
start = end = i;
mark = true;
}
else {
end = i;
}
}
}
int[] res = {start, end};
return res;
}

解法三:真正的二分查找。法一其实复杂度还是o(n)。应该先对起始下标进行二分查找,然后再对结束下标进行二分查找。代码如下(耗时5ms):

     public int[] searchRange(int[] nums, int target) {
int left = 0, right = nums.length - 1;
int[] res = {-1, -1};
if(nums.length == 0) {
return res;
}
//二分找到起始下标
while(left < right) {
int mid = (left + right) / 2;
//这里比较左值,如果<,则left更新,否则left不会更新
//所以left不更新有两种情况:>或=
if(nums[mid] < target) {
left = mid + 1;
}
//这里统统修改右值
else {
right = mid;
}
}
if(nums[left] != target) {
return res;
}
res[0] = left;
left = 0;
right = nums.length - 1;
//二分找到结束下标
while(left < right) {
//这里要+1,否则会出错
int mid = (left + right) / 2 + 1;
//比较右值,如果>,则right更新
if(nums[mid] > target) {
right = mid - 1;
}
//修改Left
else {
left = mid;
}
}
res[1] = right;
return res;
}

34.Find First and Last Position of Element in Sorted Array---头条面试题、《剑指offer》38的更多相关文章

  1. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  2. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  3. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  4. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  5. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  6. [leetcode]34.Find First and Last Position of Element in Sorted Array找区间

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  7. leetcode [34] Find First and Last Position of Element in Sorted Array

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  8. 34. Find First and Last Position of Element in Sorted Array (JAVA)

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  9. 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  10. 34. Find First and Last Position of Element in Sorted Array

    1. 原始题目 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在 ...

随机推荐

  1. MSSQL DBA权限获取WEBSHELL的过程

    前言 本文主要通过一个案例来演示一下当MSSQL是DBA权限,且不知道路径的时候如何去获取WEBSHELL.当然这种方式对站库分离的无效.我测试的环境是在Win7 64位下,数据库是SQLServer ...

  2. MyBatis之自查询,使用 递归实现 N级联动

    A:首先先看下一个简单的面试题 斐波那契数列 计算数组{1,1,2,3,5,8.......} 第30位值 规律:1 1 从第三项开始,每一项都是前两项之和 有两种实现方式 第一种方式: public ...

  3. [知识点]C++中STL容器之set

    零.STL目录 1.容器之map 2.容器之vector 3.容器之set 一.前言 继上期的vector之后,我们又迎来了另一个类数组的STL容器——set. 二.用途与特性 set,顾名思义,集合 ...

  4. oracle-DECODE()函数

    DECODE()函数 DECODE(value, if1, then1, if2,then2, if3,then3, . . . else ) 含义解释: DECODE(条件,值1,翻译值1,值2,翻 ...

  5. 图像GIF格式介绍

    1 图像GIF格式工作原理 GIF是用于压缩具有单调颜色和清晰细节的图像(如线状图.徽标或带文字的插图)的标准格式. GIF(Graphics InterchangeFormat)的原义是“图像互换格 ...

  6. Python使用redis介绍

    一.Redis的介绍 redis是业界主流的key-value nosql 数据库之一.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).se ...

  7. 前端PHP入门-019-内置函数之数学函数-很重要

    查看帮助文档为主 函数名 描述 实例 输入 输出 abs() 求绝对值 $abs = abs(-4.2); //4.2 数字 绝对值数字 ceil() 进一法取整 echo ceil(9.999); ...

  8. Ubuntu 火狐浏览器中,鼠标选择文字被删除的解决办法

    copy from :http://blog.csdn.net/shadow066/article/details/50628019 在终端中输入命令:ibus-setup 将 “在应用程序窗口中启用 ...

  9. ZOJ 3780 E - Paint the Grid Again 拓扑排序

    https://vjudge.net/problem/49919/origin 题意:给你n*n只出现O和X的字符阵.有两种操作,一种操作Ri将i行全变成X,一种操作Ci将i列全变成O,每个不同的操作 ...

  10. HDU 4990 Reading comprehension 简单矩阵快速幂

    Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...