https://leetcode.com/problems/heaters/

开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过。原因是heater会有重复情况,这时候对于飘红部分就不会往前面继续检查。所以把<改成<=让相同的情况也继续往前面走,走到最后一个,就可以了。Accepted!

package com.company;

import java.util.Arrays;

class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters); int ret = Integer.MIN_VALUE;
int i = 0;
int hlen = heaters.length;
int tmp = Integer.MIN_VALUE;
for (int h: houses) {
while (i+1 < hlen && Math.abs(heaters[i+1]-h) <= Math.abs(heaters[i]-h)) {
i++;
}
if (Math.abs(heaters[i]-h) > ret) {
ret = Math.abs(heaters[i]-h);
}
}
return ret;
}
} public class Main { public static void main(String[] args) { Solution solution = new Solution(); int[] houses = {1, 2, 2, 3, 4, 5};
int[] heaters = {1, 2, 2, 3, 4, 5};
int ret = solution.findRadius(houses, heaters); System.out.printf("Done ret: %d\n", ret); } }

heaters的更多相关文章

  1. [LeetCode] Heaters 加热器

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

  2. Leetcode: Heaters

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

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

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

  4. [Swift]LeetCode475. 供暖器 | Heaters

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

  5. LeetCode算法题-Heaters(Java实现)

    这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...

  6. 【leetcode】475. Heaters

    problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...

  7. 475. Heaters

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. 475. Heaters (start binary search, appplication for binary search)

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

  9. 『ACM C++』 Codeforces | 1066B - Heaters

    今日不写日感,直接扔上今日兴趣点: 新研究称火星曾经有一个巨大的地下水系统 链接:https://mbd.baidu.com/newspage/data/landingsuper?context=%7 ...

随机推荐

  1. Adam 演示demo内容整理

    在这个6个多G的演示demo中,还是发现了不少东西. 这篇文章八卦向的东西比较多,不过支持abc格式的话,做Cutscene一下子多了很多可以用的东西. 1.在插件目录下发现了ABC格式的导入dll. ...

  2. vsftp 使用匿名帐号登陆

    1.正常安装. 2.改配置文件:vi /etc/vsftpd/vsftpd.conf #允许匿名用户登录FTP anonymous_enable=YES #设置匿名用户的登录目录(如需要,需自己添加并 ...

  3. Entity Framework 第一篇

    这段时间研究了orm框架EF 写一写研究的历程和心得 先贴上核心代码 public interface ITransaction { bool IsTransaction { get;} void B ...

  4. zoj Abs Problem

    Abs Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Alice and Bob is pl ...

  5. CAShapeLayer

    之前讲过CALayer动画相关知识,再来看看更加复杂的CAShapeLayer相关的动画知识. 普通CALayer在被初始化时是需要给一个frame值的,这个frame值一般都与给定view的boun ...

  6. EF 存储过程

    今天我们利用EF执行sql语句的方式来执行存储过程,并得到OutPut的值. 首先新建存储过程: Create PROCEDURE proc_testEF   (     @id int,     @ ...

  7. jquery 获得星期几-根据当前日期判断是星期几

    jquery 获得星期几 <script type="text/javascript"> var date = "07/17/2014";    / ...

  8. VC++ 利用MAPI实现在程序中调用默认的电子邮件程序发送EMAIL(可以添加附件)。

    1.利用ShellExecute 可以条用默认邮件客户端,但不能发送带附件的邮件 mailto:用户账号@邮件服务器地址?subject=邮件主题&body=邮件正文   如:ShellExe ...

  9. 如何通过Button获取UITableViewCell

    发现一个奇怪的问题: 手机(ios7) 2015-06-17 15:11:29.323 ***[1412:60b]  [btn superview] =  UITableViewCellContent ...

  10. 修改ftp密码

    1.运行cmd2.在DOS窗口中输入FTP 127.0.0.13.出现用户名输入提示“user”,键入用户名,按回车4.出现输入密码提示:“Password”,键入密码后按回车登录到服务器中5.在ft ...