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. 【转】C/C++函数调用过程分析

    转自:here 这里以一个简单的C语言代码为例,来分析函数调用过程 代码: #include <stdio.h> int func(int param1 ,int param2,int p ...

  2. LeetCode-494. Target Sum(DFS&DP)

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  3. jQuery的回调管理机制(三)

    jQuery.when()方法是jQuery内部使用回调机制的范例. // 参数为多个方法,这些方法全部执行完成之后执行回调 when: function( subordinate /* , ..., ...

  4. create-react-app中img图片不现实

    场景:正常的情况下是这么引用图片,我的图片路径是 src/images/login-from-icon1.png <img src="../images/login-from-icon ...

  5. 升级后重启造成fsck.ext3: Unable to resolve UUID

    这篇文章帮了我的大忙了:转载自:http://wilber82.blog.51cto.com/1124820/724472 今天在做服务器补丁部署,有一台ESX4.1的服务器在升级后重启过程中挂了,通 ...

  6. linux下php redis扩展安装

    sudo tar vxzf redis-2.2.7.tgz cd redis-2.2.7 执行sudo /data/service/php54/bin/phpize 在目录下生成配置文件 sudo . ...

  7. ELK系列五:Logstash输出到Elasticsearch和redis

    1.Logstash与Redis的读写 1.1 Logstash 写入Redis 看完Logstash的输入,想必大家都清楚了Logstash的基本用法,那就是写配置文件. output{ { red ...

  8. 如何防御mimikatz致敬Mimikatz攻防杂谈学习笔记

    零.绪论:mimikatz简介 mimikatz是一款出色的内网渗透工具,可以抓取windows主机的明文密码.NTLMhash值或者kerberos对应的缓存凭据.mimikatz的使用在获取权限后 ...

  9. UEditor富文本WEB编辑器自定义默认值设置方法

    1.在使用UEditor编辑器编写内容时你会发现,当输入的内容较多时,编辑框的边框高度也会自动增加,若希望输入内容较多时以拉框滚动的效果. 方法:找到Ueditor文件根目录下的ueditor.con ...

  10. [转]Android中attr自定义标签详解

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:wen= ...