当前位置:网站首页>515. 在每个树行中找最大值 / 剑指 Offer II 095. 最长公共子序列
515. 在每个树行中找最大值 / 剑指 Offer II 095. 最长公共子序列
2022-06-25 03:58:00 【彼淇梁】
515. 在每个树行中找最大值【中等题】【每日一题】
思路:【BFS】
BFS模板题,注意把root = null的特殊情况排除掉。
代码:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null){
return list;
}
Queue<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()){
int size = queue.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
max = Math.max(max,node.val);
if (node.left != null){
queue.offer(node.left);
}
if (node.right != null){
queue.offer(node.right);
}
}
list.add(max);
}
return list;
}
}
剑指 Offer II 095. 最长公共子序列【中等题】
思路:【动态规划】
定义二阶dp数组,dp[i][j]表示text1的前i个字符与text2的前j个字符的最长公共子序列的长度。
边界条件:i = 0 或 j = 0 时,dp[j][j] = 0
转移方程:
设text1的第i个字符为 c1,text2的第j个字符 c2 (i 和j从1开始)c1 == c2 时dp[i][j] = dp[i-1][j-1] + 1;c2 != c2时dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
代码:
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
//记录text1的长度为m,记录text2的长度为n
int m = text1.length(),n = text2.length();
//定义二阶dp数组,dp[i][j] 代表 text1的前i个字符与text2的前j个字符能构成的最长公共子序列的长度
//边界条件为 i = 0 或 j = 0 时,dp[0][j] = 0 或 dp[i][0] = 0
int[][] dp = new int[m+1][n+1];
for (int i = 1; i <= m; i++) {
//求出text1的第 i 个字符 c1
char c1 = text1.charAt(i-1);
for (int j = 1; j <= n; j++) {
//求出text2的第 j 个字符 c2
char c2 = text2.charAt(j-1);
//转移方程
//如果 c1 == c2 那么 第[i,j]位置的最长公共子序列长度 = 第[i-1][j-1]位置的最长公共子序列长度 + 1(公共字符)
//即 dp[i][j] = dp[i-1][j-1] + 1
if (c1 == c2){
dp[i][j] = dp[i-1][j-1] + 1;
}else {
//如果 c1 != c2 那么 第[i,j]位置的最长公共子序列长度 = 第[i-1,j]位置的最长公共子序列长度 与 第 [i,j-1]位置的最长公共子序列长度 两者的最大值
//即 dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1])
dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
}
}
}
//dp[m][n]即为text1和text2的最长公共子序列长度
return dp[m][n];
}
}
边栏推荐
- Flutter Builder & FutureBuilder组件
- 95% of programmers fish here
- Openmmlab environment configuration
- Synchronous and asynchronous functions (callback function, promise, generator, async/await)
- Although the Internet in the traditional sense has long ceased to exist, this does not mean that the Internet has long disappeared
- Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
- [proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
- Vigilance against over range collection of privacy - ten mobile app violations
- 【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
- 升级cmake
猜你喜欢

1. Phase II of the project - user registration and login

Lecture record: new application of inertial navigation - inertial measurement

MySQL插入过程报错1062,但是我没有该字段。

1. first knowledge of chromatic harmonica
![LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路](/img/ad/69fce7cf064479a0ddd477fb935de2.png)
LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路

Development of trading system (VIII) -- Construction of low delay network

代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺

Mathematical analysis_ Notes_ Chapter 3: limits
![[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube](/img/28/33f3e9736a68439b5bcdc4e75c939c.png)
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube

讲座记录《捷联惯导解算的历史及发展》
随机推荐
Synchronous and asynchronous functions (callback function, promise, generator, async/await)
Where is the red area of OpenCV?
【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
How many images can opencv open?
acmStreamOpen返回值问题
1.初识半音阶口琴
Laravel document sorting 10. Request life cycle
地方/園區產業規劃之 “ 如何進行產業定比特 ”
Is opencv open source?
Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!
CMD operation MySQL in Windows
UCLA | 用于黑盒优化的生成式预训练
WMS仓储管理系统的使用价值,你知道多少
文本关键词提取:ansj
The 5th series of NFT works of missing parts was launched on the sandbox market platform
Sourcetree pulls the code and prompts to fill in authentic, but the configuration cannot change the user
Numpy NP tips: use OpenCV to interpolate and zoom the array to a fixed shape cv2 resize(res, dsize=(64, 64), interpolation=cv2. INTER_ CUBIC)
Doubts about judging the tinyint field type of MySQL
The yii2 debug toolbar is missing
AI quantitative transaction (II) -- tushare financial data framework