原题链接在这里: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. hdu-acm steps Monkey and Banana

    这道题是典型的dp题.首先是数据的处理上,因为每个长方体的3条不同长度的棱都可以作为高,因此一个长方体可以看成3个不同的长方体.从而将数据扩展为3*n,然后将所有的长方体以长度为第一排序条件,宽度为第 ...

  2. android之数据库SQLite(一)

    创建数据库 首先定义SQLiteOpenHelper的子类 代码如下: package com.example.myandroid; import android.content.Context; i ...

  3. 去掉网址中的 html编码

    修改 web\urlManager  createUrl函数,去掉 urlEncode函数

  4. OC学习笔记——类别(Category)

    类别,有些程序员又称之为分类. 类别是一种为现有的类添加新方法的方式,尤其是为系统的做扩展的时候,不用继承系统类,可以直接为类添加新的方法.也可以覆盖系统类的方法. 如: @interface NSO ...

  5. Grunt教程——初涉Grunt

    前端自动化,这样的一个名词听起来非常的有吸引力,向往力.当今时代,前端工程师需要维护的代码变得及为庞大和复杂,代码维护.打包.发布等流程也 变得极为繁琐,同时浪费的时间和精力也越来越多,当然人为的错误 ...

  6. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [1] 单例模式连接数据库

    单例模式 单例模式三大原则: ① 构造函数需要标记为非 public (防止外部使用 new 操作符创建对象),单例类不能在其他类中实例化,只能被自身实例化: ② 拥有一个保存类的实例的静态成员变量 ...

  7. DWZ中关于iframeCallback和validateCallback的注意事项

    在DWZ上传中..如果要上传图片.则一定只能使用iframeCallback. 并且要表单中注明enctype="multipart/form-data"

  8. 10 位顶级 PHP 大师的开发原则

    10 位顶级 PHP 大师的开发原则 ruby_chen 发布于: 2013年03月28日 (61评) 分享到:    收藏 +139 #深圳# 6月10日 亚马逊AWSome Day云计算免费培训报 ...

  9. Latex公式换行、对齐

    http://blog.sina.com.cn/s/blog_64827e4c0100vnqu.html 换行后等式对齐 \begin{equation}\begin{aligned}R(S_2)&a ...

  10. Tomcat安装、启动和配置

    Tomcat服务器介绍 Tomcat是一个免费开发源代码的web应用服务器,具有与IIS.Apache等web服务器一样处理html页面的功能,另外它还是一个Servlet和JSP容器,独立的serv ...