zoukankan      html  css  js  c++  java
  • Leetcode 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]
    ]

    很简单的题不多说,也可以用队列实现
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    typedef vector<vector<int> > VVI;
    
    VVI generate(int numRows){
        VVI res;
        if(numRows == 0) return res;
        vector<int> row;
        row.push_back(1);
        res.push_back(row);
        if(numRows == 1) return res;
        row.push_back(1);
        res.push_back(row);
        if(numRows == 2) return res;
        for(int i = 3; i <= numRows; ++ i){
            vector<int> newRow(i,1);
            for(int j = 1; j < i-1; ++ j){
                newRow[j] = row[j]+row[j-1];
            }
            res.push_back(newRow);
            row = newRow;
        }
        return res;
    } 
    
    int main(){
        VVI res = generate(5);
        for(int i = 0 ; i < res.size(); ++ i){
            for(int j = 0 ; j < res[i].size(); ++ j){
                cout<<" "<<res[i][j];
            }
            cout<<endl;
        }
        cout<<endl;
        
    }
    Pascal's Triangle
  • 相关阅读:
    JavaScript String常用方法和属性
    JavaScript null 和 undefined
    document.write()
    MyBatis中的@MapKey注解
    Zookeeper实现分布式锁
    zookeeper相关
    二阶段提交和三阶段提交
    代理模式
    模板方法模式
    策略模式
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3802894.html
Copyright © 2011-2022 走看看