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;
            
        }
    }
  • 相关阅读:
    React 源码剖析系列 - 生命周期的管理艺术
    大数据浪潮下的前端工程师
    win7 秘钥
    Immutable 详解及 React 中实践
    js 设置日期函数
    node 一站式 学习 教程
    Python_如何定义带参数的装饰器?
    Python-装饰器中保留被装饰函数元数据
    Python-用装饰器实现递归剪枝
    Python-通过实例方法调用-统一接口的实现-getter methodcaller
  • 原文地址:https://www.cnblogs.com/hygeia/p/5700013.html
Copyright © 2011-2022 走看看