LeetCode wrong judgement on JAVA


  • 0
    D
    public class Solution {
    public int shortestDistance(String[] words, String word1, String word2) {
        int a = 0;
        int b = -words.length;
        int min =words.length;
        for(int i = 0;i < words.length;i++)
        {
            if(words[i].matches(word1))
            {
                a=i;
                min = Math.min(min,Math.abs(a-b));
            }
            else
            {
                if(words[i].matches(word2))
                {
                    b=i;
                    min = Math.min(min,Math.abs(a-b));
                }
            }
        }
        return min;
            
    }
    

    when initialize a to words.length , it judges correctly. But it really doesn't matter.

    LeetCode is kidding me!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.