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. VC++中动态链接库的显示加载和隐式加载的区别

    两种方法对于你的程序调用动态库时没有任何区别,只是你在编程时,步骤是不一样的.显式调用麻烦了点,但可以没有相应的lib库:隐式调用,使用起来比较 简单,有函数的声明(头文件.h)就可以了,但必须有li ...

  2. 一站式学习Wireshark(七):Statistics统计工具功能详解与应用

    Wireshark一个强大的功能在于它的统计工具.使用Wireshark的时候,我们有各种类型的工具可供选择,从简单的如显示终端节点和会话到复杂的如Flow和IO图表.本文将介绍基本网络统计工具.包括 ...

  3. iOS边练边学--(Quartz2D)图片添加水印

    一.给图片添加水印的基本步骤 加载图片 手动创建位图上下文 绘制原生的图片 给原生的图片添加文字 生成一张图片给我们,从上下文中获取图片 关闭上下文 二.注意:位图上下文的获取方式跟layer上下文不 ...

  4. C# 关于JArray和JObject封装JSON对象

    直入主题,不废话... 1.JObject:基本的json对象 /// <summary> /// Gets the j object. /// </summary> /// ...

  5. js类型转换 之 转数字类型

    手动将各类型 转换成 数字类型 JS提供了三种方法: Number(object); parseInt(string, radix); parseFloat(string, radix). 三种方法具 ...

  6. android listview 总结

    ScrollView与ListView冲突: public class MyExpandableListView extends ExpandableListView { public MyExpan ...

  7. 关于Cocos2d-x节点和精灵节点的坐标、位置以及大小的设置

    1.cocos2d-X中的坐标(0,0),就是运行框的左下角位置,所以运行框看起来就是一个第一象限. 2.节点的锚点就是我们setPosition所设定的位置,默认锚点是在节点的中心,也就是setPo ...

  8. e555. 在Applet中播放音频

    // See also e551 精简的Applet public void init() { // Load audio clip AudioClip ac = getAudioClip(getDo ...

  9. 每个 case 语句的结尾不要忘了加 break,否则将导致多个分支重叠

    每个 case 语句的结尾不要忘了加 break,否则将导致多个分支重叠 (除非有意使多个分支重叠). #include <iostream> /* run this program us ...

  10. 后序线索二叉树中查找结点*p的后继

    在后序线索二叉树中查找结点*p的后继: 1.若结点*p为根,则无后继:2.若结点*p为其双亲的右孩子,则其后继为其双亲:3.若结点*p为其双亲的左孩子,且双亲无右子女,则其后继为其双亲:4.若结点*p ...