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. Jsensation | 氪加

    Jsensation | 氪加 www.jsensation.com

  2. 我的四年建站故事(X)

    今天版主们有在群里聊建站的事情了,似乎他们每个人都很热衷于搭建一个自己的网站或者博客,我突然之间觉得非常有必要做一些经验的分享.        首先我想先介绍一下CMS( 网站内容管理系统, 即 Co ...

  3. Java中判断集合类为空的方法

    *****需要引入Spring的核心Jar包***** 工具类: org.springframework.util.CollectionUtils 方法: public static boolean ...

  4. js练习【DOM操作】

    完成效果: 演示地址:http://codepen.io/anon/pen/jPbYWq HTML: <!DOCTYPE html> <html lang="en" ...

  5. ORACLE SEQUENCE 介绍

    在oracle中sequence就是所谓的序列号,每次取的时候它会自己主动添加,一般用在须要按序列号排序的地方.  1.Create Sequence  你首先要有CREATE SEQUENCE或者C ...

  6. sdut2623--The number of steps(概率dp第一弹,求期望)

    The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...

  7. Mongo服务器集群配置【转】

    http://www.cnblogs.com/wly923/tag/MongoDB/ 当前标签: MongoDB   Mongo服务器集群配置学习三——分片 风行影者 2013-04-14 22:35 ...

  8. JavaScript学习笔记:检测数组方法

    检查数组的方法 很多时候我们需要对JavaScript中数据类型(Function.String.Number.Undefined.Boolean和Object)做判断.在JavaScript中提供了 ...

  9. Internetmap.apk实现原理分析

    1.本地实现调用 程序根据data文件目录下的asinfo.json文件(包含自治域网络名和对应的坐标值),调用so文件绘制asn结点图(ASN,AutoSystemNode,自治域结点) 2.路由查 ...

  10. MVC5富文本编辑器CKEditor配置CKFinder

    富文本编辑器CKEditor的使用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...