<Array> 277 243 244 245

277. Find the Celebrity

knows(i, j):

By comparing a pair(i, j), we are able to discard one of them

  1. True: i 必定不是Celebrity, 因为Celebrity不认识任何人

  2. False: j 必定不是Celebrity, 因为Celebrity要被其他人认识。

第一轮:找出一个可能是Celebrity的Candidate,因为在之前的人都不是Celebrity,Candidate不认识后面的人,后面的人也不是Celebrity,但是前面的人可能不认识这个Candidate。

第二轮:验证这个Candidate是不是Celebrity。

public class Solution extends Relation {
    public int findCelebrity(int n) {
        int candidate = 0;
        //1. Find a candidate by one pass:(make sure other people are not a celebrity)
        for(int i = 1; i < n; i++){
            if(knows(candidate, i)){
                candidate = i;
            }
        }
        //2. Make sure if the candidate is a celebrity by one pass
        for(int i = 0; i < n; i++){
            if(i == candidate){
                continue;
            }
            if(!knows(i, candidate) || knows(candidate, i)){
                return -1;
            }
        }
        return candidate;
    }
}

243. Shortest Word Distance

用index a和b来记录xia

class Solution {
    public int shortestDistance(String[] words, String word1, String word2) {
        int a = -1;
        int b = -1;
        int result = Integer.MAX_VALUE;
        for(int i = 0; i < words.length; i++){
            if(word1.equals(words[i])){
                a = i;
            }else if(word2.equals(words[i])){
                b = i;
            }
            if(a != -1 && b != -1){
                result = Math.min(result, Math.abs(a - b));
            }
        }
        return result;
    }
}

244. Shortest Word Distance II

用HashMap来记录每个单词和他的索引,然后再计算距离的最小值。

class WordDistance {
    Map<String, List<Integer>> map = new HashMap<>();
    
    public WordDistance(String[] words) {
        for(int i = 0; i < words.length; i++){
            if(map.containsKey(words[i])){
                map.get(words[i]).add(i);
            }else{
                List<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(words[i], list);
            }
        }    
    }
    
    public int shortest(String word1, String word2) {
        List<Integer> l1 = map.get(word1);
        List<Integer> l2 = map.get(word2); 
        int i = 0, j = 0;
        int result = Integer.MAX_VALUE;
        
        while(i < l1.size() && j < l2.size()){
            if(l1.get(i) < l2.get(j)){
                result = Math.min(result, l2.get(j) - l1.get(i));
                i++;
            }else{
                result = Math.min(result, l1.get(i) - l2.get(j));
                j++;
            }
        }

        return result;
    }
}

 245. Shortest Word Distance III

用b来记录上一个相同string的位置,到第二个位置的时候赋值给a。

class Solution {
    public int shortestWordDistance(String[] words, String word1, String word2) {
        int a = words.length, b = -words.length;
        int result = Integer.MAX_VALUE;
        for(int i = 0; i < words.length; i++){
            if(words[i].equals(word1)){
                a = i;
            }
            if(words[i].equals(word2)){
                if(word1.equals(word2))
                    a = b;
                b = i;
            }
            if(a != -1 && b != -1){
                result = Math.min(result, Math.abs(a - b));  
            }
        }
        return result;
    }
}
原文地址:https://www.cnblogs.com/Afei-1123/p/11984141.html