[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道用什么数据结构指定index

[一句话思路]:

指定index只需要设成-1然后遍历就行

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

[画图]:

[一刷]:

  1. 头一次见:必须在p1 p2都摆脱初始值-1的时候,才能比较,否则一直为0

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

必须在p1 p2都摆脱初始值-1的时候,才能比较,否则一直为0

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

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

字符串判断相等用的是equals,你没记错

[关键模板化代码]:

[其他解法]:

[Follow Up]:

244. Shortest Word Distance II 多次调用:hashmap多次取

3:可重复:用数学

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

[代码风格] :

class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
//cc
if (words == null || words.length == 0) {
return 0;
} //ini
int p1 = -1, p2 = -1, min = Integer.MAX_VALUE; //for loop, renew p1 p2, compare
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word1)) {
p1 = i;
} if (words[i].equals(word2)) {
p2 = i;
} if (p1 != -1 && p2 != -1) {
min = Math.min(min, Math.abs(p2 - p1));
}
} //return
return min;
}
}

243. Shortest Word Distance 最短的单词index之差的更多相关文章

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

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

  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. [LeetCode] Shortest Word Distance 最短单词距离

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

  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. 243. Shortest Word Distance

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

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

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

  7. [LC] 243. Shortest Word Distance

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

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

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

随机推荐

  1. 为什么要编写轻量级的View Controller??

    1.作为iOS项目中最大的文件,ViewControllers中的代码复用率几乎是最低的2.重量级的View COntroller加大了测试的复杂度.所以关注ViewController的瘦身,把业务 ...

  2. Spring3.x JSR-303

    JSR303介绍 JSR303-Bean Validation描述:This JSR will define a meta-data model and API for JavaBeanTM vali ...

  3. 对实体 "useSSL" 的引用必须以 ';' 分隔符结尾。

    <property name="connection.url">jdbc:mysql://127.0.0.1/cache?useUnicode=true&cha ...

  4. Java 构造器或构造方法

    构造方法的定义 构造方法也叫构造器或者构造函数 构造方法与类名相同,没有返回值,连void都不能写 构造方法可以重载(重载:方法名称相同,参数列表不同) 如果一个类中没有构造方法,那么编译器会为类加上 ...

  5. VS下QT的自定义槽函数修改方法

    通过几天的摸索,基本发现了两个VS的槽函数的修改方法 一种是UI是通过UI 设计师拖出来的,类似VB的方法,通过拖的方法,按钮的代码是系统自动生成的,在UI.h的头文件下,这个时候,实现槽函数有个固定 ...

  6. aop log切面

    @Aspect:描述一个切面类,定义切面类的时候需要打上这个注解 @Component:spring-boot配置类 package com.*.*.tools; import org.aspectj ...

  7. C# 动态加载WebService

    项目中需要用到WebService的方式来进行两个服务之间的方法调用,之前都是在项目中添加服务引用的方式来实现,但是这种方式有一个弊端,就是如果提供WebService服务的一方的IP.端口一旦变更, ...

  8. further occurrences of HTTP header parsing errors will be logged at DEBUG level.

    1.   获取参数Json的值为null String json=request.getParameter("Json"); 首先检查是否有下面的东东, 信息: Error par ...

  9. ORACLE常识

    1. ORACLE中查看表中的外键来源于哪些表 select cl.table_name from user_cons_columns cl left join user_constraints c ...

  10. cp -f 还是提示是否覆盖

    新做了服务器,cp覆盖时,无论加什么参数-f之类的还是提示是否覆盖,这在大量cp覆盖操作的时候是不能忍受的. 把a目录下的文件复制到b目录 以下是代码片段: cp –r a/* b 执行上面的命令时, ...