LeetCode Shortest Word Distance
原题链接在这里:https://leetcode.com/problems/shortest-word-distance/
题目:
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Given word1 = “coding”
, word2 = “practice”
, return 3.
Given word1 = "makes"
, word2 = "coding"
, return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
题解:
Time Complexity: O(n). n = words.length.
Space: O(1).
AC Java:
class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
if(words == null || words.length == 0 || word1.equals(word2)){
return 0;
} int ind1 = -1;
int ind2 = -1;
int res = words.length; for(int i = 0; i < words.length; i++){
if(words[i].equals(word1)){
ind1 = i;
if(ind2 != -1){
res = Math.min(res, ind1 - ind2);
}
} if(words[i].equals(word2)){
ind2 = i;
if(ind1 != -1){
res = Math.min(res, ind2 - ind1);
}
}
} return res;
}
}
跟上Shortest Word Distance II, Shortest Word Distance III.
LeetCode Shortest Word Distance的更多相关文章
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- LeetCode Shortest Word Distance II
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...
- [LeetCode] Shortest Word Distance I & II & III
Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...
- LeetCode 245. Shortest Word Distance III (最短单词距离之三) $
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
随机推荐
- php向队列服务里插入一条insert sql例如
Iserver简介 Iserver是一个用python编写的网络服务框架(编译版本3.4.1),使用的是epool网络模型 测试机配置 处理器 2x Genuine Intel(R) CPU T205 ...
- Java泛型总结
1. 什么是泛型?泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的 ...
- 【新产品发布】EVC9001 USB 隔离器
一. 简介 EVC9001采用Analog Device 公司的基于芯片级变压器的iCoupler 磁耦合隔离方案,完成了对USB接口双向隔离功能,隔离电压达 2500V(隔离电源模块 3000V隔 ...
- 【ZZ】 DShader之位移贴图(Displacement Mapping)
http://www.myexception.cn/other/1397638.html DShader之位移贴图(Displacement Mapping) www.MyException.Cn ...
- PHP 中和 HTTP 相关的函数及使用
① get_headers 方法:取得服务器响应一个 HTTP 请求所发送的所有标头 例如: <?php $httpinfo = get_headers('http://www.baidu.co ...
- Nginx 笔记与总结(6)Location:精准匹配
在 /usr/local/nginx/conf/nginx.conf 的 server 段中,location 表示根据 URI 来进行不同的定位:把网站的不同部分定位到不同的处理方式上,例如遇到 . ...
- 10个不太为人所知的,但实用的PHP函数
10个不太为人所知的,但实用的PHP函数 您的评价: 较差 收藏该经验 阅读目录 php_check_syntax highlight_string show_source ph ...
- 开放平台鉴权以及OAuth2.0介绍
OAuth 2.0 协议 OAuth是一个开发标准,允许用户授权第三方网站或应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的内容. OAuth 2.0 ...
- HTML: 文檔流是什麼?
[作者好人]:http://www.nowamagic.net/librarys/veda/detail/1190 文档流 将窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,即为文档流. ...
- 2.PHP内核探索:一次请求的开始与结束
PHP开始执行以后会经过两个主要的阶段: 处理请求之前的开始阶段 请求之后的结束阶段 开始阶段有两个过程: 第一个过程是模块初始化阶段(MINIT), 在整个SAPI生命周期内(例如Apache启动以 ...