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;
            
        }
    }
  • 相关阅读:
    Quartz.Net系列(二):介绍、简单使用、对比Windows计划任务
    Quartz.Net系列(一):Windows任务计划程序
    Linux下swap到底有没有必要使用
    Linux服务器有大量的TIME_WAIT状态
    HTTP请求头中的X-Forwarded-For介绍
    Keepalived实现服务高可用
    Gitlab常规操作
    Git 常用命令
    HTTP常见状态码
    《Docker从入门到跑路》之多阶段构建
  • 原文地址:https://www.cnblogs.com/hygeia/p/5700013.html
Copyright © 2011-2022 走看看