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

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.



给定单词列表以及单词1和单词2,返回列表中这两个单词之间的最短距离。

例如,

假设words=[“practice”、“makes”、“perfect”、“coding”、“makes”]。

给定word1=“coding”,word2=“practice”,返回3。

给定word1=“makes”,word2=“coding”,返回1。

注:

您可以假定word1不等于word2,word1和word2都在列表中。


 1 class Solution {
 2     func shortestDistance(_ words: [String],_ word1:String,_ word2:String) -> Int {
 3         var idx:Int = -1
 4         var res:Int = Int.max
 5         for i in 0..<words.count
 6         {
 7             if words[i] == word1 || words[i] == word2
 8             {
 9                 if idx != -1 && words[idx] != words[i]
10                 {
11                     res = min(res, i - idx)
12                 }
13                 idx = i
14             }
15         }
16         return res
17     }
18 }

原文地址:https://www.cnblogs.com/strengthen/p/10213804.html

时间: 2024-08-25 19:14:45

[Swift]LeetCode243.最短单词距离 $ Shortest Word Distance的相关文章

[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

[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

[Swift]LeetCode748. 最短完整词 | Shortest Completing Word

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate Here, for letters we ignore case. For example, "P" on the licensePl

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&q

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 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存放最长和最短单词:通过

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

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

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

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

AC日记——最长最短单词 openjudge 1.7 25

25:最长最短单词 总时间限制:  1000ms 内存限制:  65536kB 描述 输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母.空格和逗号.单词由至少一个连续的字母构成,空格和逗号都是单词间的间隔. 试输出第1个最长的单词和第1个最短单词. 输入 一行句子. 输出 两行输出:第1行,第一个最长的单词.第2行,第一个最短的单词. 样例输入 I am studying Programming language C in Peking University 样例输出 P