zoukankan      html  css  js  c++  java
  • [leetcode]Pascal's Triangle II @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/

    题意:

    Given an index k, return the kth row of the Pascal's triangle.

    For example, given k = 3,
    Return [1,3,3,1].

    Note:
    Could you optimize your algorithm to use only O(k) extra space?

    解题思路:思路同上一道题。

    class Solution:
        # @return a list of integers
        def getRow(self, rowIndex):
            if rowIndex == 0: return [1]
            if rowIndex == 1: return [1, 1]
            list = [[] for i in range(rowIndex+1)]
            list[0] = [1]
            list[1] = [1, 1]
            for i in range(2, rowIndex+1):
                list[i] = [1 for j in range(i + 1)]
                for j in range(1, i):
                    list[i][j] = list[i - 1][j - 1] + list[i - 1][j]
            return list[rowIndex]
  • 相关阅读:
    Day 03
    Day 03 作业
    Day 02 作业
    Day 02
    Day 01
    Day 10 面向对象基础
    Spring学习-- Bean 的作用域
    一、基本知识
    cloud-init使用技巧
    如何在KVM中管理存储池
  • 原文地址:https://www.cnblogs.com/zuoyuan/p/3766392.html
Copyright © 2011-2022 走看看