Java Code, fail here but success in terminal

  • 0

    I'm just a beginner. My code works well in my Mac's terminal but does wrong here. Could anyone help to check it? I don't know where is wrong. Thanks!

    public int shortestDistance(String[] words, String word1, String word2) {
        int last1 = Arrays.asList(words).indexOf(word1);
        int last2 = Arrays.asList(words).indexOf(word2);
        int cur = Math.max(last1, last2)+1;
        int distance = Math.abs(last1-last2);
        while (cur<words.length){
            if (!(words[cur].equals(word1) || words[cur].equals(word2))){cur+=1;}
                if (words[cur]==word1){last1=cur;}else{last2=cur;}
                distance = Math.min(distance, Math.abs(last1-last2) );
        return distance;

Log in to reply

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