245. Shortest Word Distance III 单词可以重复的最短单词距离

[抄题]:

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;
    }
}

原文地址:https://www.cnblogs.com/immiao0319/p/9382725.html

时间: 2024-11-10 15:20:42

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

[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 words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which receives a list

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

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"]. G

来自百度贴吧的练习题 :求最长单词的长度和最短单词的长度。

In the function ex5 write code that will input a line of text, split it into words, and display these words one per line, and also print the length of the longest and shortest words. You should regard any sequence of consecutive non-space characters

[Swift]LeetCode243.最短单词距离 $ Shortest Word Distance

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"]. G

Leetcode solution 243: Shortest Word Distance

Problem Statement 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", "

编写一个程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应的单词

#include<iostream> #include<string> #include<vector> using namespace std; int main() { string maxStr,Str1,Str2; int maxNum,Num1,Num2; if(cin>>Str1) Num1=1; maxNum=0; while(cin>>Str2) { Num2=1; if(Str2==Str1) Num2=++Num1; if(N

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

搜索(BFS)---最短单词路径

最短单词路径 127. Word Ladder (Medium) Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: As one shortest transformat

统计语句中的最长最短单词

已知 string sentence="We were her pride of 10 she named us: Benjamin, Phoenix, the Pordigal and perspicacious pacific Suzanne.";编写程序,计算sentence中有多少个单次,并指出其中最长和最短的单词,如果有多个,则将它们全部输出 使用find_first_of 和find_first_not_of,寻找到单词的起始位置: 使用vector存放最长和最短单词:通过