254. Drop Eggs

https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1

28. Search a 2D Matrix

https://www.lintcode.com/problem/search-a-2d-matrix/description?_from=ladder&&fromId=1

思路1:

  1. find the row index, the last number <= target

  2. find the column index, the number equal to target

public class Solution {
/**
* @param matrix: 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;
int left = 0, right = matrix[0].length - 1;
int row = findRow(matrix, target);
if(matrix[row][0] > target || matrix[row][right] < target) {
return false;
}
while(left + 1 < right) {
int mid = left + (right - left) / 2;
if(matrix[row][mid] == target) {
return true;
} else if(matrix[row][mid] > target) {
right = mid;
} else {
left = mid;
}
}
if(matrix[row][left] == target || matrix[row][right] == target) {
return true;
} else {
return false;
}
} public int findRow(int[][] matrix, int target) {
int top = 0, bottom = matrix.length - 1, right = matrix[0].length - 1;
while(top + 1 < bottom) {
int mid = top + (bottom - top) / 2;
if(matrix[mid][right] == target) {
return mid;
} else if(matrix[mid][right] > target) {
bottom = mid;
} else {
top = mid;
}
}
if(matrix[top][right] >= target) {
return top;
} else {
return bottom;
}
}
}

思路2:

1. 可以看作是一个有序数组被分成了n段,每段就是一行。因此依然可以二分求解。

对每个数字,根据其下标 i,j 进行编号,每个数字可被编号为 0 ~ n *(n - 1)

2. 相当于是在一个数组中的下标,然后直接像在数组中二分一样来做。取得 mid 要还原成二维数组中的下标,i = mid / n, j = mid % n

3. int start = 0, end = row * row * column - 1;

int number = matrix[mid / column][mid % column];

// 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; }
}

14. First Position of Target 

https://www.lintcode.com/problem/first-position-of-target/description?_from=ladder&&fromId=1

思路:这道题很简单。套用二分法的模版即可

 public 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 == 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 {
start = mid;
}
}
if(nums[start] == target) {
return start;
}
if(nums[end] == target) {
return end;
}
return -1;
}
}

414. Divide Two Integers

https://www.lintcode.com/problem/divide-two-integers/description?_from=ladder&&fromId=1

1. 凡是要移位,要做的第一件事就是把 int 转换成 long,为了防止移位时溢出。

2. 基本思路是利用减法,看看被除数可以减去多少次除数。使用倍增的思想优化,可以将减法的次数优化到对数的时间复杂度。

3. 我们将除数左移一位(或加上它自己),即得到了二倍的除数,这时一次相当于减去了两个除数,通过不断倍增,时间复杂度很优秀。

4. 与此同时,还需要一个变量记录此时的除数是最初除数的多少倍,每次减法后都加到结果上即可。

 public class Solution {
/**
* @param dividend: the dividend
* @param divisor: the divisor
* @return: the result
*/
public int divide(int dividend, int divisor) {
// write your code here
if(divisor == 0) {
return dividend >= 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
if(dividend == 0) {
return 0;
}
if(divisor == -1 && dividend == Integer.MIN_VALUE) {
return Integer.MAX_VALUE;
}
boolean isNegative = ((divisor > 0 && dividend < 0) || (divisor < 0 && dividend > 0)) ? true : false;
long divisorL = Math.abs((long)divisor);
long dividendL = Math.abs((long)dividend);
int result = 0;
while(dividendL >= divisorL) {
int shift = 0;
while(dividendL >= (divisorL << shift)) {
shift++;
}
result += 1 << (shift - 1);
dividendL -= divisorL << (shift - 1);
}
if(isNegative) {
return result * (-1);
}
return result;
}
}

61. Search for a Range

https://www.lintcode.com/problem/search-for-a-range/description?_from=ladder&&fromId=1

这道题同样很简单,套用模版即可。

