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 ...
随机推荐
- *** target pattern contains no `%'. Stop.
windows上的ndk工程移到linux下编译就报这个错, 解决方法,把jni目录下obj目录删除,重新编译就好了
- centos6 更新gcc glibc2.17
curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repos.d/hop5.repo yum install gcc wget ...
- vuepress 学习心得
vuepress是一个静态网站生成器,在我看来就是写博客和教程的好工具.教程请见官网:https://www.vuepress.cn 安装方法建议局部安装:node8.0以上,新建vue项目,可能会出 ...
- Django框架详细介绍---中间件(认证)
一.绪论 在cookie和session的应用中,通过在视图函数内添加装饰器判断用户是否登录,把没有登录的用户请求跳转到登录页面,通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可 ...
- Hadoop 进程配置总结
HDFS: NameNode: core-site.xml <property> <name>fs.defaultFS</name> <value>hd ...
- Java并发包1--线程的状态及常用方法
一.线程主要有以下几种状态: new(新建):线程刚刚被创建 runnable(就绪):新建的线程执行start方法进入就绪状态等待系统调度分配CPU,被分配了之后就进入运行中状态 blocked(阻 ...
- 外网访问内网MariaDB数据库
外网访问本地MariaDB数据库 本地安装了MariaDB数据库,只能在局域网内访问,怎样从公网也能访问内网MariaDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Mar ...
- 前后端通信中使用Ajax与后台接口api交互(以登录功能为例)
一.查阅开发文档 首先,要做这个功能前,我们必须先查阅后台接口文档,了解使用登录接口时,需要提交哪些参数,并且接口使用返回的数据. 这里我使用了一个返回json格式数据的登录接口为例,讲解怎么使用Aj ...
- IDEA 发布Api
1.修改Main方法 代码: public class AlicsbapiApplication extends SpringBootServletInitializer { @Override pr ...
- 当前 .NET SDK 不支持将 .NET Core 2.2 设置为目标。请将 .NET Core 2.1 或更低版本设置
场景重现 Windows 10 操作系统 (64-bit) 已经安装 .NET Core SDKs 如下: C:\Users\taadis>dotnet --list-sdks 2.1.202 ...