本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. SQL Server 合并复制遇到identity range check报错的解决

        最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...

  2. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  3. [LeetCode] Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

  4. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  5. [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 ...

  6. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  7. [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 ...

  8. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

随机推荐

  1. DrawerLayout案例

    布局文件: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget ...

  2. pdflush进程详解

    一.简介     由于页高速缓存的缓存作用,写操作实际上会被延迟.当页高速缓存中的数据比后台存储的数据更新时,那么该数据就被称做脏数据.在内存中累积起来的脏页最终必须被写回磁盘.在以下两种情况发生时, ...

  3. android M Launcher之数据库实现

    前面一系列文章我们分析了LauncherModel的工作过程,它会把数据绑定到桌面上.从今天开始我们来分析下Launcher的数据来源即Launcher数据库的实现. 一个完整的数据库实现都应该包括两 ...

  4. sizeof(结构体)和内存对齐以及位域

    Win32平台下的微软C编译器的对齐策略: 1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除: 备注:编译器在给结构体开辟空间时,首先找到结构体中最宽的基本数据类型,然后寻找内存地址能被该 ...

  5. html5全解析

    htm是软件开发中非常基础的知识,也是很重要的知识,在web中是很重要的知识点,在此梳理一下主要内容: 1.HTML是什么? 全称为HyperText Markup Language,超文本标记语言, ...

  6. Bootstarp-table入门

    介绍 介绍什么的,大家自己去下面的网站看 Bootstrap中文网:http://www.bootcss.com/        Bootstrap Table Demo:http://issues. ...

  7. mysql进阶(二十六)MySQL 索引类型(初学者必看)

    mysql进阶(二十六)MySQL 索引类型(初学者必看)   索引是快速搜索的关键.MySQL 索引的建立对于 MySQL 的高效运行是很重要的.下面介绍几种常见的 MySQL 索引类型.   在数 ...

  8. SQLite 语法(http://www.w3cschool.cc/sqlite/sqlite-syntax.html)

    SQLite 语法 SQLite 是遵循一套独特的称为语法的规则和准则.本教程列出了所有基本的 SQLite 语法,向您提供了一个 SQLite 快速入门. 大小写敏感性 有个重要的点值得注意,SQL ...

  9. React Native之ViewPagerAndroid 组件

    概述 今天我们来讲解一下关于 ViewPager 的使用,它是一个允许子视图左右滚动翻页的容器.我们知道在Android开发中系统有ViewPager这个组件,作用是实现滚动翻页的,在RN中也是有这么 ...

  10. iOS开发之自己封装的提示框(警告框)样式BHAlertView

    最近需要使用到提示框(警告框)进行信息的展示和提醒,所以进行了一个类的封装,想用Swift调用此OC文件,但是发现有些困难,所以暂时先把OC代码进行展示,随后再好好研究一下在Swift中的使用. 对于 ...