LeetCode – 118. Pascal’s Triangle

題目連結: https://leetcode.com/problems/pascals-triangle/

// Test case:
// numRows: 1
// numRows: 2
// numRows: 3
    func generate(_ numRows: Int) -> [[Int]] {
        var subArr = [Int]()
        var arr = Array(repeating: subArr, count: numRows)
        for i in 0 ..< arr.count {
            for j in 0 ..< (i + 1) {
                if  j == 0 || j == i {
                    arr[i].append(1)
                } else {
                    let val = arr[i-1][j-1] + arr[i-1][j]
                    arr[i].append(val)
                }
            }
        }
        return arr
    }