LeetCode 题解(8):Pasical'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]
]

题解:

class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        vector<vector<int>> triangle;
        vector<int> temp;
        if(numRows == 0)
            return triangle;
        triangle.push_back(temp);
        triangle[0].push_back(1);
        for(int i = 1; i < numRows; i++)
        {
            triangle.push_back(temp);
            for(int j = 0; j < i + 1; j++)
            {
                if(j - 1 < 0)
                    triangle[i].push_back(triangle[i-1][j]);
                else if(j >= i)
                    triangle[i].push_back(triangle[i-1][j-1]);
                else
                    triangle[i].push_back(triangle[i-1][j-1] + triangle[i-1][j]);
            }
        }
        return triangle;
    }
};

八个月后再解:

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

Java版:

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(numRows == 0)
            return result;
        List<Integer> pre = new ArrayList<Integer>();
        pre.add(1);
        result.add(pre);
        for(int i = 1; i < numRows; i++) {
            List<Integer> current = new ArrayList<Integer>();
            for(int j = 0; j <= i; j++) {
                if( j == 0 )
                    current.add(pre.get(0));
                else if( j == i )
                    current.add(pre.get(j-1));
                else
                    current.add(pre.get(j-1) + pre.get(j));
            }
            result.add(current);
            pre = current;
        }
        return result;
    }
}

Python版:

class Solution:
    # @return a list of lists of integers
    def generate(self, numRows):
        result = []
        if numRows == 0:
            return result
        pre = [1]
        result.append(pre)
    
        if numRows == 1:
            return result
    
        for i in range(1, numRows):
            temp = []
            for j in range(0, i+1):
                if j == 0:
                    temp.append(pre[0])
                elif j == i:
                    temp.append(pre[j-1])
                else:
                    temp.append(pre[j-1] + pre[j])
            result.append(temp)
            pre = temp
        return result


你可能感兴趣的:(Algorithm,LeetCode,Triangle,Pasicals)