[LeetCode]Combination Sum II

本文介绍了一种寻找候选数字集合中所有唯一组合的算法,这些组合的数字之和等于目标数字。该算法确保了组合中每个数字仅使用一次,并且遵循非递减顺序的要求。通过示例展示了如何为给定的数字集找到所有可能的有效组合。

摘要生成于 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 (a1a2, … , 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] 

有点像01背包,  稍微改动了一下,先预处理去重,统计每个元素的数, 深度遍历的时候 约束条件添加 不能超过Num[i]


class Solution {
private:
    vector<vector <int> > ret;
    vector <int> count;
    vector <int> num;
public:
	void help(int current_index, int max_index, int target , vector<int> & candidates){

		if(target <0)
			return ;
        if(current_index == max_index)
		{
	    	if(target == 0)
			{
		        vector<int> tmp;
		    	for(int i = 0 ; i <  max_index; i++)
			    	for(int j = 0 ; j < count[i]; j++)
				    	tmp.push_back(candidates[i]);
		    	
		    	ret.push_back(tmp);
		    	return ;
	    	}
            return ;
         }
		for(int i = 0; i <= num[current_index] && i <= target/candidates[current_index]; i++)
		{
			count[current_index] = i;
			help(current_index+1,max_index, target - i*candidates[current_index],candidates);
		}
	}
    vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
     ret.clear();
	 vector <int> can;
	 int pre = 0;
    if(candidates.size()==0)
        return ret;
    sort (candidates.begin(),candidates.end()); 

    num.clear();
    num.resize(candidates.size());
    int j = -1;
	for(int i = 0 ; i < candidates.size(); i++)
	{
		if(candidates[i]==pre)
		{
			num[j]++;
		}else{
			j++;
			can.push_back(candidates[i]);
            pre =candidates[i];
			num[j] = 1;
		}
	}
    count.clear();
    count.resize(candidates.size());
	help(0,can.size(), target,can);
	return  ret;
    }
};

资源下载链接为: https://pan.quark.cn/s/67c535f75d4c Linux 中的 top 命令是一个功能强大的实时监控工具,能够详细展示系统资源的使用情况,涵盖 CPU、内存和进程等方面。本文将深入剖析 top 命令的输出内容及其含义,帮助大家更好地掌握这一工具的使用。 top 命令的输出大致可以分为以下几部分:系统状态、CPU 使用情况、内存使用情况、进程列表以及其他信息。 系统状态部分包括以下内容: 当前时间:例如“11:00:54”,表示系统当前的时间。 系统运行时间:如“up 54 days, 23:35”,表示系统已经连续运行了多长时间。 登录用户:例如“6 users”,显示当前登录到系统的用户数量。 负载平均值:例如“load average: 16.32, 18.75, 21.04”,分别表示过去 1 分钟、5 分钟和 15 分钟的平均负载。这个数值反映了系统处理任务的压力。如果负载平均值持续高于 CPU 核心数的 70%,可能意味着系统处于过载状态。 CPU 使用情况部分显示各 CPU 核心的使用情况,例如“29.7 us, 18.9 sy, 0.0 ni, 49.3 id, 1.7 wa, 0.0 hi, 0.4 si, 0.0 st”,其中: “us”表示用户空间的 CPU 使用率; “sy”表示内核空间的 CPU 使用率; “ni”表示优先级调整的 CPU 使用率; “id”表示空闲的 CPU 使用率; “wa”表示等待 I/O 完成的 CPU 使用率; “hi”表示硬件中断的 CPU 使用率; “si”表示软件中断的 CPU 使用率; “st”表示被停止的进程的 CPU 使用率。 内存使用情况部分包括: KiB Mem:显示内存的总量、空闲量、已使用量以及缓存/缓冲区量,例如“32781216 total, 1506220
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值