一、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. 2018年蓝桥杯java b组第八题

    标题:日志统计 小明维护着一个程序员论坛.现在他收集了一份"点赞"日志,日志共有N行.其中每一行的格式是: ts id 表示在ts时刻编号id的帖子收到一个"赞" ...

  2. 品Spring:注解之王@Configuration和它的一众“小弟们”

    其实对Spring的了解达到一定程度后,你就会发现,无论是使用Spring框架开发的应用,还是Spring框架本身的开发都是围绕着注解构建起来的. 空口无凭,那就说个最普通的例子吧. 在Spring中 ...

  3. 节点操作--JavaScript

    1 - 概念 网页中的所有内容都是节点(标签.属性.文本.注释),在DOM中,节点使用node来表示. HTML DOM树中的所有节点均可通过JS进行访问,所有HTML元素(节点)均可被修改,也可以创 ...

  4. eShopOnContainers学习系列(三):RabbitMQ消息总线实践

    今天研究了下eShopOnContainers里的RabbitMQ的使用,在项目里是以封装成消息总线的方式使用的,但是仍然是以其发布.订阅两个方法作为基础封装的,我们今天就来实际使用一下. 为了简单起 ...

  5. ELK系列(二):.net core中使用ELK

    ELK安装好后,我们现在.net Core中使用一下,大体思路就是结合NLog日志组件将数据写入ELK中,其它语言同理. ELK的安装还是有些复杂的,我们也可以在Docker中安装ELK:docker ...

  6. python beautiful soup

    官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ 使用前需要先安装模块,并安装解析器 pip install beautif ...

  7. mysql root密码忘记

    首先停掉mysql服务,在/etc/my.cnf中添加 skip-grant-tables,同时可以添加skip-networking选项来禁用网络功能,防止这时其他人通过网络连接到数据库 [mysq ...

  8. Vue:获取当前定位城市名

    实现思想:通过定位获取到当前所在城市名: 1.在工程目录index.html中引入: <script type="text/javascript" src="htt ...

  9. LitePal的查询

    转载:http://blog.csdn.net/guolin_blog/article/details/40153833 传统的查询数据方式 其实最传统的查询数据的方式当然是使用SQL语句了,Andr ...

  10. 从单片机到操作系统⑦——深入了解FreeRTOS的延时机制

    >没研究过操作系统的源码都不算学过操作系统 # FreeRTOS 时间管理 时间管理包括两个方面:系统节拍以及任务延时管理. ## 系统节拍: 在前面的文章也讲得很多,想要系统正常运行,那么时钟 ...