力扣118,1920题解

记录

2525.5.6

题目:

思路:

用一个二维数组dpnumRowsnumRows保存每一次动态规划的结果

1.令dp00=1(第一列)

2.找规律

3.得到如下规律(以下情况均为列数大于1)

if(col==0){

dprowcol=1

} else {

dprowcol=dprow-1col-1+dprow-1col

}

代码:

java 复制代码
class Solution {
    public List<List<Integer>> generate(int numRows) {
		List<List<Integer>> result=new ArrayList<List<Integer>>();
		int[][] dp=new int[numRows][numRows];
		dp[0][0]=1;
		for (int i = 1; i < numRows; i++) {
			for (int j = 0; j <=i; j++) {
				if(j==0)dp[i][j]=1;
				else dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
			}
		}
		for (int i = 0; i < dp.length; i++) {
			List<Integer> tmp=new ArrayList<>();
			for (int j = 0; j < dp.length; j++) {
				if(dp[i][j]==0) break;
				tmp.add(dp[i][j]);
			}
			result.add(new ArrayList<>(tmp));
		}
		
		return result;
    }
}

复杂度:

O(N2

O(N2

题目:

思路:

构建一个与原数组 nums 等长的新数组,同时令新数组中下标为 i 的元素等于 numsnums\[i]。

代码:

java 复制代码
class Solution {
    public int[] buildArray(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n];
        for (int i = 0; i < n; ++i) {
            ans[i] = nums[nums[i]];
        }
        return ans;
    }
}

复杂度:

O(N)

O(N)

相关推荐
数模竞赛Paid answer8 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆8 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
JL159 小时前
面试项目被问到自闭-如何把课设包装成亮眼项目
面试·职场和发展
想吃火锅100510 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
Nil20811 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣11 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论
qeen8714 小时前
【数据结构】自平衡二叉搜索树各种旋转算法原理解析及AVL树的C++实现
数据结构·c++·算法
lucas_AI14 小时前
1.2B 小模型赢过 235B 大模型:NaviDC-OCR 把文档解析卷明白了
人工智能·深度学习·算法
冻柠檬飞冰走茶15 小时前
PTA基础编程题目集 7-35有理数均值(C++语言实现)
开发语言·数据结构·c++·算法·均值算法