Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

Solution1:

O(N^2)

class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int res = words.length;
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
for (int j = 0; j < words.length; j++) {
if (words[j].equals(word2)) {
res = Math.min(res, Math.abs(i - j));
}
}
}
}
return res;
}
}

Solution2:

O(N)

class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int res = words.length;
int s1 = -1, s2 = -1;
for (int i = 0; i < words.length; i++) {
String cur = words[i];
if (cur.equals(word1)) {
s1 = i;
} else if (cur.equals(word2)) {
s2 = i;
}
if (s1 != -1 && s2 != -1) {
res = Math.min(res, Math.abs(s1 - s2));
}
}
return res;
}
}

[LC] 243. Shortest Word Distance的更多相关文章

  1. 243. Shortest Word Distance 最短的单词index之差

    [抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...

  2. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  3. 243. Shortest Word Distance

    题目: Given a list of words and two words word1 and word2, return the shortest distance between these ...

  4. LeetCode 243. Shortest Word Distance (最短单词距离)$

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  5. [leetcode]243. Shortest Word Distance最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  6. 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 ...

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

  8. [LC] 244. Shortest Word Distance II

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  9. 【LeetCode】243. Shortest Word Distance 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

随机推荐

  1. 关于 tf.image.crop_and_resize的使用

    https://blog.csdn.net/m0_38024332/article/details/81779544 关于 tf.image.crop_and_resize 的使用  最近在学习fas ...

  2. u-boot中filesize环境变量【转载】

    转载地址:https://blog.csdn.net/fzs333/article/details/48518559 U-Boot中的环境命令可以使用$(filesize)来确定刚下载(传输)得到的文 ...

  3. ASP.NET ZERO 学习 导航菜单

    定义PageNames和PermissionName PageNames : Web/App_Start/Navigation/PageNames.cs public const string Das ...

  4. Android自定义的弹窗

    package com.microduino.qoobot.view; import android.app.Activity; import android.app.Dialog; import a ...

  5. Go-map-字符串-指针-结构体

    Maps 什么是 map ? 类似Python中的字典数据类型,以k:v键值对的形式. map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值. 如何创建 ma ...

  6. latex学习笔记----数学公式

    https://www.jianshu.com/p/d7c4cf8dc62d 1.数学公式在  \(  和  \)之间,$和$之间,或者\begin{math}和\end{math}之间 2.对于较大 ...

  7. Django专题-ugettext_lazy

    标准翻译 使用函数 ugettext() 来指定一个翻译字符串. 作为惯例,使用短别名 _ 来引入这个函数以节省键入时间. 在下面这个例子中,文本 "Welcome to my site&q ...

  8. GCC与gcc,g++区别

    看的Linux公社的一篇文章,觉得不错,内容复制过来了. 其实在这之前,我一直以为gcc和g++是一个东西,只是有两个不同的名字而已,今天在linux下编译一个c代码时出现了错误才找了一下gcc和g+ ...

  9. requset请求处理与BeanUtils封装

    HTTP: 概念:Hyper Text Transfer Protocol 超文本传输协议 传输协议:定义了,客户端和服务器端通信时,发送数据的格式 特点: 基于TCP/IP的高级协议 默认端口号:8 ...

  10. 使用sshfs

    寻找winscp在mac下的代替品, 没有找到. 但找到一个更好用的方法: 通过sshfs挂载文件目录,当作普通文件夹使用. 使用方法 sshfs -o defer_permissions -o no ...