My java solution, 6 lines, easy to understand


  • 3
    H
    public List<Integer> grayCode(int n) {
    	List<Integer> output = new ArrayList<Integer>();	
    	output.add(0);
    	for (int i = 1; i <= n; i++)
    		for (int j = output.size() - 1; j >= 0; j--)
    			output.add(output.get(j) + (int)Math.pow(2, i - 1));   	
    	return output;
    }

Log in to reply
 

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