leecode day3 杨辉三角(2)

这篇博客介绍了在LeetCode的第2题中,如何利用ArrayList解决杨辉三角的问题。作者讨论了ArrayList作为动态数组的优势,如动态增删元素、实现ICollection和IList接口等,并分享了尝试使用ArrayList时遇到的困惑,最终转向使用#vector#成功解决问题,认识到vector在构建二维数组时的灵活性和C++的面向对象特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

leecode2 杨辉三角

1.背景知识

  • 什么是ArrayList
    ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处:
    动态的增加和减少元素
    实现了ICollection和IList接口
    灵活的设置数组的大小

  • 如何使用ArrayList

  • 最简单的例子:

class Solution {
public:
        public ArrayList<Integer> getRow(int rowIndex) {
        ArrayList<Integer> last=new ArrayList<Integer>;
        last.add(1);
        if(rowIndex==0){
            return last;
        }
        for(int i;i<rowIndex;i++){
        ArrayList<Integer> tmp=new ArrayList<Integer>;
        tmp.add(1);
        for(int j;j<i;j++){
        tmp.add(last.get(j),last.get(j-1));
        }
        tmp.add(1)  
        last=tmp;
        return last;
    }


    }
};

但是好像不能通过.(我是看的别人的博客仿照的哇。。。。哇的哭啦。。。??
于是我决定重新开始使用 # vector #

class Solution {
    public:
        vector<int> getRow(int rowIndex){
        vector<int> res;
        res.push_back(1);
        vector<int> col;
        for(int i=1;i<=rowIndex;i++){
            col.push_back(1);
            for(int j=1;j<i;j++){
                col.push_back(res[j-1]+res[j]);
                
            }
            col.push_back(1);
            res=col;
            col.clear();
        }
        return res;
    }


};

AC啦!!!
思考:我这次的感悟是 vector很灵活,2维的数组可以像拼拼图一样可以把东西拼好~vector是手段~ c++是面向对象的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值