[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 word2.
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
word1 and word2 may be the same and they represent two individual words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “makes”, word2 = “coding”, return 1.
Given word1 = "makes", word2 = "makes", return 3.
Note:
You may assume word1 and word2 are both in the list.
243. Shortest Word Distance 和 245. Shortest Word Distance II 的拓展,这次两个单词可能会相同。
Python:
# Time: O(n)
# Space: O(1)
class Solution:
# @param {string[]} words
# @param {string} word1
# @param {string} word2
# @return {integer}
def shortestWordDistance(self, words, word1, word2):
dist = float("inf")
is_same = (word1 == word2)
i, index1, index2 = 0, None, None
while i < len(words):
if words[i] == word1:
if is_same and index1 is not None:
dist = min(dist, abs(index1 - i))
index1 = i
elif words[i] == word2:
index2 = i if index1 is not None and index2 is not None:
dist = min(dist, abs(index1 - index2))
i += 1 return dist
C++:
class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int p1 = -1, p2 = -1, res = INT_MAX;
for (int i = 0; i < words.size(); ++i) {
int t = p1;
if (words[i] == word1) p1 = i;
if (words[i] == word2) p2 = i;
if (p1 != -1 && p2 != -1) {
if (word1 == word2 && t != -1 && t != p1) {
res = min(res, abs(t - p1));
} else if (p1 != p2) {
res = min(res, abs(p1 - p2));
}
}
}
return res;
}
};
C++:
class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int p1 = words.size(), p2 = -words.size(), res = INT_MAX;
for (int i = 0; i < words.size(); ++i) {
if (words[i] == word1) p1 = word1 == word2 ? p2 : i;
if (words[i] == word2) p2 = i;
res = min(res, abs(p1 - p2));
}
return res;
}
};
C++:
class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int idx = -1, res = INT_MAX;
for (int i = 0; i < words.size(); ++i) {
if (words[i] == word1 || words[i] == word2) {
if (idx != -1 && (word1 == word2 || words[i] != words[idx])) {
res = min(res, i - idx);
}
idx = i;
}
}
return res;
}
};
类似题目:
[LeetCode] 243. Shortest Word Distance 最短单词距离
[LeetCode] 245. Shortest Word Distance II 最短单词距离 II
All LeetCode Questions List 题目汇总
[LeetCode] 245. Shortest Word Distance III 最短单词距离 III的更多相关文章
- [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 ...
- [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 ...
- LeetCode 243. 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 II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- 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 ...
- [LeetCode] 243. 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 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [Swift]LeetCode245.最短单词距离 III $ Shortest Word Distance III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
随机推荐
- 【Difference Between Primes HDU - 4715】【素数筛法打表+模拟】
这道题很坑,注意在G++下提交,否则会WA,还有就是a或b中较大的那个数的范围.. #include<iostream> #include<cstdio> #include&l ...
- Django REST framework —— 权限组件源码分析
在上一篇文章中我们已经分析了认证组件源码,我们再来看看权限组件的源码,权限组件相对容易,因为只需要返回True 和False即可 代码 class ShoppingCarView(ViewSetMix ...
- 含-SH的ACE抑制药的青霉胺样反应
关于 含-SH的血管紧张素转化酶(ACE)抑制药如卡托普利具有青霉胺样反应.而依那普利则不含-SH. 青霉胺样反应 青霉胺样反应,指应用含-SH的ACE抑制药产生的皮疹.嗜酸性粒细胞(E)增多.味觉异 ...
- Cramer-Rao Bounds (CRB)
克拉美-罗界.又称Cramer-Rao lower bounds(CRLB),克拉美-罗下界. 克拉美罗界是对于参数估计问题提出的,为任何无偏估计量的方差确定了一个下限.无偏估计量的方差只能无限制的逼 ...
- ssh配置基础
1:hostname r12:R1(config)#username xxx secret ppp3:R1(config)#ip domain-name baidu.com 设置域名4:R1(conf ...
- RSDS pdb格式
本描述了“RSDS”或“DS”类型的pdb(程序数据库)文件的格式,这些文件是由Miscrosoft的link.exe从版本7及更高版本发出的. 什么是PDB文件? 如果选择了/DEBUG选项或/DE ...
- [codewars] - int32 to IPv4 二进制十进制 ip地址转换
原题 https://www.codewars.com/kata/int32-to-ipv4/train/java Take the following IPv4 address: 128.32.10 ...
- epoch,iteration与batchsize的区别
神经网络中epoch与iteration是不相等的 batchsize:中文翻译为批大小(批尺寸).在深度学习中,一般采用SGD训练,即每次训练在训练集中取batchsize个样本训练: iterat ...
- TODO list(咕咕咕。。。
1. Segment Tree Beats 2. ZR集训补题(还剩10题) 3. 尽可能将各种数据结构写得熟练...(某神仙:要把平衡树写得跟线段树一样熟练) 4. 树相关数据结构和算法(dsu o ...
- Android入门教程(三)
对Android五大布局的描述,分别是 FrameLayout (框架布局),LinearLayout (线性布局),AbsoluteLayout (绝对布局),RelativeLayout (相对布 ...