119.杨辉三角II

这道题和第118题是一样的,需要注意这道题目对行数的要求

        # 定义一个列表,用来存放数据
        num_list = []
        for index1 in range(rowIndex + 1):
            # 每一行要先添加一个空列表
            num_list.append([])
            # 注意这里的for循环的范围
            for index2 in range(index1 + 1):
                # 将值为一的位置规定好
                if index1 == 0  or index2 == 0 or index2 == index1 :
                    num_list[index1].append(1)
                # 按照题目要求计算就好了
                else:
                    num_list[index1].append(num_list[index1 - 1][index2 - 1] + num_list[index1 - 1][index2])
        return num_list[rowIndex]
posted @ 2020-06-30 16:20  月为暮  阅读(186)  评论(0编辑  收藏  举报