38. Search a 2D Matrix II

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

这道题与二分法有什么关系呢?

  -把整个二维数组从对角线分成两半,从左下角开始,往右上角逼近。

 public class Solution {
/**
* @param matrix: A list of lists of integers
* @param target: An integer you want to search in matrix
* @return: An integer indicate the total occurrence of target in the given matrix
*/
public int searchMatrix(int[][] matrix, int target) {
// write your code here
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int row = matrix.length - 1, col = matrix[0].length - 1;
int r = row, c = 0;
int result = 0;
while(r >= 0 && c <= col) {
if(matrix[r][c] == target) {
result++;
c++;
r--;
} else if(matrix[r][c] < target) {
c++;
} else if(matrix[r][c] > target) {
r--;
}
}
return result;
}
}

457. Classical Binary Search

https://www.lintcode.com/problem/classical-binary-search/description?_from=ladder&&fromId=1

很简单,套用模版即可

 public class Solution {
/**
* @param nums: An integer array sorted in ascending order
* @param target: An integer
* @return: An integer
*/
public int findPosition(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) return mid;
if(nums[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if(nums[start] == target) {
return start;
} else if(nums[end] == target) {
return end;
}
return -1;
}
}

141. Sqrt(x)

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

凡是 平方、移位,一定要类型转换!!!转换成 long !!!

套用模版即可

 public class Solution {
/**
* @param x: An integer
* @return: The sqrt of x
*/
public int sqrt(int x) {
// write your code here
if(x < 0) return -1;
long start = 0;
long end = x;
long xl = (long)x;
while(start + 1 < end) {
long mid = start + (end - start) / 2;
if(mid * mid == xl) {
return (int)(mid);
} else if(mid * mid > xl) {
end = mid;
} else {
start = mid;
}
}
if(end * end <= xl) {
return (int)(end);
} else {
return (int)(start);
}
}
}

617.  Maximum Average Subarray

https://www.lintcode.com/problem/maximum-average-subarray-ii/note/171478

586. Sqrt(x) II

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

牛顿迭代法。

public class Solution {
/**
* @param x: a double
* @return: the square root of x
*/
public double sqrt(double x) {
// write your code here
double result = 1.0;
while(Math.abs(x - result * result) > 1e-12) {
result = (result + x / result) / 2;
}
return result;
}
}

160. Find Minimum in Rotated Array II

https://www.lintcode.com/problem/find-minimum-in-rotated-sorted-array-ii/description?_from=ladder&&fromId=1

要知道最坏情况下,[1, 1, 1, .........., 1] 里有一个 0

这种情况使得时间复杂度必须是 O(n)

if mid equals to end, that means it's fine to remove end

the smallest element won't be removed

public class Solution {
/**
* @param nums: a rotated sorted array
* @return: the minimum number in the array
*/
public int findMin(int[] nums) {
// write your code here
if(nums == null || nums.length == 0) return 0;
int start = 0;
int end = nums.length - 1;
while(start + 1 < end) {
int mid = start + (end - start) / 2;
if(nums[mid] == nums[end]) {
end--;
}
if(nums[mid] > nums[end]) {
start = mid;
}
if(nums[mid] < nums[end]) {
end = mid;
}
}
return Math.min(nums[start], nums[end]);
}
}

63. Search in Rotated Sorted Array II

https://www.lintcode.com/problem/search-in-rotated-sorted-array-ii/description?_from=ladder&&fromId=1

 class Solution {
public boolean search(int[] nums, int target) {
if(nums == null || nums.length == 0) return false;
int start = 0;
int end = nums.length - 1;
int mid;
while(start + 1 < end){
mid = start + (end - start) / 2;
if(nums[mid] == target) return true;
if(nums[mid] > nums[start]){
if(target >= nums[start] && target <= nums[mid]){
end = mid;
}else{
start = mid;
}
}else if(nums[mid] < nums[start]){
if(target >= nums[mid] && target <= nums[end]){
start = mid;
}else{
end = mid;
}
}
else{
start++;
}
}
if(target == nums[start] || nums[end] == target) return true;
return false;
}
}

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

  1. 2 - Binary Search & LogN Algorithm

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

  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. [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 ...

  4. [Algorithms] Binary Search Algorithm using TypeScript

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

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

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

  6. js binary search algorithm

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

  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. [Leetcode] Binary search, Divide and conquer--240. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. Ansible 批量管理Windows Server服务器

    Ansible批量管理Windows Server         Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具,  它用Python写成,类似于saltstack和Puppe ...

  2. php读取和导出Excel文件

    require 'vendor/PHPExcel/PHPExcel.php';require 'vendor/PHPExcel/PHPExcel/IOFactory.php'; public func ...

  3. CentOS7 安装 MySQL8.0

    [1]安装步骤过程 (1)yum仓库下载MySQL 命令:yum localinstall https://repo.mysql.com//mysql80-community-release-el7- ...

  4. 2018-2019-2 《网络对抗技术》Exp2 后门原理与应用 20165215

    目录 实验内容 基础问题回答 常用后门工具 Netcat windows 获取 linux 的shell linux 获取 winsdows 的shell 使用nc传输数据 使用nc传文件 Socat ...

  5. 问题 1923: [蓝桥杯][算法提高VIP]学霸的迷宫 (BFS)

    题目链接:https://www.dotcpp.com/oj/problem1923.html 题目描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在 ...

  6. create-react-app不暴露配置设置proxy代理

    此方法可以在不暴露配置的情况下直接设置代理,非常便捷 在package.json里添加 "proxy":"http://institute.dljy.lzdev" ...

  7. Qt文档阅读笔记-QGraphicsItem::paint中QStyleOptionGraphicsItem *option的进一步认识

    官方解析 painter : 此参数用于绘图;option : 提供了item的风格,比如item的状态,曝光度以及详细的信息:widget : 想画到哪个widget上,如果要画在缓存区上,这个参数 ...

  8. Hibernate一级缓存和二级缓存详解

    (1)一级缓存 是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中,如果短时间内这个session(一定要同一个session)又做了同一个操作,那么h ...

  9. hiberate 映射关系 详解

    在我们平时所学的关系型数据库中,我们会大量处理表与表之间的关系,如果表比较多的话处理起来就比较繁琐了,但是hibernate给我们提供了很大的便利,这些便利让我们处理起来方便.我们所讲的源码地址:ht ...

  10. PyGame实现情人节表白利器

    前提:写不出那么那个的话哇,随便写写,随便看看,重在代码(文章末尾有免费完整源代码) 实验环境: pygame 1.9.4 pycharm python3.6 实现思路: pygame.display ...