国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 244. Shortest Word Distance II

Nekron / 1147人閱讀

Problem

Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list. Your method will be called repeatedly many times with different parameters.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

Solution
class WordDistance {
    private Map> map;
    public WordDistance(String[] words) {
        map = new HashMap>();
        for(int i = 0; i < words.length; i++) {
            String word = words[i];
            if (!map.containsKey(word)) map.put(word, new ArrayList<>());
            map.get(word).add(i);
        }
    }

    public int shortest(String word1, String word2) {
        List list1 = map.get(word1);
        List list2 = map.get(word2);
        int min = Integer.MAX_VALUE;
        int i = 0, j = 0;
        while (i < list1.size() && j < list2.size()) {
            int index1 = list1.get(i), index2 = list2.get(j);
            if (index1 < index2) {
                min = Math.min(min, index2 - index1);
                i++;
            } else {
                min = Math.min(min, index1 - index2);
                j++;
            }
        }
        return min;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/72707.html

相關文章

  • [LeetCode] 126. Word Ladder II

    摘要:存放過程中的所有集合為所有的結尾,則順序存放這個結尾對應的中的所有存放同一個循環的新加入的,在下一個循環再依次對其中元素進行進一步的把首個字符串放入新,再將放入,并將鍵值對放入,進行初始化 Problem Given two words (start and end), and a dictionary, find all shortest transformation sequenc...

    wayneli 評論0 收藏0
  • [Leetcode] Shortest Word Distance 最短單詞間距

    摘要:代碼第一次寫入就先不比較第一次寫入就先不比較哈希表法復雜度時間空間思路因為會多次調用,我們不能每次調用的時候再把這兩個單詞的下標找出來。我們可以用一個哈希表,在傳入字符串數組時,就把每個單詞的下標找出存入表中。 Shortest Word Distance Given a list of words and two words word1 and word2, return the ...

    jsliang 評論0 收藏0
  • [LeetCode] 245. Shortest Word Distance III

    Problem 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 i...

    csRyan 評論0 收藏0
  • [LeetCode] 243. Shortest Word Distance

    Problem 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]. In...

    高勝山 評論0 收藏0
  • 490. The Maze && 505. The Maze II

    摘要:題目鏈接和上一題不一樣的是這道題要求最短的路徑,普通的遍歷和都是可以做的,但是求最短路徑的話還是用。這里相當于每個點有至多條相連,每條的就是到墻之前的長度。 490. The Maze 題目鏈接:https://leetcode.com/problems... 又是圖的遍歷問題,就是簡單的遍歷,所以dfs和bfs都可以做,復雜度也是一樣的。這道題要求球不能停下來,即使碰到destina...

    BoYang 評論0 收藏0

發表評論

0條評論

Nekron

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<