Combination Sum II - LeetCode

本文介绍了一个算法问题,即在候选数字集合中寻找所有唯一组合,这些组合的数字之和等于目标数字。每个数字在每种组合中只能使用一次,并且解决方案不能包含重复的组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

 

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6] 

 

思路:和Combination Sum这道题基本思想是相同的。需要注意的不同点主要有两个:一个是因为每个数只能用一次,因此在枚举完后继续递归时要从下一位开始枚举,而不再是当前位置;另一个还是因为每个数只能用一次,因此要注意原数组中可能有重复数字的情况,因此这里在枚举过程中如果遇见了相同的数,则只枚举一次,然后跳过剩下的重复数字。

 1 class Solution {
 2 public:
 3     void assist(vector<int>& candidates, vector<vector<int> >& res, vector<int>& cur, int target, int st)
 4     {
 5         if (target == 0)
 6         {
 7             res.push_back(cur);
 8             return;
 9         }
10         if (st == candidates.size()) return;
11         for (int i = st, n = candidates.size(); i < n && candidates[i] <= target; i++)
12         {
13             cur.push_back(candidates[i]);
14             assist(candidates, res, cur, target - candidates[i], i + 1);
15             cur.pop_back();
16             while (i < n && candidates[i] == candidates[i + 1])
17                 i++;
18         }
19     }
20     vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
21         vector<vector<int> > res;
22         vector<int> cur;
23         sort(candidates.begin(), candidates.end(), less<int>());
24         assist(candidates, res, cur, target, 0);
25         return res;
26     }
27 };

 

转载于:https://www.cnblogs.com/fenshen371/p/4952944.html

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值