A Simple C++ Solution


  • 0
    Q
    class Solution {
    public:
      unordered_map<int,vector<vector<int>> > cache;  
      vector<vector<int>> getFactors(int n) {
          if(cache.count(n))
              return cache[n];     
          for(int i = 2; i<=sqrt(n); i++) 
              if(n % i == 0) {
                  cache[n].push_back({i,n/i});
                  auto factors = getFactors(n/i);
                  for(auto v: factors)
                     if(i <= v.front()) {
                        v.insert(v.begin(),i);
                        cache[n].push_back(v);
                     }   
          }
          return cache[n];
       }     
    };
    

Log in to reply
 

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