`
SunnyYoona
  • 浏览: 366587 次
社区版块
存档分类
最新评论

[LeetCode]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]
]

思路

模拟

代码

    /**------------------------------------
    *   日期:2015-02-06
    *   作者:SJF0115
    *   题目: 118.Pascal's Triangle
    *   网址:https://oj.leetcode.com/problems/pascals-triangle/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        vector<vector<int> > generate(int numRows) {
            vector<int> row;
            vector<vector<int> > triangle;
            if(numRows >= 1){
                row.push_back(1);
                triangle.push_back(row);
            }//if
            if(numRows >= 2){
                row.push_back(1);
                triangle.push_back(row);
            }//if
            for(int i = 2;i < numRows;++i){
                row.clear();
                row.push_back(1);
                for(int j = 1;j < i;++j){
                    row.push_back(triangle[i-1][j-1]+triangle[i-1][j]);
                }//for
                row.push_back(1);
                triangle.push_back(row);
            }//for
            return triangle;
        }
    };

    int main(){
        Solution s;
        int n = 5;
        vector<vector<int> > result = s.generate(n);
        // 输出
        for(int i = 0;i < result.size();++i){
            for(int j = 0;j < result[i].size();j++){
                cout<<result[i][j]<<"  ";
            }//for
            cout<<endl;
        }//for
        return 0;
    }

运行时间

这里写图片描述

思路二

    /**------------------------------------
    *   日期:2015-02-06
    *   作者:SJF0115
    *   题目: 118.Pascal's Triangle
    *   网址:https://oj.leetcode.com/problems/pascals-triangle/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    class Solution {
    public:
        vector<vector<int> > generate(int numRows) {
            vector<vector<int>> triangle(numRows);
            for (int i = 0;i < numRows;++i) {
                triangle[i].resize(i + 1);
                triangle[i][0] = triangle[i][i] = 1;
                for (int j = 1;j < i;++j) {
                    triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
                }//for
            }//for
            return triangle;
        }
    };

运行时间

这里写图片描述

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
分享到:
评论

相关推荐

    基于Java实现杨辉三角 LeetCode Pascal's Triangle

    主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    LeetCode C++全解

    Pascal's Triangle v. Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. ...

    圆和矩形是否重叠leetcode-leetcode_solutions:leetcode_solutions

    2.使用数组作为带符号的缓冲区118.Pascal's Triangle -&gt; 理解结构并做167 Two Sum II - 输入数组已排序:使用排序数组的条件,使用前后两个指针35.Search Insert Position -&gt; 线性搜索/二分搜索(左右各有1个间隙) ...

    leetcode-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    leetcode-js Leecode 经典题目 JavaScript TypeScript 题解。 Leetcode's answers by JavaScript and TypeScript. easy ...118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)

    leetcode2sumc-Leetcode-2020:刷刷Leetcode并作纪录

    Pascal's Triangle easy O O 119 Pascal's Triangle II easy O 要满足只用一个array大小空间O(k) k为input大小来完成,须具备backtracking概念 151 Reverse Words in a String medium O 这题有点算是easy的程度, ...

    gasstationleetcode-LeetCode_Practice:我的LeetCode练习从2020年开始

    118_Pascal's_Triangle_I 119_Pascal's_Triangle_II 169_Majority_Element 229_Majority_Element_II 274_H_索引 275_H_Index_II 217_Contain_Duplicate 55_Jump_Game 45_Jump_Game_II 121_Best_Time_to_Buy_and_Sell...

    leetcode答案-leetcode:每日三题

    Pascal's Triangle Given two sorted integer arrays A and B, merge B into A as one sorted array.Note: You may assume that A has enough space (size that is greater or equal to m + n)to hold additional ...

    leetcode卡-LeetCode:我使用JavaScript的LeetCode解决方案

    Pascal's Triangle (杨辉三角) 124 二叉树最大路径和 136 x ^ x = 0 169 Majority Vote Algorithm (最大投票数算法) 240 检索二阶矩阵 189 数组操作的时间复杂度比较 206 反转单向链表 226 反转二叉树 459 重复子...

    leetcode添加元素使和等于-Leetcode:力码

    leetcode添加元素使和等于 Leetcode Part1 共55道 1 plusOne easy 描述:用一组数据表示一个整数,实现整数加一的操作 主要思路:主要考虑最高位进位的情况,可以创建一个长度加一的...Pascal's Triangle II easy 描

    leetcode浇花-LCSolutions:我的力扣解决方案

    Pascal's Triangle #0121 - Best Time to Buy and Sell Stock #0125 - Valid Palindrome #0136 - Single Number #0167 - Two Sum - Input Array is sorted #0189 - Rotate Array #0217 - Contains Duplicate #0242 -...

    cpp-算法精粹

    Pascal's Triangle II Spiral Matrix Spiral Matrix II ZigZag Conversion Divide Two Integers Text Justification Max Points on a Line Community QQ 群: 237669375 Github: ...

    javalruleetcode-SDE-Problems:标准SDE问题列表

    leetcode SDE-问题 标准 SDE 问题列表 第一天:(数组) 日 问题陈述 解决方案 困难 使用的数据结构 使用的算法 时间复杂度 空间复杂度 补充阅读 在 N 个整数的数组中查找重复项 中等的 大批 不适用 上) O(1) 在不...

Global site tag (gtag.js) - Google Analytics