SHARE
TWEET

Untitled

a guest Mar 26th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Solution {
  7. public:
  8.     vector<vector<int>> generate(int numRows) {
  9.         vector<vector<int>> result{};
  10.  
  11.         if (numRows == 0) {
  12.             return result;
  13.         }
  14.         else {
  15.             vector<int> temp{1};
  16.             result.push_back(temp);
  17.             helper(result, numRows);
  18.             return result;
  19.         }
  20.     }
  21.    
  22.     void helper(vector<vector<int>> &result, int numRows) {
  23.         vector<int> current = result[result.size() - 1];
  24.  
  25.         if (current.size() == numRows)
  26.             return;
  27.        
  28.         vector<int> nextArray{1};
  29.        
  30.         auto current_it = current.begin();
  31.         while(1) {
  32.             if ((current_it + 1) == current.end()) {
  33.                 nextArray.push_back(1);
  34.                 break;
  35.             }
  36.             int current_val = *current_it++;
  37.             int next_val = *current_it;
  38.             nextArray.push_back(current_val + next_val);
  39.         }
  40.         result.push_back(nextArray);
  41.        
  42.         helper(result, numRows);
  43.     }
  44. };
  45.  
  46. int main(int argc, char**argv) {
  47.     auto obj = Solution();
  48.     vector<vector<int>> result = obj.generate(10);
  49.  
  50.     for (auto it = result.begin(); it != result.end(); ++it) {
  51.         vector<int> current = *it;
  52.         for (auto it = current.begin(); it != current.end(); ++it) {
  53.             cout << *it << ',';
  54.         }
  55.         cout << endl;
  56.     }
  57.  
  58.     return 0;
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top