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=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
要知道最坏情况下,[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
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的更多相关文章
- 2 - Binary Search & LogN Algorithm
254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...
- 将百分制转换为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 ...
- [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 ...
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- 【437】Binary search algorithm,二分搜索算法
Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...
- js binary search algorithm
js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- matlab多个曲面如何画在一个坐标系中的疑问
matlab多个曲面如何画在一个坐标系中的疑问 [复制链接] [X,Y]=meshgrid(-3:0.1:3);Z=X.^2+Y.^2;mesh(X,Y,-Z)hold onmesh(X,Y,Z)
- Java多线程处理List数据
实例1: 解决问题:如何让n个线程顺序遍历含有n个元素的List集合 import java.util.ArrayList; import java.util.List; import org.apa ...
- 构建RN或Weex项目时,使用Android Studio常遇到的问题
1 . androidStudio报错No cached version available for offline mode 解决方法 原因是之前为了提高编译速度,在Gradle设置选项中开启了Of ...
- java.lang.IllegalAccessError: org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z
做spring和mybaits整合时出现的错误,让这个问题困扰了一早上,通过查资料终于把这个问题解决了 具体问题描述: java.lang.IllegalAccessError: org.apache ...
- Linux环境上部署Flask
[该文章只涉及个人部署的简单流程,读者可通过其它途径了解详细部署流程] 依个人部署项目可预先安装好需要的环境,这里已提前安装好LNMP环境 1.安装Python环境 安装virtualenv环境 配置 ...
- RAID技术详解
RAID:Redundant Array of Independent Disks 中文我们称为独立冗余磁盘阵列.基本上是见名知意.RAID的基本思想就是将多个容量较小且价格实惠的磁盘进行组合起来构成 ...
- Spring boot 源码分析(前言)
开坑达人 & 断更达人的我又回来了 翻译的坑还没填完,这次再开个新坑= = 嗯,spring boot的源码分析 本坑不打算教你怎么用spring boot = = 也不打算跟你讲这玩意多方便 ...
- PHP百度AI的OCR图片文字识别
第一步可定要获取百度的三个东西 要到百度AI网站(http://ai.baidu.com/)去注册 然后获得 -const APP_ID = '请填写你的appid'; -const API_KEY ...
- java内存机制和GC垃圾回收机制
Java 内存区域和GC机制 转载来源于:https://www.cnblogs.com/zhguang/p/3257367.html 感谢 目录 Java垃圾回收概况 Java内存区域 Java对象 ...
- flutter sqflite
https://www.jianshu.com/p/88998af66e4b https://www.jianshu.com/p/7ac3ce2bc0c6