本文主要是介绍LeetCode 题解(267) : 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.
HashMap。
C++版:
class Solution {
public:int shortestDistance(vector<string>& words, string word1, string word2) {unordered_map<string, vector<int>> d;for(int i = 0; i < words.size(); i++) {if(d.find(words[i]) == d.end()) {vector<int> t(1, i);
这篇关于LeetCode 题解(267) : Shortest Word Distance的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!