活字印刷_组合总数

组合总和

组合总和

image-20220507151217709

image-20220507151056333

import java.util.LinkedList;
import java.util.List;
public class Solution {
    public static void DFS(int[] candidates, int pos, int target,int cur, List<List<Integer>> result, List<Integer> tmp){
        if(cur>=target){
            //保存一组解
            if(cur==target)
            result.add(new ArrayList<>(tmp));//这里需要通过 new ArrayList<>(tmp);拷贝到result中,
             //result.add(tmp)   //error!
            return;
        }
        for(int i = pos;i<candidates.length;i++){
             //处理当前位置!
             tmp.add(candidates[i]);
            //深度优先!
             DFS(candidates,i,target,cur+candidates[i],result,tmp);
            //DFS(candidates,i+1,target,cur+candidates[i],result,tmp); 
            // error 这里向下遍历下次拼接还是 i,因为这里的元素可以重复!
             //回退!
             tmp.remove(tmp.size()-1);
        }

    }
    public static List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result = new LinkedList<>();//保存解集!
        List<Integer> tmp = new LinkedList<>();//保存一组解
        DFS(candidates,0,target,0,result,tmp);
        return result;
    }

    public static void main(String[] args) {
        List<List<Integer>> lists = combinationSum(new int[]{2,3,4,5},7);
    }
}

一组解不能直接add到结果集,这里只是一个引用,如果add引用,最终 str改变,结果集中的元素也会改变,所以要通过构造方法!

new String(str)拷贝到结果集中!

这里 的 同一个数可以无限制的使用,那么深度遍历时就还要从当前位置开始!而不是 i+1

活字印刷

活字印刷

image-20220507160144973

class Solution {
    public void DFS(String tiles, Set<String> result,StringBuilder str,boolean[] book){
        for(int i = 0;i<tiles.length();i++){
           if(!book[i]){//当前字符没用过!
              book[i] = true;//标记!
              //保存当前位置!
              str.append(tiles.charAt(i));
              result.add(str.toString());
              //深度遍历!
              DFS(tiles,result,str,book);
              //回退
              str.deleteCharAt(str.length()-1);
              //注意! 回退时记得把标记位也回退!
              //我们已经删除了i下标的字符,那么i标记位也需要改变!
              book[i] = false;
           }
        }
    }
    public int numTilePossibilities(String tiles) {
       Set<String> result = new HashSet<>();//结果集(set去重)
       boolean[] book = new boolean[tiles.length()];//标记数组
        StringBuilder str = new StringBuilder();//保存一组结果!
        DFS(tiles,result,str,book);
        return result.size();
    }
}

我们可以通过Set集合达到去重的效果! 这里的字符序列可以往前所以 i 要从0开始!

这里不需要pos位置,所以要有标记数组,标记该字符是否使用过!

回退时,记得将标记位也回退!

#笔试#
全部评论
学习活字印刷组合总数
点赞 回复 分享
发布于 2022-08-27 13:00 河南

相关推荐

重生我想学测开:嵌入式的问题,我准备入行京东外卖了
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在午休:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
秋招不是要开始了吗,我都打算润了,看大家还在找不敢润了
一条从:因为不是人人都像佬一样有实习像我们这种二本仔秋招没有实习也是白忙活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务