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


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

Example:


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

Source: leetcode | Difficulty: EASY
Tags: Dynamic programming

Data structures

Starter code: class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        

Solutions:
class Solution:

	def generate(self, numRows):
		res = [[1]]
		for i in range(1, numRows):
			res.append(list(map(lambda x, y: x + y, res[-1] + [0], [0] + res[-1])))
		return res[:numRows]


class Solution:

	def generate(self, numRows):
		if numRows == 0:
			return []
		tem = [0, 1]
		l = []
		for i in range(numRows):
			rowlist = []
			for j in range(len(tem) - 1):
				rowlist.append(tem[j] + tem[j + 1])
			l.append(rowlist)
			tem = rowlist[:]
			tem.insert(0, 0)
			tem.append(0)
		return l


class Solution:

	def generate(self, numRows):
		if numRows == 0:
			return []
		res = [[1]]
		for i in range(1, numRows):
			newRow = [1]
			for j in range(1, i):
				newRow.append(res[-1][j - 1] + res[-1][j])
			newRow.append(1)
			res.append(newRow)
		return res


class Solution:

	def generate(self, numRows):
		ans = [[1] * n for n in range(1, numRows + 1)]
		for i in range(1, numRows + 1):
			for j in range(0, i - 2):
				ans[i - 1][1 + j] = ans[i - 2][j] + ans[i - 2][j + 1]
		return ans


class Solution:

	def generate(self, numRows):
		A = []
		for i in range(numRows):
			A.append([1 for _ in range(i + 1)])
			for j in range(1, i):
				A[i][j] = A[i - 1][j - 1] + A[i - 1][j]
		return A


class Solution:

	def generate(self, numRows):
		if numRows < 1:
			return []
		base = [1]
		triangle = [base]
		for i in range(1, numRows):
			new_row = [1]
			prev_row = triangle[i - 1]
			for j in range(len(prev_row) - 1):
				new_row.append(prev_row[j] + prev_row[j + 1])
			new_row.append(1)
			triangle.append(new_row)
		return triangle


class Solution:

	def generate(self, numRows):
		A = [[1], [1, 1]]
		if numRows == 0:
			return []
		elif numRows < 3:
			return A[0:numRows]
		else:
			i = 3
			while i <= numRows:
				temp = A[i - 2][:]
				temp.append(0)
				temp2 = temp[::-1]
				temp3 = [X + Y for (X, Y) in zip(temp, temp2)]
				A.append(temp3)
				i += 1
		print(A)
		return A


class Solution:

	def generate(self, numRows):
		result = []
		for i in range(numRows):
			result.append([])
			for j in range(i + 1):
				if j in (0, i):
					result[i].append(1)
				else:
					result[i].append(result[i - 1][j - 1] + result[i - 1][j])
		return result


class Solution:

	def generate(self, numRows):
		result = [[]] * numRows
		for i in range(numRows):
			if i == 0:
				result[0] = [1]
			if i == 1:
				result[1] = [1, 1]
			if i > 1:
				temp = []
				for k in range(i + 1):
					left = result[i - 1][k - 1] if k >= 1 else 0
					right = result[i - 1][k] if k < i else 0
					temp.append(left + right)
				result[i] = temp
		return result


class Solution:

	def my_fact(self, n):
		if n == 1 or n == 0:
			return 1
		else:
			return n * self.my_fact(n - 1)

	def generate(self, numRows):
		res = [[] for i in range(numRows)]
		for n in range(numRows):
			for k in range(0, n + 1):
				val = self.my_fact(n) / (self.my_fact(n - k) * self.my_fact(k))
				res[n].append(int(val))
		return res


class Solution:

	def generate(self, numRows):
		if numRows == 0:
			return []
		result = [[1]]
		if numRows == 1:
			return result
		result.append([1, 1])
		if numRows == 2:
			return result
		for row in range(2, numRows):
			(l, r) = (0, 1)
			tmp = [1]
			while r < len(result[row - 1]):
				tmp.append(result[row - 1][l] + result[row - 1][r])
				l += 1
				r += 1
			tmp.append(1)
			result.append(tmp)
		return result


class Solution:

	def generate(self, numRows):
		if numRows == 0:
			return []
		if numRows == 1:
			return [[1]]
		if numRows == 2:
			return [[1], [1, 1]]
		result = [[1], [1, 1]]
		for i in range(3, numRows + 1):
			if len(result) < i:
				result.append([])
			result[i - 1].append(1)
			count = 0
			while len(result[i - 1]) < i - 1:
				result[i - 1].append(result[i - 2][count] + result[i - 2][count + 1])
				count += 1
			result[i - 1].append(1)
		return result


class Solution:

	def generate(self, numRows):
		if numRows == 0:
			return []
		if numRows == 1:
			return [[1]]
		res = [[1], [1, 1]]

		def add(nums):
			res = nums[:1]
			for (i, j) in enumerate(nums):
				if i < len(nums) - 1:
					res += [nums[i] + nums[i + 1]]
			res += nums[:1]
			return res
		while len(res) < numRows:
			res.append(add(res[-1]))
		return res