**475. Heaters
思路:每趟循环查找离房子最近的热水器,计算距离,最后取最大距离

public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters); int j = 0;
int res = 0;
for(int i = 0; i < houses.length; i++){
//找离house[i]最近的heater
while(j < heaters.length - 1 && heaters[j] + heaters[j + 1] <= 2 * houses[i]){
j++;
}
res = Math.max(res,Math.abs(heaters[j] - houses[i]));
}
return res;
}
**410. Split Array Largest Sum
思路:结果一定在最大值和总和之间,因此在这两个数之间用二分查找;看中间值能不能符合区间个数

public int splitArray(int[] nums, int m) {
int max = 0;
int sum = 0;
for(int i = 0; i < nums.length; i++){
max = Math.max(max,nums[i]);
sum += nums[i];
} int i = max;
int j = sum;
while(i <= j){
int d = i + (j - i) / 2;
if(isValid(nums,m,d)) j = d - 1;
else i = d + 1;
}
return i;
} public boolean isValid(int[] nums,int m,int d){
int count = 1;
int total = 0;
for(int i = 0; i < nums.length; i++){
total += nums[i];
if(total > d){
total = nums[i];
count++;
if(count > m) return false;
}
}
return true;
}
**354. Russian Doll Envelopes
思路:将区间按第一个数升序,第二个数降序排列,只需要计算第二个数的最长递增序列个数即可,注意Arrays.sort()的自定义排序的使用,即第300题

public int maxEnvelopes(int[][] envelopes) {
if(envelopes.length == 0 || envelopes[0].length == 0) return 0;
Arrays.sort(envelopes,new Comparator<int[]>(){
public int compare(int[] a,int[] b){
if(a[0] == b[0]){
return b[1] - a[1];
}
else{
return a[0] - b[0];
}
}
}); int[] dp = new int[envelopes.length];
for(int i = 0; i < dp.length; i++) dp[i] = 1;
for(int i = 1; i < envelopes.length; i++){
for(int j = 0; j < i; j++){
if(envelopes[i][1] > envelopes[j][1]) dp[i] = Math.max(dp[i],dp[j] + 1);
}
}
int res = 0;
for(int k : dp){
res = Math.max(k,res);
}
return res;
}
总结
278. First Bad Version:经典二分查找,属于查找临界点,注意最终终止索引
374. Guess Number Higher or Lower:经典二分查找,属于查找某个数
441. Arranging Coins:二分查找,属于查找临界点,注意最终终止索引
367. Valid Perfect Square:二分查找,属于查找某个数
240. Search a 2D Matrix II:从右上角开始查找,小则往下,大则往左
378. Kth Smallest Element in a Sorted Matrix:将matrix放在PriorityQueue中实现排序
392. Is Subsequence:按照s的顺序在t中查找,用计数器计数,等于s.length()则返回true
69. Sqrt(x):二分查找,两个临界点m和m + 1,考虑所有区间的情况即可
训练
300. Longest Increasing Subsequence:dp中的元素表示当前索引值为序列最后一个数时的长度
275. H-Index II:属于查找临界点,注意最终终止索引
**436. Find Right Interval:用TreeMap,注意ceilingEntry()的使用
50. Pow(x, n):递归但是要考虑减少栈帧,还有边界问题
174. Dungeon Game:构造二维数组存每个格子所需最小体力,从终点开始一次往前计算所需最小体力,先计算边界
提示
1.直接查找某个数直接二分查找i <= j,并且最终一定能找到
2.若是找某个临界点,也直接i <= j,只不过要想清楚最终终止索引的位置,相邻数中点一定是较小数,并且一定在临界点左边或就是临界点减小了范围
3.若查找过程中判定条件会越界,则将int转化为long

LeetCode---Binary Search的更多相关文章

  1. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  2. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

  3. LeetCode & Binary Search 解题模版

    LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...

  4. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. LeetCode Binary Search Tree Iterator

    原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...

  6. [Leetcode] Binary search -- 475. Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  7. 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...

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

  9. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  10. LeetCode: Binary Search Tree Iterator 解题报告

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

随机推荐

  1. 第五章 搭建S3C6410开发板的测试环境

    在PC上可以开发Linux驱动,重新编译成ARM架构的Linux驱动模块,但最后还是要在开发板上进行测试.目前最流行的是基于三星S3C6410 ARM11架构的开发板,很多厂商在其基础上进行了扩展,开 ...

  2. 关于JQuery简单介绍

    jQuery是一个兼容多浏览器的javascript库,核心理念是写得更少,做得更多.如今,jQuery已经成为最流行的javascript库,在世界前10000个访问最多的网站中,有超过55%在使用 ...

  3. mariadb配置允许远程访问方式

    首先配置允许访问的用户,采用授权的方式给用户权限 1 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRAN ...

  4. Java学习1 - java 历史

    Sun的Java语言开发小组成立于1991年,其目的是开拓消费类电子产品市场,例如:交互式电视,烤面包箱等.Sun内部人员把这个项目称为 Green,那时World Wide Web还在图纸上呢.该小 ...

  5. WebGL中添加天空盒的两种方法

    天空盒 的添加可以让模型所在的场景非常漂亮,而其原理也是非常简单的,相信看完下面代码就可以明白了. 说到天空盒的两种方法,倒不如说是两种写法,分别用了纹理加载的两个方法:loadTexture和loa ...

  6. 如何快速在当前目录打开cmd命令提示符

    对于稍微熟悉电脑一些的朋友来说.cmd绝对是个很方便的东西.但是每次使用cmd都要cd半天才能到当前目录.怎么快速打开当前目录呢? 当前目录按住shift再右键.然后会看到右键菜单里有一个" ...

  7. JSONObject

    JAR包简介: commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.j ...

  8. JavaScript笔记及总结

    前言: 网页中HTML为内容,CSS做展现(修饰内容),Js为行为(交互). Js属于基于对象型的脚本语言,在学习时当作编程语言(如java,c#)学习更好理解. javascript是实现网页动态效 ...

  9. Could not find result map com.lrlz.weixin.dao.SysUserDAO.SysUser

    <select id="selectByTimeCount" resultType="java.lang.Integer" parameterType=& ...

  10. C#知识体系(二)用案例来理解委托与事件

    上一篇博客讲到了LinQ和lambda的常用方法 还有很多我们未知但c#设计团队已经为我们封装好的类和方法.随着我们不断的熟悉C#语言,渐渐的就会接触到其他的知识点,委托.事件.反射.线程.同步,异步 ...