2 minutes
Leetcode 118
The idea of this solution is pretty simple:
- We first have a matrix array called
res
- Then we loop through
numRows
- While iterating through
numRows
we have to make an array calledtemp
. It is going to be our current row. - We make
temp[0]
andtemp[len(temp)]
equal to1
so we get the outer1
’s. - Then we loop through the middle elements (basically excluding the outside
1
’s) - We have to make every
temp[j]
equal to the previous rowsprevious rows[j - 1] + previous rows[j]
. We doj - 1
andj
. If you don’t understand, why look at the image below: - Then we append
temp
tores
- After iterating, we can return
res
.
The Code:
We can Take Out Temp And Get:
Read other posts