Why my solution went to an wrong result in OJ using the String "3", but test right in my local machine?


  • 0
    K
    public class LetterCombinationsofaPhoneNumber {
    public static String[] letters = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    public static List<String> frl = new ArrayList<String>();
    public static void Combinations(String digits,String s)
    {
    	String ts = s;
    	if(digits.length()<1)
    		return;
    	int flag = Integer.parseInt(digits.substring(0, 1));
    	String letter = letters[flag];
    	for(int i=0;i<letter.length();i++)
    	{
    		s = ts+letter.charAt(i);
    		if(digits.length()>1)
    		{
    			String tmpd=digits.substring(1, digits.length());
    			Combinations(tmpd,s);
    		}
    		else
    		{
    			frl.add(s);
    		}
    	}
    	
    }
    public static List<String> letterCombinations(String digits) {
    	String s = "";
    	Combinations(digits,s);
    	return frl;
    }
    public static void main(String[] args) {
    	// TODO Auto-generated method stub
    	List<String> list = letterCombinations("123");
    	for(String t:list)
    	{
    		System.out.println(t);
    	}
    }
    

    }


Log in to reply
 

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