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

Summary: Just a practice of binary search.

     vector<int> searchRange(int A[], int n, int target) {
vector<int> range(,-);
int start = ;
int end = n - ;
while(start <= end) {
int median = start + (end - start + ) / ;
if(A[median] > target) {
end = median - ;
}else if (A[median] < target) {
start = median + ;
}else { //equals
//go right
int i = median + ;
for(; i <= end; i ++) {
if(A[i] != target){
range[] = i - ;
break;
}
}
if(range[] == -)
range[] = i - ;
//go left
i = median - ;
for(; i >= start; i --) {
if(A[i] != target){
range[] = i + ;
break;
}
}
if(range[] == -)
range[] = i + ; break;
}
}
return range;
}

Search for a Range [LeetCode]的更多相关文章

  1. Search for a Range ——LeetCode

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  2. Search for a Range leetcode java

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  3. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  4. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  5. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  6. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

  7. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  8. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  9. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

随机推荐

  1. git生成SSH key

    使用https:// 提交GitHub上的私有项目时,需要每次都输入帐号和密码,比较麻烦. 比如我自己的笔记本,在push的时候我可不想每次都输入这些. 那就使用SSH吧,这个需要在GitHub的帐号 ...

  2. MySQL(五) —— 子查询

    子查询(SubQuery)是指出现在其他SQL语句内的SELECT语句. 如: SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2); 其中 SELE ...

  3. File Checksum Integrity Verifier

    Microsoft (R) File Checksum Integrity Verifier V2.05 README file =================================== ...

  4. Struts2-S2-032远程命令执行EXP

    看到网上好多写的EXP有后门啊  还是自己写个吧! 工具下载:链接: http://pan.baidu.com/s/1miRbi2k 密码: fzfv

  5. CUBRID学习笔记17 事务的回滚

    语法:ROLLBACK [ WORK ] 下面的语句会报错 ALTER TABLE code DROP s_name; INSERT INTO code (s_name, f_name) VALUES ...

  6. Java Base64加密、解密原理Java代码

    Java Base64加密.解密原理Java代码 转自:http://blog.csdn.net/songylwq/article/details/7578905 Base64是什么: Base64是 ...

  7. Javascript屏蔽回车提交表单

    html利用input防止回车提交 默认情况下,单个输入框,无论按钮的type="submit"还是type="button"类型,回车即提交. 1.当type ...

  8. Oracle中synonym和index

    笔记: Oracle-同义词--通过用户名(模式名).表名       --授权:grant create synonym to test1(system用户下授权))     --私有  creat ...

  9. c function

    /* #include<stdio.h> int is_prime(int n) { for(int i = 2; i <= n/2; i ++) if(n % 2 == 0) re ...

  10. java SE (java Standard Edition)

    14.10.22 学习java SE的Object: -------------------------------------15.11.18----