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"].

  1. Input: word1 = coding, word2 = practice
  2. Output: 3
  1. Input: word1 = "makes", word2 = "coding"
  2. Output: 1

Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

利用一个字典记录所有相同的字母的位置,然后就是两个有序数组比较最近的元素。

我设想把一个数组插入另一个数组,然后比较。C++ lower_bound大法。

  1. class WordDistance {
  2. private:
  3. unordered_map<string,vector<int>> map;
  4. public:
  5. WordDistance(vector<string> words) {
  6. for(int i=; i<words.size(); i++) map[words[i]].push_back(i);
  7. }
  8.  
  9. int shortest(string word1, string word2) {
  10. vector<int> l1 = map[word1];
  11. vector<int> l2 = map[word2];
  12. int idx = ;
  13. int ret = INT_MAX;
  14. for(int i=; i<l2.size(); i++){
  15. idx = lower_bound(l1.begin(), l1.end(),l2[i]) - l1.begin();
  16. if(idx == l1.size()){
  17. ret = min(ret, l2[i] - l1.back());
  18. }else if(idx == ){
  19. ret = min(ret, abs(l2[i] - l1.front()));
  20. }else{
  21. ret = min(ret, abs(l2[i] - l1[idx]));
  22. ret = min(ret, abs(l2[i] - l1[idx-]));
  23. }
  24. if(ret == ) return ;
  25. }
  26. return ret;
  27. }
  28. };

下面是网上的简单做法,思路差不多,找最小的时候遍历,结果runtime24ms...

  1. class WordDistance {
  2. public:
  3. unordered_map<string, vector<int> > map;
  4. WordDistance(vector<string> words) {
  5. for(int i = ; i < words.size(); i++){
  6. map[words[i]].push_back(i);
  7. }
  8. }
  9. int shortest(string word1, string word2) {
  10. vector<int> v1, v2;
  11. v1 = map[word1];
  12. v2 = map[word2];
  13. int diff = INT_MAX;
  14. for(int i = ; i < v1.size(); i++)
  15. for(int j = ; j < v2.size(); j++)
  16. if(abs(v1[i]-v2[j]) < diff)
  17. diff = abs(v1[i]-v2[j]);
  18. return diff;
  19. }
  20. };

LC 244. Shortest Word Distance II 【lock, Medium】的更多相关文章

  1. LC 245. Shortest Word Distance III 【lock, medium】

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  2. [LC] 244. Shortest Word Distance II

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  3. 244. Shortest Word Distance II

    题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...

  4. [LeetCode#244] Shortest Word Distance II

    Problem: This is a follow up of Shortest Word Distance. The only difference is now you are given the ...

  5. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  6. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  7. 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...

  8. 244. Shortest Word Distance II 实现数组中的最短距离单词

    [抄题]: Design a class which receives a list of words in the constructor, and implements a method that ...

  9. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

随机推荐

  1. 第十三章、元类(metaclass)

    目录 第十三章.元类(metaclass) 一.什么是元类 二.为什么用元类 第十三章.元类(metaclass) 一.什么是元类 在python中一切皆对象,那么我们用class关键字定义的类本身也 ...

  2. java_day05_类和对象

    chap05目标:类和对象---------------------------------------------- 1.OOP特征概述 Java的编程语言是面向对象的,采用这种语言进行编程称为面向 ...

  3. 利用openssl完成自签发证书步骤--精华版

    #CentOS 7 CA目录 cd /etc/pki/CA #建立 demoCA 目录结构mkdir -p ./demoCA/{private,newcerts}touch ./demoCA/inde ...

  4. thinkphp5 二维码生成

    1.下载二维码插件Phpqrcode,地址 https://sourceforge.net/projects/phpqrcode/files/,把下载的文件夹放到\thinkphp\vendor下 2 ...

  5. PAM安全认证模块

  6. 重温JSP学习笔记

    <% double d1 = 3.5; double d2 = 4.4; pageContext.setAttribute("d1", d1); pageContext.se ...

  7. Java 转发和重定向的区别

    转发是服务器行为,重定向是客户端行为 1.转发在服务器端完成的;重定向是在客户端完成的 2.转发的速度快;重定向速度慢 3.转发的是同一次请求;重定向是两次不同请求 4.转发不会执行转发后的代码;重定 ...

  8. mysql5.7 环境准备

    原文源自:https://www.cnblogs.com/activiti/p/7810166.html 操作系统为centos7 .修改 /etc/my.cnf,在 [mysqld] 小节下添加一行 ...

  9. SpringBoot AOP介绍

    说起spring,我们知道其最核心的两个功能就是AOP(面向切面)和IOC(控制反转),这边文章来总结一下SpringBoot如何整合使用AOP. 一.示例应用场景:对所有的web请求做切面来记录日志 ...

  10. Malloc Maleficarum复盘

    1.hos复盘 hos即伪造堆块,free栈上地址,然后下一个malloc去分配一个fastbin(栈上),包含返回地址. 代码来源 他这个我直接复现有问题,咨询了joker师傅,应该是gcc版本问题 ...