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;
            
        }
    }
  • 相关阅读:
    解决WampServer窗口界面乱码问题
    PHP 中空字符串介绍0、null、empty和false之间的关系
    位(bit)、字节(byte)、字
    SQL GROUP BY 和 ORDER BY 区别
    爬虫安装相关软件
    nginx自动切割日志脚本
    tengine日志切割-配置分钟级别日志自动切割
    flutter进行自动编译操作步骤
    03-字符编码-读写模式-课堂笔记
    04-函数-课堂笔记
  • 原文地址:https://www.cnblogs.com/hygeia/p/5700013.html
Copyright © 2011-2022 走看看