lintcode:Binary Search 二分查找
题目:
给定一个排序的整数数组(升序)和一个要查找的整数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 二分查找的更多相关文章
- [01]Binary Search二分查找
Binary Search二分查找 作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2) 1.Pyt ...
- LeetCode 704. Binary Search (二分查找)
题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime: 0 ms, faster than 100 % Memory Usage ...
- STL模板整理 Binary search(二分查找)
前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...
- 【算法模板】Binary Search 二分查找
模板:(通用模板,推荐) 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. ...
- Leetcode704.Binary Search二分查找
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- 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 ...
- Codeforces Round #678 (Div. 2) C. Binary Search (二分,组合数)
题意:有长度\(n\)的序列,让你构造序列,使得二分查找能在\(pos\)位置找到值\(x\).问最多能构造出多少种排列? 题解:题目给出的\(pos\)是固定的,所以我们可以根据图中所给的代码来进行 ...
- LintCode Binary Search
For a given sorted array (ascending order) and a target number, find the first index of this number ...
随机推荐
- Linux上iptables防火墙的基本应用
1.安装iptables防火墙 yum install iptables -y 2. 清除已有的iptables规则 iptables -F iptables -X iptables -Z 3.显示i ...
- 什么是AJAX技术及其常识
1.什么是Ajax? Ajax的全称是:AsynchronousJavaScript+XML 2.Ajax的定义: Ajax不是一个技术,它实际上是几种技术,每种技术都有其独特这处,合在一起就成了一个 ...
- Enhanced RCP: How views can communicate – The e4 way | Tomsondev Blog
Some weeks ago I published how views can communicate using the EventAdmin-Service. To get things wor ...
- 利用Apply的参数数组化来提高代码的优雅性,及高效性
利用Apply的参数数组化来提高代码的优雅性,及高效性 Function.apply()在提升程序性能方面的技巧 我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所 ...
- hdu 5545 The Battle of Guandu spfa最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...
- Oracle查询出最最近一次的一条记录
需求:从一个表中查询数据,得到的数据为最新的一条记录. -------------建立测试表 --drop table TB ),dtDate date) -------------插入测试数据 ,' ...
- 快书包CEO徐智明反思:我犯下哪些错误
新浪科技 刘璨 1月23日,快书包CEO徐智明在微博上公开“叫卖”快书包,在业内引起不小反响.这家创立于2010年要做“网上711”的创业公司,曾以独特的“一小时送达”服务在业内成为关注焦点. “如果 ...
- How to install DIG dns tool on windows 7
This guide explain how to install dig dns tool on windows 7 in few steps: 1. First go to http://www. ...
- 使用go语言后的感受
前两天我说过为了学习go语言去学习了一遍python,当我完成了python的学习后,昨天中午就去学习了go语言.以下简称之为golang. 我用的操作系统是windows xp,golang对xp还 ...
- js eval()执行传参函数的写法
.cs public class Message<T> { // 数据总数 public int? Total { get; set; } // 关键数据 public List<T ...