力扣118,1920题解

记录

2525.5.6

题目:

思路:

用一个二维数组dp[numRows][numRows]保存每一次动态规划的结果

1.令dp[0][0]=1(第一列)

2.找规律

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

if(col==0){

dp[row][col]=1

} else {

dp[row][col]=dp[row-1][col-1]+dp[row-1][col]

}

代码:

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 的元素等于 nums[nums[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)

相关推荐
军训猫猫头34 分钟前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
success1 小时前
【爆刷力扣-数组】二分查找 及 衍生题型
算法
Orlando cron1 小时前
数据结构入门:链表
数据结构·算法·链表
牛客企业服务2 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
糖葫芦君3 小时前
Policy Gradient【强化学习的数学原理】
算法
向阳@向远方5 小时前
第二章 简单程序设计
开发语言·c++·算法
github_czy5 小时前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁6 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子6 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z6 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法