首页 > 其他分享 >Day 20 回溯法part02| LeetCode 39. 组合总和 ,40.组合总和II,131.分割回文串

Day 20 回溯法part02| LeetCode 39. 组合总和 ,40.组合总和II,131.分割回文串

时间:2024-09-22 17:34:26浏览次数:1  
标签:39 target 组合 int res List candidates new 总和

39. 组合总和

39. 组合总和

 class Solution {
        public List<List<Integer>> res = new ArrayList<>();
        public List<Integer> path = new LinkedList<>();

        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            backtracking(candidates, target, 0);
            return res;
        }

        void backtracking(int[] candidates, int target, int startIndex) {
            if (target < 0)
                return;
            if (target == 0) {
                res.add(new ArrayList<>(path));
                return;
            }
            //单层搜索

            for (int i = startIndex; i < candidates.length ; i++) {

                path.add(candidates[i]);
                //target -= candidates[i];
                backtracking(candidates, target - candidates[i],i );
                int size = path.size();
                path.remove(size - 1);
               // target += candidates[i];
            }

        }


    }

剪枝

 class Solution {
        public List<List<Integer>> res = new ArrayList<>();
        public List<Integer> path = new LinkedList<>();

        public List<List<Integer>> combinationSum(int[] candidates, int target) {

            //排序
            Arrays.sort(candidates);
            backtracking(candidates, target, 0);
            return res;
        }

        void backtracking(int[] candidates, int target, int startIndex) {
            if (target < 0)
                return;
            if (target == 0) {
                res.add(new ArrayList<>(path));
                return;
            }
            //单层搜索

            for (int i = startIndex; i < candidates.length ; i++) {

                //剪枝优化
                if(target<0) break;
                path.add(candidates[i]);
                //target -= candidates[i];
              
                backtracking(candidates, target - candidates[i],i );
                int size = path.size();
                path.remove(size - 1);
                // target += candidates[i];
            }

        }


    }

40.组合总和II

40. 组合总和 II

class Solution {
        public  List<Integer> path=new LinkedList<>();
        public  List<List<Integer>> res=new ArrayList<>();


        public List<List<Integer>> combinationSum2(int[] candidates, int target) {

            int l=candidates.length;
//            boolean [] used=new boolean[][l];
            Arrays.sort(candidates);
            backtracking(candidates,target,0);
            return res;
        }
        void backtracking(int[] candidates, int target,int startIndex)
        {

            if(target<0)return;
            if(target==0)
            {
                res.add(new ArrayList<>(path));
                return;
            }
            for(int i=startIndex;i<candidates.length;i++)
            {
                //去重,不使用used数组
                if(i>startIndex&& candidates[i]==candidates[i-1]) continue;

                //剪枝
                if(target<0) break;
                path.add(candidates[i]);
                backtracking(candidates,target-candidates[i],i+1);
               int size=path.size();
               path.remove(size-1);
            }
        }
    }

131.分割回文串

131. 分割回文串

  class Solution {
        List<List<String>> res=new ArrayList<>();
        List<String> path=new LinkedList<>();
        public List<List<String>> partition(String s) {

            backtracking(s,0,new StringBuilder());
            return res;
        }
        void backtracking(String s,int startIndex,StringBuilder b)
        {
            if(startIndex==s.length())
            {
                res.add(new ArrayList<>(path));
                return ;
            }


            for(int i=startIndex;i<s.length();i++)
            {
                b.append(s.charAt(i));
                if(isPalindrome(b))
                {
                        path.add(b.toString());
                        backtracking(s,i+1,new StringBuilder());
                        path.remove(path.size()-1);
                }
            }
        }

        boolean isPalindrome(StringBuilder b)
        {

            for(int i=0,j=b.length()-1;i<j;i++,j--)
            {
                if(b.charAt(i)!=b.charAt(j))
                    return false;
            }
            return true;
        }
    }

