243.Shortest Word Distance

    /*
     * 243.Shortest Word Distance 
     * 2016-6-18 by Mingyang
     * 这个题目就太简单了,不讲
     */
     public int shortestDistance(String[] words, String word1, String word2) {
            int len=words.length;
            if(words==null||len==0)
              return 0;
            int first=-1;
            int second=-1;
            int res=Integer.MAX_VALUE;
            for(int i=0;i<len;i++){
                if(words[i].equals(word1)){
                    first=i;
                }
                if(words[i].equals(word2)){
                    second=i;
                }
                if(first>=0&&second>=0){
                    res=Math.min(res,Math.abs(first-second));
                }
            }
            return res;
        }
原文地址:https://www.cnblogs.com/zmyvszk/p/5597363.html