【Pascal's Triangle】cpp

题目:

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

代码:

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
            vector<vector<int> > ret;
            if ( numRows<1 ) return ret;
            vector<int> pre, curr;
            for ( int i=0; i<numRows; ++i )
            {
                curr.clear();
                curr.push_back(1);
                for ( int j=0; j<pre.size(); ++j )
                {
                    if ( j==pre.size()-1 ){
                        curr.push_back(1);
                    }
                    else{
                        curr.push_back(pre[j]+pre[j+1]);
                    }
                }
                pre = curr;
                ret.push_back(curr);
            }
            return ret;
    }
};

tips:

数组基本操作。

【Pascal's Triangle】cpp

时间: 2024-11-03 22:11:24

【Pascal's Triangle】cpp的相关文章

leetcode 【 Pascal&#39;s Triangle 】python 实现

题目: 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] ] 代码:oj测试通过 Runtime: 46 ms 1 class Solution: 2 # @return a list of lists of integers 3 def generat

【Pascal&#39;s Triangle II 】cpp

题目: 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? 代码: class Solution { public: vector<int> getRow(int rowIndex) { vector

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: 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? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

【Triangle 】cpp

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

【Jump Game II 】cpp

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j

【Path Sum II】cpp

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 代码: /** * Definition f

【Longest Consecutive Sequence】cpp

题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run

【Linked List Cycle】cpp

题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 代码: 用hashmap版 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next

【Power of Two】cpp

题目: Given an integer, write a function to determine if it is a power of two. 代码: class Solution { public: bool isPowerOfTwo(int n) { if ( n<=0 ) return false; int countOne = 0; for ( int i=0; i<sizeof(n)*8 && countOne<=1; ++i ) { countOne