标签:39,target,组合,int,res,List,candidates,new,总和
From: https://www.cnblogs.com/FreeDrama/p/18425572

相关文章

  • U389139 至少有一位重复的数字-题解
    题目传送门一、举例说明以\(654923\)为例要判断在\([0,654923]\)区间至少有一位重复的数值的数,可以考虑其补集,即\(\color{red}所有位数均不重复的数\),用\(N\)减去补集即为结果。首先可以将其分为两种情况。情况一,位数小于\(6\)位。所有位数均不重复的有\(9\time......
  • 帝国cms安装问题Cann't connect to DB!解决办法
    当你在安装帝国CMS时遇到“Cann'tconnecttoDB!”的问题,这通常意味着PHP脚本无法连接到数据库。这种情况可能是由多种因素引起的,包括数据库服务未运行、数据库配置错误、网络问题等。解决方法1.检查数据库服务状态确认MySQL服务是否运行如果是在本地开发环境中,检查是否......
  • 通过组合使用这些工具,您可以实现灵活的 WIM 备份和恢复方案。每个工具都有其特定功能,
    使用Windows的WIM(WindowsImagingFormat)备份和恢复可以通过命令行工具DISM(DeploymentImagingServiceandManagementTool)来实现。以下是一些常用的WIM备份和恢复命令参数示例:1. 备份(Capture)使用dism命令将系统映像备份为WIM文件:bashCopyCodedism/Cap......
  • ipsec+nat组合
    1、网络拓扑图2、需要注意的关键技术nat转换中需要排除ipsec网络R1端:aclnumber3000rule5denyipsource192.168.1.00.0.0.255destination172.16.1.00.0.0.255rule10permitIPsource192.168.1.00.0.0.255destinationanyintg0/0/0natoutbound3000R3端:aclnumber3......
  • C语言中if else组合
    一bool变量与“零值”进行比较bool变量与“零值”进行比较的if语句怎么写?boolbTestFlag=FALSE;//想想为什么一般初始化为FALSE比较好?A),if(bTestFlag==0);if(bTestFlag==1);B),if(bTestFlag==TRUE);if(bTestFlag==FLASE);C),if(bTestFlag);if(!bT......
  • shell中$后加引号有什么用($"string"和$'string')
    (1).如果没有特殊定制bash环境或有特殊需求,$"string"和"string"是完全等价的,使用$""只是为了保证本地化。以下是manbash关于$""的解释:Adouble-quotedstringprecededbyadollarsign($"string")willcausethestringtobetranslatedaccordi......
  • Can't connect to local MySQL server through socket
    mysql-urootERROR2002(HY000):Can'tconnecttolocalMySQLserverthroughsocket'/tmp/mysql.sock'(2)这是mysql登录时找不到套接字的问题。首先需要明白的是,Linux端的mysqlserver启动时会开启一个socket,Linux上的MySQL的客户端在不使用IP连接时mysqlserver时,默认......
  • Provide the path to the executable if it can't be found by the app, shim executa
    Ifyourappcan'tfindtheNode.jsexecutable,andyoureceiveamessagesayingthat"shimexecutablesarenotsupported,"youwillneedtomanuallyprovidethefullpathtotheNode.jsexecutable.HerearethestepstofindtheNode.jsexe......
  • 代码随想录训练营第39天|树形dp
    198.打家劫舍classSolution{public:introb(vector<int>&nums){nums.insert(nums.begin(),0);intn=nums.size();vector<int>dp(n,INT_MIN);dp[0]=0;dp[1]=nums[1];for(inti=2;i<n;i++......
  • 在 Effect-TS 中组合选项:实用指南
    effect-ts提供了几种在函数式编程上下文中组合可选值或选项的强大方法。无论您想要将多个选项配对在一起还是将选项内的函数应用于其他值,该库都提供了多种方法来简化这些操作。在本文中,我们将探讨组合选项的四个关键函数:o.product、o.productmany、o.all和o.ap。示例1:使......