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的更多相关文章
- SQL Server 合并复制遇到identity range check报错的解决
最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
随机推荐
- 序列化战争:主流序列化框架Benchmark
序列化战争:主流序列化框架Benchmark GitHub上有这样一个关于序列化的Benchmark,被好多文章引用.但这个项目考虑到完整性,代码有些复杂.为了个人学习,自己实现了个简单的Benchm ...
- MySQL注释中的sql也可能执行
MySql支持三种注释形式:# 和–属于单行注释,注释范围为该行的结尾:/* */注释属于多行注释,此外该种注释还可以实现行内注释.具体的使用情况如下图中所示(四种使用情形): 除此之外,/* */这 ...
- Linux 高性能服务器编程——Linux服务器程序规范
问题聚焦: 除了网络通信外,服务器程序通常还必须考虑许多其他细节问题,这些细节问题涉及面逛且零碎,而且基本上是模板式的,所以称之为服务器程序规范. 工欲善其事,必先利其器,这篇主要来探 ...
- How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer
How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer 1 ...
- shell 数据流重定向操作符总结
最近看了鸟哥私房菜关于shell数据流重定向的内容,总结一下. 操作符: 1.标准输入(stdin):代码为0,符号:< 或者<< 2.标准输出(stdout):代码为1,符号:&g ...
- JQuery其他常用函数
isArray(obj) 检测obj否为一个数组对象 isFunction(obj) 检测obj否为一个函数 isEmptyO ...
- Cocos2D在Xcode7和iOS 9.2上IMP调用出错
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 原来的代码一直在Xcode6.4上和iOS 8.4上运行,没有 ...
- Centos中git的安装
CentOS的yum源中没有git,只能自己编译安装,现在记录下编译安装的内容,留给自己备忘. 确保已安装了依赖的包 yum install curl yum install curl-deve ...
- TCP协议三次握手与四次挥手详解
在计算机网络的学习中TCPi协议与Http协议是我们必须掌握的内容,其中Tcp协议属于传输层,而Http协议属于应用层,本博客主要讲解Tcp协议中的三次握手与四次挥手,关于Http协议感兴趣的可以参看 ...
- Android简易实战教程--第三话《自己实现打电话》
需要一个文本输入框输入号码,需要一个按钮打电话.本质:点击按钮,调用系统打电话功能. xml布局文件代码:: <LinearLayout xmlns:android="http://s ...