Description:

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

public class Solution {
public int[] searchRange(int[] nums, int target) {
int flag = -1, start=-1, end = -1;
for(int i=0; i<nums.length; i++) {
if(nums[i] == target) {
start = i;
for(int j=i; j<nums.length; j++) {
if(nums[j] != target) {
end = j-1;
flag = 1;
break;
}
if(nums[j]==target && j==nums.length-1) {
end = j;
flag = 1;
break;
}
}
}
if(flag == 1) {
break;
} } return new int[]{start, end};
}
}

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

  1. LeetCode: Search for a Range 解题报告

    Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...

  2. [LeetCode] Search for a Range 搜索一个范围

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

  3. [LeetCode] Search for a Range(二分法)

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

  4. leetcode Search for a Range python

    class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...

  5. [LeetCode] Search for a Range 二分搜索

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

  6. Leetcode Search for a Range

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

  7. leetcode:Search for a Range(数组,二分查找)

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

  8. leetcode -- Search for a Range (TODO)

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

  9. [LeetCode] Search for a Range [34]

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

  10. LeetCode Search for a Range (二分查找)

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

随机推荐

  1. [转]RabbitMQ,ActiveMQ,ZeroMQ,Kafka之间的比较与资料汇总

    MQ框架非常之多,比较流行的有RabbitMq.ActiveMq.ZeroMq.kafka.这几种MQ到底应该选择哪个?要根据自己项目的业务场景和需求.下面我列出这些MQ之间的对比数据和资料. 第一部 ...

  2. Html5之web workers多线程

    Web Workers 是 HTML5 提供的一个javascript多线程解决方式,我们能够将一些大计算量的代码交由web Worker执行而不冻结用户界面. 1.首先看一个实例: 1)js文件(t ...

  3. rails rake 版本问题

    rails rake 版本问题 通常情况下,如果我们电脑上同时装了不同版本的rake时,运行rake命令时会出错,如: rake db:migrate rake aborted! You have a ...

  4. Eclipse 中link一个异地的Folder

    Eclipse 中link一个外地的Folder New -> Folder -> Click "Advanced" --> Check "Link t ...

  5. 什么是服务端渲染、客户端渲染、SPA、预渲染,看完这一篇就够了

    服务端渲染(SSR) 简述:     又称为后端渲染,服务器端在返回html之前,在html特定的区域特定的符号里用数据填充,再给客户端,客户端只负责解析HTML.     鼠标右击点击查看源码时,页 ...

  6. 关于Cocos2d-x中数据的存储提取和类型转换

    1.获得存储在UserDefault中的变量,但是获得的变量是一个String类型的值,要用atoi函数转换为整型,但是atoi函数的传递参数是一个char*类型的值,所以用_Score.c_str( ...

  7. Unity3D深入浅出 -创造 物理材质(Physics Materials)

    在Unity3d中已经配置好了5种常用的物理材质,Bouncy.Ice.Metal.Rubber.Wood,在菜单中依次选择Assets - Import Package - Physics Mate ...

  8. linux -- ubuntu 通过命令行,设置文件及其子文件的权限

    想一次修改某个目录下所有文件的权限,包括子目录中的文件权限也要修改,要使用参数-R表示启动递归处理. 例如: [root@localhost ~]# chmod 777 /home/user 注:仅把 ...

  9. asp.net管线

  10. YII2中的Html助手和Request组件

    Html助手 1 .在@app\views\test的index.php中: <?php //引入命名空间 use yii\helpers\Html; ?> <?php //[一]表 ...