当前位置:网站首页>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];
}
}
边栏推荐
- Laravel document sorting 11. System architecture
- BSC smart contract dividend mainstream currency | including marketing wallet | deflation | reflow | dividend free token | available for direct deployment
- GbASE 8s中的Blob 页(Blobspace page)
- Lecture record: data processing methods and applications of various spatial geodetic techniques
- php开发支付宝支付功能之扫码支付流程图
- GBase 8s 锁的分类
- GBASE 8s的数据导入和导出
- LabVIEW开发气体调节器
- Detailed explanation of flex attributes in flex layout
- CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep
猜你喜欢
GBASE 8s 索引B+树
cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
A detailed summary of four handshakes (or four waves) over TCP connections
机器学习深度学习——向量化
CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
Read lsd-slam: large scale direct monolithic slam
论文笔记: 多标签学习 ESMC (没看懂, 还没写出来, 暂时放这里占个位置)
Coinlist queuing tutorial to improve the winning rate
Lecture record: data processing methods and applications of various spatial geodetic techniques
LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
随机推荐
Laravel document sorting 3. CSRF protection
A-table mouse over the display hand, the current line can be clicked
论文笔记: 多标签学习 ESMC (没看懂, 还没写出来, 暂时放这里占个位置)
GBASE 8s 索引B+树
Laravel document sorting 7. View
cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
GBASE 8s的触发器
Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'
BSC smart contract dividend mainstream currency | including marketing wallet | deflation | reflow | dividend free token | available for direct deployment
Simple text analysis of malicious samples - Introduction
Finereport displays and hides column data according to conditions
CTF_ Web: Advanced questions of attack and defense world expert zone WP (19-21)
Doubts about judging the tinyint field type of MySQL
第二十五周记录
什么是持久化?redis 持久化中的RDB和AOF是什么?
CTF_ Web:php weak type bypass and MD5 collision
unity Quad剔除背面并剔除透明部分的shader
Shutter fittedbox component
kenlm
Classification of gbase 8s locks