leetcode_34_Search for a Range
来源:互联网 发布:兄弟连it教育怎么样 编辑:程序博客网 时间:2024/06/02 03:57
欢迎转载,如有错误或疑问请留言纠正,谢谢
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].
//vs2012测试代码//Binary Search//we must know how to process the 3 cases below://1.how to find the right most target//2.how to find the left most target//3.how to find the insert position#include<iostream>#include<vector>using namespace std;#define N 6class Solution {public: vector<int> searchRange(int A[], int n, int target) { vector<int> ans(2);if(n==0)return vector<int>( 2,-1 );int left = 0 , right = n-1;while( left<=right ){int mid = left + (right-left)/2;if( A[mid] >= target) //find the left most targetright = mid-1;else if (A[mid] < target)left = mid+1;}int target_left = left;left=0 , right=n-1;while( left<=right ){int mid = left + (right-left)/2;if(A[mid] <= target) //find the right most targetleft = mid +1;else if ( A[mid] > target)right = mid -1;}int target_right = right;if( A[target_left]!=target || A[target_right]!=target)return vector<int>( 2,-1 );ans[0] = target_left;ans[1] = target_right;return ans; }};int main(){int a,target;int A[N];vector<int> ans(2);for(int i=0; i<N; i++){cin>>a;A[i]=a;}cin>>target;Solution lin;ans = lin.searchRange( A,N,target);for(int i=0; i<2; i++)cout<<ans[i];cout<<endl;}
//方法一:自测Accepted//Binary Search//we must know how to process the 3 cases below://1.how to find the right most target//2.how to find the left most target//3.how to find the insert positionclass Solution {public: vector<int> searchRange(int A[], int n, int target) { vector<int> ans(2);if(n==0)return vector<int>( 2,-1 );int left = 0 , right = n-1;while( left<=right ){int mid = left + (right-left)/2;if( A[mid] >= target) //find the left most targetright = mid-1;else if (A[mid] < target)left = mid+1;}int target_left = left;left=0 , right=n-1;while( left<=right ){int mid = left + (right-left)/2;if(A[mid] <= target) //find the right most targetleft = mid +1;else if ( A[mid] > target)right = mid -1;}int target_right = right;if( A[target_left]!=target || A[target_right]!=target)return vector<int>( 2,-1 );ans[0] = target_left;ans[1] = target_right;return ans; }};
1 0
- leetcode_34_Search for a Range
- Leetcode_34_Search for a Range
- Search for a Range
- Search for a Range
- Search for a Range
- Search for a Range !!!
- Search for a Range
- Search for a Range
- Search For A Range
- Search for a Range
- Search for a Range
- Search for a Range
- Search for a Range
- Search for a Range
- Search for a Range
- Search for a Range
- Search for a Range
- Search for a Range
- 2015.2.7--记录我的机器学习之路--现代启发式算法之遗传算法
- Python C++学习记录
- hibernate 保护链接的选项
- C++ 泛型算法
- [历年IT笔试题]百度2014校园招聘长沙站笔试题
- leetcode_34_Search for a Range
- 关于Oracle的内存问题
- 海量Web日志分析 用Hadoop提取KPI统计指标
- 初学Linux
- Hibernate组件映射配置
- QQ空间自动评论自动转发 微博自动自动转发神器带源码(超简单) 升级版
- Event Loop
- POJ 3264 Balanced Lineup(线段树水题)
- 【Unity】理解协程的原理1——实现一个自己的WaitForSeconds