首页 > 编程语言 >第二十七天 | 回溯算法

第二十七天 | 回溯算法

时间:2022-11-08 15:38:21浏览次数:60  
标签:return target int list 第二十七 算法 candidates 回溯 backTracking

第二十七天,继续回溯算法

 

39. 组合总和

class Solution {
    List<Integer> list = new ArrayList<>();
    List<List<Integer>> ans = new ArrayList<>();
    int target;
    int len;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        this.target = target;
        len = candidates.length;
        backTracking(0, candidates, 0);
        return ans;
    }
    public void backTracking(int sum, int[] candidates, int index){
        if(sum > target){
            return;
        }
        if(sum == target){
            ans.add(new ArrayList<>(list));
            return;
        }

        for(int i = index; i < len; i++){
            list.add(candidates[i]);
            backTracking(sum + candidates[i], candidates, i);
            list.remove(list.size() - 1);
        }
    }
}

很典型的回溯

40.组合总和II

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> list =new ArrayList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backTracking(candidates,target,0);
        return result;
    }
    private void backTracking(int[] candidates, int target, int start) {
        if (target < 0) {
            return;
        }

        if (target == 0) {
            result.add(new ArrayList<Integer>(list));
            return;
        }

        for (int i = start; i < candidates.length; ++i) {
            int one = candidates[i];
            list.add(one);
            backTracking(candidates,target-one,i+1);
            list.remove(list.size()-1);
        
            while (i < candidates.length -1 && candidates[i] == candidates[i+1]) ++i;
        }
    }
}

需要再加一步去重

131.分割回文串

class Solution {
    List<List<String>> lists = new ArrayList<>();
    Deque<String> deque = new LinkedList<>();

    public List<List<String>> partition(String s) {
        backTracking(s, 0);
        return lists;
    }

    private void backTracking(String s, int startIndex) {
        //如果起始位置大于s的大小,说明找到了一组分割方案
        if (startIndex >= s.length()) {
            lists.add(new ArrayList(deque));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            //如果是回文子串,则记录
            if (isPalindrome(s, startIndex, i)) {
                String str = s.substring(startIndex, i + 1);
                deque.addLast(str);
            } else {
                continue;
            }
            //起始位置后移,保证不重复
            backTracking(s, i + 1);
            deque.removeLast();
        }
    }
    //判断是否是回文串
    private boolean isPalindrome(String s, int startIndex, int end) {
        for (int i = startIndex, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}

这道题属于不太会的

 

今天的两道模版题,和一道不是很会的题

标签:return,target,int,list,第二十七,算法,candidates,回溯,backTracking
From: https://www.cnblogs.com/catSoda/p/16869834.html

相关文章

  • 漏桶算法和令牌桶算法
     一、漏桶算法 漏桶算法原理:水(请求)先进入到漏桶里,人为设置一个最大出水速率,漏桶以<=最大出水速率的速度出水,当水流速度过大会直接溢出(拒绝服务) 因此,此算法的核心......
  • 《数据结构与算法分析(C++语言描述)》
    在看这本书总结了笔记,并分享出来。有问题请及时联系博主:​​Alliswell_WP​​,转载请注明出处。书籍:《数据结构与算法分析(C++语言描述)》作者:LarryNyhoff著、黄达明等译源代......
  • SIFT算法相关资料
    SIFT算法相关资料一、SIFT算法的教程、源码及应用软件1、ubc:DAVIDLOWE---SIFT算法的创始人,两篇巨经典经典的文章​​​http://www.cs.ubc.ca/~lowe/​​​2、cmu:YanKe---......
  • 使用PyTorch实现简单的AlphaZero的算法(1):背景和介绍
    在本文中,我们将在PyTorch中为ChainReaction[2]游戏从头开始实现DeepMind的AlphaZero[1]。为了使AlphaZero的学习过程更有效,我们还将使用一个相对较新的改进,称为“Playout......
  • vue源码分析-diff算法核心原理
    这一节,依然是深入剖析Vue源码系列,上几节内容介绍了VirtualDOM是Vue在渲染机制上做的优化,而渲染的核心在于数据变化时,如何高效的更新节点,这就是diff算法。由于源码中关于d......
  • 数据结构 最短生成路径(BFS算法、Floyd(弗洛伊德)算法、Dijkstra算法)
    8.9、最短生成路径-BFS算法BFS算法只能处理无权图BFS算法的基本思想代码实现#include<stdio.h>#include<stdlib.h>#include<math.h>#defineMaxSize100#defin......
  • 机器学习算法:K-NN(K近邻算法)
    导读本文将介绍机器学习中的K-最近邻算法,K-NearestNeighbors是一种机器学习技术和算法,可用于回归和分类任务。1.简介k-最近邻算法,也称为kNN或k-NN,是一种非参数......
  • 数据结构与算法
    数据结构基础知识体系系统性梳理  学习思路避免孤立的学习知识点,要关联学习。比如实际应用当中,我们经常使用的是查找和排序操作,这在我们的各种管理系统、数据库......
  • SHA与SM3算法简介
    一、SHA-224和SHA-256算法原理协议标准:https://csrc.nist.gov/CSRC/media/Publications/fips/180/2/archive/2002-08-01/documents/fips180-2withchangenotice.pdf算法处......
  • 《数论女王-数论与算法的奇幻故事》知识点
    目录约数、素数、合数(第一章)素因数分解(第一章、第二章)盈数、亏数、完满数(第二章)亲和数斐波那契数列(第三章、第五章)费马小定理、伪素数、卡迈克尔数(第六章)素数的生成算式(第......