Pascal's Triangle I II

想清楚了,安排好细节不难

public class Solution {
    public ArrayList<ArrayList<Integer>> generate(int numRows) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(numRows<=0) return res;
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        tmp.add(1);
        res.add(tmp);
        for(int i=1;i<numRows;i++){ // current row to be added
            ArrayList<Integer> r = new ArrayList<Integer>();
            r.add(1);
            ArrayList<Integer> t = res.get(i-1);
            for(int j=1;j<i;j++){

                r.add(t.get(j)+t.get(j-1));
            }
            r.add(1);
            res.add(r);
        }
        return res;
    }
}

II

public class Solution {
    public ArrayList<Integer> getRow(int rowIndex) {
        ArrayList<Integer> res = new ArrayList<Integer>();
        if(rowIndex<0) return res;
        res.add(1);
        for(int i=1;i<=rowIndex;i++){
            ArrayList<Integer> cur = new ArrayList<Integer>();
            cur.add(1);
            for(int j=1;j<i;j++){
                cur.add(res.get(j)+res.get(j-1));
            }
            cur.add(1);
            res =cur;
        }
        return res;
    }
}

Pascal's Triangle I II

时间: 2024-10-16 08:56:58

Pascal's Triangle I II的相关文章

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

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] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

【leetcode】Pascal&#39;s Triangle I &amp; II (middle)

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] ] 思路:杨辉三角,直接按规律生成即可 vector<vector<int> > generate(int numRows) { vector<vector<int>>

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? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 5 vect

Java [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]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

Pascal&#39;s Triangle II 解答

Question Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Solution Similar with Pascal's Triangle. Note that the index starts from 0. 1 public class Solution { 2 public List<Integer> getRow(in

[leedcode 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]. public class Solution { public List<Integer> getRow(int rowIndex) { //由于需要O(k)空间,因此需要借助两个数组保存中间值,并交换两个数组,注意交换的方法! List<Integer> list=new

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

Pascal&#39;s Triangle II 杨辉三角

1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 vector<vector<int>> tri; 5 if(rowIndex==0) 6 { 7 vector<int> c; 8 c.push_back(1); 9 return c; 10 } 11 vector<int> b; 12 b.push_back(0); 13 b.push_back(1); 14