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

     

  • 相关阅读:
    在 Android 4.1上,分析 input -- android framework 部分 2
    Linux内核spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析
    module_init 和 late_initcall 区别
    在 Android 4.4.4 上,分析 input -- android framework 部分
    Android 输入系统 与 按键
    INIT_WORK和INIT_DELAYED_WORK详解
    Android 中多点触摸协议
    android 电容屏(四):驱动调试之驱动程序分析篇 -- FocalTech
    android 电容屏(三):驱动调试之驱动程序分析篇
    android 电容屏(二):驱动调试之基本概念篇
  • 原文地址:https://www.cnblogs.com/littletail/p/5200975.html
Copyright © 2011-2022 走看看