Leetcode - 杨辉三角II E[119]

news/2024/7/8 7:30:41

问题描述

给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。
示例 1:
        输入: rowIndex = 3
        输出: [1,3,3,1]
示例 2:
        输入: rowIndex = 0
        输出: [1]
示例 3:
        输入: rowIndex = 1
        输出: [1,1]
提示:
    0 <= rowIndex <= 33

方法1

        根据杨辉三角的规律求出前 rowindex 行,然后返回需要的行。

代码

        public List<Integer> getRow(int rowIndex) {
            List<List<Integer>> lists = new ArrayList<>();
            for(int i = 0;i <= rowIndex;i++){
                List<Integer> aa = new ArrayList<>();
                for(int j = 0;j <= i;j++){
                    if(j == 0 || j == i){
                        aa.add(1);
                        continue;
                    }
                    aa.add(lists.get(i - 1).get(j - 1) + lists.get(i - 1).get(j));
                }
                lists.add(aa);
            }
            return lists.get(rowIndex);
        }

执行结果

方法2

        也是根据规律求出前 rowindex 行,然后返回需要的行。但是我们只存储杨辉三角的 一半,在返回的时候对数据进行处理然后返回完整的杨辉三角的一行。

        注意:

                1. 在存储杨辉三角的时候,如何设置每行存储终点下标的设置

                2. array[i - 1][j] 不存在

                3. 还原 返回List 集合 开始还原的下标的设置

代码

        public List<Integer> getRow(int rowIndex) {
            List<List<Integer>> lists = new ArrayList<>();
            for (int i = 0; i <= rowIndex; i++) {
                List<Integer> aa = new ArrayList<>();
                for (int j = 0; j <= i / 2; j++) {
                    if (j == 0) {
                        aa.add(1);
                        continue;
                    }
                    if (lists.get(i - 1).size() < j + 1) {
                        aa.add(lists.get(i - 1).get(j - 1) * 2);
                    } else {
                        aa.add(lists.get(i - 1).get(j - 1) + lists.get(i - 1).get(j));
                    }
                }
                lists.add(aa);
            }
            if (rowIndex > 0) {
                for (int i = (rowIndex + 1) / 2 - 1; i >= 0; i--) {
                    lists.get(rowIndex).add(lists.get(rowIndex).get(i));
                }
                return lists.get(rowIndex);
            }
            return lists.get(0);
        }

执行结果

在计算的时候我们可以只使用一个数组

因为第 i 行 只使用了第 i -1 行的数据,所有采用滚动数组的方式对原先代码进行优化

代码:

            List<Integer> list = new ArrayList<>();
            list.add(1);
            for(int i = 1;i <= rowIndex;i++){
                list.add(0);
                for(int j = i;j > 0;j--){
                    list.set(j,list.get(j) + list.get(j - 1));
                }
            }
            return list;

执行结果和上图差不多


http://www.niftyadmin.cn/n/4557795.html

相关文章

JAVA的 计机二级 那个网站有<南开一百题>买

cid2F35BFABE23E23FFCF1629CD90A9223FB4FAD7DC&t2&fmt- 答案补充 淘宝上多的是 cid8FF7C259950B06053CC5E9A71270E178E387F7AB&t13&fmt-下种子 答案补充 java :http://119.147.41.16/down 迅雷http://119.147.41.16/down

BZOJ 4395: [Usaco2015 dec]Switching on the Lights

4395: [Usaco2015 dec]Switching on the Lights Description Farmer John has recently built an enormous barn consisting of an NNNN grid of rooms (2≤N≤100), numbered from (1,1)up to (N,N). Being somewhat afraid of the dark, Bessie the cow wants to turn on the…

Leetcode - 买卖股票的最佳时机 E[121]

问题描述 给定一个数组 prices &#xff0c;它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 你只能选择 某一天 买入这只股票&#xff0c;并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。 返回你可以从这笔交易中获取的最…

谁能给我介绍一点关于C语言的问题

如果你想找C语言本身的缺陷 找本C语言的脚本 你可以看看这本书《C缺陷与陷阱》 里面有很多题目

Leetcode - 寻找两个正序数组的中位数 H[4]

问题描述 给定两个大小分别为 m 和 n 的正序&#xff08;从小到大&#xff09;数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。示例 1&#xff1a;输入&#xff1a;nums1 [1,3], nums2 [2]输出&#xff1a;2.00000解释&#xff1a;合并数组 [1,2,3] &#x…

VB高手帮忙看看这10道多选择题

1. ABD2. CE3. CD4. AD(钙奶....)5. AC6. BE7. BE8. CD9. CE10. CD

通过格式化字符串漏洞绕过canary

1.1 canary内存保护机制 1.1.1 canary工作原理 canary保护机制类似于/GS保护机制&#xff0c;是Linux下gcc编译器的安全保护机制之一&#xff0c;在栈中的结构如下图所示&#xff1a; 在函数初始化的时候回初始化一个随机的canary值置于缓冲区的末端&#xff0c;在函数返…

配置IIS服务器 - win10

1. 打开控制面板&#xff0c;点击 [ 程序 ] 2. 点击 [ 启动或关闭Windows功能 ] 3. 像如下配置 4. 浏览器上方输入 localhost&#xff0c;出现下方页面便配置成功