zoukankan      html  css  js  c++  java
  • LeetCode 118 _ 数组

    1. 题目描述

    https://leetcode-cn.com/problems/pascals-triangle/

    2. 代码

    class Solution:
        def generate(self, numRows: int) -> 'List[List[int]]':
            result = []#定义一个空数组,用于保存基本最终结果(二维数组))
            for i in range(numRows):
                if i == 0:#第一行:[1]
                    result.append([1])
                elif i == 1:#第二行:[1,1]
                    result.append([1,1])
                else:# i >= 2#第三行以后
                    prerow = result[-1]#上一行
                    currow = []#当前行
                    for j in range(i+1):#循环j,表示二维数组中的:第i行、第j列
                        if j == 0 or j == i:#第一列和最后一列是1
                            currow.append(1)
                        else:#中间列,上一行的第j-1列元素 + 上一行的第j列元素
                            currow.append(prerow[j-1] + prerow[j])
                    result.append(currow)#将当前行添加到结果数组中
            return result#返回结果数组

    3. 语法整理

    3.1 list.append(obj)

    在列表末尾添加新的对象

    list1 = ['Google', 'Runoob', 'Taobao']
    list1.append('Baidu')
    print ("更新后的列表 : ", list1)
    更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']

    3.2 for 语句

    Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

    1 languages = ["C", "C++", "Perl", "Python"] 
    2 for x in languages:
    3     print (x)
    1 C
    2 C++
    3 Perl
    4 Python

    3.3 range()函数

    可遍历数字序列

    for i in range(5):
         print(i)
    0
    1
    2
    3
    4
    for i in range(0, 10, 3) :
        print(i) 0
    3
    6
    9
  • 相关阅读:
    软件开发模型
    Java学习--数组
    10年软件测试经验
    Log4net
    文件下载界面
    VB.NET中文双引号的处理方法
    SOAPUI使用
    .net数字转换成汉字大写
    SQL调用Webservices
    SQL大小写金额转换
  • 原文地址:https://www.cnblogs.com/vvzhang/p/13658791.html
Copyright © 2011-2022 走看看