Share my solution(392ms)


  • 1
    X
    public List<Integer> grayCode(int n) {
    	List<Integer> li = new LinkedList<Integer>();
    	for(int i = 0;i<Math.pow(2, n);i++)
    	{
    		int i2 = i>>1;
    		int x = i^i2;
    		li.add(x);
    	}
        return li;
    }
    

    My solution is based on how to transform binary code into gray code. Google what gray code is, then it's easy to understand.


Log in to reply
 

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