原题链接在这里: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 IIShortest Word Distance III.

LeetCode Shortest Word Distance的更多相关文章

  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 II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

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

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

  4. LeetCode Shortest Word Distance III

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

  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 I & II & III

    Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...

  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]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

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

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

随机推荐

  1. 你能不用计算机来计算S=a+(a+1)+(a+2) + ...... + b的解的数目吗?

    S=a + (a + 1) + (a + 2) + ...... + b(其中a, b > 0) 现在我们要求,给定一个正整数S,求有多少种不同的<a,b>,使得上述的等式成立. 这 ...

  2. VTKMY 3.3 VS 2010 Configuration 配置

    Download VTKMY 3.3 Download VS2010 Download CMake 3.2.0 I assume you've already installed VS2010 and ...

  3. people 0919

    package liu0919; public class People { private double height;// 身高 private String name;// 名字 private ...

  4. 移动web app开发小贴士 收藏有用

    1 创建主屏幕图标 (Creating a home screen icon ,for ios)   1 2 3 4 5 6 //57*57 <link rel="apple-touc ...

  5. 绕过杀毒软件抓取windows密码

    使用procdump,由于是微软的东西,带微软签名杀软不会报毒. procdump -accepteula -ma lsass.exe lsass.dmp copy出 lsass.dmp到本机. mi ...

  6. IOS 今天学到太多的知识了,赶快记录下来

    TabBarController 修改tabbar的背景颜色和选中时候的颜色: func application(application: UIApplication, didFinishLaunch ...

  7. Apache Spark源码走读之1 -- Spark论文阅读笔记

    欢迎转载,转载请注明出处,徽沪一郎. 楔子 源码阅读是一件非常容易的事,也是一件非常难的事.容易的是代码就在那里,一打开就可以看到.难的是要通过代码明白作者当初为什么要这样设计,设计之初要解决的主要问 ...

  8. mysql insert一条记录后怎样返回创建记录的主键id,last_insert_id(),selectkey

    mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...

  9. Windows与Linux/Mac系统时间不一致的解决方法

    Windows与Linux/Mac系统时间不一致的解决方法 分类: linux2012-02-12 14:25 1691人阅读 评论(1) 收藏 举报 windowsubuntusystemlinux ...

  10. Yii源码阅读笔记(十四)

    Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: /** * Returns a list of scenarios and the corresponding active attr ...