My concise java solution


  • 0
    B
    public class Solution {
    public List<Integer> grayCode(int n) {
     
           List<Integer> answer = new ArrayList<Integer> ();
           if(n == 0){answer.add(0);return answer;}
           int layer = 0;
           answer.add(0);
           answer.add(1);
           for( layer = 1; layer < n; layer++){
        	   int last = answer.size() - 1;
        	   int bigbit = (int)Math.pow(2, layer);
        	   for(int i = last; i >= 0; i--){
        		  answer.add(bigbit + answer.get(i));
        		   
        	   }
        	   
           }
           return answer;
        }
    

    }


Log in to reply
 

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