282. Expression Add Operators

本文介绍了一种通过递归回溯算法解决给定数字串中添加运算符使其等于目标值的问题的方法。通过C++实现,详细展示了如何遍历所有可能的运算组合。

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

题目:

Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +-, or *between the digits so they evaluate to the target value.

Examples: 

"123", 6 -> ["1+2+3", "1*2*3"] 
"232", 8 -> ["2*3+2", "2+3*2"]
"105", 5 -> ["1*0+5","10-5"]
"00", 0 -> ["0+0", "0-0", "0*0"]
"3456237490", 9191 -> []

Credits:
Special thanks to @davidtan1890 for adding this problem and creating all test cases.

翻译:、
给定一个仅包含数字0-9和目标值的字符串,返回在数字之间添加二进制运算符(不是一元)+, - 或*的所有可能性,以便它们计算目标值

例子:
“123”,6 - > [“1 + 2 + 3”,“1 * 2 * 3”]
“232”,8 - > [“2 * 3 + 2”,“2 + 3 * 2”]
“105”,5 - > [“1 * 0 + 5”,“10-5”]
“00”,0 - > [“0 + 0”,“0-0”,“0 * 0”]
“3456237490”,9191 - > []
学分:
特别感谢@ davidtan1890添加此问题并创建所有测试用例。
解题代码:
class Solution {
private:
		void f(vector<string> &strs, int target, string cur,string nums, long cv, long pv) {
			if(nums.length() == 0) {
				if(target == cv) strs.push_back(cur);
				return ;
			}
			else {
				for(int i = 1; i<= nums.length();i++) {
					string num = nums.substr(0,i);
					if(num.length() > 1 && num[0] == '0') return ;
				//	char* flag = NULL;
				//	char tem[i+1];
				//	strcpy(tem,num.c_str());
				//	cout << tem <<endl;
				//	long l = strtol(tem,&flag,10);
				long l  = stol(num);
				//	if(*flag != '\0') continue;
				//	cout << l<<endl; 
					string nextNums = nums.substr(i);
					if(cur.length()!=0) {
						f(strs,target,cur+'+'+num,nextNums,cv+l,l);
						f(strs,target,cur+'-'+num,nextNums,cv-l,-l);
						f(strs,target,cur+'*'+num,nextNums,cv-pv+pv*l,pv*l);
					}	
					else {
						f(strs,target,num,nextNums,l,l);
					}
				}
			}
		}
public:
    vector<string> addOperators(string num, int target) {
		vector<string> strs;
		strs.clear();
		if(num.empty()) return strs;
		 f(strs,target,"",num,0,0);
		 return strs;
    }
};

解题状态:


class Solution {
private:
		void f(vector<string> &strs, int target, string cur,string nums, long cv, long pv) {
			if(nums.length() == 0) {
				if(target == cv) strs.push_back(cur);
				return ;
			}
			else {
				for(int i = 1; i<= nums.length();i++) {
					string num = nums.substr(0,i);
					if(num.length() > 1 && num[0] == '0') return ;
				//	char* flag = NULL;
				//	char tem[i+1];
				//	strcpy(tem,num.c_str());
				//	cout << tem <<endl;
				//	long l = strtol(tem,&flag,10);
				long l  = stol(num);
				//	if(*flag != '\0') continue;
				//	cout << l<<endl; 
					string nextNums = nums.substr(i);
					if(cur.length()!=0) {
						f(strs,target,cur+'+'+num,nextNums,cv+l,l);
						f(strs,target,cur+'-'+num,nextNums,cv-l,-l);
						f(strs,target,cur+'*'+num,nextNums,cv-pv+pv*l,pv*l);
					}	
					else {
						f(strs,target,num,nextNums,l,l);
					}
				}
			}
		}
public:
    vector<string> addOperators(string num, int target) {
		vector<string> strs;
		strs.clear();
		if(num.empty()) return strs;
		 f(strs,target,"",num,0,0);
		 return strs;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值