[leetcode]119 Pascal ' s Triangle II

Source: Internet
Author: User

https://oj.leetcode.com/problems/pascals-triangle-ii/

http://blog.csdn.net/linhuanmars/article/details/23311629

Public class solution {    public list<integer> getrow (int  rowindex)      {        // solution  a:        // return getrow_okspace (RowIndex);                 // solution  b:        return getrow_extralist (RowIndex);     }        /////////////////////////      Solution A: OKSpace    //    private List< Integer> getrow_okspace (Int rowindex)     {         if  (rowindex < 0)              return null;                     //  calculates the value of each location directly,        //  but when you calculate the value for a single location, you need to iterate         List<Integer> toReturn = new  Arraylist<> (rowindex + 1);        for  (int i  = 0 ; i <= rowindex ; i ++)          {            toreturn.add (Val ( Rowindex, i));        }         return toReturn;    }        //  Calculate the value of row r, index i.    // row  and index are&nBsp;0 based.    // recursive    private int val ( Int r, int i)     {        //  out case        if  (i < 0 | | &NBSP;I&NBSP;&GT;&NBSP;R)             return 0 ;                 // first  row        if  (r == 1)              return 1;                 else return val (r - 1, i -  1)  + val (r - 1, i);    }         //////////////////////    // solution b: extralist    //     public list<integer> getrow_extralist (Int rowindex)     {         if  (rowindex < 0)              return null;                 List<Integer> toReturn =  Collections.singletonlist (1);        for  (int i =  0 ; i < rowindex ; i ++)          {            toreturn = getrow (ToReturn) ;        }        return  Toreturn;    }&nbSp;       private list<integer> getrow (List<Integer>  lastrow)     {        int len =  lastrow.size ()  + 1;        List<Integer>  Toreturn = new arraylist<> (len);        for  (int i = 0 ; i < len ; i ++)          {            int a  = i - 1 < 0 ? 0 : lastrow.get (i - 1);             int b = i == len  - 1 ? 0 : lastrow.get (i);             toreturn.add (A&NBSP;+&NBSP;B);        }         return toreturn;    }}


[leetcode]119 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.