Leetcode: Pascal's triangle II

Source: Internet
Author: User

1. initialize tri to [1]. When rowindex = 0, the return result is: 1, and the question requirement should be: [1], therefore, the TRI Initialization is [[1], and the returned result is set to Tri [0] to meet the requirements;

2, the first layer loop is from 1 to I traversal, so it is difficult to control the data update, because the update of the J data to use the original tri row of the J-1 data, at this time the first J-1 data has been updated in the previous round;

3. To solve the problem in 2, modify the second-layer loop to traverse from I to 1 (because the first element is always 1 and does not need to be updated)

4. Function usage: range (START, end, step)

1 class solution: 2 # @ return a list of integers 3 def getrow (self, rowindex): 4 tri = [[1] 5 for I in range (1, rowindex + 1): 6 tri [0]. append (0) 7 for J in range (I, 0,-1): 8 if J = I: 9 tri [0] [J] = 110 else: 11 Tri [0] [J] + = tri [0] [J-1] 12 Return tri [0]

 

Leetcode: Pascal's triangle II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.