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

Array——LeetCode——Two SumPascal's Triangle

时间:2018-09-13 12:05:59      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:java   自己的   code   nbsp   div   tco   方法   new   static   

【学到的知识点——ArrayList是一个对象】
1、赋值后,赋值给他的ArrayList改变,被赋值的ArrayList跟着改变
【学到的知识点——ArrayList的几种遍历方法】
1、一般for循环遍历
2、增强for循环遍历
3、迭代器遍历
【学到的知识点——ArrayList只能依次add】
1、查看底层代码
【学到的知识点——ArrayList can‘t convert to List】
1、List<List<Integer>> result = new ArrayList<List<Integer>>() 可行
2、List<List<Integer>> result = new List<List<Integer>>() 不可行

3、List<List<Integer>> result = new ArrayList<ArrayList<Integer>>();不可行
4、List<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();可行
-----------------------------------------------------------------------------------------------------
【反思】

-----------------------------------------------------------------------------------------------------
【别人的Java解法代码】

-----------------------------------------------------------------------------------------------------
【自己的Java解法代码】

    public static List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> lastList = new ArrayList<Integer>();        //上一个List
        int num = 0;
        for (int i =0; i < numRows; i++) {        //排数
            ArrayList<Integer> tmp = new ArrayList<Integer>();        //当前的List
            if (i > 1) {    //i-1个数
                tmp.add(1);
                for (int j = 0; j < i-1; j++) {
                    num = lastList.get(j) + lastList.get(j+1);
                    tmp.add(num);
                }
                tmp.add(1);
                lastList = tmp;
                result.add(tmp);
            }
            if (i == 0) {
                tmp.add(1);
                result.add(tmp);
            }
            if (i == 1) {
                tmp.add(1);
                tmp.add(1);
                lastList = tmp;
                result.add(tmp);
            }
        }
        return result;
    }

 

Array——LeetCode——Two SumPascal's Triangle

标签:java   自己的   code   nbsp   div   tco   方法   new   static   

原文地址:https://www.cnblogs.com/Dbbf/p/9639407.html

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