码迷,mamicode.com
首页 > 其他好文 > 详细

pascals-triangle-ii

时间:2017-05-31 19:40:41      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:public   str   integer   new   nbsp   turn   int   ret   array   

//递归

public class PascalsTriangleii
{
    public ArrayList<Integer> getRow(int rowIndex)
    {
        ArrayList<Integer> res = new ArrayList<Integer>();
        ArrayList<Integer> item1 = new ArrayList<Integer>();
        ArrayList<Integer> item2 = new ArrayList<Integer>();
        item1.add(1);
        item2.add(1);
        item2.add(1);
        if (rowIndex == 0)
        {
            res = item1;
        }
        else if (rowIndex == 1)
        {
            res = item2;
        }
        else
        {
            ArrayList<Integer> temp = getRow(rowIndex - 1);
            res.add(1);
            for (int i = 0; i < rowIndex - 1; i++)
            {
                res.add(temp.get(i) + temp.get(i + 1));
            }
            res.add(1);
        }
        return res;
    }
}

pascals-triangle-ii

标签:public   str   integer   new   nbsp   turn   int   ret   array   

原文地址:http://www.cnblogs.com/qingtianBKY/p/6925627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!