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. 三.jquery.datatables.js表格编辑与删除

    1.为了使用如图效果(即将按钮放入行内http://www.datatables.net/examples/ajax/null_data_source.html) 采用了另一个数据格式 2.后台php ...

  2. liunx trac 插件使用之DateFieldPlugin

    插件GanttCalendarPlugin安装完以后,有一个问题,就是在选择起始与结束时间的时候,为了方便有datepicker功能,如图 需要用到插件DateFieldPlugin,官网链接http ...

  3. sonarqube插件开发(二) 开发插件

    一.环境准备 java 1.8, maven 3.1 检查自己的环境是否支持 sonarqube的插件开发 java -version mvn -version 二.创建maven项目 pom.xml ...

  4. slf4j + log4j 是如何初始化的

    SLF4J的全称是 Simple Logging Facade for Java(简单java日志门面) SLF4J自己不提供具体的日志功能实现,只是提供了一个统一的日志门面,在这个统一的门面之下,用 ...

  5. SDWebImage第三方库使用注意的一些问题

    1.利用"UIImageView+WebCache.h"加载图片数据 例如: UIImage *placeHolderImg = [UIImage imageNamed:@&quo ...

  6. vue之cli脚手架安装和webpack-simple模板项目生成

    ue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目. GitHub地址是:https://github.com/vuejs/vue-cli 一.安 ...

  7. java 中的this

    this 关键字 1.在类的方法定义中使用this关键字 代表使用该方法的对象的引用 2.必须指出当前使用方法的对象是谁时 使用this 3.有时使用this可以处理方法中成员变量和参数重名的情况 4 ...

  8. java 类中的细节

    java 中类: 类是用于描述统一类型的对象的一个抽象的概念,类中定义了这一类对象所因具有的静态和动态属性. 举例: 瓶子静态: 有一个口.长长的形状-->java类中的成员变量动态属性: 可以 ...

  9. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  10. 【Python算法】遍历(Traversal)、深度优先(DFS)、广度优先(BFS)

    图结构: 非常强大的结构化思维(或数学)模型.如果您能用图的处理方式来规范化某个问题,即使这个问题本身看上去并不像个图问题,也能使您离解决问题更进一步. 在众多图算法中,我们常会用到一种非常实用的思维 ...