Share my 4ms c++ solution, just using an array copy from itself iteratively


  • 1
    C
     class Solution {
     public:
    	 vector<int> grayCode(int n) {
    		 vector<int> res{0};
    		 for (int i = 1; i <= n; i++)
    		 {
    			 int mask = 1 << (i - 1);
    			 for (int j = res.size() - 1; j >= 0; j--)
    				 res.push_back(res[j]+mask);					
    		 }
    		 return res;
    	 }
     };

  • 0
    G

    What would be the time complexity of your code?


Log in to reply
 

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