一、475. Heaters

输入: [1,2,3],[2]
输出: 1
解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。 输入: [1,2,3,4],[1,4]
输出: 1
解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。

  1.思路:(1)先对heaters数组进行排序(2)对于每个house,计算其在heaters中的位置(3)计算这个house到其左和右heater的距离的最小值,也就是说heater只管离自己最近的house。(4)然后,取这些最小值的最大值即可。

houses = [1,2,3,4], heaters = [1,4]
house = 1:0-0,3-0,最小值:0,1在heaters中 ,index为0,dist1=MAX_VALUE,dist2=heaters[0]-1=0,result = 0
house = 2:1-0,3-1,最小值:1,2不在heaters中,index为-2,修正为1,dist1 = 2-heaters[0] = 1,dist2 = heaters[1]-2=2,result = 1
house = 3:2-0,3-2,最小值:1,3不在heaters中,index为-2,修正为1,dist1 = 3-heaters[0] = 2,dist2 = heaters[1]-3=1,result = 1
house = 4:3-0,3-3,最小值:0,4在heaters中 ,index为1,dist1 = 4-heaters[0] = 3,dist2 = heaters[1]-4=0,result = 0

  2.代码:需要注意的点:if (index < 0) index = -(index + 1);,这是因为如果house没有出现在heaters中,返回的是-(low+1),看上面↑

    public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(heaters);
int result = Integer.MIN_VALUE;
for (int house : houses) {
int index = Arrays.binarySearch(heaters, house);
if (index < 0) index = -(index + 1);
int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;
result = Math.max(result, Math.min(dist1, dist2));
}
return result;
}

  二、69. Sqrt(x)

输入: 4
输出: 2
输入: 8
输出: 2
说明: 8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。

  1.思路:由于当mid*mid <= x < (mid+1) * (mid+1)时,返回的结果是mid,又由于根号下x小于等于二分之x,所以右边界可以设为x/2,使用二分法寻找即可。

  2.代码:以x = 33,为例,left=1,right=16,mid=8,mid*mid>x,left=1,right=7,mid=4,mid*mid<33,(mid+1)(mid+1)<33,left=5,right=7,mid=6,mid*mid>33,

  left=5,right=6,mid=5,mid*mid<x,(mid+1)(mid+1)>33,return mid=5.

  需要注意的地方:(1)while (true)条件(2)使用mid > x/mid而不用mid*mid是为了避免出现overFlow的问题。

    public int mySqrt(int x) {
if (x == 0) return 0;
int left = 1, right = x/2;
while (true) {
int mid = left + (right - left)/2;
if (mid > x/mid) {
right = mid - 1;
} else {
if (mid + 1 > x/(mid + 1)) return mid;
left = mid + 1;
}
}
}

  三、

Leetcode Tags(8)Binary Search的更多相关文章

  1. LeetCode(173) Binary Search Tree Iterator

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

  2. Leetcode Tags(3)String(TODO)

    一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...

  3. Leetcode Tags(13)Bit Manipulation

    一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance( ...

  4. Leetcode Tags(13)Tree

    1.前序.中序.后序递归方式遍历二叉树 public void preOrderRecur(Node T) { if (T != null) { System.out.print(T.val + &q ...

  5. Leetcode Tags(6)Math

    一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 ...

  6. Leetcode Tags(5)Hash Table

    一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", &q ...

  7. Leetcode Tags(2)Array

    一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了 ...

  8. Leetcode Tags(1)Linked List

    1.知识点回顾 https://www.cnblogs.com/BigJunOba/p/9174206.html https://www.cnblogs.com/BigJunOba/p/9174217 ...

  9. Leetcode Tags(4)Stack & Queue

    一.232. Implement Queue using Stacks private Stack<Integer> stack; /** Initialize your data str ...

随机推荐

  1. Unity - 存读档机制简析

    本文旨在于简要分析Unity中的两种存档机制,即:PlayerPrefs数据持久化方法及Serialization数据序列化方法 较比与源项目,我另加了JSON方法.XML方法等及一些Unity设置, ...

  2. linux中将video转换成gif

    我使用的机器是Linux mint 17,因为习惯了在linux中开发而有时候在写小demo的时候要带一些演示,虽然可以使用录屏也可以但是视屏演示这些小demo也不是特别的方便.之前一直在linux中 ...

  3. java基础面试集结

    1.hashMap实现原理及相关问题 :https://blog.csdn.net/h1130189083/article/details/78303865

  4. 洛谷:P5072 [Ynoi2015]盼君勿忘

    原题地址:https://www.luogu.org/problem/P5072 题目简述 给定一个序列,每次查询一个区间[l,r]中所有子序列分别去重后的和mod p 思路 我们考虑每个数的贡献.即 ...

  5. 面试题解析|ACL权限控制机制

    ACL(Access Control List)访问控制列表 包括三个方面: 一.权限模式(Scheme) 1.IP:从 IP 地址粒度进行权限控制 2.Digest:最常用,用类似于 usernam ...

  6. SpringBoot自定义异常,优雅解决业务逻辑中的错误

    概要 你是不是在为业务逻辑中出现的异常弄的焦头烂额,常常在后台报错,前端却无法提示错误内容,导致用户体验极差?比如下单失败,前端只能提示下单失败,但是却不知道为什么失败,是库存不足,还是余额不足,亦或 ...

  7. MySQL8身份验证问题解决

    开新项目.使用MySQL8,在经历过B级别的网速下载后,终于安装好了MySQL,虽然在终端上是可以直接登录的. 但是我使用Navicat就无法访问了,提示什么登录失败,还有乱码. 搜索了一下,发现是M ...

  8. spring5 源码深度解析----- Spring事务 是怎么通过AOP实现的?(100%理解Spring事务)

    此篇文章需要有SpringAOP基础,知道AOP底层原理可以更好的理解Spring的事务处理. 自定义标签 对于Spring中事务功能的代码分析,我们首先从配置文件开始人手,在配置文件中有这样一个配置 ...

  9. C# 8 的模式匹配

    C# 7 里面的Pattern Mathing is 模式 switch 和 when C# 8 里面的Pattern Matching 使用Deconstructor 和 位置匹配模式 下面两个类T ...

  10. 用OllyDbg爆破一个小程序

    用OllyDbg爆破一个小程序 一.TraceMe小程序 TraceMe是对用户名.序列号判断是否合法的一个小程序.我们任意输入一组用户名.序列号进行check判断,结果如下: 二.用OllyDbg对 ...