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. ...
随机推荐
- 从钉钉微应用定制化导航栏看如何实现Hybrid App开发框架
钉钉是阿里的一款企业应用APP,里面提供了混合微应用的SDK,这其实最好的一种APP架构模式.微信公众号浏览器JSSDK也提供了类似功能特性,在在交互性上没有钉钉深入. http://ddtalk.g ...
- oozie中使用sqoop导入hive表时提示hive找不到
根据出错信息是找不到hive的位置,所以想到的解决方法是: 在sqoop的workflow中添加job-xml,属性写hive-site.xml在hdfs上的位置. (fail) 直接配置一个hive ...
- DevExpress gridcontrol学习知识点
.//允许自动合并单元格gv1.OptionsView.AllowCellMerge = true; 设置某列不合并 选中该列,在OptionsColumns-->AllowMerge中设置为f ...
- PHP变量解析顺序variables_order
转载自:http://blog.csdn.net/knight0450/article/details/4291556 故事从一个有点诡异的BUG开始,后台一个使用频率不是很高的广告提交功能有时候会莫 ...
- springmvc拦截器的配置、使用
springmvc拦截器的配置.使用:1.自定义拦截器,实现HandlerInterceptor接口. package com.bybo.aca.web.interceptor; import jav ...
- 关于只能上QQ而其他电脑软件(IE/优酷/腾讯视频...)不能联网的解决
1.应该是Winsock协议配置有问题,所以进行一下重置工作. 开始-cmd-输入netsh winsock reset命令来重置Winsock目录重新初始化网络环境来恢复网络畅通-重启电脑才能生效 ...
- Spring 4 官方文档学习(十一)Web MVC 框架
介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...
- Java 阻塞
对于用ServerSocket 及 Socket 编写的服务器程序和客户程序, 他们在运行过程中常常会阻塞. 例如, 当一个线程执行 ServerSocket 的accept() 方法时, 假如没有客 ...
- Java集合类相关面试题
1.Collection和Collections的差别 java.util.Collection 是一个集合接口,Collection接口在Java类库中有非常多详细的实现.比如List.Set ja ...
- CentOS7设置开机自启动命令大全
任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd.service 使某服务不自 ...