-
-
Save NicolaM94/df7b4551a4e737a875a11b9d406fb35f to your computer and use it in GitHub Desktop.
Pascal's triangles algorithm based on an iterative aproach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func pascalTriangleIterative(row int) []int { | |
if row == 1 { | |
return []int{1} | |
} | |
if row == 2 { | |
return []int{1, 1} | |
} | |
out := []int{1, 1} | |
for n := 3; n <= row; n++ { | |
temp := []int{1} | |
for i, j := range out[:len(out)-1] { | |
temp = append(temp, j+out[i+1]) | |
} | |
temp = append(temp, 1) | |
out = temp | |
} | |
return out | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment