首页 > 其他分享 >[Leetcode] 0118. 杨辉三角

[Leetcode] 0118. 杨辉三角

时间:2023-11-06 12:12:40浏览次数:40  
标签:int 0118 个数 numRows vector ans 杨辉三角 Leetcode

118. 杨辉三角

题目描述

给定一个非负整数 numRows生成「杨辉三角」的前 numRows 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

 

示例 1:

输入: numRows = 5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

示例 2:

输入: numRows = 1
输出: [[1]]

 

提示:

  • 1 <= numRows <= 30

解法

先设置每一行首尾元素为 1,其它元素为 0。然后根据杨辉三角,设置每一行其它元素即可。
其他每个数字等于上一行的左右两个数字之和,可用此性质写出整个杨辉三角。即第 \(n\) 行的第 \(i\) 个数等于第 \(n-1\) 行的第 \(i-1\) 个数和第 \(i\) 个数之和。

时间复杂度:\(O(\textit{numRows}^2)\)。

空间复杂度:\(O(1)\)。不考虑返回值的空间占用。

Python3

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        ans = []
        for i in range(numRows):
            t = [
                1 if j == 0 or j == i else ans[-1][j] + ans[-1][j - 1]
                for j in range(i + 1)
            ]
            ans.append(t)
        return ans

C++

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> ans;
        for (int i = 0; i < numRows; ++i) {
            vector<int> t(i + 1, 1);
            for (int j = 1; j < i; ++j) t[j] = ans[i - 1][j] + ans[i - 1][j - 1];
            ans.push_back(t);
        }
        return ans;
    }
};

标签:int,0118,个数,numRows,vector,ans,杨辉三角,Leetcode
From: https://www.cnblogs.com/yege/p/17812363.html

相关文章

  • LeetCode 精选100题-70题爬楼梯
    题目描述:假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?思路:第一阶楼梯:n=1,有一种方法f(1)=1;第二阶楼梯:n=2,有两种方法f(2)=2;当我们第一步爬了1个台阶时,我们可以有f(n-1)种方法爬到楼顶;当我们第一步爬了2......
  • LeetCode/在树上执行操作以后得到的最大分数
    有一棵n个节点的无向树,节点编号为0到n-1,根节点编号为0。给你一个长度为n-1的二维整数数组edges表示这棵树,其中edges[i]=[ai,bi]表示树中节点ai和bi有一条边。同时给你一个长度为n下标从0开始的整数数组values,其中values[i]表示第i个节点的值......
  • LeetCode101.对称二叉树
    题目描述给你一个二叉树的根节点root,检查它是否轴对称。示例提条的代码importjava.util.List;importjava.util.ArrayList;importjava.util.Deque;importjava.util.LinkedList;importjava.util.Collections;classSolution{publicbooleanisSymmetric(Tr......
  • [LeetCode] 1535. Find the Winner of an Array Game
    Givenanintegerarrayarrofdistinctintegersandanintegerk.Agamewillbeplayedbetweenthefirsttwoelementsofthearray(i.e.arr[0]andarr[1]).Ineachroundofthegame,wecomparearr[0]witharr[1],thelargerintegerwinsandremainsat......
  • leetcode 第一题 两数之和
    题目给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。 初级阶段Java ......
  • [LeetCode] 2149. Rearrange Array Elements by Sign
    Youaregivena0-indexedintegerarraynumsofevenlengthconsistingofanequalnumberofpositiveandnegativeintegers.Youshouldrearrangetheelementsofnumssuchthatthemodifiedarrayfollowsthegivenconditions:Everyconsecutivepairofint......
  • 11月LeetCode每日一题: 117. 填充每个节点的下一个右侧节点指针 II
    题目描述:给定一个二叉树:structNode{intval;Node*left;Node*right;Node*next;}填充它的每个next指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将next指针设置为 NULL 。初始状态下,所有 next指针都被设置为 NULL 。 考察......
  • LeetCode347.前K个高频元素
    题目描述给你一个整数数组nums和一个整数k,请你返回其中出现频率前k高的元素。你可以按任意顺序返回答案。示例提交的代码你被骗了,我没做出来,能想到的方法时间复杂度是nlogn,还不如不写,想到小顶堆了,但是Java这里我不知道怎么实现:(学习到的东西经典使用堆实现,但是个......
  • LeetCode150.逆波兰表达式求值
    题目描述给你一个字符串数组tokens,表示一个根据逆波兰表示法表示的算术表达式。请你计算该表达式。返回一个表示表达式值的整数。示例提交的代码importjava.util.Deque;importjava.util.LinkedList;classSolution{publicintevalRPN(String[]tokens){......
  • LeetCode1047.删除字符串中的所有相邻重复项
    题目描述给出由小写字母组成的字符串S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在S上反复执行重复项删除操作,直到无法继续删除。在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。示例提交的代码importjava.util.Deque;importjava.util.Link......