leetcode 二分查找
https://oj.leetcode.com/problems/search-for-a-range/就是一个二分查找,没事练练手
- public class Solution {
- public int[] searchRange(int[] A, int target) {
- int a[]=new int[2];
- int ans=bSearch(A,target);
- if(ans==-1)
- {
- a[0]=-1;
- a[1]=-1;
- return a;
- }
- else
- {
- int beg=ans;
- int end=ans;
- while(beg>=0&&(A[beg]==A[ans]))
- {
- beg--;
- }
- while(end<A.length&&(A[end]==A[ans]))
- {
- end++;
- }
- a[0]=beg+1;
- a[1]=end-1;
- return a;
- }
- }
- public int bSearch(int[] A,int target)
- {
- int low=0;
- int high=A.length-1;
- while(low<=high)
- {
- int mid =(low+high)/2;
- if(A[mid]==target)
- {
- return mid;
- }
- else if(A[mid]>target)
- {
- high=mid-1;
- }
- else
- {
- low=mid+1;
- }
- }
- return -1;
- }
- }
2.另外一种二分查找 beg=0 end=len; end指向的是最后一个元素的后面,
- public class Solution {
- public int[] searchRange(int[] A, int target) {
- int a[]=new int[2];
- int ans=bSearch(A,target);
- if(ans==-1)
- {
- a[0]=-1;
- a[1]=-1;
- return a;
- }
- else
- {
- int beg=ans;
- int end=ans;
- while(beg>=0&&(A[beg]==A[ans]))
- {
- beg--;
- }
- while(end<A.length&&(A[end]==A[ans]))
- {
- end++;
- }
- a[0]=beg+1;
- a[1]=end-1;
- return a;
- }
- }
- public int bSearch(int[] A,int target)
- {
- int low=0;
- int high=A.length;
- while(low<high) //low<high不能等于
- {
- int mid =(low+high)/2;
- if(A[mid]==target)
- {
- return mid;
- }
- else if(A[mid]>target)
- {
- high=mid;
- }
- else
- {
- low=mid+1;
- }
- }
- return -1;
- }
- }
3.第一种方法的递归方式
- public class Solution {
- public int[] searchRange(int[] A, int target) {
- int a[]=new int[2];
- int ans=bSearch(A,target,0,A.length);
- if(ans==-1)
- {
- a[0]=-1;
- a[1]=-1;
- return a;
- }
- else
- {
- int beg=ans;
- int end=ans;
- while(beg>=0&&(A[beg]==A[ans]))
- {
- beg--;
- }
- while(end<A.length&&(A[end]==A[ans]))
- {
- end++;
- }
- a[0]=beg+1;
- a[1]=end-1;
- return a;
- }
- }
- public int bSearch(int[] A,int target,int low,int high)
- {
- while(low<high)
- {
- int mid =(low+high)/2;
- if(A[mid]==target)
- {
- return mid;
- }
- else if(A[mid]>target)
- {
- bSearch(A,target,low,mid);
- }
- else
- {
- bSearch(A,target,mid,high);
- }
- }
- return -1;
- }
- }
leetcode 二分查找的更多相关文章
- leetcode二分查找问题整理
自从做完leetcode上的三道关于二分查找的题后,我觉得它是比链表找环还恶心的题,首先能写出bugfree代码的人就不多,而且可以有各种变形,适合面试的时候不断挑战面试者,一个程序猿写代码解决问题的 ...
- [leetcode]二分查找总结
Search for a Range 1.最简单的想法,用最普通的二分查找,找到target,然后向左右扩张,大量的重复的target,就会出现O(n)效率. class Solution { pub ...
- leetcode 二分查找 Search in Rotated Sorted ArrayII
Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- LeetCode 二分查找模板 II
模板 #2: int binarySearch(vector<int>& nums, int target){ if(nums.size() == 0) return -1; in ...
- LeetCode 二分查找模板 I
模板 #1: int binarySearch(vector<int>& nums, int target){ if(nums.size() == 0) return -1; in ...
- LeetCode 二分查找模板 III
模板 #3: int binarySearch(vector<int>& nums, int target){ if (nums.size() == 0) return -1; i ...
- leetcode二分查找相关
目录 33/81搜索旋转排序数组 34在排序数组中查找元素的第一个和最后一个位置 35搜索插入位置 74搜索二维矩阵 300最长上升子序列,354俄罗斯套娃信封问题 33/81搜索旋转排序数组 假设按 ...
随机推荐
- 九度OJ 1370 数组中出现次数超过一半的数字
题目地址:http://ac.jobdu.com/problem.php?pid=1370 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2 ...
- Null Pointer --设计模式
在Joshua Bloch很有名的一本书<Effective in java>中建议不要在代码中返回空的collection/map/array,就像下面的代码一样: public Lis ...
- DIV+CSS 网页布局之:两列布局
1.宽度自适应两列布局 两列布局可以使用浮动来完成,左列设置左浮动,右列设置右浮动,这样就省的再设置外边距了. 当元素使用了浮动之后,会对周围的元素造成影响,那么就需要清除浮动,通常使用两种方法.可以 ...
- get值乱码(gbk编码浏览器造成)
$condition = urldecode($condition); 即可
- lucene解决全文检索word2003,word2007的办法
在上一篇文章中 ,lucene只能全文检索word2003,无法检索2007,并且只能加载部分内容,无法加载全文内容.为解决此问题,找到了如下方法 POI 读取word (word 2003 和 wo ...
- 快速生成json实体类
读取一个json文件,并与实体相对应: static void Main(string[] args) { string json = ""; FileStream fs = ne ...
- cocos2d-x笔记2: 编译到安卓的步骤与注意事项
博客地址: www.cnblogs.com/wolfred7464/ 不得不说,真心复杂,本篇博客总结的基本是最简最直接的步骤了,不用Cygwin和Ant的,当然用也可以... 以下用 %PROJEC ...
- 如何跳到系统设置界面-b
NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"]; if ([[UIApplication sharedApplicati ...
- Code First 指定外键名称
指定类外键有注释(DataAnnotation)和FluentAPI两种方式, 目前掌握的知识我们先把DataAnnotation可用的四种方式总结如下 第一种方法: //1-指定导航属性,会自动生成 ...
- 李洪强iOS开发Swift篇—09_属性
李洪强iOS开发Swift篇—09_属性 一.类的定义 Swift与Objective-C定义类的区别 Objective-C:一般需要2个文件,1个.h声明文件和1个.m实现文件 Swift:只需要 ...