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

链接:  http://leetcode.com/problems/pascals-triangle/

题解:

帕斯卡三角形,按照题意编写就可以了。现在每次写完以后都会去翻一翻discussion,看一看更好的写法。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        if(numRows <= 0)
            return res;
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        res.add(list);
        
        for(int i = 1; i < numRows; i++) {
            ArrayList<Integer> temp = new ArrayList<>();
            temp.add(1);
            
            for(int j = 1; j < res.get(i - 1).size(); j++)
                temp.add(res.get(i - 1).get(j) + res.get(i - 1).get(j - 1));
                
            temp.add(1);
            res.add(temp);
        }
        
        return res;
    }
}

 

Update:

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        ArrayList<Integer> list = new ArrayList<>();
        
        for(int i = 0; i < numRows; i++) {
            list.add(0, 1);
            
            for(int j = 1; j < list.size() - 1; j++)
                list.set(j, list.get(j) + list.get(j + 1));
            
            res.add(new ArrayList<Integer>(list));
        }
        
        return res;
    }
}

 

二刷:

主要还是使用一个list作为辅助,从1 到 numRows开始遍历,先在level末尾加1,使用memorization倒序遍历level就可以了。

Java:

Time Complexity - O(n), Space Complexity - O(n)。     n =  numRows

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        if (numRows < 1) {
            return res;
        }
        List<Integer> level = new ArrayList<>();
        for (int i = 1; i <= numRows; i++) {
            level.add(1);
            for (int j = level.size() - 2; j >= 1; j--) {
                level.set(j, level.get(j - 1) + level.get(j)); 
            }
            res.add(new ArrayList<Integer>(level));
        }
        return res;
    }
}

 

三刷:

跟二刷一样,利用一个buffer  curRow来做memorization。 每次先在curRow尾部加1,然后从curRow.size() - 2倒序遍历到1, 设置curRow.set(j, curRow.get(j) + curRow.get(j + 1)),遍历完比以后加到结果集里,最后返回结果集就可以了。

Java:

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        if (numRows <= 0) {
            return res;
        }
        List<Integer> curRow = new ArrayList<>();
        for (int i = 1; i <= numRows; i++) {
            curRow.add(1);
            for (int j = curRow.size() - 2; j >= 1; j--) {
                curRow.set(j, curRow.get(j) + curRow.get(j - 1));
            }
            res.add(new ArrayList<Integer>(curRow));
        }
        return res;
    }
}

 

Update:

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        if (numRows <= 0) return res;
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= numRows; i++) {
            list.add(1);
            for (int j = list.size() - 2; j > 0; j--) {
                list.set(j, list.get(j) + list.get(j - 1));
            }
            res.add(new ArrayList<>(list));
        }
        return res;
    }
}

 

 

Reference:

https://leetcode.com/discuss/59669/beat-100%25-fastest-java-solution-with-brief-explanation

https://leetcode.com/discuss/20606/my-concise-solution-in-java

posted @ 2015-04-19 00:10  YRB  阅读(621)  评论(0编辑  收藏  举报