**475. Heaters
  1. 思路:每趟循环查找离房子最近的热水器,计算距离,最后取最大距离
  2. public int findRadius(int[] houses, int[] heaters) {
  3. Arrays.sort(houses);
  4. Arrays.sort(heaters);
  5. int j = 0;
  6. int res = 0;
  7. for(int i = 0; i < houses.length; i++){
  8. //找离house[i]最近的heater
  9. while(j < heaters.length - 1 && heaters[j] + heaters[j + 1] <= 2 * houses[i]){
  10. j++;
  11. }
  12. res = Math.max(res,Math.abs(heaters[j] - houses[i]));
  13. }
  14. return res;
  15. }
**410. Split Array Largest Sum
  1. 思路:结果一定在最大值和总和之间,因此在这两个数之间用二分查找;看中间值能不能符合区间个数
  2. public int splitArray(int[] nums, int m) {
  3. int max = 0;
  4. int sum = 0;
  5. for(int i = 0; i < nums.length; i++){
  6. max = Math.max(max,nums[i]);
  7. sum += nums[i];
  8. }
  9. int i = max;
  10. int j = sum;
  11. while(i <= j){
  12. int d = i + (j - i) / 2;
  13. if(isValid(nums,m,d)) j = d - 1;
  14. else i = d + 1;
  15. }
  16. return i;
  17. }
  18. public boolean isValid(int[] nums,int m,int d){
  19. int count = 1;
  20. int total = 0;
  21. for(int i = 0; i < nums.length; i++){
  22. total += nums[i];
  23. if(total > d){
  24. total = nums[i];
  25. count++;
  26. if(count > m) return false;
  27. }
  28. }
  29. return true;
  30. }
**354. Russian Doll Envelopes
  1. 思路:将区间按第一个数升序,第二个数降序排列,只需要计算第二个数的最长递增序列个数即可,注意Arrays.sort()的自定义排序的使用,即第300
  2. public int maxEnvelopes(int[][] envelopes) {
  3. if(envelopes.length == 0 || envelopes[0].length == 0) return 0;
  4. Arrays.sort(envelopes,new Comparator<int[]>(){
  5. public int compare(int[] a,int[] b){
  6. if(a[0] == b[0]){
  7. return b[1] - a[1];
  8. }
  9. else{
  10. return a[0] - b[0];
  11. }
  12. }
  13. });
  14. int[] dp = new int[envelopes.length];
  15. for(int i = 0; i < dp.length; i++) dp[i] = 1;
  16. for(int i = 1; i < envelopes.length; i++){
  17. for(int j = 0; j < i; j++){
  18. if(envelopes[i][1] > envelopes[j][1]) dp[i] = Math.max(dp[i],dp[j] + 1);
  19. }
  20. }
  21. int res = 0;
  22. for(int k : dp){
  23. res = Math.max(k,res);
  24. }
  25. return res;
  26. }
总结
  1. 278. First Bad Version:经典二分查找,属于查找临界点,注意最终终止索引
  2. 374. Guess Number Higher or Lower:经典二分查找,属于查找某个数
  3. 441. Arranging Coins:二分查找,属于查找临界点,注意最终终止索引
  4. 367. Valid Perfect Square:二分查找,属于查找某个数
  5. 240. Search a 2D Matrix II:从右上角开始查找,小则往下,大则往左
  6. 378. Kth Smallest Element in a Sorted Matrix:将matrix放在PriorityQueue中实现排序
  7. 392. Is Subsequence:按照s的顺序在t中查找,用计数器计数,等于s.length()则返回true
  8. 69. Sqrt(x):二分查找,两个临界点mm + 1,考虑所有区间的情况即可
训练
  1. 300. Longest Increasing Subsequencedp中的元素表示当前索引值为序列最后一个数时的长度
  2. 275. H-Index II:属于查找临界点,注意最终终止索引
  3. **436. Find Right Interval:用TreeMap,注意ceilingEntry()的使用
  4. 50. Pow(x, n):递归但是要考虑减少栈帧,还有边界问题
  5. 174. Dungeon Game:构造二维数组存每个格子所需最小体力,从终点开始一次往前计算所需最小体力,先计算边界
提示
  1. 1.直接查找某个数直接二分查找i <= j,并且最终一定能找到
  2. 2.若是找某个临界点,也直接i <= j,只不过要想清楚最终终止索引的位置,相邻数中点一定是较小数,并且一定在临界点左边或就是临界点减小了范围
  3. 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. C#如何获取物理网卡,虚拟网卡,以及无线网卡

    就不废话了,直接上代码 /// <summary></summary> /// 显示本机各网卡的详细信息 /// <summary></summary> ...

  2. Go语言并发编程示例 分享(含有源代码)

    GO语言并发示例分享: ppt http://files.cnblogs.com/files/yuhan-TB/GO%E8%AF%AD%E8%A8%80.pptx 代码, 实际就是<<Go ...

  3. android性能测试与调优:使用 DDMS 查看内存分配情况

    1. 启用自己的APK后 2. 点击左边更新heap 3. 点击右边的heap中的垃圾回收cause GC,等待数秒出现回收内存与数据情况(由于内存回收了APK运行出现异常crash) 4. 点击一个 ...

  4. opensuse sublime 配置

    fcitx输入法支持: 准备文件sublime-imfix.c: /* sublime-imfix.c Use LD_PRELOAD to interpose some function to fix ...

  5. HTML教程-各窗口间相互操作(Frame Target)

    由Frames分出来的几个窗口的内容并不是静止不变的,往往一个窗口的内容随着另一个窗口的要求而不断变化,这就提高了Frames的利用价值.为了完成各窗口之间的相互操作,我们必须为每一个窗口起一个名字, ...

  6. SQLAutoCode - error when attempting to generate schema

    I'm trying to auto generate a schema for use in SQLalchemy, I'm using sqlautocode to do this, I use ...

  7. Maven生命周期

    Maven的生命周期抽象了构建的各个步骤,定义了他们的次序,但没有提供实现.Maven设计了插件机制.每个构建步骤都可以绑定一个或多个插件行为,而且Maven为大多数构建步骤编写并绑定了默认插件. M ...

  8. 对git的理解及常用指令

    以前总听说git[分布式版本控制系统]自己愣是搞不懂它到底要干哈-什么叫版本控制系统根本理解不了.现在工作需要必须要用到,结果好像就突然懂了git是干什么滴. 所以!原理这个东西的理解是要建立在大量的 ...

  9. wamp出现You don’t have permission to access/on this server提示的解决方法

    本地搭建wamp 输入http://127.0.0.1访问正常,当输入http://localhost/ apache出现You don't have permission to access/on ...

  10. HDU5840 (分块+树链剖分)

    Problem This world need more Zhu 题目大意 给一颗n个点的有点权的树,有m个询问,对于每个询问u,v,k,首先将点u到点v的最短路径上的所有点按顺序编号,u的编号为1, ...