277. Find the Celebrity

knows(i, j):

By comparing a pair(i, j), we are able to discard one of them

  1. True: i 必定不是Celebrity, 因为Celebrity不认识任何人

  2. False: j 必定不是Celebrity, 因为Celebrity要被其他人认识。

第一轮:找出一个可能是Celebrity的Candidate,因为在之前的人都不是Celebrity,Candidate不认识后面的人,后面的人也不是Celebrity,但是前面的人可能不认识这个Candidate。

第二轮:验证这个Candidate是不是Celebrity。

public class Solution extends Relation {
public int findCelebrity(int n) {
int candidate = 0;
//1. Find a candidate by one pass:(make sure other people are not a celebrity)
for(int i = 1; i < n; i++){
if(knows(candidate, i)){
candidate = i;
}
}
//2. Make sure if the candidate is a celebrity by one pass
for(int i = 0; i < n; i++){
if(i == candidate){
continue;
}
if(!knows(i, candidate) || knows(candidate, i)){
return -1;
}
}
return candidate;
}
}

243. Shortest Word Distance

用index a和b来记录xia

class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int a = -1;
int b = -1;
int result = Integer.MAX_VALUE;
for(int i = 0; i < words.length; i++){
if(word1.equals(words[i])){
a = i;
}else if(word2.equals(words[i])){
b = i;
}
if(a != -1 && b != -1){
result = Math.min(result, Math.abs(a - b));
}
}
return result;
}
}

244. Shortest Word Distance II

用HashMap来记录每个单词和他的索引,然后再计算距离的最小值。

class WordDistance {
Map<String, List<Integer>> map = new HashMap<>(); public WordDistance(String[] words) {
for(int i = 0; i < words.length; i++){
if(map.containsKey(words[i])){
map.get(words[i]).add(i);
}else{
List<Integer> list = new ArrayList<>();
list.add(i);
map.put(words[i], list);
}
}
} public int shortest(String word1, String word2) {
List<Integer> l1 = map.get(word1);
List<Integer> l2 = map.get(word2);
int i = 0, j = 0;
int result = Integer.MAX_VALUE; while(i < l1.size() && j < l2.size()){
if(l1.get(i) < l2.get(j)){
result = Math.min(result, l2.get(j) - l1.get(i));
i++;
}else{
result = Math.min(result, l1.get(i) - l2.get(j));
j++;
}
} return result;
}
}

245. Shortest Word Distance III

用b来记录上一个相同string的位置,到第二个位置的时候赋值给a。

class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
int a = words.length, b = -words.length;
int result = Integer.MAX_VALUE;
for(int i = 0; i < words.length; i++){
if(words[i].equals(word1)){
a = i;
}
if(words[i].equals(word2)){
if(word1.equals(word2))
a = b;
b = i;
}
if(a != -1 && b != -1){
result = Math.min(result, Math.abs(a - b));
}
}
return result;
}
}

<Array> 277 243 244 245的更多相关文章

  1. ***CodeIgniter集成微信支付(转)

    微信支付Native扫码支付模式二之CodeIgniter集成篇  http://www.cnblogs.com/24la/p/wxpay-native-qrcode-codeigniter.html ...

  2. Bitmap vs 2Bitmap的实现

    [本文链接] http://www.cnblogs.com/hellogiser/p/bitmap-vs-2bitmap.html [题目] 在2.5亿个整数找出不重复的整数,内存不足以容纳着2.5亿 ...

  3. java中常用的工具类(三)

    继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类       ...

  4. java中常用的工具类(二)

    下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil           Java   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  5. PHP和Golang使用Thrift1和Thrift2访问Hbase0.96.2(ubuntu12.04)

    目录: 一.Thrift1和Thrift2的简要介绍 1) 写在前面 2) Thrift1和Thrift2的区别  二.Thrift0.9.2的安装 1) 安装依赖插件 2) Thrift0.9.2的 ...

  6. Android中多线程下载列表的封装实现(含进度反馈)

    来源:http://blog.csdn.net/u011638883/article/details/17347015 实现了一下Android中的文件多线程下载模块,支持自定义线程数.断点续传.下载 ...

  7. js正则验证方法大全

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  8. retrofit2 okhttp3 RxJava butterknife 示例

    eclipse的jar包配置 eclipse中貌似用不了butterknife buildToolsVersion "23.0.2" defaultConfig { applica ...

  9. WCF的执行过程

    既然是实现互通信.那么肯定会有概念意义上的服务端Server  和概念意义上的客户端 Client,在这里,我所说明的都是概念意义上的,单独强调此,是因为,基于WCF的通信没有物理上的划分,但是概念上 ...

随机推荐

  1. 微信公众号开发 ,redirect_uri域名还是与后台配置不一致

    测试账号地址: https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index https ...

  2. Filter 原理

    二.Filter 原理 2.1 Filter 概述 Filter(过滤器)是 DirectShow 中最基本的概念.DirectShow 是通过 Filter Graph 来管理 Filter 的.F ...

  3. Java并发编程入门,看这一篇就够了

    Java并发编程一直是Java程序员必须懂但又是很难懂的技术内容.这里不仅仅是指使用简单的多线程编程,或者使用juc的某个类.当然这些都是并发编程的基本知识,除了使用这些工具以外,Java并发编程中涉 ...

  4. [Ljava.lang.String是一个字符串数组的字节码表示

    打印一个字符串数组的话,会发现 String[] arr = new String[10]; // String[] arr = {"yanggb", "huangq&q ...

  5. 基于 HTML5 WebGL 的 3D 科幻风机

    前言 许多世纪以来,风力机同水力机械一样,作为动力源替代人力.畜力,对生产力的发展发挥过重要作用.近代机电动力的广泛应用以及二十世纪50年代中东油田的发现,使风机发电机的发展缓慢下来. 70年代初期, ...

  6. Ubuntu安装CUDA、CUDNN比较有用的网址总结

    Ubuntu安装CUDA.CUDNN比较有用的网址总结 1.tensorflow各个版本所对应的的系统要求和CUDA\CUDNN适配版本 https://tensorflow.google.cn/in ...

  7. WPF中DataGrid在没有数据的时候也可以显示水平滚动条

    今天做项目中遇到个问题,就是页面加载后默认DataGrid是不加载数据的,但是DataGrid的列很多,就导致了运行效果上,此窗口的DataGrid没有水平滚动条,类似图片的效果. 经过百度和摸索,使 ...

  8. 记一次asp.net core 在iis上运行抛出502.5错误

    asp.net core 在iis上运行抛出502.5异常的部分原因以及解决方案 环境说明 已安装 .net core runtime 2.1.401 已安装 .net core windows ho ...

  9. Django 的ORM 表间操作

    Django之ORM表间操作   之前完成了简单的数据库数据增加操作.这次学习更多的表间操作. 单表操作 增加 方式一 b = Book(title="Python基础", pub ...

  10. 5. this关键字

    一.this关键字概述 1. this作为对象的引用,它总是指向调用该方法的对象 2. this的最大作用:让类中的一个方法访问该类中的另一个方法或实例变量 二.this关键字的两种用法 1. 在方法 ...