Pascal ' s Triangle II

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source:http://blog.csdn.net/pistolove/article/details/41851069


Given an index k, return the kth row of the Pascal ' s triangle.

For example, given k = 3,
Return [1,3,3,1] .

Note:
Could optimize your algorithm to use only O (k) extra space?


Ideas:

(1) This algorithm is an improved version of Pascal's triangle. If you know how Pascal's triangle answers, this is a very simple question.

(2) The difference is that the starting position of the problem starts from 0 and requires special attention.

(3) The idea of this article and Pascal's triangle idea is basically the same (Pascal's triangle please see http://blog.csdn.net/pistolove/article/details/41827325 ),Pascal's triangle The result is to give the number of each line, which only gives the number of rows where num is located. This article is done by traversing the 2-num rows (0 rows and 1 lines of special processing), each traversing a row of the number of the row to the list, when the next line of the row is traversed, the next line of numbers to replace the number of the previous row, traverse all rows, and finally get the number in the list is the result.

(4) Note that the space complexity of this article is not O (k), but between O (k) and O (2K), if you want the space complexity of O (k), you need to request a K-size array, and in the process of traversal in order to replace the middle of each line the number, in fact, is not difficult to achieve, interested can be studied under, on their own has improved.

(5) I hope this article will be helpful to you. If you have any questions, I will reply as soon as possible. Thank you.


The algorithm code is implemented as follows:

public static list<integer> getsingle (int num) {list<integer> row = new linkedlist<integer> (); if (num= =0) {row.add (1); return row;} if (num==1) {row.add (1); Row.add (1); return row;} if (num>1) {row.add (1); Row.add (1); for (int i=2; i <= num; i++) {list<integer> temp = new linkedlist<integer& gt; (); for (int j = 0; J < i+1; J + +) {if (j==0) Temp.add (1), if (j==i) Temp.add (1), if (j!=0 && j!=i) {Temp.add (row.get (j-1) +row.get (j));}} Row=temp;}} return row;}


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.