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

     

  • 相关阅读:
    pip install uwsgi 报错 AttributeError: module 'os' has no attribute 'uname'
    npm安装vue
    Node.js安装及环境配置之Windows篇
    Centos7 安装nodejs
    Centos7 Jenkins 插件下载速度慢、安装失败
    Centos7 使用docker 安装redis
    Centos7 安装jdk
    supervisor配置文件详解
    MySQL5.7 group by新特性,报错1055
    配置python虚拟环境Virtualenv及pyenv
  • 原文地址:https://www.cnblogs.com/littletail/p/5200975.html
Copyright © 2011-2022 走看看