Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created August 12, 2020 07:54
Show Gist options
  • Select an option

  • Save RP-3/6dd9d22c93780d76d514e3731cb9006f to your computer and use it in GitHub Desktop.

Select an option

Save RP-3/6dd9d22c93780d76d514e3731cb9006f to your computer and use it in GitHub Desktop.
func getRow(rowIndex int) []int {
if rowIndex == 0 { return []int{1} }
if rowIndex == 1 { return []int{1, 1} }
result, next := []int{1, 1}, []int{}
for i := 2; i <= rowIndex; i++ { // for each row
for j := 0; j <= len(result); j++ { // generate it based on the prev row
var prev, curr int
switch j {
case 0: // handle left edge
prev, curr = 0, result[0]
case len(result): // handle right edge
prev, curr = result[j-1], 0
default: // all other cases
prev, curr = result[j-1], result[j]
}
next = append(next, prev+curr)
}
result = next
next = []int{}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment