Learning GoLang: Generate a Pascal Triangle
Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]Example 2:
Input: numRows = 1
Output: [[1]]
Generate a Pascal Triangle using GoLang
First declare a 2-dimension array with N rows, then fill each row, both ends are 1 and the internal value is equal to the sum of both numbers on its shoulder (Dynamic Programming Algorithm).
func generate(numRows int) [][]int {
var res = make([][]int, numRows)
for i := 0; i < numRows; i ++ {
res[i] = make([]int, i + 1)
res[i][0], res[i][i] = 1, 1
for j := 1; j < i; j ++ {
res[i][j] = res[i - 1][j] + res[i - 1][j - 1]
}
}
return res
}
Another GoLang Implementation that appends row by row (beware that we need to deepcopy each row):
func generate(numRows int) [][]int {
var res [][]int
var cur []int
cur = append(cur, 1)
for i := 0; i < numRows; i ++ {
res = append(res, append([]int(nil), cur...))
for j := i; j > 0; j -- {
cur[j] += cur[j - 1]
}
cur = append(cur, 1)
}
return res
}
Pascal Triangle Implementations:
- Teaching Kids Programming – Pascal Triangle Algorithms and Applications
- Coding Exercise – Pascal Triangle II – C++ and Python Solution
- How to Print Pascal Triangle in C++ (with Source Code)
- Compute the Nth Row of a Pascal's Triangle using Dynamic Programming Algorithm
- GoLang: Generate a Pascal Triangle
Reposted to Blog
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Important Update of Delegation Service!
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses

A greater move and job, you are have done so far sir.
that concept is really good, i love it.
hello sir @justyy, again you missed my post to upvote. I'm a delegator (66K SP) and I only post once in 24 hours.
here my post link : https://steemit.com/hive-129948/@rme/4fmfwq-grayscale-photography
UPDATE: got upvoted. the issue has been resolved. thanks :)
done.
thank you very much sir :)