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. Idea创建sbt项目

    这篇文章开始演示如何使用sbt新建项目. 1. 新建,选择Scala,SBT 填写项目名称,Scala版本号等信息. 单击完成后,后台会一直处于下载依赖文件的状态,等吧.. 最终后台运行完成后的完整目 ...

  2. shell命令之根据字符串查询文件对应行记录

    显示xxx字符串对应的行数,并向前打印3行,向后打印2行,查找对应文件为filename.txt 命令:grep -n 'xxx' -A3 -B2 --color=auto filename.txt ...

  3. m个苹果放在n个盘子里面有多少种放法?(动态规划)

    m个苹果放在n个盘子里面有多少种放法?(动态规划) 实现代码如下: #include <iostream> using namespace std; int s(int m ,int n) ...

  4. SGU 120 Archipelago (简单几何)

    120. Archipelago time limit per test: 0.25 sec.  memory limit per test: 4096 KB Archipelago Ber-Isla ...

  5. iOS边练边学--UIScrollView和xib文件实现简单分页+定时器初使用

    一.xib文件构成 二.自定义控件类(xib文件与自定义控件类的文件名字相同,并且将xib文件中父类控件的类名改成自定义控件类的名称) ***********自定义控件类需要的属性********** ...

  6. CodeIgniter(3.1.4)框架使用静态文件(js,css)

    调整目录结构: 可以在控制器中这样加载视图: * 加载url辅助类. views视图中可以这样引用静态文件: 则最终的静态文件url会生成这样:

  7. JetBrains PyCharm 4.0.4 key

    用户名 yueting3527 注册码 ===== LICENSE BEGIN ===== 93347-12042010 00001FMHemWIs"6wozMZnat3IgXKXJ 2!n ...

  8. 数据库 proc编程九

    第一种动态sql EXEC SQL EXECUTE IMMEDIATE :psql; .仅适用于非select语句 .嵌入SQL语句中不能包含输入宿主变量 void main() { EXEC SQL ...

  9. 关于Cocos2d-x的瓦片地图

    1.cocos2d-x的瓦片地图是用Tiled地图编辑器做的,这个软件开源,免费,一般都是用它制作瓦片地图. 2.瓦片地图是由块层和对象组成的,块层的作用是显示和一些重叠的时候覆盖角色的作用,而对象是 ...

  10. 基于<最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)>的一些个人总结

    最近因为项目接近收尾阶段,所以变的没有之前那么忙了,所以最近重新拿起了之前的一些FFMPEG和SDL的相关流媒体播放器的例子在看. 同时自己也用FFMPEG2.01,SDL2.01结合MFC以及网上罗 ...