Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

题目:

Given numRows, generate the first numRows of Pascal’s triangle.

For example, given numRows = 5,

Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

翻译:

给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形、帕斯卡三角形)。

分析:

除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数据。

Java版代码:

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result=new ArrayList<>();
        if(numRows<=0){
            return result;
        }
        List<Integer> line=new ArrayList<>();
        line.add(1);
        result.add(line);
        List<Integer> lastLine=line;
        for(int i=1;i<numRows;i++){
            line=new ArrayList<>();
            line.add(1);
            for(int j=1;j<i;j++){
                line.add(lastLine.get(j-1)+lastLine.get(j));
            }
            line.add(1);
            result.add(line);
            lastLine=line;
        }
        return result;
    }
}

Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

时间: 2024-10-25 15:20:45

Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]的相关文章

Leet Code OJ 119. Pascal&#39;s Triangle II [Difficulty: Easy]

题目: 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? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

题目: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 翻译: 给定一个整数数组和一个整数k.找出是否存在下标i,j使得nums[i] = nums[j].同一时候i

Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]

题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements

LeetCode开心刷题五十一天——118. Pascal&#39;s Triangle 接触跳转表概念,不知用处 lamda逗号导致表达式加法奇怪不理解119. Pascal&#39;s Triangle II

118. Pascal's Triangle Easy 87984FavoriteShare 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],

118. Pascal&#39;s Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 分析 第k层有k个元素,每层第一个及最后一个元素值为1,对于(k>2)层,第n(n>1 && n < k)个元素A[k][n] = A[k-1][n-1]+A[k-1][n];

LeetCode OJ:Pascal&#39;s Triangle(帕斯卡三角)

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 帕斯卡三角,很简单的问题,见代码: 1 class Solution { 2 public: 3 vector<vector<int>> generate(int numRows) {

leetcode 118 Pascal&#39;s Triangle ----- java

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 用的比价暴力的方法, 也是最快的. public class Solution { List list = new ArrayList<List<Integer>>(); public

No.118 Pascal&#39;s Triangle

No.118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 解法:其实很简单,想清楚就好,找好规律!! 1 #include "stdafx.h" 2 #include <string>

leetCode 118. Pascal&#39;s Triangle 数组 (杨辉三角)

118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [      [1],     [1,1],    [1,2,1],   [1,3,3,1],  [1,4,6,4,1] ] 题目大意: 输入行数,输出如上图所示的数组.(杨辉三角) 思路: 用双vector来处理当前行和下一行. 代码如下: cla