Some notes about libgit2/rugged
Go Channel Practice I

Leetcode - Pascal's Triangle

violet posted @ Apr 07, 2020 06:21:31 AM in 笔记 with tags Algorithm Golang array , 215 阅读

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

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

 

func generate(numRows int) [][]int {
    result := [][]int{}
    for i := 0; i < numRows; i++ {
        row := make([]int, i+1)
        for j := 0; j < len(row); j++ {
            row[j] = 1
        }
        if len(result) == 0 {
            result = append(result, row)
            continue
        }
        last := result[len(result)-1]
        for j := 1; j < len(last); j++ {
            row[j] = last[j-1] + last[j]
        }
        result = append(result, row)
    }
    return result
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter