力扣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)

相关推荐
IronMurphy5 小时前
【算法四十三】279. 完全平方数
算法
墨染天姬5 小时前
【AI】Hermes的GEPA算法
人工智能·算法
papership6 小时前
【入门级-数据结构-3、特殊树:完全二叉树的数组表示法】
数据结构·算法·链表
smj2302_796826526 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
Beginner x_u7 小时前
链表专题:JS 实现原理与高频算法题总结
javascript·算法·链表
wxy不爱写代码7 小时前
C++多线程
面试·职场和发展
野生技术架构师9 小时前
金三银四面试总结篇,汇总 Java 面试突击班后的面试小册
java·面试·职场和发展
_深海凉_10 小时前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
ja哇10 小时前
大厂面试高频八股
java·面试·职场和发展
踩坑记录10 小时前
leetcode hot100 寻找两个正序数组的中位数 hard 二分查找 双指针
leetcode