Shortest Word Distance 解答
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 解答的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- LeetCode Shortest Word Distance III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- LeetCode Shortest Word Distance II
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...
- LeetCode Shortest Word Distance
原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
- 244. Shortest Word Distance II
题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...
- [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 ...
随机推荐
- 齐B小短裙
女模周蕊微博引关注 火了齐B小短裙毁了干爹[图]_网易新闻中心 齐B小短裙
- [原创作品]web网页中的锚点
因为近来在从事web前端开发的工作,所以写的文章也都是关于web这一块.以后将分享算法和web高级编程的内容,很多公司的web前端不够重视,以为是很low-level,给的待遇也很一般,其实,这都是很 ...
- C++内存分配的五种方法
在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区. 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数 ...
- [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
Now we want each time we click refresh button, we will get new group of users. So we need to get the ...
- Javascript进阶篇——(DOM—节点---属性、访问节点)—笔记整理
节点属性在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType : ...
- html的空格显示距离问题
一.使用全角空格 全角空格被解释为汉字,所以不会被被解释为HTML分隔符,可以按照实际的空格数显示. 二.使用空格的替代符号 替代符号就是在需要显示空格的地方加入替代符号,这些符号会被浏览器解释为空格 ...
- (转)sql语句中charindex的用法
假如你写过很多程序,你可能偶尔会碰到要确定字符或字符窜串否包含在一段文字中,在这篇文章中,我将讨论使用CHARINDEX和PATINDEX函数来搜索文字列和字符串.我将告诉你这两个函数是如何运转的,解 ...
- PowerDesigner Mysql 主键自增、初始值、字符集
自增 在你所要设为自增型的键上(比如你的id)双击,弹出一个Column Properties对话框,右下角有一个Identify的选择框,选中它OK,就可以了. 再去查看Preview,就能看到AU ...
- C#重写Equals方法步骤
检查传入的参数是否为null, 如果为null,那么返回false, 否则执行步骤2 调用ReferenceEquals查看是否为统一个对象,如果是,那么返回true, 否则执行步骤3 判断两者是否为 ...
- JS生成不重复随机数
说明 我们可以用Math.random()的方法轻松的生成 一个随机的数字,但是这个数字可能是重复的.有时候,我们需要一个不重复的随机数,可以用很多的方法来实现这个要求,以下方法是效率最高的. 解释 ...