 public class Solution {
/**
* @param A: an integer sorted array
* @param target: an integer to be inserted
* @return: a list of length 2, [index1, index2]
*/
public int[] searchRange(int[] A, int target) {
// write your code here
if(A == null || A.length == 0) return new int[]{-1, -1};
int start = findStart(A, target);
int end = findEnd(A, target);
return new int[]{start, end}; } public int findStart(int[] A, int target) {
int start = 0;
int end = A.length - 1;
while(start + 1 < end) {
int mid = start + (end - start) / 2;
if(A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if(A[start] == target) {
return start;
}
if(A[end] == target) {
return end;
}
return -1;
} public int findEnd(int[] A, int target) {
int start = 0;
int end = A.length - 1;
while(start + 1 < end) {
int mid = start + (end - start) / 2;
if(A[mid] <= target) {
start = mid;
} else {
end = mid;
}
}
if(A[end] == target) {
return end;
}
if(A[start] == target) {
return start;
}
return -1;
}
}

2 - Binary Search & LogN Algorithm的更多相关文章

  1. 2 - Binary Search & LogN Algorithm - Apr 18

    38. Search a 2D Matrix II https://www.lintcode.com/problem/search-a-2d-matrix-ii/description?_from=l ...

  2. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

  3. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

  4. 【437】Binary search algorithm,二分搜索算法

    Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...

  5. js binary search algorithm

    js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...

  6. [Algorithm] Delete a node from Binary Search Tree

    The solution for the problem can be divided into three cases: case 1: if the delete node is leaf nod ...

  7. [Algorithm] Check if a binary tree is binary search tree or not

    What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...

  8. [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search

    Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...

  9. my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏

    If you understand the comments below, never will you make mistakes with binary search! thanks to A s ...

随机推荐

  1. MySql数据库中,判断表、表字段是否存在,不存在就新增

    本文是针对MySql数据库创建的SQL脚本,别搞错咯. 判断表是否存在,不存在就可新增 CREATE TABLE IF NOT EXISTS `mem_cardtype_resource` ( ... ...

  2. 【数据结构】算法 LinkList (Remove Nth Node From End of List)

    删除链表中倒数第n个节点 时间复杂度要控制在O(n)Solution:设置2个指针,一个用于确定删除节点的位置,一个用于计算倒数间距n.移动时保持2个指针同时移动. public ListNode r ...

  3. JVM探秘6--图解虚拟机栈的局部变量表和操作数栈工作流程

    案例代码如下: public class JVMTest { public static Integer num = 10; public int add(int i){ int j = 5; int ...

  4. linux重启Oracle服务

    linux重启oracle服务命令(完整版) (1) 以oracle身份登录数据库,命令:su – oracle (2) 进入Sqlplus控制台,命令:sqlplus /nolog (3) 以系统管 ...

  5. [转载]ISO 8601规则

    1.每年有52周或者53周2.周一至周日为一个完整周.3.每周的周一是该周的第1天.周日是该周的第7天4.每年的第一周 为 每年的第一个周四所在的周.比如 2017年1月5日为当年的第一个周四,那么 ...

  6. java 接口详解

    定义接口 接口继承和实现继承的规则不同,一个类只有一个直接父类,但可以实现多个接口.Java 接口本身没有任何实现,只描述 public 行为,因此 Java 接口比 Java 抽象类更抽象化.Jav ...

  7. Jenkins>>>应用篇>>>插件使用>>>Publish over SSH

    依赖环境 SSH: 远程机开启SSH服务.同意Jenkins所在机器通过SSH服务登录到远程机运行脚本. 能够设置SSH使用username/password或通过key登录,SSH配置请查专门的资料 ...

  8. Linux 主要目录速查表

    /:根目录,一般根目录下只存放目录,在 linux 下有且只有一个根目录,所有的东西都是从这里开始 当在终端里输入 /home,其实是在告诉电脑,先从 /(根目录)开始,再进入到 home 目录 /b ...

  9. 原创《如何用vue来轻松的驾驭 html5 webapp的页面体验》

    由于最近开始要做mobile的webapp 项目,所以利用周末的时间思考了下页面的体验问题,我主要参考了"微信"的页面体验,总结主要有2个页面切换效果(点击进入页面效果 和 返回上 ...

  10. PHP内核深入研究 - 数组及其遍历顺序

    事实上,广义上来讲,PHP就是C语言应用在Web上的一个模板,PHP中smarty模板用得比较多,就好比JSP是Java Servlet的模板一样(喔,对了,JSP中有个JSTL标签),复杂的模板语法 ...