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

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

基本思路为O(n^2) 就是每次扫了word1之后再扫word2.

Imporve 思路为O(n), 也就是每次找到两个相应的最近的点, 我们就跟ans比较一下, ans的初始化为len(words)

Code

class Solution:
def shortestDistance(self, words, word1,word2):
point1, point2, ans, words = None, None, len(words), [0] + words # 有[0]是为了方便edge case, 我们直接用point1 and point2来判断, 否则point1 or point2 == 0 的时候会影响 for i in range(1, len(words)):
if words[i] == word1:
point1 = i
if words[i] == word2:
point2 = i
if point1 and point2:
ans = min(ans, abs(point1 - point2))
return ans

[LeetCode] 243. Shortest Word Distance_Easy的更多相关文章

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

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

  2. LeetCode 243. Shortest Word Distance (最短单词距离)$

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

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

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

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

  5. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

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

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

  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

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

  9. 243. Shortest Word Distance 最短的单词index之差

    [抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...

随机推荐

  1. java框架---->commonmark的使用(一)

    commonmark-java是一个Markdown 解析器,一个基于CommonMark规范解析和渲染Markdown文本的Java库.偶尔要回头看看,否则永远都在追寻,而不知道自己失去了什么. c ...

  2. sencha touch 在线实战培训 第一期 第四节

    2014.1.4晚上8点开的课 第一节收费课程,还是有几位同学付费了,这些课程也录像了的,以后也会持续销售. 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内容:          ...

  3. jquery.fly.min.js 拋物插件

    插件官方: https://github.com/amibug/fly, 官方例子: http://codepen.io/hzxs1990225/full/ogLaVp 首先加载jQuery.js和j ...

  4. SQL Server2008 R2 安装失败后的解决办法

    当你第一次安装SQL Server2005,SQL Server2008,SQL Server2012失败后,第二次重新安装一般还是容易安装失败,原因就是你没有完全卸载,还存留残留文件和注册表. 我安 ...

  5. 关于Jmeter3.0,你必须要知道的5点变化

    2016.5.18日,Apache 发布了jmeter 3.0版本,本人第一时间上去查看并下载使用了,然后群里或同事都会问有什么样变化呢?正好在网上看到一遍关于3.0的文章,但是是英文的.这里翻译一下 ...

  6. C# 验证XML

    一.验证XML文档 若要验证 XML 文档,需要构造一个 XmlReaderSettings 对象,其中包含用于验证 XML 文档的 XML 架构定义语言 (XSD) 架构.Schema是用于描述和规 ...

  7. Fiddler 教程(转载,鉴于原作者关闭了访问fiddler系列文章)

    阅读目录 Fiddler的基本介绍 Fiddler的工作原理 同类的其它工具 Fiddler如何捕获Firefox的会话 Fiddler如何捕获HTTPS会话 Fiddler的基本界面 Fiddler ...

  8. linux下配置Java_web环境

    I安装JDK 1下载jdk*.bin/jdk*.rpm文件 2把安装包copy到安装目录 cp jdk*.bin /www/java 3安装JDK 如果是.bin文件,直接在复制的当前目录执行即可 . ...

  9. Thread和Runable的区别、Synchronized锁关键字

    一.Thread和Runable的区别 Thread是基类,子类必继承他实现其run方法.其也是实现了Runable接口.Thread是普通的类,并非抽象类或者密封类等. Runnable是接口,子类 ...

  10. POJ-1157 LITTLE SHOP OF FLOWERS(动态规划)

    LITTLE SHOP OF FLOWERS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19877 Accepted: 91 ...