Golang simple solution


  • 0

    Let's say current is the current row list and prev is previous row. Then
    current[i] = prev[i] + prev[i-1]
    this equation suffices except for current[-1].

    func getRow(rowIndex int) []int {
    	if rowIndex == 0 {
    		return []int{1}
    	} else if rowIndex == 1 {
    		return []int{1, 1}
    	}
    
    	res := make([]int, rowIndex+1)
    	res[0], res[1] = 1, 1
    	for i := 2; i <= rowIndex; i++ {
    		for j := i; j >= 0; j-- {
    			if j == 0 || j == i {
    				res[j] = 1
    				continue
    			}
    
    			res[j] = res[j] + res[j-1]
    		}
    	}
    	return res
    }
    

Log in to reply
 

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