小white刷题记——LeetCodeHot100_39

文章介绍了如何使用回溯算法解决LeetCode上的组合总和问题。通过定义递归函数,设置终止条件和单层搜索逻辑,详细解释了解题步骤,并提供了C++实现代码。关键在于理解回溯算法在解决组合问题中的应用,以及如何通过递归和路径回溯找到所有可能的解决方案。
摘要由CSDN通过智能技术生成

LeetCodeHot100_39——《组合总和》

解题思路:这道题回溯算法的典型例题之一了属于,回溯算法的解题步骤,可以用树结构画出来便于理解。用例二举例来看(图太长了,不画完了,思路大概就是这样):

 回溯法的使用,从代码随想录中学到了大概的步骤流程:

①确定递归函数参数:

        

vector<vector<int>> result; //建立一个二维数组,存最后的答案
vector<int> path;//建立一个数组,用来临时存放答案
void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {

}

②确定递归的终止条件:

        这道题从树结构中就能看出来,只有当sum这个和大于等于target才会停止递归,如果等于target还需要返回答案数组。

if (sum > target)
	{
		return;
	}
	if (sum == target)
	{
		result.push_back(path);//如果sum == target把临时答案数组压入最后输出答案数组result
		return;
	}

③确定单层搜索的逻辑:

for (int i = startIndex; i < candidates.size(); i++) //这里注意的是树结构的每一层就可以看成是for循环遍历一次candidates
	{
		sum += candidates[i]; //求和
		path.push_back(candidates[i]); //讲用于求和的数放进临时答案数组中
		backtracking(candidates, target, sum, i); //进行递归调用,因为是可以重复运算自身的,所以这个这里传入i,意思是下次的递归调用也是从这次的数字开始算
		sum -= candidates[i];//调用之后需要回溯,回溯则需要和sum和path都减掉上一次增加的值
		path.pop_back();
	}

最后代码:(回溯算法感觉就是需要多练习并且多调试,多理解,才会做)

#include<iostream>
#include<vector>
using namespace std;
vector<vector<int>> result; //建立一个二维数组,存最后的答案
vector<int> path;//建立一个数组,用来临时存放答案
void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
	if (sum > target)
	{
		return;
	}
	if (sum == target)
	{
		result.push_back(path);
		return;
	}
	for (int i = startIndex; i < candidates.size(); i++)
	{
		sum += candidates[i];
		path.push_back(candidates[i]);
		backtracking(candidates, target, sum, i);
		sum -= candidates[i];
		path.pop_back();
	}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
	result.clear();
	path.clear();
	backtracking(candidates, target, 0, 0);
	return result;
}
int main() {
	vector<int> candidates = { 2,3,5 };
	int target = 8;
	vector<vector<int>> test = combinationSum(candidates, target);
	return 0;
}

