博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Locked] Shortest Word Distance I & II & III
阅读量:4923 次
发布时间:2019-06-11

本文共 3754 字,大约阅读时间需要 12 分钟。

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
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
hash;public: Solution(vector
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
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;}

 

 

转载于:https://www.cnblogs.com/littletail/p/5200975.html

你可能感兴趣的文章
《浙大版-数据结构(第二版)》习题2.5 两个有序链表序列的合并(15 分)<有疑问?变化之后 L1 L2没办法NULL >...
查看>>
Ubuntu18.04 安装Chrome浏览器
查看>>
Linux命令总结_文件的输入与 输出
查看>>
[ZJOI2010]数字计数
查看>>
BW顾问必需要清楚的:时间相关数据建模场景需求分析
查看>>
JSON.parse()与JSON.stringify()的区别
查看>>
idea设置
查看>>
java几种常用的算法
查看>>
关于图书管理系统简单的定位
查看>>
MSIL指令大全
查看>>
Java基础_面向对象之接口
查看>>
微信小程序开发中的二三事之网易云信IMSDK DEMO
查看>>
RXSwift 入坑记
查看>>
消息模式Toast.makeText用法
查看>>
IOS学习路线图
查看>>
UWP Tiles
查看>>
记录JavaScript的util.js类库
查看>>
2017.10.16
查看>>
JavaWeb学习笔记——jsp基础语法
查看>>
从url中拿参数和传递参数。正则获取参数
查看>>