当前位置:网站首页>515. find the maximum value / Sword finger offer II 095 in each tree row Longest common subsequence
515. find the maximum value / Sword finger offer II 095 in each tree row Longest common subsequence
2022-06-25 04:35:00 【Biqiliang】
515. Find the maximum in each tree row 【 Medium question 】【 A daily topic 】
Ideas :【BFS】
BFS The template questions , Pay attention to the root = null The special circumstances of .
Code :
/** * 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;
}
}
The finger of the sword Offer II 095. Longest common subsequence 【 Medium question 】
Ideas :【 Dynamic programming 】
Define second order dp Array ,dp[i][j] Express text1 Before i Characters and text2 Before j The length of the longest common subsequence of characters .
The boundary conditions :i = 0 or j = 0 when ,dp[j][j] = 0
Transfer equation :
set up text1 Of the i The characters are c1,text2 Of the j Characters c2 (i and j from 1 Start )c1 == c2 when dp[i][j] = dp[i-1][j-1] + 1;c2 != c2 when dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
Code :
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
// Record text1 The length of is m, Record text2 The length of is n
int m = text1.length(),n = text2.length();
// Define second order dp Array ,dp[i][j] representative text1 Before i Characters and text2 Before j The length of the longest common subsequence of characters
// The boundary condition is i = 0 or j = 0 when ,dp[0][j] = 0 or dp[i][0] = 0
int[][] dp = new int[m+1][n+1];
for (int i = 1; i <= m; i++) {
// Find out text1 Of the i Characters c1
char c1 = text1.charAt(i-1);
for (int j = 1; j <= n; j++) {
// Find out text2 Of the j Characters c2
char c2 = text2.charAt(j-1);
// Transfer equation
// If c1 == c2 that The first [i,j] Length of the longest common subsequence of the position = The first [i-1][j-1] Length of the longest common subsequence of the position + 1( Common characters )
// namely dp[i][j] = dp[i-1][j-1] + 1
if (c1 == c2){
dp[i][j] = dp[i-1][j-1] + 1;
}else {
// If c1 != c2 that The first [i,j] Length of the longest common subsequence of the position = The first [i-1,j] Length of the longest common subsequence of the position And The first [i,j-1] Length of the longest common subsequence of the position The maximum of both
// namely 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] That is to say text1 and text2 The longest common subsequence length of
return dp[m][n];
}
}
边栏推荐
- CTF_ Web:8-bit controllable character getshell
- CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
- Data view for gbase 8s
- Comparison of towe/ JIRA / tapd / Zen collaboration platforms
- 使用文本分析识别一段文本中的主要性别
- Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
- Can Navicat directly operate the Android database SQLite
- Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
- jsz中的join()
- JS arrow function
猜你喜欢
![Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding](/img/ad/69fce7cf064479a0ddd477fb935de2.png)
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
![L'épée leetcode fait référence au chemin leetcode de l'offre II 091 pour peindre la maison [planification dynamique] heroding](/img/ad/69fce7cf064479a0ddd477fb935de2.png)
L'épée leetcode fait référence au chemin leetcode de l'offre II 091 pour peindre la maison [planification dynamique] heroding

冰冰学习笔记:循环队列的实现

Summary of various problems encountered by cocos2d-x

5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate

【esp32学习之路6——flash加密】

Value transfer between parent and child components of wechat applet

Coinlist queuing tutorial to improve the winning rate

Unit test coverage

Read lsd-slam: large scale direct monolithic slam
随机推荐
CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep
Gbase 8s stored procedure flow control
js中的concat()
CTF_ Web: how to recognize and evaluate a regular expression
GBASE 8s的级联删除功能
CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)
Use of deferred environment variable in gbase 8s
记录小知识点
微信小程序父子组件之间传值
GBASE 8s的并行操作问题场景描述
GBASE 8s 索引B+树
简单的恶意样本行文分析-入门篇
Office macro virus bounce shell experiment
Basic use of OBS browser+ browser
Win10 environment phpstudy2016 startup failure record
Anaconda安装+TensorFlow安装+Keras安装+numpy安装(包含镜像和版本信息兼容问题)
Musk released humanoid robot. Why is AI significant to musk?
A-table mouse over the display hand, the current line can be clicked
Solution of gbase 8s livelock and deadlock
单元测试覆盖率