Medium 244题 Shortest Word Distance II Medium 245题 Shortest Word Distance III

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.

Solution:

我的解法并不好~

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值