<Array> 277 243 244 245
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的更多相关文章
- ***CodeIgniter集成微信支付(转)
微信支付Native扫码支付模式二之CodeIgniter集成篇 http://www.cnblogs.com/24la/p/wxpay-native-qrcode-codeigniter.html ...
- Bitmap vs 2Bitmap的实现
[本文链接] http://www.cnblogs.com/hellogiser/p/bitmap-vs-2bitmap.html [题目] 在2.5亿个整数找出不重复的整数,内存不足以容纳着2.5亿 ...
- java中常用的工具类(三)
继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类 ...
- java中常用的工具类(二)
下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- PHP和Golang使用Thrift1和Thrift2访问Hbase0.96.2(ubuntu12.04)
目录: 一.Thrift1和Thrift2的简要介绍 1) 写在前面 2) Thrift1和Thrift2的区别 二.Thrift0.9.2的安装 1) 安装依赖插件 2) Thrift0.9.2的 ...
- Android中多线程下载列表的封装实现(含进度反馈)
来源:http://blog.csdn.net/u011638883/article/details/17347015 实现了一下Android中的文件多线程下载模块,支持自定义线程数.断点续传.下载 ...
- 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 ...
- retrofit2 okhttp3 RxJava butterknife 示例
eclipse的jar包配置 eclipse中貌似用不了butterknife buildToolsVersion "23.0.2" defaultConfig { applica ...
- WCF的执行过程
既然是实现互通信.那么肯定会有概念意义上的服务端Server 和概念意义上的客户端 Client,在这里,我所说明的都是概念意义上的,单独强调此,是因为,基于WCF的通信没有物理上的划分,但是概念上 ...
随机推荐
- 2个ajax的相互调用解决异步问题
- 怎么解决 hash 冲突
开放定址法: 线性探测再散列 二次探测再散列 伪随机 再hash: 同时构造,多个不同的hash函数 链地址: 链表, 建立公共溢出区: 分为基本表和溢出表两个部分 开放散列(open hashing ...
- Centos7升级gcc极简教程
centos7默认gcc版本为4.8,一般不满足编译需求,因此升级gcc版本为常见操作: 现有博客中,大多数教程都是基于源码重新编译安装:但是源码编译过程等待时间很长且编译麻烦. 因此,直接基于命令升 ...
- SQL查询--关于查询的练习题
下面的练习题出自LeetCode:https://leetcode-cn.com/problemset/database/,有兴趣的可以去上面刷刷题 练习题1:超过经理收入的员工 分析: 使用sql ...
- x86-64数据格式、通用寄存器与操作数格式
x86-64数据格式.通用寄存器与操作数格式 数据格式 Intel用术语"字(word)"表示16位数据类型,32位为"双字(double words)", ...
- .NET Core NuGet 多项目套餐打包的正确姿势
NuGet 默认只支持一个菜一个菜打包,不支持套餐打包.当对一个 csproj 项目进行 nuget 打包时(比如使用 dotnet pack 命令),只会将当前项目 build 出来的 dll 程序 ...
- 同步IDEA系列软件的设置,再也不用但心我的配置丢失了
同步IDEA系列软件的设置 问题描述:重装idea,之前配置好的快捷键就没有了.之前一直是每隔几个月要把配置导出一下,上传百度云盘.现在好了,通过配置可以自动同步配置了.我再也不用但心配置丢失了. 快 ...
- MySQL 中的外键
表和表之间可存在引用关系,这在抽象数据到表时,是很常见的.这种联系是通过在表中创建外键(foreign key)来实现的. 比如一个订单,可能关联用户表和产品表,以此来记录谁买了什么产品. 约定两个概 ...
- C#上手练习4(Break、CONITINUE语句)
C# 中的 continue 语句有点像 break 语句.但它不是强制终止,continue 会跳过当前循环中的代码,强制开始下一次循环. 对于 for 循环,continue 语句会导致执行条件测 ...
- 百度Sitemap生成器
今天用了两个小时, 为无限影视(https://www.88tv.org)开发了一个小工具, 用来生成baidu的sitemap. 方便用. 因为该电影站的视频内容详情网页的ID是自增长的,所以可以 ...