博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
243. Shortest Word Distance
阅读量:5326 次
发布时间:2019-06-14

本文共 2756 字,大约阅读时间需要 9 分钟。

题目:

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.

Note:

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

链接: 

题解:

找两个单词在数组中的距离。这个题比较没有意义...就跟找两个字母的距离一样。乍看题目还以为是word ladder。设置两个变量,分别代表每个单词的lastIndex,之后就是判断和计算了。也可以用一个变量来记录。

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {    public int shortestDistance(String[] words, String word1, String word2) {        if(words == null || words.length == 0 || word1 == null || word2 == null)            return Integer.MAX_VALUE;        int minDistance = Integer.MAX_VALUE;        int word1Index = -1, word2Index = -2; for(int i = 0; i < words.length; i++) { if(words[i].equals(word1)) word1Index = i; if(words[i].equals(word2)) word2Index = i; if(word1Index >= 0 && word2Index >= 0) minDistance = Math.min(minDistance, Math.abs(word1Index - word2Index)); } return minDistance; } }

 

 

二刷:

使用两个变量存下 word1和word2在数组中的位置,然后进行计算。

Java:

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {    public int shortestDistance(String[] words, String word1, String word2) {        if (words == null || words.length < 2 || word1 == null || word2 == null) {            return Integer.MAX_VALUE;        }        int word1Pos = -1, word2Pos = -1, minDistance = Integer.MAX_VALUE;        for (int i = 0; i < words.length; i++) {            String curWord = words[i];            if (curWord.equals(word1) || curWord.equals(word2)) {                if (curWord.equals(word1)) {                    word1Pos = i;                }                if (curWord.equals(word2)) {                    word2Pos = i;                }                if (word1Pos >= 0 && word2Pos >= 0) {                    minDistance = Math.min(minDistance, Math.abs(word1Pos - word2Pos));                        }            }        }        return minDistance;    }}

 

三刷:

Java:

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

 

 

Reference:

 

转载于:https://www.cnblogs.com/yrbbest/p/5006422.html

你可能感兴趣的文章
String类中的equals方法总结(转载)
查看>>
标识符
查看>>
内存地址对齐
查看>>
创新课程管理系统数据库设计心得
查看>>
Could not resolve view with name '***' in servlet with name 'dispatcher'
查看>>
[转载] redis 的两种持久化方式及原理
查看>>
MyBaits学习
查看>>
管道,数据共享,进程池
查看>>
[Cypress] Stub a Post Request for Successful Form Submission with Cypress
查看>>
SDUTOJ3754_黑白棋(纯模拟)
查看>>
php中的isset和empty的用法区别
查看>>
把word文档中的所有图片导出
查看>>
ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
查看>>
正则表达式
查看>>
arcgis api 4.x for js 结合 Echarts4 实现散点图效果(附源码下载)
查看>>
YTU 2625: B 构造函数和析构函数
查看>>
apache自带压力测试工具ab的使用及解析
查看>>
加固linux
查看>>
WPF中Image显示本地图片
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>