LeetCode——Search for a Range
2015-06-28 00:47
375 查看
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
For example, Given
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}; } }
相关文章推荐
- DP项目开发随记5-攀爬初步完成
- 有了装逼大全,谁也阻止不了我装逼了
- 定义一个Block
- PHP本地实现虚拟域名访问:Apache虚拟主机配置
- leetcode解题报告228——Summary Ranges
- Zxing在CameraManager.get().closeDriver()报错
- Python 3.5 终于将 Type Hint(类型提示)官方化
- php页面静态化—优化页面响应时间
- Android横向滑动选项卡
- [综] Sparse Representation 稀疏表示
- SDWebImage
- 解决安装laravel/homestead vagrant环境报"A VirtualBox machine with the name 'homestead' already exists."的错误
- struts2 json includeProperties返回list为空
- 苹果Mac OS系统终端命令大全介绍
- Codeforces Round #310 (Div. 1) A B C
- 记一次理想浪漫的毕旅
- vijos 1054 牛场围栏 【想法题】
- 字符串右移函数
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点