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

题意:

给定一列单词和俩单词,求这俩单词在一列中的最短距离。

Solution1: Two Pointers

扫words,若当前指针i 对应元素等于word1, 则将 i 赋给 a

若当前指针i 对应元素等于word2, 则将 i 赋给 b

同时更新result

code

 class Solution {
public int shortestDistance(String[] words, String word1, String word2) {
int result = Integer.MAX_VALUE;
int a = -1; // word1Idx
int b = -1; // word2Idx
for(int i = 0; i< words.length; i++){
if(words[i].equals(word1)){
a = i;
}else if(words[i].equals(word2)){
b = i;
}
if(a!=-1 && b !=-1){
result = Math.min(result, Math.abs(a-b));
}
}
return result;
}
}

[leetcode]243. Shortest Word Distance最短单词距离的更多相关文章

  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] 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 最短的单词index之差

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

  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]244. Shortest Word Distance II最短单词距离(允许连环call)

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

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

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

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

  9. [LeetCode#244] Shortest Word Distance II

    Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...

随机推荐

  1. java_oop_方法2

    基本和引用数据类型    存储方式    数据类型总结 jvm运行时数据区域        方法区        虚拟机栈!        本地方法栈        堆!        程序计数器 虚 ...

  2. sql server紧急状态下登录脚本

    --打开xp_cmdshell功能  EXEC [sys].[sp_configure] @configname = 'xp_cmdshell', -- varchar(35)    @configv ...

  3. Word中一条删除不掉的单或双横线的解决办法

    Word中一条删除不掉的单或双横线 有时你或许会遇到这样一种情况:在word中,有一条单或双横线怎么都删除不了,并且具有这样的特点: 在上面输入文字,横线会自动下调一行,如果文章过页,每页的尾部会有一 ...

  4. public class PageRender implements ResponseRender

    package cn.ubibi.jettyboot.demotest.controller.render; import cn.ubibi.jettyboot.framework.commons.S ...

  5. HTML/CSS基础知识(一)

    Q:浏览器页面有哪三层构成,分别是什么,作用是什么? A:由三部分构成: 网页结构层(Structural Layer)——由(X)HTML等标记语言负责创建,实现页面结构. 网页表示层(Presen ...

  6. 【亲测】关于HTTP协议~

    如果有一点点基本的开发者工具基础知识,我们知道:Elements是用来查看网页结构的,也就是可以看到整体的HTML语言:Console是控制台,Network是请求想相应状态. 1)一个Name就是一 ...

  7. Google Colab Notebook 的外部文件引用配置

    Google Colab Notebook 的外部文件引用配置 Reference: How to upload the file and read Google Colab 先装工具:google- ...

  8. script 写在body和head的区别(WEUI中使用swiper删除)

    区别简述: 在HTML body部分中的JavaScripts会在页面加载的时候被执行.在HTML head部分中的JavaScripts会在被调用的时候才执行.   JavaScript应放在哪里 ...

  9. [leetcode]335. Self Crossing

    You are given an array x of n positive numbers. You start at point (,) and moves x[] metres to the n ...

  10. shell文件测试,菜单表示思想

    ---恢复内容开始--- 文件测试表达式    -f 文件存在且为普通文件     -d 文件存在且为目录文件    -s 文件大小不为0则真    -e 文件存在则真        -r 文件存且可 ...