Clean Java Backtrack Solution


  • 1
     public class Solution{
    public int countNumbersWithUniqueDigits(int n){
    	if(n==0){
    		return 1;
    	}
    	int[] count=new int[1];
    	helper(0,0,new boolean[10],count,n);
    	return count[0];
    }
    
    public void helper(int num,int index,boolean[] used,int[] count,int n){
    	if(index>0){
    		count[0]++;
    	}
    	if(index==n){
    		return;
    	}
    
    	for(int i=0;i<=9;i++){
    		if(used[0]&&index==1){
    			continue;
    		}
    		if(!used[i]){
    			used[i]=true;
    			helper(num*10+i,index+1,used,count,n);
    			used[i]=false;
    		}
    	}
    }
    

    }


  • 1

    Hey @bxiao0801, would you mind explaining in more detail what your code does, and how you came to this solution? I would greatly appreciate any help!


Log in to reply
 

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