LC 245. Shortest Word Distance III 【lock, medium】
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.
Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Input: word1 =“makes”
, word2 =“coding”
Output: 1
Input: word1 ="makes"
, word2 ="makes"
Output: 3
Note:
You may assume word1 and word2 are both in the list.
这题虽然可以沿用上一题的思路,但是可以有更简单的两个index的做法,最优解O(n),runtime 8ms。
class Solution {
public:
int shortestWordDistance(vector<string>& words, string word1, string word2) {
int index = -;
int minval = words.size();
for(int i=; i<words.size(); i++){
if((index != -) && (words[i] == word1 || words[i] == word2)){
if((words[index] == word1 && words[i] == word2) ||
(words[index] == word2 && words[i] == word1)){
minval = min(minval, i - index);
}
}
if(words[i] == word1 || words[i] == word2)
index = i;
}
return minval;
}
};
LC 245. Shortest Word Distance III 【lock, medium】的更多相关文章
- LC 244. Shortest Word Distance II 【lock, Medium】
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [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 ...
- 245. Shortest Word Distance III
题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...
- 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】245. Shortest Word Distance III 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
- 245. Shortest Word Distance III 单词可以重复的最短单词距离
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- [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 III
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...
- [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 ...
随机推荐
- SpringBoot-整合Swagger2
swagger2是一个用于生成.并能直接调用的可是话restful风格的服务 下面贴出springboot整合swagger2代码 一.maven依赖 这里使用的spring-boot版本是2.1.1 ...
- odoo字段属性
以下为可用的非关联字段类型以及其对应的位置参数: Char(string)是一个单行文本,唯一位置参数是string字段标签. Text(string)是一个多行文本,唯一位置参数是string字段标 ...
- 陌上花开 HYSBZ - 3262 (CDQ分治)
陌上花开 HYSBZ - 3262 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另 ...
- 使用幕布时,在Session过期后,弹出框加载出登陆的HTML的问题
思路:在登陆页面判断当前加载的Url是否时login/index ,如果不是跳转到登陆页 //设置或获取对象指定的文件名或路径. var Url = window.location.pathname; ...
- Struct2的简单的CRUD配置和使用
1. 首先是Struct2使用的jar包,可以在官网下载https://struts.apache.org/ ,其中包只要下面这些就够用了. 或者点击下面链接下载 链接:https://pan.b ...
- JAVA8之日期操作详解
package org.date; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; im ...
- Python3之Requests模块详解
# 导入 Request模块 # 若本机无自带Request模块,可自行下载或者使用pip进行安装 # python版本Python3 import requests import json #### ...
- Remote API(RAPI)之 系统信息
RAPI提供了一些取系统信息的函数 CeGetSystemInfo:返回当前系统信息 CeGetSystemMetrics:获取Windows元素的尺寸和系统设置 CeGetVersionEx:获取当 ...
- java输出乱码专题
https://blog.csdn.net/liaoYu1887/article/details/82714727(其他) @Controller public class ItemCatContro ...
- 「SNOI2017」礼物
题目链接:Click here Solution: 设\(f(x)\)代表第\(x\)个人送的礼物的数量,\(s(x)\)代表\(f(x)\)的前缀和,即: \[ f(x)=s(x-1)+x^k\\ ...