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)

    重建二叉树 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历 ...

  2. R随机森林交叉验证 + 进度条

    library(data.table) library(randomForest) data <- iris str(data) #交叉验证,使用rf预测sepal.length k = 5 d ...

  3. 第一章 Html+Css使用总结(下)

    1 开场 <!DOCTYPE html> <html lang="en"> <head> <!-- 对于中文网页需要使用 <meta ...

  4. Maven项目中在properties 中使用 ${} 来引用pom文件中的属性

    比如在pom文件中定义了属性如下: <jdbc.host.global>127.0.0.1</jdbc.host.global> <jdbc.databasename.g ...

  5. vscode编辑Markdown时的贴图工具

    参看 https://www.jianshu.com/p/74b960efb697 说明: 1. 文件-->首选项-->设置-->填入paste,设置Path值为 ${current ...

  6. 文件处理,三元操作符,seek()函数,迭代函数和列表解析,reduce函数

    1.文件读取方类型 r,r+,w,x,a, r,读文件 w,写文件,文件内容全部删除,并将新内容从第一行开始赋值 x,写文件,只有文件不存在,可写,文件存在,报错 a,在文件莫问追加信息 r+,w+, ...

  7. Flask--(一对多demo)作者书籍模型

    一对多模型的增加和删除 后端实现: from flask import Flask from flask import flash from flask import redirect from fl ...

  8. Centos6搭建sftp服务器

    一.创建sftp相关用户和目录 [root@mail samba]# useradd -s /sbin/nologin -M sftp_user #创建一个系统用户,按需设置登录的shell和家目录 ...

  9. 关于报错:There is already 'xxxController' bean method的解决方法

    报这个错的原因是因为你controller里的@RequestMapping中的路径有重复! 如:

  10. 2-zookeeper、ha

    1.zookeeper ①背景: Zookeeper 最早起源于雅虎研究院的一个研究小组.在当时,研究人员发现,在雅虎内部很多大型系统基本都需要依赖一个类似的系统来进行分布式协调, 但是这些系统往往都 ...