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) 级别. 如果数组中不存在 ...
随机推荐
- kettle、Oozie、camus、gobblin
kettle简介 http://www.cnblogs.com/limengqiang/archive/2013/01/16/KettleApply1.html Oozie介绍 http://blog ...
- hdu6021[BestCoder #93] MG loves string
这场BC实在是有趣啊,T2是个没有什么算法但是细节坑的贪心+分类讨论乱搞,T3反而码起来很顺. 然后出现了T2过的人没有T3多的现象(T2:20人,T3:30人),而且T2的AC率是惨烈的不到3% ( ...
- 【开发工具IDE】Eclipse相关配置
1. 修改workspace编码为UTF-8 1.1. 修改jsp编码为UTF-8 2. 修改字体 3. 添加系统中的JDK 4. 导入formatter模板 5. 修改maven配置文件 打开文件: ...
- OpenFlow协议中如何提高交换机流表的匹配成功率
写在前面 这段时间一直在研究如何提高流表空间的利用率.一直没能想到好的idea.有一篇文献中比较了现有研究中提到的手段,在这里记录一下都有哪些类型的手段以及这些手段存在的不足.这些手段不仅局限于如何提 ...
- redis的sort排序
Redis排序命令是sort,完整的命令格式如下:SORT key [BY pattern] [LIMIT start count] [GET pattern] [ASC|DESC] [ALPHA] ...
- Jenkins远程代码执行漏洞检查(CVE-2017-1000353)
Jenkins的反序列化漏洞,攻击者使用该漏洞可以在被攻击服务器执行任意代码,漏洞利用不需要任何的权限 漏洞影响范围: 所有Jenkins主版本均受到影响(包括<=2.56版本)所有Jenkin ...
- spark(二)
一.spark的提交模式 --master(standalone\YRAN\mesos) standalone:-client -cluster 如果我们用client模式去提交程序,我们在哪个地方 ...
- 解题:POI 2018 Prawnicy
题面 网上好像都是堆的做法啊......我这个不算离散化是$O(n)$的说(虽然有一坨vector可能不开O2会爆炸) 题目即是让我们求是否存在一个最长的是不少于$k$个给出区间子集的区间,如果存在输 ...
- hihocoder #1584 : Bounce
题意; 有一个n*m的网格阵,球从左上角开始在网格中碰撞,碰到边界就直角反弹,到达格子的角落结束,求途中经过一次的格子数. 代码: //神马规律啊,设x表示球与垂直面的撞击次数,y为球与水平墙面的撞击 ...
- python基础--文件操作实现全文或单行替换
python修改文件时,使用w模式会将原本的文件清空/覆盖.可以先用读(r)的方式打开,写到内存中,然后再用写(w)的方式打开. 替换文本中的taste 为 tasting Yesterday whe ...