zoukankan      html  css  js  c++  java
  • 【leetcode】118. Pascal's Triangle

    @requires_authorization
    @author johnsondu
    @create_time 2015.7.23 19:54
    @url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
    /************************
     * @description: simple.
     * @time_complexity:  O(n)
     * @space_complexity: O(n)
     ************************/
     class Solution {
    public:
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> ans;
    
            for(int i = 1; i <= numRows; i ++) {
                vector<int> layer;
                if(i == 1) layer.push_back(1);
                else {
                    for(int j = 1; j <= i; j ++) {
                        if(j == 1 || j == i)  layer.push_back(1);
                        else layer.push_back(ans[i-2][j-2] + ans[i-2][j-1]);
                    }
                }
                ans.push_back(layer);
            }
            return ans;
        }
    };
    @requires_authorization
    @author johnsondu
    @create_time 2015.7.23 19:54
    @url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)
    /************************
     * @description: simple.
     * @time_complexity:  O(n)
     * @space_complexity: O(n)
     ************************/
    class Solution {
    public:
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> ans;
            if(numRows < 1) return ans;
    
            vector<int> first;
            first.push_back(1);
            ans.push_back(first);
            if(numRows < 2) return ans;
    
            vector<int> second;
            second.push_back(1);
            second.push_back(1);
            ans.push_back(second);
    
            for(int i = 3; i <= numRows; i ++) {
                vector<int> layer;
                layer.push_back(1);
                for(int j = 0; j < ans[i-2].size()-1; j ++) {
                    layer.push_back(ans[i-2][j] + ans[i-2][j+1]);
                }
                layer.push_back(1);
                ans.push_back(layer);
            }
            return ans;
        }
    };
  • 相关阅读:
    poj 2187 Beauty Contest (凸包)
    poj 1584 A Round Peg in a Ground Hole(计算几何)
    poj 1039 Pipe (计算几何)
    struts2开篇
    Action的内容
    输入校验
    文件上传
    Struts.xml内容
    .NET资源推荐:数据持久层
    线程间操作无效: 从不是创建控件“...”的线程访问它。
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7236256.html
Copyright © 2011-2022 走看看