# CodeTop每日系列三题------------------2021.11.25

LC1143. 最长公共子序列

class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int m = text1.length(),n = text2.length();
int[][] dp = new int[m + 1][n + 1];
for(int i = 1;i <= m;i++){
for(int j = 1;j <= n;j++){
if(text1.charAt(i - 1) == text2.charAt(j - 1)){
dp[i][j] = dp[i - 1][j - 1] + 1;
}else{
dp[i][j] = Math.max(dp[i - 1][j],dp[i][j - 1]);
}
}
}
return dp[m][n];
}
}


LC129. 求根节点到叶节点数字之和
//递归当然，要注意的细节就是根节点到叶节点的数字之和，那么我们每一次的求和都要传给下一个路径的求和，考虑此在递归函数上加入一个temp保存上一次的路径和供下一个递归函数使用。

/**
* 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 int sumNumbers(TreeNode root) {
return helper(root,0);
}

public int helper(TreeNode root,int i){
if(root == null)
return 0;
int temp = i * 10 + root.val;
if(root.left == null && root.right == null)
return temp;
return helper(root.left,temp) + helper(root.right,temp);
}
}


LC155. 最小栈

class MinStack {

private Deque<Integer> auxilary;
private Deque<Integer> minstack;

public MinStack() {
minstack.push(Integer.MAX_VALUE);
}

public void push(int val) {
auxilary.push(val);
minstack.push(Math.min(minstack.peek(),val));
}

public void pop() {
auxilary.pop();
minstack.pop();
}

public int top() {
return auxilary.peek();
}

public int getMin() {
return minstack.peek();
}
}

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。