My code run success in my eclipse but wrong on leetcode


  • 0
    N

    this is my code:

    public class Solution {
        public int ladderLength(String beginWord, String endWord, List<String> wordList) {
            int length = trans_length(beginWord,endWord,wordList);
            if(length<Integer.MAX_VALUE)
            	return length;
            else return 0;
        }
        
       public int trans_length(String beginWord, String endWord, List<String> wordList){
    		if(beginWord == endWord)
    			return 1;
    		int length =Integer.MAX_VALUE;
            for(int i=0;i<wordList.size();i++){       	
            	String s = wordList.get(i);
            	if(valid(beginWord,s)){
            		wordList.remove(i);
            		int length_temp = Integer.MAX_VALUE;
            		if(trans_length(s,endWord,wordList) != Integer.MAX_VALUE)
            			length_temp = trans_length(s,endWord,wordList)+1;
            		length = length_temp>length?length:length_temp;
            		wordList.add(i,s);
            	}
            }
            return length;
    	}
    	
    	public boolean valid(String s1,String s2){
    		int count = 0;
    		int index = 0;
    		while(count<2&&index<s1.length()){
    			if(s1.charAt(index) != s2.charAt(index))
    				count+=1;
    			index++;
    		}
    		if(count == 1)
    			return true;
    		else return false;
    	}
    }
    

    when I run my code in my eclipse to test the example case, it can output the length:5, however it output 0 if I subscribe it to leetcode. Anyone can help me?


  • 0
    S

    I am facing the same issue (I get output 5 in my IDE but the exact same code returns 0 in leetcode and fails! )

    Did you figure out what's the issue?


Log in to reply
 

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