[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;
        }
    };

运行时间

这里写图片描述

目录
相关文章
LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs)作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row index starts from 0. 在杨辉三角中,每个数是它左上方和右上方的数的和。
886 0
Leetcode 118:Pascal's Triangle 杨辉三角
118:Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
955 0
[LeetCode]--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] ] 我是用数组做的,在草稿纸上稍微画一画应该就能找
1338 0
[LeetCode]--119. Pascal&#39;s 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? public
1057 0
|
Java
LeetCode 118 Pascal's Triangle(帕斯卡三角形)(vector)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50568461 翻译 给定一个行数字,生成它的帕斯卡三角形。
1019 0

热门文章

最新文章