zoukankan      html  css  js  c++  java
  • 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.

    解法一:

    public class Solution {
    public int shortestWordDistance(String[] words, String word1, String word2) {
            int p1 = -1, p2 = -1, distance = words.length;
            
            for(int i = 0; i<words.length; i++){
                if(words[i].equals(word1)){
                    p1 = i;
                    if(p2 != -1){
                        distance = (p1!=p2) ? Math.min(distance, Math.abs(p1-p2)): distance;
                    }
                }
                if(words[i].equals(word2)){
                    p2 = i;
                    if(p1 != -1){
                        distance = (p1!=p2) ? Math.min(distance, Math.abs(p1-p2)): distance;
                    }
                }
            }
            return distance;
        }
    }

    解法二:

    hashtable

    public class Solution {
        public int shortestWordDistance(String[] words, String word1, String word2) 
        {
            HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
            for(int i=0;i<words.length;i++)
            {
                String str = words[i];
                if(!map.containsKey(str))
                {
                    map.put(str,new ArrayList<Integer>());
                }
                map.get(str).add(i);
            }
            
            int ret = Integer.MAX_VALUE;
            if(word1.equals(word2))
            {
                List<Integer> list = map.get(word1);
                for(int i=0; i<list.size()-1;i++)
                {
                    int index1 = list.get(i), index2 = list.get(i+1);
                    ret = Math.min(ret,index2-index1);
                }
                
            }
            else
            {
                List<Integer> list1 = map.get(word1);
                List<Integer> list2 = map.get(word2);
                for(int i = 0, j = 0; i < list1.size() && j < list2.size(); ) 
                {
                    int index1 = list1.get(i), index2 = list2.get(j);
                    if(index1 < index2) 
                    {
                        ret = Math.min(ret, index2 - index1);
                        i++;
                    } 
                    else 
                    {
                        ret = Math.min(ret, index1 - index2);
                        j++;
                    }
                }
            }
            return ret;
            
        }
    }
  • 相关阅读:
    强化训练1
    强化训练2
    变量的本质
    抽象层
    安装vs2015
    解决思路
    分析栈的缺点
    (转)使用yuicompressor-maven-plugin压缩js及css文件(二)
    (转)yuicompressor 与 maven结合,打包,压缩js,css (一)
    (转)Properties Editor为你解除通过native2ascii进行Unicode转码的烦恼
  • 原文地址:https://www.cnblogs.com/hygeia/p/5700013.html
Copyright © 2011-2022 走看看