当前位置: 首页 > news >正文

wp做购物网站互联网金融营销案例

wp做购物网站,互联网金融营销案例,东莞建筑设计公司排名,网站建设 模板网站组合总和 力扣原题链接 问题描述 给定一个无重复元素的整数数组 candidates 和一个目标整数 target,找出 candidates 中可以使数字和为目标数 target 的所有不同组合,并以列表形式返回。你可以按任意顺序返回这些组合。 示例 示例 1: 输…

组合总和

力扣原题链接

问题描述

给定一个无重复元素的整数数组 candidates 和一个目标整数 target,找出 candidates 中可以使数字和为目标数 target 的所有不同组合,并以列表形式返回。你可以按任意顺序返回这些组合。

示例

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。7 也是一个候选, 7 = 7 。仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入: candidates = [2], target = 1
输出: []

解题思路

这是一个典型的回溯算法问题。我们需要在给定的数字数组中寻找所有可能的组合,使其和等于目标数 target

代码思路

  1. 初始化结果列表: 创建一个空的列表 result,用于存储最终的组合结果。
  2. 回溯搜索: 定义一个回溯函数 backtrack,其参数包括当前处理的索引 start、当前的目标值 target、当前的组合路径 path
  3. 结束条件: 如果当前目标值等于 0,说明找到了一个有效组合,将其加入结果列表,并返回。
  4. 选择列表: 候选数字数组中的所有数字。
  5. 遍历选择: 从当前处理索引 start 开始遍历候选数字数组。
  6. 做出选择: 将当前数字加入路径,并更新目标值为 target - candidates[i]
  7. 递归进入下一层: 递归调用回溯函数,传入新的索引 i 和更新后的目标值。
  8. 撤销选择: 回溯到上一层时,将当前选择的数字从路径中删除。

Java解题

写法一

直接用target来减,return判断条件为target == 0

import java.util.*;class Solution {public List<List<Integer>> combinationSum(int[] candidates, int target) {List<List<Integer>> result = new ArrayList<>();backtrack(candidates, target, 0, new ArrayList<>(), result);return result;}private void backtrack(int[] candidates, int target, int start, List<Integer> path, List<List<Integer>> result) {// 结束条件:如果目标值为 0,说明找到了一个有效组合,将其加入结果列表if (target == 0) {result.add(new ArrayList<>(path));return;}// 从当前处理索引开始遍历候选数字数组for (int i = start; i < candidates.length; i++) {// 做出选择:将当前数字加入路径,并更新目标值if (target - candidates[i] >= 0) {path.add(candidates[i]);// 递归进入下一层,传入新的索引和更新后的目标值backtrack(candidates, target - candidates[i], i, path, result);// 撤销选择:回溯到上一层时,将当前选择的数字从路径中删除path.remove(path.size() - 1);}}}
}
写法2

加入一个sum来收集当前path里的总和,return判断条件为sum = target

class Solution {List<List<Integer>> res = new ArrayList<>();public List<List<Integer>> combinationSum(int[] candidates, int target) {List<Integer> path = new ArrayList<>();backstrack(candidates,target,0,0,path);return res;}public void backstrack(int[] candidates,int target,int index,int sum,List<Integer> path){if(sum == target){res.add(new ArrayList<>(path));return;}for( int i = index;i<candidates.length;i++){if(sum>target) return;if(candidates[i]>target) continue;path.add(candidates[i]);backstrack(candidates,target,i,sum+candidates[i],path);path.remove(path.size()-1);}}
}
http://www.ds6.com.cn/news/20640.html

相关文章:

  • 湖南手机版建站系统哪家好上海百度推广客服电话
  • 多个链接的网站怎么做整站营销系统
  • 甘肃网站建设八百客crm系统登录入口
  • 个人网站怎么做淘宝客怎样找推广平台
  • 钓鱼网站怎么制作html企业应该如何进行网站推广
  • 网站开发需要什么人员可以直接打开网站的网页
  • 专门做餐厅设计的网站广州关键词搜索排名
  • mac电脑用什么软件做网站网站建设流程是什么
  • 极简wordpress模板怎么学seo基础
  • 股票做空网站steam交易链接在哪
  • 四川省城乡住房与建设厅网站首页永久免费跨境浏览app
  • wordpress db host龙岗seo网络推广
  • asp网站相册sem优化服务公司
  • php做网站访问记录拉新app推广平台排名
  • 淘宝客如何建设推广网站郑州百度快照优化
  • 使用html做网站的网页西安seo哪家好
  • 网站的构思怎么简单制作一个网页
  • 哪个网站 可以做快递单录入市场营销策划ppt
  • 镇江大港属于哪个区广东seo网站推广
  • 青岛病毒感染最新消息网络seo招聘
  • 网站页面一般以多大标准做合适软文营销步骤
  • 珠海企业宣传片制作网站运营推广选择乐云seo
  • 邯郸如何做企业网站网页设计制作网站模板图片
  • 快站如何做网站seo优化与推广招聘
  • 网站开发人员年薪千峰培训多少钱
  • 郑州网站开发yipinpai网站优化查询
  • 电子商务网站开发技术品牌推广方案ppt
  • 一流高职院校建设工作网站站牛网是做什么的
  • 吉林网站建设司优化是什么梗
  • 晋江做网站模板广东互联网网络营销推广