LeetCode--二分查找相关算法
-(1)有一个升序排列的非负数组,要求利用o(logn)的时间复杂度找到数组中确定数字target的第一次出现的位置下标和最后一次出现的位置下标,如果不存在该target返回[-1,-1]
解决方案:本题是考察二分查找算法,通过两次二分查找算法找到target的起始下标和终止下标,放在一个数组中返回
public int[] findFirstAndLastIndex(int[] nums,int target){
int[] res = new int[]{-1,-1};
if(nums==null || nums.length<=1||target<0)
return res;
res[0]=findFirst(nums,target);
res[1]=findLast(nums,target);
return res;
}
public int findFirst(int[] nums,int target){
int idx=-1;
int start=0,end=nums.length-1,mid=0;
while(start<=end){
mid=start+(end-start)/2;
if(nums[mid]>=target)
end=mid-1;
else
start=mid+1;
if(nums[mid]==target)
idx=mid;
}
return idx;
}
public int findLast(int[] nums,int target){
int idx=-1;
int start=0,end=nums.length-1,mid=0;
while(start<=end){
mid=start+(end-start)/2;
if(nums[mid]<=target)
start=mid+1;
else
end=mid-1;
if(nums[mid]==target)
idx=mid;
}
return idx;
}
(2) 每一个app开发出来后,我们需要定期的对其进行更新(不管是升级还是修改bug),假设app当前已有n个版本,如果某一个版本出现错误,那么从这个版本开始后面的版本全部会出错,要求我们找出第一个出现错误的版本
解决方案:还是二分查找,当我们当前的mid对于的版本是错误版本(假设给定一个函数isError(viersion),true为错误版本,flase为正确版本),我们就往mid的左半部分找到初始错误版本,否则,就往mid右半部分找到初始错误版本
public int findFirstErrorVersion(int n){
int start=1,end=n,mid=1;
while(start<end){
mid=start+(end-start)/2;//为什么不是mid=(start+end)/2
if(!isError(mid))
start=mid+1;
else
end=mid;
}
return start;
}
接下来,我解释一下上面的一个问题,为什么不能写成mid=(start+end)/2,因为,start,end均为整型数据,int型数据的最大值为2147483647,假如这里n=Integer.MAX_VALUE ,start=Integer.MAX_VALUE-1,end=Integer.MAX_VALUE,那么此时,(start+end)必须超过整型数据的返回,自然会出现问题,所以,一般情况下,最好还是写成start+(end-start)/2形式,何乐而不为呢?
补加:
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.
private int solveNLogN(int s, int[] nums) {
int[] sums = new int[nums.length + 1];
for (int i = 1; i < sums.length; i++) sums[i] = sums[i - 1] + nums[i - 1];
int minLen = Integer.MAX_VALUE;
for (int i = 0; i < sums.length; i++) {
int end = binarySearch(i + 1, sums.length - 1, sums[i] + s, sums);
if (end == sums.length) break;
if (end - i < minLen) minLen = end - i;
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
private int binarySearch(int lo, int hi, int key, int[] sums) {
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (sums[mid] >= key){
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return lo;
}
}
LeetCode--二分查找相关算法的更多相关文章
- leetcode二分查找相关
目录 33/81搜索旋转排序数组 34在排序数组中查找元素的第一个和最后一个位置 35搜索插入位置 74搜索二维矩阵 300最长上升子序列,354俄罗斯套娃信封问题 33/81搜索旋转排序数组 假设按 ...
- leetcode二分查找问题整理
自从做完leetcode上的三道关于二分查找的题后,我觉得它是比链表找环还恶心的题,首先能写出bugfree代码的人就不多,而且可以有各种变形,适合面试的时候不断挑战面试者,一个程序猿写代码解决问题的 ...
- leetcode 二分查找
https://oj.leetcode.com/problems/search-for-a-range/就是一个二分查找,没事练练手 public class Solution { public in ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- [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 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 ...
随机推荐
- Android 开机启动服务
在xml中注册 <!-- 开机广播 --> <receiver android:name=".receiver.BootBroadcastReceiver"> ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource
spring boot web项目运行时提示如下错误 org.springframework.beans.factory.BeanCreationException: Error creating b ...
- SQLite C/C++ 教程
目录 1安装 2 C/C++ Interface APIs 3连接到数据库 4创建表 5插入操作 6更新操作 7删除操作 安装 在我们开始使用SQLite在C / C++程序,我们需要确保SQLite ...
- (六)使用Docker镜像(下)
1. 创建镜像 创建镜像的方法有三种: 基于已有镜像的容器创建 基于本地模板导入 基于Dockerfile创建 1.1 基于已有镜像的容器创建 该方法主要是使用docker commit命令,其格式 ...
- MATLAB——解数独
数独 数独是一种逻辑游戏,玩家需要根据9x9盘面的已知数字,推理出剩余所有空格的数字,并满足每一行.每一列和每个粗线宫(3x3)内均含1~9,不重复. MATLAB中有关函数 M = dlmread( ...
- HTML5拖放(drag和drog)作品
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- iOS之WKWebView
Xcode8发布以后,编译器开始不支持IOS7,所以很多应用在适配IOS10之后都不在适配IOS7了,其中包括了很多大公司,网易新闻,滴滴出行等.因此,我们公司的应用也打算淘汰IOS7. 支持到IOS ...
- UIScreen, UIWindow
模仿书上或网上的例子,每次最开始就是 在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: ...
- Xcode及Mac快捷键
1. 文件 CMD + N: 新文件CMD + SHIFT + N: 新项目CMD + O: 打开CMD + S: 保存CMD + SHIFT + S: 另存为CMD + W: 关闭窗口CMD + S ...
- awk纯干货
AWK的惊人表现: Awk设计的目的:简化一般文本处理的工作. 属于POSIX的一部分. AWK命令行: Awk的调用可以定义变量.提供程序并且指定输入文件: Awk [ -F fs ] [ -v ...