首页 > 编程语言 >算法--2023.2.2

算法--2023.2.2

时间:2023-02-03 00:11:54浏览次数:43  
标签:int right temp -- 2023.2 算法 targetSum null root

1.力扣72--编辑距离

class Solution {
    //典型动态对话问题
    public int minDistance(String word1, String word2) {
        int m = word1.length(), n = word2.length();
        int[][] dp = new int[m+1][n+1];
        //这里需要初始化边界,代表如果另一个字符串未空,我们需要修改多少个字符串才能一致
        for(int i = 0;i<=m;i++){
            dp[i][0] = i;
        }
        for(int i = 0;i<=n;i++){
            dp[0][i] = i;
        }
        //动态规划的遍历
        for(int i = 1;i<=m;i++){
            for(int j = 1;j<=n;j++){
                //如果当前两个字符相等,则从两者前一个、删除+1,插入+1中选一个最小的
                if(word1.charAt(i-1) == word2.charAt(j-1)){
                    dp[i][j] = Math.min(dp[i-1][j-1],Math.min(dp[i-1][j]+1,dp[i][j-1]+1));
                }else{
                    //否则从插入、删除、替换中选一个一个然后加一
                    dp[i][j] = Math.min(Math.min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]) + 1;
                }
            }
        } 
        return dp[m][n];

    }
}

2.力扣112--路径总和

class Solution {
    public int temp = 0;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return false;
        }
        temp += root.val;
        if(temp == targetSum&&root.left==null&&root.right==null){
            return true;
        }
        boolean left = hasPathSum(root.left,targetSum);
        if(root.left!=null){
            temp -= root.left.val;
        }
        boolean right = hasPathSum(root.right,targetSum);
        if(root.right!=null){
            temp -= root.right.val;
        }

        return left||right;

    }
}

3.力扣113--路径总和2

class Solution {
    public Deque<Integer> path;
    public List<List<Integer>> res;
    public int temp;
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        path = new LinkedList<>();
        res = new LinkedList<>();
        temp = 0;
        dfs(root,targetSum);
        return res;
    }
    public void dfs(TreeNode root, int targetSum){
        if(root == null){
            return;
        }
        temp += root.val;
        path.addLast(root.val);
        if(temp == targetSum&&root.left == null&&root.right == null){
            res.add(new LinkedList<>(path));
        }
        dfs(root.left,targetSum);
        if(root.left!=null){
            temp -= root.left.val;
            path.removeLast();
        }
        dfs(root.right,targetSum);
        if(root.right!=null){
            temp -= root.right.val;
            path.removeLast();
        }
    }
}

  

 

  

标签:int,right,temp,--,2023.2,算法,targetSum,null,root
From: https://www.cnblogs.com/lyjps/p/17087803.html

相关文章

  • 内存分析 - 初始
    内存分析-初始内存内存栈堆栈、堆//1.声明数组//在栈中创建一个array的名字,这个时候array是空的,没有实际意义//这个时候的array就相当......
  • 力扣1423. 可获得的最大点数
    几张卡牌 排成一行,每张卡牌都有一个对应的点数。点数由整数数组 cardPoints 给出。每次行动,你可以从行的开头或者末尾拿一张卡牌,最终你必须正好拿 k 张卡牌。你的点......
  • Jmeter 处理MD5加密
    引入MD5加密所需要的jar包。该jar包的名字是:commons-codec-1.9.jarhttps://mvnrepository.com/artifact/commons-codec/commons-codec/1.9 线程组下添加一个前置......
  • Linux五大网络模型之I/O多路复用浅入深出
    网上寻得一篇讲解LinuxI/O模型很好的文章,特此引用。文章摘录自:https://new.qq.com/rain/a/20210610A05G9600正文:基本概念我们先来了解几个基本概念。什么是I/O?所谓......
  • Snowflake 雪花算法补充
    雪花算法,要保持全局唯一,必须要指定唯一的dataCenterId和workerId,正常这两个数都是0-31之间的一个值。如果我们自己的商用节点,应该依赖注册中心,手动的为每隔节点指定......
  • SpringBoot发送虚拟请求~
    1、创建一个测试用的TestController@RestControllerpublicclassTestController{@GetMapping("/test")publicStringtest(){System.out.println(......
  • 树论 基础
    本文包含树的定义,树的存储,树的遍历(包括定义,求法).基础定义我们把\(n\)个点,\(n-1\)条边的图称为树.特别情况对于树,存在部分情况,使其有着特殊的性质......
  • 基本类型
    基本类型整数类型byteshortintlong小数——浮点类型floatdouble字符类型char布尔类型boolean......
  • day02|977.有序数组的平方|209.长度最小的子数组|59.螺旋矩阵II
    977.有序数组的平方leetcode题目:https://leetcode.cn/problems/squares-of-a-sorted-array/description/题目描述:给你一个按非递减顺序排序的整数数组nums,返回每个数......
  • GOROOT、GOPATH、Go Modules 三者的关系介绍
    GOROOTGOROOT路径即为存放Golang语言内建的程序库的所在位置,简单地说就是Golang的安装路径若按照Folang-Downloadandinstall流程,则由goenv命令查询到的结果为GORO......