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) 级别. 如果数组中不存在 ...
随机推荐
- HDU4787_GRE Words Revenge
这个题目做得泪牛满面. 题目为给你若干串,有的表示添加一个串,有的表示询问一个串有多少个字串为前面出现过的串. 题目一看就知道肯定是AC自动机(不过后缀自动机也是可以的) 但是细想就会发现AC自动机好 ...
- Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)
题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...
- (转)Nginx图片服务器
本文转至博客http://wenxin2009.iteye.com/blog/2117079 Nginx搭建图片服务器 Nginx下载地址:http://nginx.org/en/download.h ...
- BZOJ1564 NOI2009二叉查找树(区间dp)
首先按数据值排序,那么连续一段区间的dfs序一定也是连续的. 将权值离散化,设f[i][j][k]为i到j区间内所有点的权值都>=k的最小代价,转移时枚举根考虑是否修改权值即可. #includ ...
- Static全局变量(函数)与普通的全局变量(函数)的区别
转自:http://www.cnblogs.com/zjvskn/p/5548879.html Static全局变量与普通的全局变量有什么区别? 答: 全局变量(外部变量)的说明之前再冠以static ...
- 洛谷P4609 [FJOI2016]建筑师 【第一类斯特林数】
题目链接 洛谷P4609 题解 感性理解一下: 一神带\(n\)坑 所以我们只需将除了\(n\)外的\(n - 1\)个元素分成\(A + B - 2\)个集合,每个集合选出最大的在一端,剩余进行排列 ...
- Linux内核设计与实现第四周读书笔记
第5章系统调用 5.1与内核通信 主要作用: 为用户控件提供了一种硬件的抽象接口. 保证了系统稳定性与安全性. 为用户空间&系统提供公共接口. 5.2API.POSIX和C库 一般情况,应用程 ...
- 函数式编程(1)-高阶变成(3)-sorted
sorted 排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大 ...
- @Springboot搭建项目controller层接收json格式的对象失败
今天在使用swagger2测试的时候出错 1.@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说: ...
- Bootsrap 直接使用
Bootstrap3 直接使用 <!DOCTYPE html> <html> <head> <title>Bootstrap3</title> ...