LeetCode——Search for a Range
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的更多相关文章
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range(二分法)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- [LeetCode] Search for a Range 二分搜索
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode -- Search for a Range (TODO)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- LeetCode Search for a Range (二分查找)
题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- BusyBox编译配置
1. 下载Busybox tar包,如busybox-1.23.0.tar.bz2. 官网:http://www.busybox.net/ 2. make distclean:清除原有配置 ma ...
- Bootstrap3.0 栅格系统背后的精妙魔法(Bootstrap3.0的栅格布局系统实现原理)
这个标题取的有点奇怪,怪我翻译的有问题吧.英文学平有限,有道词典和google翻译齐上阵是必须的.还好翻译的不是小说,对于技术文章,还是能勉强翻过来的. 本文主要讲解了Bootstrap3.0的栅格布 ...
- log4j.properties的配置与详细说明
1. 新建测试项目testLog,并引入jar包. 2. 在src目录下新建log4j.properties文件,然后开始配置文件. log4j.rootLogger=INFO,stdout,debu ...
- CMake使用入门
一.开胃菜 hello目录下的文件结构: ├── CMakeLists.txt ├── hello.c ├── hello.h └── main.c C代码见下节. 最简单的cmake配置文件: pr ...
- Comet——随着AJAX技术兴起而产生的新技术
不得不说Ajax确实是一个好东西,由它的出现使得WEB端新技术不断产生,Comet就属于这么一个技术,这个技术有时叫做反向AJAX,有时叫做服务器"推"技术,嗯,不要被牛逼闪闪的名 ...
- HTTP协议详解(文档)
目录引言................................................................................................ ...
- CSS边框-属性详解
图解CSS padding.margin.border属性 W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层 ...
- 用Jquery获取checkbox多个选项
1,下拉框: var cc1 = $(".formc select[@name='country'] option[@selected]").text(); //得到下拉菜单的 ...
- jQuery的Cookie操作插件
jQuery的cookie插件 01 // jQuery.cookie.js 02 jQuery.cookie = function(name, value, options) { 03 if ...
- Maven学习日记(一)----构建web项目
创建maven-web项目: dos进入workspace目录: D:\workspace>mvn archetype:create -DgroupId=org.sonatype.mavenbo ...