Medium 244题 Shortest Word Distance II
Question:
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 calledrepeatedly 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.
Solution:
public class WordDistance {
public String[] words;
public HashMap<String, List<Integer>> map=new HashMap<String, List<Integer>>();
public WordDistance(String[] words) {
for(int i=0;i<=words.length-1;i++)
{
if(map.containsKey(words[i]))
{
map.get(words[i]).add(i);
}
else
{
List<Integer> tmp = new ArrayList<Integer>();
tmp.add(i);
map.put(words[i],tmp);
}
}
}
public int shortest(String word1, String word2) {
List<Integer> w1=map.get(word1);
List<Integer> w2=map.get(word2);
int min=Math.abs(w1.get(0)-w2.get(0));
for(int i=0,j=0;i<=w1.size()-1&&j<=w2.size()-1;)
{
int tmp=Math.abs(w1.get(i)-w2.get(j));
min=tmp<min?tmp:min;
if(w1.get(i)<w2.get(j))
i++;
else
j++;
}
return min;
}
}
// Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance = new WordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");
Medium 245题 Shortest Word Distance III
Question:
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.
我的解法并不好~
public class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
HashMap<String, Integer> map=new HashMap<String, Integer>();
int min=Integer.MAX_VALUE;
for(int i=0;i<=words.length-1;i++)
{
if(word1.equals(word2))
{
if(words[i].equals(word1)&&map.get(words[i])!=null)
{
min=min<(i-map.get(words[i]))?min:(i-map.get(words[i]));
}
map.put(words[i],i);
}
else
{
if(words[i].equals(word1)||words[i].equals(word2))
{
map.put(words[i],i);
if(map.containsKey(word1)&&map.containsKey(word2))
{
int tmp=Math.abs(map.get(word1)-map.get(word2));
min=min<tmp?min:tmp;
}
}
}
}
return min;
}
}
必须用long!!!!!!!!!
public class Solution {
public int shortestWordDistance(String[] words, String word1, String word2) {
long min=Integer.MAX_VALUE;
long i1=min;
long i2=-min;
boolean same=word1.equals(word2);
for(int i=0;i<=words.length-1;i++)
{
if(words[i].equals(word1))
{
if(same)
{
i1=i2;
i2=i; //we we get here the second time, i1 will get the old position and i2 will get the new position
}
else
i1=i;
}
else if(words[i].equals(word2))
{
i2=i;
}
min=Math.min(min, Math.abs(i1-i2));
}
return (int)min;
}
}