原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/

题目:

This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

word1 and word2 may be the same and they represent two individual words in the list.

For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Given word1 = “makes”word2 = “coding”, return 1.
Given word1 = "makes"word2 = "makes", return 3.

题解:

若是两个指针p1 和 p2相等时,不更新res.

Time Complexity: O(n). Space: O(1).

AC Java:

 class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
if(words == null){
return -1;
} 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 && ind1 < ind2){
res = Math.min(res, ind2 - ind1);
}
}
} return res;
}
}

类似Shortest Word Distance.

LeetCode Shortest Word Distance III的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  5. LeetCode Shortest Word Distance II

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...

  6. LeetCode Shortest Word Distance

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...

  7. 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 ...

  8. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  9. 245. Shortest Word Distance III

    题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...

随机推荐

  1. oracle存储过程、声明变量、for循环|转|

    oracle存储过程.声明变量.for循环 1.创建存储过程 create or replace procedure test(var_name_1 in type,var_name_2 out ty ...

  2. UIWebView弹出键盘按钮显示中文

    UIWebView是一个很常用的视图,一般用来加载网页,比如百度: 点击文本框输入框后,会弹出一个带有toolbar的键盘,toolbar中有3个辅助按钮 有了这3个按钮,是方便很多,但默认是英文的, ...

  3. BibTex插入Reference

    1.  在tex文件中加入: \bibliographystyle{plain} %这是格式 \bibliography{reffile} % reffile.bib是reference的文件   2 ...

  4. Symantec Liveupdate Administrator的搭建

      1. My Symantec Products 选择需要更新的产品 2. Source Servers 选择ftp, 下载比较稳定 3. Distribute Center 类似于WSUS中的ap ...

  5. nginx 配置文件分析以及配置负载均衡器

    修改Nginx核心配置文件nginx.conf # cat /usr/local/nginx/conf/nginx.conf user www www; worker_processes ; # 工作 ...

  6. linux常用命令的英文单词缩写

    命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户rpm:redhat package manager 红帽子打包管 ...

  7. UITextField里面的 placeholder颜色和字体

    //  placeholder 颜色 [field setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor& ...

  8. Rice Rock

    先翻译评分要点,然后一点点翻译程序实现过程 如何产生一堆岩石? rock_group = set([])#空集合,全局变量   rock_group.add(a_rock) 要画出来draw hand ...

  9. 特征向量-Eigenvalues_and_eigenvectors#Graphs

    https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors#Graphs A               {\displaystyle A} ...

  10. Running Kafka At Scale

    https://engineering.linkedin.com/kafka/running-kafka-scale If data is the lifeblood of high technolo ...