My Swift Solution Easy to Understand


  • 0
    Y
    func generate(_ numRows: Int) -> [[Int]] {
        var ans:[[Int]] = [[1], [1,1]]
        if numRows <= 0 { return [] }
        if numRows == 1 { return [[1]] }
        for i in 1..<numRows-1{
            var temp:[Int] = [1,1]
            for j in 0..<ans[i].count{
                if j+1<ans[i].count{
                    temp.insert(ans[i][j] + ans[i][j+1], at: 1)
                }
            }
            ans.append(temp)
        }
        return ans
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.