“ # 设置按钮的背景颜色 self.m_button1.SetBackgroundColour(&#39;#0a74f7&#39;) self.m_button1.SetForegroundColour(&#39;white&#39;) self.m_button2.SetBackgroundColour(&#39;#0a74f7&#39;) self.m_button2.SetForegroundColour(&#39;white&#39;) self.m_button3.SetBackgroundColour(&#39;#0a74f7&#39;) self.m_button3.SetForegroundColour(&#39;white&#39;) self.m_button4.SetBackgroundColour(&#39;#238E23&#39;) self.m_button4.SetForegroundColour(&#39;white&#39;) self.m_button5.SetBackgroundColour(&#39;#238E23&#39;) self.m_button5.SetForegroundColour(&#39;white&#39;) self.m_button6.SetBackgroundColour(&#39;#238E23&#39;) self.m_button6.SetForegroundColour(&#39;white&#39;) self.m_button7.SetBackgroundColour(&#39;#6F4242&#39;) self.m_button7.SetForegroundColour(&#39;white&#39;) self.m_button8.SetBackgroundColour(&#39;#6F4242&#39;) self.m_button8.SetForegroundColour(&#39;white&#39;) self.m_button9.SetBackgroundColour(&#39;#6F4242&#39;) self.m_button9.SetForegroundColour(&#39;white&#39;) self.m_button10.SetBackgroundColour(&#39;#8E6B23&#39;) self.m_button10.SetForegroundColour(&#39;white&#39;) self.m_button11.SetBackgroundColour(&#39;#8E6B23&#39;) self.m_button11.SetForegroundColour(&#39;white&#39;) self.m_button12.SetBackgroundColour(&#39;#8E6B23&#39;) self.m_button12.SetForegroundColour(&#39;white&#39;) self.m_button13.SetBackgroundColour(&#39;#8E6B23&#39;) self.m_button13.SetForegroundColour(&#39;white&#39;) self.m_button14.SetBackgroundColour(&#39;#545454&#39;) self.m_button14.SetForegroundColour(&#39;white&#39;) self.m_button15.SetBackgroundColour(&#39;#545454&#39;) self.m_button15.SetForegroundColour(&#39;white&#39;) self.m_button16.SetBackgroundColour(&#39;#545454&#39;) self.m_button16.SetForegroundColour(&#39;white&#39;) self.m_panel1.SetBackgroundColour(&#39;white&#39;) # 设置面板的背景颜色”逐行解释代码
06-07
这段代码是用来设置图形用户界面中多个按钮和一个面板的背景和前景颜色的。具体解释如下: - `self.m_button1.SetBackgroundColour(&#39;#0a74f7&#39;)`:设置按钮1的背景颜色为蓝色。 - `self.m_button1.SetForegroundColour(&#39;white&#39;)`:设置按钮1的前景颜色为白色。 - `self.m_button2.SetBackgroundColour(&#39;#0a74f7&#39;)`:设置按钮2的背景颜色为蓝色。 - `self.m_button2.SetForegroundColour(&#39;white&#39;)`:设置按钮2的前景颜色为白色。 - `self.m_button3.SetBackgroundColour(&#39;#0a74f7&#39;)`:设置按钮3的背景颜色为蓝色。 - `self.m_button3.SetForegroundColour(&#39;white&#39;)`:设置按钮3的前景颜色为白色。 - `self.m_button4.SetBackgroundColour(&#39;#238E23&#39;)`:设置按钮4的背景颜色为绿色。 - `self.m_button4.SetForegroundColour(&#39;white&#39;)`:设置按钮4的前景颜色为白色。 - `self.m_button5.SetBackgroundColour(&#39;#238E23&#39;)`:设置按钮5的背景颜色为绿色。 - `self.m_button5.SetForegroundColour(&#39;white&#39;)`:设置按钮5的前景颜色为白色。 - `self.m_button6.SetBackgroundColour(&#39;#238E23&#39;)`:设置按钮6的背景颜色为绿色。 - `self.m_button6.SetForegroundColour(&#39;white&#39;)`:设置按钮6的前景颜色为白色。 - `self.m_button7.SetBackgroundColour(&#39;#6F4242&#39;)`:设置按钮7的背景颜色为红色。 - `self.m_button7.SetForegroundColour(&#39;white&#39;)`:设置按钮7的前景颜色为白色。 - `self.m_button8.SetBackgroundColour(&#39;#6F4242&#39;)`:设置按钮8的背景颜色为红色。 - `self.m_button8.SetForegroundColour(&#39;white&#39;)`:设置按钮8的前景颜色为白色。 - `self.m_button9.SetBackgroundColour(&#39;#6F4242&#39;)`:设置按钮9的背景颜色为红色。 - `self.m_button9.SetForegroundColour(&#39;white&#39;)`:设置按钮9的前景颜色为白色。 - `self.m_button10.SetBackgroundColour(&#39;#8E6B23&#39;)`:设置按钮10的背景颜色为橙色。 - `self.m_button10.SetForegroundColour(&#39;white&#39;)`:设置按钮10的前景颜色为白色。 - `self.m_button11.SetBackgroundColour(&#39;#8E6B23&#39;)`:设置按钮11的背景颜色为橙色。 - `self.m_button11.SetForegroundColour(&#39;white&#39;)`:设置按钮11的前景颜色为白色。 - `self.m_button12.SetBackgroundColour(&#39;#8E6B23&#39;)`:设置按钮12的背景颜色为橙色。 - `self.m_button12.SetForegroundColour(&#39;white&#39;)`:设置按钮12的前景颜色为白色。 - `self.m_button13.SetBackgroundColour(&#39;#8E6B23&#39;)`:设置按钮13的背景颜色为橙色。 - `self.m_button13.SetForegroundColour(&#39;white&#39;)`:设置按钮13的前景颜色为白色。 - `self.m_button14.SetBackgroundColour(&#39;#545454&#39;)`:设置按钮14的背景颜色为灰色。 - `self.m_button14.SetForegroundColour(&#39;white&#39;)`:设置按钮14的前景颜色为白色。 - `self.m_button15.SetBackgroundColour(&#39;#545454&#39;)`:设置按钮15的背景颜色为灰色。 - `self.m_button15.SetForegroundColour(&#39;white&#39;)`:设置按钮15的前景颜色为白色。 - `self.m_button16.SetBackgroundColour(&#39;#545454&#39;)`:设置按钮16的背景颜色为灰色。 - `self.m_button16.SetForegroundColour(&#39;white&#39;)`:设置按钮16的前景颜色为白色。 - `self.m_panel1.SetBackgroundColour(&#39;white&#39;)`:设置面板1的背景颜色为白色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值