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

运行时间

时间: 2024-08-01 18:55:11

[LeetCode]118.Pascal&#39;s Triangle的相关文章

[LeetCode]119.Pascal&amp;#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? 思路 无 代码 /**------------------------------------ * 日期:2015-02-06 * 作者:S

【LeetCode从零单排】No118 Pascal&amp;#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] ] 代码 public class Solution { public List<List<Integer>> generate(int numRows) { List<List

Pascal&amp;#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? 用递归的方法实现,C++代码如下: #include<iostream> #include<vector> using namespac

Pascal&amp;#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] ] c++代码如下: #include<iostream> #include<vector> using namespace std; class Solution { public: ve

基于Java实现杨辉三角 LeetCode Pascal&#039;s Triangle_java

 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] ] 这道题比较简单, 杨辉三角, 可以用这一列的元素等于它头顶两元素的和来求. 数学扎实的人会看出, 其实每一列都是数学里的排列组合, 第4行, 可以用 C30 =

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

List中的set方法和add方法

[LeetCode]–119. Pascal's Triangle II 在做这个题的时候,我发现了一个list初始化的问题.就是set必须是new出来的具体list初始化之后才能使用,不然就会报错.下面就研究一下set和add. package yanning; import java.util.LinkedList; import java.util.List; public class TestListSet { public static void main(String[] args)

基于Solr的空间搜索

如果需要对带经纬度的数据进行检索,比如查找当前所在位置附近1000米的酒店,一种简单的方法就是:获取数据库中的所有酒店数据,按经纬度计算距离,返回距离小于1000米的数据. 这种方式在数据量小的时候比较有效,但是当数据量大的时候,检索的效率是很低的,本文介绍使用Solr的Spatial Query进行空间搜索. 空间搜索原理 空间搜索,又名Spatial Search(Spatial Query),基于空间搜索技术,可以做到: 1)对Point(经纬度)和其他的几何图形建索引 2)根据距离排序