"Leetcode-Interview algorithm classic-java Implementation" "119-pascal ' s Triangle II (Pascal triangle (Yang Hui triangle) II)"

Source: Internet
Author: User

"119-pascal ' s Triangle II (Pascal triangle (Yang Hui triangle) II)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

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?

Main Topic

Given a positive integer k, the K-line of Pascal is obtained.

Thinking of solving problems

For any n>0, there are
F (1, N) =1, (n>0)
F (n, N) =1, (N>2)
F (i,j) = f (i-1, j-1) +f (i, j-1), I>2,j>2,
Ask for the K line.

Code Implementation

Algorithm implementation class

Import java.util.*; Public classSolution { PublicList<integer>GetRow(intRowIndex) {rowindex++;//rowindex number of substantive data        if(RowIndex <0) {return NULL; } list<integer> result =NewArraylist<> (RowIndex);if(RowIndex >=1) {Result.add (1); }if(RowIndex >=2) {Result.add (1); }intline =0;//record which line is currently in use        intPrev//What line is on the previous line        if(RowIndex >=3) {int[] data =New int[2][rowindex];//"1"data[0][0] =1; data[1][0] =1; data[1][1] =1; for(inti =2; i < RowIndex; i++) {line = i%2;//New calculated data is saved on line No. 0 or 1thPrev = (I-1+2) %2;//data[line][0] = 1;//Set the first number, no, "1" is already in place, Data[x][0] Total is 1                 for(intj =1; J < I; J + +) {Data[line][j] = data[prev][j-1] + data[prev][j]; } Data[line][i] =1;//Set last number}// Result.clear (); for(inti =0; i < RowIndex;            i++) {Result.add (data[line][i]); }        }returnResult }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47589235"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "119-pascal ' s Triangle II (Pascal triangle (Yang Hui 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.