zoukankan      html  css  js  c++  java
  • [Locked] Shortest Word Distance I & II & III

    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.

    代码:

    int swd(vector<string> words, string word1, string word2) {
        int last = 0, pos1 = -1, pos2 = -1, mindist = INT_MAX;
        for(int i = 0; i < words.size(); i++) {
            if(words[i] == word1) {
                pos1 = i;
                if(last == 2)
                    mindist = min(mindist, i - pos2);
                last = 1;
            }
            if(words[i] == word2) {
                pos2 = i;
                if(last == 1)
                    mindist = min(mindist, i - pos1);
                last = 2;
            }
        }
        return mindist;
    }
    Shortest Word Distance 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 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.

    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.

    代码:

    class Solution {
    private:
        unordered_multimap<string, int> hash;
    public:
        Solution(vector<string> words) {
            for(int i = 0; i < words.size(); i++)
                hash.insert(make_pair(words[i], i));
        }
        int swd(string word1, string word2) {
            auto range1 = hash.equal_range(word1), range2 = hash.equal_range(word2);
            auto pos1 = range1.first, pos2 = range2.first;
            int mindist = INT_MAX;
            while(pos1 != range1.second && pos2 != range2.second){
                mindist = min(mindist, abs(pos1->second - pos2->second));
                pos1->second > pos2->second ? pos1++ : pos2++;
            }
            return mindist;
        }
    };

     

    Shortest Word Distance III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.

    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.

    For example,
    Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

    Given word1 = “makes”word2 = “coding”, return 1.
    Given word1 = "makes"word2 = "makes", return 3.

    Note:
    You may assume word1 and word2 are both in the list.

    代码:

    int swd(vector<string> words, string word1, string word2) {
        int last = 0, pos1 = -1, pos2 = -1, mindist = INT_MAX;
        if(word1 == word2) {
            for(int i = 0; i < words.size(); i++) {
                if(words[i] == word1) {
                    if(pos1 != -1)
                        mindist = min(mindist, i - pos1);
                    pos1 = i;
                }
            }
            return mindist;
        }
        for(int i = 0; i < words.size(); i++) {
            if(words[i] == word1) {
                pos1 = i;
                if(last == 2)
                    mindist = min(mindist, i - pos2);
                last = 1;
            }
            if(words[i] == word2) {
                pos2 = i;
                if(last == 1)
                    mindist = min(mindist, i - pos1);
                last = 2;
            }
        }
        return mindist;
    }

     

  • 相关阅读:
    CSS3中的结构伪元素选择器和伪类选择器的使用
    ul li居中
    vue 点击按钮,边框变色
    vue 实现简单的遮罩层
    Vue 控制鼠标移入移出事件文字的隐藏和显示
    vue 文字点击变色
    vue v-for的用法
    使用CSS修改radio样式
    vue 点击显示隐藏,鼠标移动上去显示隐藏
    sql序列(3)基本语法
  • 原文地址:https://www.cnblogs.com/littletail/p/5200975.html
Copyright © 2011-2022 走看看