Leetcode_34_Search for a Range
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44021767
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
思路:
(1)题意为给定一个排好序的int类型数组以及一个整数,求该整数在int数组中出现的范围。
(2)该题主要考察对排序数组中元素的查找。对于已排好序的数组,要查找给定的元素,首先应该想到的是二分查找。本文也是运用二分查找的思想。首先,对于只包含一个元素的数组进行判断并返回相应值;其次,创建一个大小为2的数组,并初始化为[-1,-1],用于存储目标元素在数组中的起始位置和终止位置;最后,使用二分查找算法对目标整数进行查找,如果没有查到目标整数,则返回[-1, -1];如果查找到了目标整数,由于目标函数可能在数组中连续出现了多次,所以需要从目标函数所在位置开始分别向前、向后进行查找可能存在的目标函数,向前直到数组第一个元素或出现非目标整数时停止,向后直到数组最后一个元素或出现非目标整数时停止,所得到的向前、向后遍历中最后出现的目标函数在数组中的下标,即为起始位置和终止位置,将其存入数组中,即为所得。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
/*** * @author liqq**/
public class Search_for_a_Range {public static int[] searchRange(int[] A, int target) {if(A==null || A.length==0) return null;if(A.length==1){int[] pos = new int[2];if(A[0]==target){pos[0]=pos[1]=0;return pos;}else{pos[0]=pos[1]=-1;return pos;}}int[] result = new int[2];result[0] = -1;result[1] = -1;int end = A.length-1;int start = 0;//如果没找到返回默认值 找到了返回找到的位置//首先确定元素起始位置 然后在确定终止位置while(start<=end){int mid = start + ((end - start)>>1);if(A[mid]>target){end = mid-1;}else if(A[mid]<target){start = mid +1;}else if(A[mid]==target){//找到了//从该位置分别往前往后寻找//往前寻找int head = 0;int temp = mid;if(temp-1>=0){while(temp-1>=0){if(A[temp-1]==A[temp]){head=temp-1;if(temp-1==0){head = 0;result[0] = 0;break;}temp = temp-1;}else{if(head==0){result[0] =mid;break;}else{result[0] = head < mid ? head : mid;break;}}}}else{result[0]=0;}//往后寻找int last = 0;int temp2 = mid;if(temp2+1<=A.length-1){while(temp2+1<=A.length-1){if(A[temp2] == A[temp2+1]){last = temp2+1;if(temp2+1==A.length-1){result[1] = temp2+1;return result;}temp2 = temp2+1;}else{result[1] = last>temp2?last:temp2;return result;}}}else{result[1] = A.length-1;return result;}}}return result;}
}
Leetcode_34_Search for a Range相关推荐
- [转]python各模块的学习
[-] 01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 ...
- Go 知识点(06)— for range 切片并对切片进行 append 不会造成死循环
想想下面代码会发生什么? func main() {a := []int{1, 2, 3}for _, v := range a {a = append(a, v)fmt.Println(" ...
- Go 学习笔记(63)— Go 中的 for ... range 对切片和数组的差异
1. 迭代对象是切片,迭代过程中修改切片 package mainimport "fmt"func main() {// 示例1.numbers1 := []int{1, 2, 3 ...
- pytorch.range() 和 pytorch.arange() 的区别
总结: torch.range(start=1, end=6) 的结果是会包含end的, 而torch.arange(start=1, end=6)的结果并不包含end. 两者创建的tensor的类型 ...
- Go 中 time.Parse 报错:year/month/day hour/minute/second out of range 时间格式化为什么是 2006-01-02 15:04:05?
1. 问题现象 在使用 Go 语言的 time.Parse 解析时间时遇到以下错误: func main() {timeParse, err := time.Parse("2006-11-0 ...
- Go 学习笔记(29)— range 作用于字符串、数组、切片、字典、通道
1. 使用说明 range 应用于不同数据类型时,类似迭代器操作,返回 (索引, 值) 或 (键, 值). 下表是对应的结构: type 1st value 2nd value string inde ...
- Go 学习笔记(9)— 循环(for、for range访问数组、切片、字符串和通道、goto、continue、break)
1. for 循环格式 Go 语言仅支持一种循环语句即 for 循环,它有 3 种形式,只有其中的一种使用分号. 和 C 语言的 for 一样 for init; condition; post { ...
- usaco Home on the Range
dp关系式搞清楚就行了,我看题解的没想出来.a[i][j]=min(a[i+1][j],a[i][j+1],a[i+1][j+1])+1;他的右方下方右下方保证了他可以延伸的长度. /* ID: ji ...
- opencv中Range类的使用
#include<iostream> #include<opencv2/opencv.hpp> #include<vector> using namespace s ...
最新文章
- 微软Win8将有多个版本 四款面向ARM平台
- 联想筹资13.5亿美元 支付收购摩托罗拉移动剩余款
- Windows下使用apache模块实现合并多个js、css提高网页加载速度
- 一些SqlServer常见和入门操作
- 计算机原理期中考试答案,微机原理与接口技术期中考试题(带答案)
- python工程师工资多少-2018年涨工资了吗?Python 工程师薪资最新出炉
- 支持-vsdoc.js的jQuery智能提示的VS2008 SP1补丁发布
- 【转】如何有效地记忆与学习
- Linux之Shell编程学习总结
- html让字数超过多少,css强制省略号 css设置超过多少个字显示省略号
- 标准单元库的corner简述
- 手机尾号(与网上有点不一样的代码)
- 普通云硬盘,高性能云硬盘和SSD云硬盘三者之间有什么区别?
- 小学数学奥林匹克试题集锦 bt
- 从输入URL到页面加载…
- 比较好用的linux桌面系统,目前最好用的Linux桌面系统Mint
- 【BDWP不限速小妙招】真测有效
- 微信小程序图片裁剪工具we-cropper
- 苏宁易购开放平台_前三季度线上规模突破2000亿,苏宁易购三季度盈利7.14亿
- 微信小程序获取rich-text(富文本)渲染内容高度,rich-text(富文本)里img 样式设置