{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

118. Pascal's Triangle

\n", "
\n", "\n", "\n", "

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.

\n", "\n", "

\"\"
\n", "In Pascal's triangle, each number is the sum of the two numbers directly above it.

\n", "\n", "

Example:

\n", "\n", "
Input: 5\n",
    "Output:\n",
    "[\n",
    "     [1],\n",
    "    [1,1],\n",
    "   [1,2,1],\n",
    "  [1,3,3,1],\n",
    " [1,4,6,4,1]\n",
    "]\n",
    "
\n", "\n", "\n", "

 

\n", "Source \n", "
\n", "\n", "

Code

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def pascal_triangle(numRows):\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Check

" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pascal_triangle(5)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1],\n", " [1, 1],\n", " [1, 2, 1],\n", " [1, 3, 3, 1],\n", " [1, 4, 6, 4, 1],\n", " [1, 5, 10, 10, 5, 1],\n", " [1, 6, 15, 20, 15, 6, 1],\n", " [1, 7, 21, 35, 35, 21, 7, 1],\n", " [1, 8, 28, 56, 70, 56, 28, 8, 1],\n", " [1, 9, 36, 84, 126, 126, 84, 36, 9, 1],\n", " [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1],\n", " [1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1],\n", " [1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1],\n", " [1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1],\n", " [1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pascal_triangle(15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Follow up:

\n", "

Solve it both recursively and iteratively.

\n", "\n", "

Code

" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def pascal_triangle(numRows):\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Check

" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pascal_triangle(5)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1],\n", " [1, 1],\n", " [1, 2, 1],\n", " [1, 3, 3, 1],\n", " [1, 4, 6, 4, 1],\n", " [1, 5, 10, 10, 5, 1],\n", " [1, 6, 15, 20, 15, 6, 1],\n", " [1, 7, 21, 35, 35, 21, 7, 1],\n", " [1, 8, 28, 56, 70, 56, 28, 8, 1],\n", " [1, 9, 36, 84, 126, 126, 84, 36, 9, 1],\n", " [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1],\n", " [1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1],\n", " [1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1],\n", " [1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1],\n", " [1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pascal_triangle(15)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }