题目:

给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1

样例

在数组 [1, 2, 3, 3, 4, 5, 10] 中二分查找3,返回2

挑战

如果数组中的整数个数超过了2^32,你的算法是否会出错?

解题:

利用二分查找,先判断左侧数据,满足条件则查找左侧,左侧不满足的时候在右侧找

当left>=right 时候表示没有找到返回 -1

当nums[left] ==target时候最左侧满足条件则找到了

再判断中位数是否满足条件

nums[median]==target and nums[median-1]!=target,return median

else:

对median的两侧进行判断

时间复杂度:O(logn)

Java程序:

class Solution {
/**
* @param nums: The integer array.
* @param target: Target to find.
* @return: The first position of target. Position starts from 0.
*/
public int binarySearch(int[] nums, int target) {
//write your code here
if(nums.length==1 && nums[0]== target)
return 0;
int numsLen = nums.length;
int index = binaryFind(nums,target,0,numsLen);
return index;
}
public int binaryFind(int[] nums,int target,int left,int right){
if(left>=right)
return -1;
if(nums[left]==target)
return left;
int median = (left+right)/2;
if(target==nums[median] && target!=nums[median-1])
return median;
if(nums[median]>=target)
return binaryFind(nums,target,left,median);
else if(nums[median]<target)
return binaryFind(nums,target,median+1,right);
return -1;
}
}

总耗时: 1504 ms

发现这里的运行时间,与我本地的网速关系很大。

直接线性查找,也是很简单的

    public int linFind(int[] nums ,int target){
int numsLen = nums.length;
for(int i=0;i<numsLen;i++)
if(nums[i]==target)
return i;
return -1;
}

总耗时: 1598 ms

耗时差不多的

当数组个数超过232 ,数组的下标是int型,已经越界,很大很大的时候median也会越界

Python程序:

class Solution:
# @param nums: The integer array
# @param target: Target number to find
# @return the first position of target in nums, position start from 0
def binarySearch(self, nums, target):
# write your code here
return self.linFind(nums,target)
# return self.binaryFind(nums,target,0,len(nums))
def linFind(self,nums,target):
for i in range(len(nums)):
if nums[i]==target:
return i
return -1 def binaryFind(self,nums,target,left,right):
if left>=right:
return -1
if nums[left]==target:
return left
median = (left+right)/2
if nums[median]==target and nums[median-1]!=target:
return median
if nums[median]>=target:
return self.binaryFind(nums,target,left,median)
elif nums[median]<target:
return self.binaryFind(nums,target,median+1,right)
else:
return -1

两个运行时间,也差不多的。

总耗时: 314 ms

总耗时: 303 ms

lintcode:Binary Search 二分查找的更多相关文章

  1. [01]Binary Search二分查找

    Binary Search二分查找 作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2) 1.Pyt ...

  2. LeetCode 704. Binary Search (二分查找)

    题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime:  0 ms, faster than 100 % Memory Usage ...

  3. STL模板整理 Binary search(二分查找)

    前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...

  4. 【算法模板】Binary Search 二分查找

    模板:(通用模板,推荐) 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. ...

  5. Leetcode704.Binary Search二分查找

    给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...

  6. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  7. 501. Find Mode in Binary Search Tree查找BST中的众数

    [抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...

  8. Codeforces Round #678 (Div. 2) C. Binary Search (二分,组合数)

    题意:有长度\(n\)的序列,让你构造序列,使得二分查找能在\(pos\)位置找到值\(x\).问最多能构造出多少种排列? 题解:题目给出的\(pos\)是固定的,所以我们可以根据图中所给的代码来进行 ...

  9. LintCode Binary Search

    For a given sorted array (ascending order) and a target number, find the first index of this number ...

随机推荐

  1. jquery循环table中tbody的tr中input:text,将值进行拼接传入控制器并返回状态和描述

    引用jquery $(function(){ $("#按钮id").click(function(){ var nums="";//变量 $("#ta ...

  2. 【Qt】Qt之自定义界面(QMessageBox)【转】

    简述 通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如:QWidget.QDialog.QMainWindow. 大多数窗体的实现都是采用控件堆积来完 ...

  3. 9更令人兴奋的WebGL演示

    Firefox OS,asm.js和推动浏览器性能改进,画布和WebGL技术是打开一个世界的可能性.我上9令人兴奋的帆布演示,然后把它与9 WebGL演示,但我想超越这篇文章.我发现9个更多的精神We ...

  4. [转]AIX下调整分区大小

    AIX下调整文件系统大小 - [work] 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://wangsuiri.blogbus.com/logs/35448074.htm ...

  5. 常用的php数组排序函数

    分享几个php数组排序函数,每个函数出去sort是排序的意思前缀字母的含义分别代表: a 索引 k 数组键 r 逆向 u 用户自定义 顺序排序函数 sort — 对数组排序  ksort — 对数组按 ...

  6. 一,XAML基础

    RuntimeNameProperty特性:为什么<Grid x:Name="grid1"></Grid>等价于<Grid Name="gr ...

  7. C# 获得手机归属地功能

    今天通过查资料了解到web的页面抓取功能,应用HttpWebRequest和HttpWebResponse功能,从http://www.showji.com网站中抓取归属地信息 应该说这个方法是从别的 ...

  8. C# 链接Sql和Access数据库语句

    1.sql数据库: 1.1.链接数据语句:server=localhost;database=Data; uid=sa;pwd=123; 或 Data Source=localhost;DataBas ...

  9. Excel REPT函数使用

    需要制作1K大小的数据 使用Excel REPT函数可以迅速制造 Excel REPT 函数 =REPT(1,1024) 结果直接黏贴进txt文件,注意删除尾空格.

  10. Python GUI编程实践

    看完了<python编程实践>对Python的基本语法有了一定的了解,加上认识到python在图形用户界面和数据库支持方面快捷,遂决定动手实践一番. 因为是刚接触Python,对于基本的数 ...