How can I get an answer like this?


  • 0
    C

    Input:
    "2"

    Output:
    ["a

    Expected:
    ["a","b","c"]

    and this is my code:

    enter code here :  public List<String> letterCombinations(String digits) {
        char[] min = new char[11];
        char[] max = new char[11];
        char[] t = new char[1000];
    	List l = new ArrayList<String>();
    	if (digits.length()==0) {
    	    l.add(digits);
    		return l;
    	}
        t = digits.toCharArray();
        int n=digits.length();
        char[] last = new char[1000], temp = new char[1000];
        for(int i=2; i<=9; i++) {
        	min[i] = (char) ('a'+(i-2)*3);
        	max[i] = (char) ('c'+(i-2)*3);
        }
        for (int i=0; i<n; i++) {
        	temp[i] = min[t[i]-'0'];
        	last[i] = max[t[i]-'0'];
        }
        l.add(String.valueOf(temp));
        while (1 == 1) {
        	temp = getnext(temp,n,min,max,t);
        	l.add(String.valueOf(temp));
        	if (String.valueOf(temp).equals(String.valueOf(last)))
        		break;
        }
        return l;
    }
    public char[] getnext(char[] a,int n, char[] min, char[] max, char[] t) {
    	int i=0;
    	for (i=n-1; i>=0; i--) {
    		if (a[i] < max[t[i]-'0']) {
    			a[i] = (char) (a[i]+1);
    			break;
    		} else 
    			continue;
    	}
    	if (i != n-1) 
    		for (int j=i+1; j<n; j++)
    			a[j] = min[t[j]-'0'];
    	return a;
    }

  • 0
    S

    Hope this would be helpful. Anyhow, questions about code you've written must describe the specific problem clearly, elaborate thoughts based on code. Please read the FAQ (http://oj.leetcode.com/discuss/faq) for more info.


Log in to reply
 

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