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 ...
随机推荐
- redis运用连接池报错解决
redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...
- Mac or Centos 下如何编译objective-C
#import <Foundation/Foundation.h> int main(int argc,const char *argv[]){ @autoreleasepool{ NSL ...
- linux-centos下源代码安装subversion (svn)
1.svn的源代码 1.1 可以在官方下载,官方地址 :svn 1.6.17源码包 http://subversion.tigris.org/servlets/ProjectDocumentList ...
- 给NSString增加Java风格的方法
给NSString增加Java风格的方法 文章目录 我实在受不了 NSString 冗长的方法调用了,每次写之前都要查文档.特别是那个去掉前后多余的空格的方法,长得离谱.与之对应的别的语言,拿 jav ...
- Toll-Free Bridge
引 在深入了解桥接机制的时候看到一篇好文,虽然已经很久远,但是忍不住看了好几遍,心中诸多不解一扫而光.在此放上链接: 原文:http://ridiculousfish.com/blog/posts/b ...
- SDR和DDR1/2/3全系列频率对照表
- Java垃圾收集机制
通常,我们把分配出去后,却无法回收的内存空间称为"内存渗漏体(Memory Leaks)". 以上这种程序设计的潜在危险 性在Java这样以严谨.安全著称的语言中是不允许的.但是J ...
- Codeforces Round #357 (Div. 2) E 计算几何
传说中做cf不补题等于没做 于是第一次补...这次的cf没有做出来DE D题的描述神奇 到现在也没有看懂 于是只补了E 每次div2都是hack前2~3题 终于打出一次hack后的三题了...希望以后 ...
- DX11.2 Tiled Resource Pool
Nvidia white paper : https://developer.nvidia.com/content/taking-advantage-directx112-tiled-resource ...
- jQuery 找到当前元素之前最后一次出现的某个同辈元素
DOM 树状图如下所示,要找到 div id = 'a' 的元素之前的(同辈)离该 div 最近的一个 div class = 'a' 的元素(图中左至右第 2 个 div class = 'a' 的 ...