二分法:通过O(1)的时间,把规模为n的问题变为n/2。T(n) = T(n/2) + O(1) = O(logn)。

基本操作:把长度为n的数组,分成前区间和后区间。设置start和end下标。int start = 0, end = nums.length - 1。循环结束条件为start + 1 < end ,即相邻时结束循环,所以最后需判断start和end中哪个是目标值。指针变化为start = mid,取后半区间;end = mid,取前半区间。

经典二分搜索:1. First postion of target:找到目标后取前半区间继续搜索

2. Last position of target: 找到目标后取后半区间继续搜索

public int binarySearch(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
} int start = 0, end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] == target) {
end = mid;
} else if (nums[mid] < target) {
start = mid;
} else {
end = mid;
}
} if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
return -1;
}
public int lastPosition(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
} int start = 0, end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] == target) {
start = mid;
} else if (nums[mid] < target) {
start = mid;
} else {
end = mid;
}
} if (nums[end] == target) {
return end;
}
if (nums[start] == target) {
return start;
}
return -1;
}

【lintcode459】排序数组中最接近元素。

public int closestNumber(int[] A, int target) {
if (A == null || A.length == 0) {
return -1;
} int index = firstIndex(A, target);
if (index == 0) {
return 0;
}
if (index == A.length) {
return A.length - 1;
} if (target - A[index - 1] < A[index] - target) {
return index - 1;
}
return index;
} private int firstIndex(int[] A, int target) {
int start = 0, end = A.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (A[mid] < target) {
start = mid;
} else if (A[mid] > target) {
end = mid;
} else {
end = mid;
}
} if (A[start] >= target) {
return start;
}
if (A[end] >= target) {
return end;
}
return A.length;
}

【lintcode28】搜索二维矩阵

思路:先寻找相应行数,可利用二分法,本人使用直接查找法,只是复杂度较高。需要注意的是要用某行末位数和target比较,而不是行数首位数。

public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
if(matrix == null || matrix.length == 0 ||matrix[0].length == 0){
return false;
}
int start = 0;
int row = 0;
int column = matrix[0].length - 1;
while(matrix[row][column]<target && row < matrix.length-1){
row ++;
} int end = matrix[0].length-1; while(start + 1 < end){
int mid = start + (end - start)/2;
if(matrix[row][mid] == target){
return true;
}
else if(matrix[row][mid] < target){
start = mid;
}
else{
end = mid;
}
}
if(matrix[row][start] == target){
return true;
}
if(matrix[row][end] == target){
return true;
}
return false;
}

O(logn+logm)参考答案

// Binary Search Twice
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) {
return false;
}
if (matrix[0] == null || matrix[0].length == 0) {
return false;
} int row = matrix.length;
int column = matrix[0].length; // find the row index, the last number <= target
int start = 0, end = row - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (matrix[mid][0] == target) {
return true;
} else if (matrix[mid][0] < target) {
start = mid;
} else {
end = mid;
}
}
if (matrix[end][0] <= target) {
row = end;
} else if (matrix[start][0] <= target) {
row = start;
} else {
return false;
} // find the column index, the number equal to target
start = 0;
end = column - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (matrix[row][mid] == target) {
return true;
} else if (matrix[row][mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (matrix[row][start] == target) {
return true;
} else if (matrix[row][end] == target) {
return true;
}
return false;
}
} // Binary Search Once
public class Solution {
/**
* @param matrix, a list of lists of integers
* @param target, an integer
* @return a boolean, indicate whether matrix contains target
*/
public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
if(matrix == null || matrix.length == 0){
return false;
} if(matrix[0] == null || matrix[0].length == 0){
return false;
} int row = matrix.length;
int column = matrix[0].length; int start = 0, end = row * column - 1;
while(start <= end){
int mid = start + (end - start) / 2;
int number = matrix[mid / column][mid % column];
if(number == target){
return true;
}else if(number > target){
end = mid - 1;
}else{
start = mid + 1;
}
} return false; }
}


【lintcode】 二分法总结 I的更多相关文章

  1. 【lintcode】二分法总结 II

    Half and Half 类型题 二分法的精髓在于判断目标值在前半区间还是后半区间,Half and Half类型难点在不能一次判断,可能需要一次以上的判断条件. Maximum Number in ...

  2. 【刷题笔记】--lintcode木头加工(java)

    木头加工 题目描述 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 注意事项 木头 ...

  3. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

  4. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  5. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  6. lintcode 刷题 by python 总结(1)

    博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...

  7. Leetcode Lect3 二分法总结

    二分法模板 非递归版本: public class Solution { /** * @param A an integer array sorted in ascending order * @pa ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

随机推荐

  1. Global.asax

    ASP.NET Global.asax 文件使用方法 - .net 标签:               asp.net.netapplicationauthenticationsessionobjec ...

  2. 通过Charles获取看书神器API

    Charles Charles是一个可以做HTTP代理/ HTTP监视器/反向代理的软件,使开发人员能够查看其机器和Internet之间的所有HTTP和SSL / HTTPS流量.包括请求,响应和HT ...

  3. js 手动插入meta标签和script标签

    // 插入 meta 标签 var oMeta = document.createElement('meta'); oMeta.content = 'width=device-width, initi ...

  4. domain

    babibobucecicudadedidufafugeguhehujijukakekulalelilumimomunapapipopuqiqurerirusasesisutatetituwawowu ...

  5. Haproxy官方文档翻译(第二章)配置Haproxy 附英文原文

    2.配置 HAProxy 2.1 配置文件格式 Haproxy的配置过程包含了3部分的参数资源:- 命令行中的参数,此种参数总是享有优先权被使用- 配置文件中global节点中的参数,此种参数是进程范 ...

  6. P4174 [NOI2006]最大获利(网络流)

    P4174 [NOI2006]最大获利 还是最大权闭合子图的题 对于每个中转站$k$:$link(k,T,P_k)$ 对于每个用户$i$.中转站$A_i,B_i$.贡献$C_i$ $link(S,i, ...

  7. 记一次JAVAWEB项目部署

    需求 原本服务器上tomcat部署了一个javaweb项目在80端口,这次要部署另一个javaweb项目在8090端口,或者同时部署在同一端口不同目录下. 解决方法 不同端口部署 不同端口部署我们需要 ...

  8. 【题解】Luogu P3674 小清新人渣的本愿

    原题传送门 这题还算简单(我记得我刚学oi时就来写这题,然后暴力都爆零了) 看见无修改,那么这题应该是莫队 维护两个bitset,第二个是第一个的反串,bitset内维护每个数字是否出现过 第一种操作 ...

  9. Bugku-CTF之welcome to bugkuctf(php://filter和php://input的妙用)

    Day24 welcome to bugkuctf http://123.206.87.240:8006/test1/ 本题要点:代码审计,PHP://filter ,  php://input ,  ...

  10. day13函数的嵌套定义,global、nonlocal关键字,闭包及闭包的运用场景,装饰器

    复习 ''' 1.函数对象:函数名 => 存放的是函数的内存地址 1)函数名 - 找到的是函数的内存地址 2)函数名() - 调用函数 => 函数的返回值 eg:fn()() => ...