LeetCode - Search For a Range
来源:互联网 发布:tomcat数据库配置文件 编辑:程序博客网 时间:2024/06/01 13:44
https://leetcode.com/problems/search-for-a-range/
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 int[] searchRange(int[] A, int target) { int[] rst = {-1, -1}; if(A==null || A.length==0) return rst; int left = 0, right = A.length-1; while(left<=right){ int mid=(left+right)/2; if(A[mid]==target){ int l = mid; while(l>0 && A[l-1]==target) l--; rst[0] = l; int r = mid; while(r<A.length-1 && A[r+1]==target) r++; rst[1] = r; return rst; } else if(A[mid]<target) left = mid+1; else right = mid-1; } return rst; }
http://fisherlei.blogspot.com/2013/01/leetcode-search-for-range.html
0 0
- LeetCode: Search for a Range
- LeetCode Search for a Range
- [Leetcode] Search for a Range
- LeetCode : Search for a Range
- [LeetCode] Search for a Range
- [Leetcode] Search for a Range
- Leetcode Search for a Range
- [LeetCode]Search for a Range
- LeetCode-Search for a Range
- [leetcode] Search for a Range
- LeetCode - Search for a Range
- Search for a Range leetcode
- LeetCode | Search for a Range
- LeetCode - Search for a Range
- LeetCode: Search for a Range
- leetcode: Search for a Range
- 【leetcode】Search for a Range
- Leetcode: Search for a Range
- eclipse修改JSP模板编码为UTF-8
- 凌大坑实验报告模板
- oracle笔记大全
- Spark下如何运行Java版本的WordCount
- 【后缀数组|最长回文子串】URAL-1297 Palindrome
- LeetCode - Search For a Range
- 每天一剂 WebView 良药
- 多个线程如何操作同一个epoll fd
- html 网页中插入视频没有画面只有声音的问题
- NYOJ-1043-高数
- window多线程简单实例
- 欢迎使用CSDN-markdown编辑器
- R语言函数union、setdiff、insect
- Swift语法基础:19 - Swift的控制转移的语句, Continue, Break, Fallthrough, Labeled Statements