Pascal's Triangle II Solutions in C++
Number 119
Difficulty Easy
Acceptance 50.6%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/pascals-triangle-ii/// Author : Hao Chen// Date : 2014-06-18#include <stdlib.h>#include <vector>#include <iostream>using namespace std;vector<int> getRow(int rowIndex) {vector<int> v(rowIndex+1, 0);v[0]=1;for (int i=0; i<rowIndex; i++){for(int j=i+1; j>0; j--){v[j] += v[j-1];}}return v;}void printVector( vector<int> pt){cout << "{ ";for(int j=0; j<pt.size(); j++){cout << pt[j] << ", ";}cout << "} " << endl;}int main(int argc, char** argv){int n = 3;if (argc>1) {n = atoi(argv[1]);}printVector(getRow(n));}
C++ solution by pezy/LeetCode
#include <vector>using std::vector;class Solution {public:vector<int> getRow(int rowIndex) {vector<int> vec(rowIndex+1, 0);vec[0] = 1;for (int i=0; i<=rowIndex; ++i)for (int j=i; j>0; --j)vec[j] += vec[j-1];return vec;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/pascals-triangle-ii/description//// Author : liuyubobobo/// Time : 2018-08-10#include <iostream>#include <vector>using namespace std;/// Simulate Pascal Triangle////// Time Complexity: O(rowIndex^2)/// Space Complexity: O(rowIndex^2)class Solution {public:vector<int> getRow(int rowIndex) {vector<vector<int>> res(rowIndex + 1, vector<int>(rowIndex + 1, 1));for(int i = 2; i <= rowIndex ; i ++)for(int j = 1; j < i; j ++ )res[i][j] = res[i-1][j-1] + res[i-1][j];return res[rowIndex];}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {for(int i = 0 ; i < 10 ; i ++)print_vec(Solution().getRow(i));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/pascals-triangle-ii/description//// Author : liuyubobobo/// Time : 2018-08-10#include <iostream>#include <vector>using namespace std;/// Simulate Pascal Triangle with only O(n) space////// Time Complexity: O(rowIndex^2)/// Space Complexity: O(rowIndex)class Solution {public:vector<int> getRow(int rowIndex) {vector<vector<int>> res(2, vector<int>(rowIndex + 1, 1));for(int i = 2; i <= rowIndex ; i ++)for(int j = 1; j < i; j ++ )res[i&1][j] = res[(i-1)&1][j-1] + res[(i-1)&1][j];return res[rowIndex&1];}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {for(int i = 0 ; i < 10 ; i ++)print_vec(Solution().getRow(i));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/pascals-triangle-ii/description//// Author : liuyubobobo/// Time : 2018-08-10#include <iostream>#include <vector>using namespace std;/// Using Binomial Coefficients////// Time Complexity: O(rowIndex^2)/// Space Complexity: O(rowIndex)class Solution {public:vector<int> getRow(int rowIndex) {vector<int> res(rowIndex + 1, 1);for(int i = 1; i <= (rowIndex + 1) / 2; i ++)res[i] = res[rowIndex - i] = C(rowIndex, i);return res;}private:int C(int m, int n){long long up = 1ll;int down1 = n, down2 = m - n;for(int i = m; i > 0; i --){up *= i;if(down1 && up % down1 == 0ll) up /= down1, down1 --;if(down2 && up % down2 == 0ll) up /= down2, down2 --;}return up;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {for(int i = 0 ; i < 10 ; i ++)print_vec(Solution().getRow(i));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/pascals-triangle-ii/description//// Author : liuyubobobo/// Time : 2018-08-10#include <iostream>#include <vector>using namespace std;/// Using Binomial Coefficients Recurrence Relationship/// Using C(m, n) = (m - n + 1) / n * C(m, n - 1)////// Time Complexity: O(rowIndex)/// Space Complexity: O(rowIndex)class Solution {public:vector<int> getRow(int rowIndex) {vector<int> res(rowIndex + 1, 1);for(int i = 1; i <= (rowIndex + 1) / 2; i ++)res[i] = res[rowIndex - i] = (long long)(rowIndex - i + 1) * res[i - 1] / i;return res;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {for(int i = 0 ; i < 10 ; i ++)print_vec(Solution().getRow(i));return 0;}