Few lines of Java code 1ms no recursion

  • 5
    public List<Integer> grayCode(int n) {
        List<Integer> result = new ArrayList<Integer>();
    	for(int i=1; i<=n; i++){
        	int size = result.size()-1;
        	for(int j=size; j>=0; j--){
        		result.add(result.get(j) | 1<<i-1);
    	return result;

Log in to reply

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