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) 级别. 如果数组中不存在 ...
随机推荐
- BZOJ 2141 排队(树状数组套主席树)
解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...
- OSPF虚连接简单配置
实验实例:<华为路由器学习指南>P715 本实例的拓扑结构如图:Area 2没有直接与骨干区域直接相连,Area 1被用作传输区域来连接Area 0与Area2.为了使Area2与骨干区域 ...
- Unified Networking Lab 安装使用IOL镜像
Unified Networking Lab 安装使用IOL镜像 Unified Networking Lab 很久以前,在一个星系远的地方,很远的工程师们为eBay寻找二手路由器来满足家庭实验的需求 ...
- 【BZOJ3566】概率充电器(动态规划)
[BZOJ3566]概率充电器(动态规划) 题面 BZOJ Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品--概率充电器: "采用全新纳米级加工 ...
- python学习笔记(七) 类和pygame实现打飞机游戏
python中类声明如下: class Student(object): def __init__(self, name, score): self.name = name self.score = ...
- python编码问题FAQ
http://note.youdao.com/noteshare?id=2cfb0ac4da042c2550aa3918beda81ec
- STL源码分析-deque
http://note.youdao.com/noteshare?id=66f21dca07c1984f41848700021644fd
- CountUp.js让页面数字跳动起来
CountUp.js 无依赖的.轻量级的 JavaScript 类,可以用来快速创建以一种更有趣的动画方式显示数值数据.尽管它的名字叫 countUp,但其实可以在两个方向进行变化,这是根据你传递的 ...
- JS中的匿名函数自执行、函数声明与函数表达式
先看一段jQuery源码中匿名函数自执行的例子: (function( window, undefined ) { // jquery code })(window); 另外一种常见的写法: +fun ...
- CSS全局声明*与body的区别
通常在写CSS前,为了保证兼容性及展示效果的一致,先会清除浏览器默认样式.此时有两种用法比较常用:1.*{}; 2.body{}; 之前我比较偏好于用*{},比较便于把控样式的设置.一直也没试过bo ...