Question

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.

Solution

双指针的方法。last_word1为word1最后一次出现的坐标。last_word2为word2最后一次出现的坐标。所以当last_word1变化时,我们计算last_word1 - last_word2 + 1,并且和当前最小值比较更新。Same to last_word2.

 class Solution(object):
def shortestDistance(self, words, word1, word2):
"""
:type words: List[str]
:type word1: str
:type word2: str
:rtype: int
"""
last_word1 = -1
last_word2 = -1
length = len(words)
result = length
for i in range(length):
if words[i] == word1:
last_word1 = i
if last_word2 != -1:
result = min(result, last_word1 - last_word2)
elif words[i] == word2:
last_word2 = i
if last_word1 != -1:
result = min(result, last_word2 - last_word1)
return result

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

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...

  7. 245. Shortest Word Distance III

    题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...

  8. 244. Shortest Word Distance II

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

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

随机推荐

  1. IOS深入学习(12)之Archiving

    1 前言 本文介绍的是一个归档解档方法,也是编码和解码时候所做的事情,和如何进行,编码和归档其实就是将对象关系转化为字节流并且归档为特殊的文件,解码和解档是逆过程. 英文原文:http://blog. ...

  2. linux内核--中断和中断处理(一)

        让硬件在需要的时候再向内核发出信号.这就是中断机制,先讨论中断,进而讨论内核如何使用所谓的中断处理函数处理对应的中断. 一.中断   1)中断     中断使得硬件得以发出通知给处理器.例如, ...

  3. Thinkphp显示系统常量信息的方法(php的用法)

    输入 :public function Main()    {        dump(get_defined_constants(true));    }显示系统信息, 其中: 'APP_PATH' ...

  4. DirectX Sample-Blobs实现原理

    这个例子的实现主要包括两步: 1.计算三维采样坐标和color,实现代码是for( i = 0; i < NUM_Blobs; ++i )那个循环,计算完成以后g_pTexGBuffer[0]保 ...

  5. Redhat6.4 配置本地网络的FTP YUM源

    Redhat6.4 配置本地网络的FTP YUM源 如果本机IP: 192.168.8.47 (一) 配置本机的yum源 使用以下的方法能够配置本机的yum源: 1) scp命令上传ISO文件到: / ...

  6. 使用游标循环进行SQL更新插入的SQL语句

    使用SQL中的循环,可以实现许多我们需要的操作,比如SQL更新操作.下面就为您介绍使用游标循环进行SQL更新插入的SQL语句写法,希望对您深入学习SQL更新有所帮助. --开始事务 BEGIN TRA ...

  7. SignalR2.0开发实例之——群发消息

    一.前言 ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信.什么是实时通信的Web呢?就是让客户端(Web页面)和服务器端可以互相 ...

  8. unity绘制线和绘制面

    绘制线条代码,其实就是指定至少两个点,然后赋予贴图即可,不废话,上代码: using UnityEngine; using System.Collections; public class LineT ...

  9. Private Members in JavaScript

    Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most mis ...

  10. 大家来找茬-SpringMVC中Tomcat正常启动,始终访问不了Controller,出404错

    创建了一个空的SpringMVC项目,Tomcat可以正常启动,但是运行的时候,始终进不了Controller,并且报404错误. 百度各种查,结果也是查不到原因.各个群里面各种求,各种贴源码,也没有 ...