[抄题]:

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

[暴力解法]:

把word1所有的index存一个数组,把word2所有的index存一个数组,再i*j全部扫一遍

时间分析:n^2

空间分析:

[优化后]:

每次走一个index都随时更新一下i1 i2

时间分析:n

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

虽然不麻烦,但是Integer.MAX_VALUE 必须要存成long型,然后转回来就行了。不然会报错。

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

p1 p2相等的时候,暂存一下之前的p2

if (word1.equals(word2))
//store in p1 temporarily
p1 = p2;
p2 = i;

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
//ini: p1, p2 into the long type
long result = Integer.MAX_VALUE;
long p1 = Integer.MAX_VALUE;
long p2 = -Integer.MAX_VALUE; //for loop: for each words[i], renew the answer
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) p1 = i;
if (words[i].equals(word2))
{
if (word1.equals(word2))
//store in p1 temporarily
p1 = p2;
p2 = i;
}
//renew the answer
result = Math.min(result, Math.abs(p2 - p1));
} //return
return (int)result;
}
}

245. Shortest Word Distance III 单词可以重复的最短单词距离的更多相关文章

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

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

  3. 245. Shortest Word Distance III

    题目: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the sam ...

  4. 【LeetCode】245. Shortest Word Distance III 解题报告 (C++)

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

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

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

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

  8. LeetCode Shortest Word Distance III

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-iii/ 题目: This is a follow up of Shortes ...

  9. 244. Shortest Word Distance II 实现数组中的最短距离单词

    [抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...

随机推荐

  1. lua tasklib 之lumen 分析

    sched.sleep分析 sleep会填充M.running_task.waitds数据表示当前task需要等待,最后yield出去到主线程 M.sleep = function (timeout) ...

  2. 1.1.20 Word不能保存问题

    1.进入如下目录:C:\用户(user)\Administrator\AppData\Roaming\Microsoft\Templates 2.找到Normal和NormalOld的两个文件,删除. ...

  3. 分页传参数的两种形式,url正则 ?id=1

    目的: 打开http://127.0.0.1:8000/home    点击查看详情转到 http://127.0.0.1:8000/detail-1-1.html实现查看具体信息 一,利用url路由 ...

  4. 将string转为同名类名,方法名。(c#反射)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace stri ...

  5. pyqt5在xp下的配置

    qt支持库得下载,pip的不行, designer在这个里面 https://sourceforge.net/projects/pyqt/files/PyQt5/PyQt-5.4.1/ python只 ...

  6. 廖雪峰Java6 IO编程-3Reader和Writer-2Writer

    1.java.io.Writer和java.io.OutputStream的区别 OutputStream Writer 字节流,以byte为单位 字符流,以char为单位 写入字节(0-255):v ...

  7. CSS之display

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Ajax请求的几个小练习

    Ajax请求的几个小练习 准备工作 路由中做了分发: re_path('^app01/',include('app01.url')) app01中url.py文件的内容: from django.ur ...

  9. (转)SQLServer查询数据库各种历史记录

    原文地址https://www.cnblogs.com/seusoftware/p/4826958.html 在SQL Server数据库中,从登陆开始,然后做了什么操作,以及数据库里发生了什么,大多 ...

  10. (转发)storm 入门原理介绍

    1.hadoop有master与slave,Storm与之对应的节点是什么? 2.Storm控制节点上面运行一个后台程序被称之为什么?3.Supervisor的作用是什么?4.Topology与Wor ...