congr / world

2 stars 1 forks source link

LeetCode : 119. Pascal's Triangle II #497

Closed congr closed 5 years ago

congr commented 5 years ago

https://leetcode.com/problems/pascals-triangle-ii/

image

congr commented 5 years ago

use only O(k) extra space

class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> list = new ArrayList();

        for (int i = 0; i <= rowIndex; i++) {
            list.add(0, 1);  // !!!!!!!!!!! 11 -> (1)11 => 121
            for (int j = 1; j < i; j++) {
                list.set(j, list.get(j) + list.get(j+1)); // !!
            }
            System.out.println(list);
        }

        return list;
    }
}
congr commented 5 years ago

image