My 1ms Solution


  • 0
    B
        public List<Integer> grayCode(int n) {
            List<Integer> res = new ArrayList<>();
    		int bits = 1;
    		int ntmp=0;
    		res.add(0);
    		for(int i=0;i<n;i++)
    		{
    			bits=1<<i;
    			for(int j = res.size()-1;j>=0;j--){
    				ntmp = res.get(j)+bits;
    				res.add(ntmp);
    			}
    			
    		}
    		return res;
        }

Log in to reply
 

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