119. Pascal's Triangle II

Given an integer rowIndex, returns the rowIndexth (indexed 0) row of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it, as shown: Example 1: Input: rowIndex = 3 Output: [1,3,3,1] Example 2: Input: rowIndex = 0 Output: [1] Example 3: Input: rowIndex = 1 Output: [1,1] 0 <= rowIndex <= 33 Solution class Solution { public: vector getRow(int rowIndex) { vector> ans; ans.push_back({1}); for(int i = 1 ; i < rowIndex + 1; i++) { vector level(i); level[0] = 1; ans.push_back(level); for(int j = 1; j < i; j++) { if(j == 0) ans[i][j] = 1; else ans[i][j] = ans[i - 1][j] + ans[i - 1][j - 1]; } ans[i].push_back(1); } return ans[rowIndex]; } };

Comments

Popular posts from this blog

141. Linked List Cycle

67. Add Binary

88. Merge Sorted Array