Clean java solution


  • 0
    D
    public class Solution {
    public List<Integer> grayCode(int n) {
        
        List<Integer> list = new ArrayList<Integer>();
        list.add(0);
        if(n==0)return list;
        
        list.add(1);
        if(n==1) return list;
        
        else{
            
            for(int i=1;i<n;++i){
                List<Integer> list2 = new ArrayList<Integer>();
                int len = list.size();
                for(int j=0;j<len;++j){
                    list2.add(list.get(j));
                }
                for(int j=len-1;j>=0;--j){
                    list2.add(list.get(j)+(1<<i));
                }
                list=list2;
            }
            return list;
        }
        
    }
    

    }


Log in to reply
 

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