• 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;
            
        }
    }
  • 相关阅读:
    POJ 1426 Find The Multiple(数论——中国同余定理)
    POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)
    POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)
    POJ 3278 Catch That Cow(模板——BFS)
    HDU 1071 The area
    HDU 1213 How Many Tables(模板——并查集)
    POJ 1611 The Suspects
    light oj 1214 Large Division
    POJ 1258 Agri-Net(Prim算法求解MST)
    POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
  • 原文地址:https://www.cnblogs.com/hygeia/p/5700013.html
Copyright © 2020-2023  润新知