Leetcode Tags(8)Binary Search
一、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的更多相关文章
- LeetCode(173) Binary Search Tree Iterator
题目 Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ...
- Leetcode Tags(3)String(TODO)
一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...
- Leetcode Tags(13)Bit Manipulation
一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance( ...
- Leetcode Tags(13)Tree
1.前序.中序.后序递归方式遍历二叉树 public void preOrderRecur(Node T) { if (T != null) { System.out.print(T.val + &q ...
- Leetcode Tags(6)Math
一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 ...
- Leetcode Tags(5)Hash Table
一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", &q ...
- Leetcode Tags(2)Array
一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了 ...
- Leetcode Tags(1)Linked List
1.知识点回顾 https://www.cnblogs.com/BigJunOba/p/9174206.html https://www.cnblogs.com/BigJunOba/p/9174217 ...
- Leetcode Tags(4)Stack & Queue
一.232. Implement Queue using Stacks private Stack<Integer> stack; /** Initialize your data str ...
随机推荐
- Kerberoasting攻击
前面我们介绍了<Windows本地认证>.<Windows网络认证>.<Windows域认证>和<SPN扫描>,这次继续讲解域内相关的东西. 0x01介 ...
- The type java.lang.AutoCloseable cannot be resolved. It is indirectly referenced from required .class files
出现问题: The type java.lang.AutoCloseable cannot be resolved. It is indirectly referenced from required ...
- Redis 相关功能和实用命令(五)
慢查询原因分析 由于 Redis 是单线程的,它内部维护了一个命令队列,所以当有耗时的命令出现时,比如 keys *,后面的命令会被阻塞,通查查出慢查询可以对服务进一步优化. 设置慢查询阀值:默认10 ...
- element取表格对应id数据
<el-button size="mini" type="danger" @click="editor(scope.row)"> ...
- python pytesseract——3步识别验证码的识别入门
验证码识别是个大工程,但入门开始只要3步.需要用到的库PIL.pytesserac,没有的话pip安装.还有一个是tesseract-ocr 下载地址:https://sourceforge.net/ ...
- Maven私服Nexus的搭建
# Maven私服Nexus的搭建 ## 私服存在的合理性 Maven中的依赖是从服务器仓库中下载的,Maven的仓库只有两大类: - 1) 本地仓库 - 2) 远程仓库,其中在远程仓库中又分成了3种 ...
- Java 学习笔记之 Thread运行过程分析
Thread运行过程分析: 以下是一个最普通的Thread实现过程,我们今天就来看仔细分析下他是如何运行的. public class ThreadRunMain { public static vo ...
- ELK 学习笔记之 elasticsearch head插件安装
elasticsearch head插件安装: 准备工作: 安装nodejs和npm https://nodejs.org/en/download/ node-v6.11.2-linux-x64.ta ...
- @ConfigurationProperties、@Value、@PropertySource
@ConfigurationProperties(spring-boot依赖下).@Value(spring-beans依赖下).@PropertySource(spring-context依赖下) ...
- spring和springmvc的基础知识点
1.spring中使用@Service("userservice")如何在其他地方引用这个service? (1)getBean("userservice") ...