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

Source: Internet
Author: User
Tags prev
"119-pascal ' s Triangle II (Pascal triangle (Yang Hui's Triangle) II)" " leetcode-interview algorithm classic-java Implementation" "All topic Directory Index" Original title

Given a index k, return the kth row of the Pascal ' s triangle.
For example, given k = 3,
return [1,3,3,1].
  Note:
Could you optimize your algorithm to use only O (k) extra spaces?
The main effect of the topic

Given a positive integer k, the line K of Pascal is obtained.
ideas for 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,
Find the K line.
Code Implementation

Algorithm implementation class

Import java.util.*;

        Number of substantive data for public class Solution {public list<integer> getRow (int rowIndex) {rowindex++;//RowIndex
        if (RowIndex < 0) {return null;

        } list<integer> result = new arraylist<> (rowIndex);
        if (RowIndex >= 1) {result.add (1);
        } if (RowIndex >= 2) {result.add (1); int line = 0; record which line of int prev is currently being used;
            Which line is 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 (int i = 2; i < RowIndex. i++) {line = i% 2;//new calculated data saved in No. 0 or 1th row prev = (i-1 +
2)% 2; Data[line][0] = 1;  Set the first number, you can not, "1" has been done, data[x][0] total 1 for (int j = 1; j < I; J +) {Data[line][j] = Data[prev][j-1] + DATA[PREV][J]; } Data[line][i] = 1;
            Set last number}//Result.clear ();
            for (int i = 0; i < RowIndex i++) {result.add (data[line][i]);
    } return result;
 }
}
Evaluation Results

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

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

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.