34.Find First and Last Position of Element in Sorted Array---头条面试题、《剑指offer》38
题目大意:找出一串升序数组中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的更多相关文章
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 刷题34. Find First and Last Position of Element in Sorted Array
一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...
- [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 ...
- [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 ...
- (二分查找 拓展) 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 ...
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 34. Find First and Last Position of Element in Sorted Array
1. 原始题目 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在 ...
随机推荐
- bzoj2699 更新
题意 对于一个数列A[1..N],一种寻找最大值的方法是:依次枚举A[2]到A[N],如果A[i]比当前的A[1]值要大,那么就令A[1]=A[i],最后A[1]为所求最大值.假设所有数都在范围[1, ...
- 超链接提示效果jQuery+CSS+html
我们知道浏览器自带了超链接提示, 只需要在超链接中加入 title 属性就可以了. <a href="#" title="吉大砍人案致1死1伤 受害者死前大喊他手里 ...
- 《Unix网络编程卷1:套接字联网API》读书笔记
第一部分:简介和TCP/IP 第1章:简介 第2章:传输层:TCP.UDP和SCTP TCP:传输控制协议,复杂.可靠.面向连接协议 UDP:用户数据报协议,简单.不可靠.无连接协议 SCTP:流控制 ...
- 命令行下django-admin.py参数不起作用的问题解决
django官方turial中创建本地web站点时,使用如下命令 django-admin.py startproject mysite 说一下我在使用时碰到的几个问题: 1.无法找到django-a ...
- Cycle Sort (交换次数最少的排序)
该算法的效率并不高.但是却提供了一个很好的思路.如何让一个序列在最小交换次数下实现有序. Cycle Sort 翻译成中文是 圈排序. 这个圈在于需要交换的数据形成圈. 具体一点: 如: Array ...
- Mybatis笔记三:iBatis与MyBatis区别
转载:http://www.tuicool.com/articles/auaAru iBatis 框架的主要优势: 1.iBatis 封装了绝大多数的 JDBC 样板代码,使得开发者只需关注 SQL ...
- Linux内核分析5
周子轩 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.学习总结 通过gdb ...
- 排座位&&Little Elephant And Permutation——排列dp的处理
排列的问题,就是要把序列排个序,使之达到某种最优值或者统计方案数 dp可以解决部分排列问题. 通常的解决方案是,按照编号(优先级)排序决策,从左到右决策两种. 这里主要是第一个. 排座位• 有
- Dom4j 操作, 节点查找 添加 删除 修改 。。。xPath
转: Dom4j 操作, 节点查找 添加 删除 修改 ...xPath 2013年11月28日 10:48:59 今晚打酱油8 阅读数:8506更多 个人分类: JavaWeb 版权声明:本文为博 ...
- Codeforces 894.D Ralph And His Tour in Binary Country
D. Ralph And His Tour in Binary Country time limit per test 2.5 seconds memory limit per test 512 me ...