Why does this code work on my machine but has wrong answer in Leetcode


  • 0
    H
    class Solution {
    public:
       std:: vector<int> grayCode(int n) {
       //case 0                                                                                                                                            
       std::vector<int> help;
       if (n==0) {
       help.push_back(0);
       return help;
       }
    
        int base = 1;
        help.push_back(0);
        help.push_back(1);//[0, 1]
       for (int i = 1; i<n;i++) {
      std::vector<int>::iterator it = help.begin();
      std::vector<int>::iterator itp = it; // iterator itp points at beginning
      base = 2 * base;
      it = it + (base-1);  //iterator it points at the last element
      while(it != itp){//iterating from last to first, add base
        help.push_back(base + *it);
        --it;
      }
      help.push_back(base + *it);
      }
      return help;
      }
     };
    

    This code with an input of 3, has an output of 0 1 3 2 6 7 5 4 on my machine, while Leetcode gave me the answer of [0,1,3,2,6,7,4,17453780]


Log in to reply
 

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