C++算法:282给表达式添加运算符

LeetCode282给表达式添加运算符

给定一个仅包含数字 0-9 的字符串 num 和一个目标值整数 target ,在 num 的数字之间添加 二元 运算符(不是一元)+、- 或 * ,返回 所有 能够得到 target 的表达式。
注意,返回表达式中的操作数 不应该 包含前导零。
示例 1:
输入: num = “123”, target = 6
输出: [“1+2+3”, “123”]
解释: “123” 和 “1+2+3” 的值都是6。
示例 2:
输入: num = “232”, target = 8
输出: [“23+2", "2+32”]
解释: “23+2” 和 “2+32” 的值都是8。
示例 3:
输入: num = “3456237490”, target = 9191
输出: []
解释: 表达式 “3456237490” 无法得到 9191 。

2023年5月版

class Solution {
public:
vector addOperators(string num, int target) {
std::unordered_map < string, std::tuple< long long, long long, long long >> preValueMulValue;
preValueMulValue.emplace(std::string(“”) + num[0], std::make_tuple(num[0] - ‘0’, num[0] - ‘0’, num[0] - ‘0’));
for (int i = 1; i < num.size(); i++)
{
const char& ch = num[i];
const int iBit = num[i] - ‘0’;
std::unordered_map < string, std::tuple< long long, long long, long long >> valueMulValue;
for (const auto& it1 : preValueMulValue)
{
const long long& iValue = std::get<0>(it1.second);
const long long& iMul = std::get<1>(it1.second);
const long long& iEnd = std::get<2>(it1.second);
const long long iMulPre = (0 == iEnd) ? 0 : iMul / iEnd;
//不加符号
if ((0 != iEnd) )
{
valueMulValue.emplace(it1.first + ch, std::make_tuple(iValue + iMulPre * (iEnd * 9 + iBit), iMulPre * (iEnd * 10 + iBit), iEnd * 10 + iBit));
}
//增加加号
valueMulValue.emplace(it1.first + ‘+’ + ch, std::make_tuple(iValue + iBit,iBit,iBit));
//增加减号
valueMulValue.emplace(it1.first + ‘-’ + ch, std::make_tuple(iValue - iBit, -iBit, iBit));
//增加乘号
valueMulValue.emplace(it1.first + '’ + ch, std::make_tuple(iValue + iMul(iBit - 1), iMul*iBit,iBit));
}
preValueMulValue.swap(valueMulValue);
}
vector vRet;
for (const auto& it1 : preValueMulValue)
{
if (target == std::get<0>( it1.second))
{
vRet.emplace_back(it1.first);
}
}
return vRet;
}

};

2023年8月

class Solution {
public:
vector addOperators(string num, int target) {
m_strNum = num;
m_iTarget = target;
const auto& iBit = num.front() - ‘0’;
dfs(num.substr(0, 1),1, iBit, iBit, iBit);
return m_vRet;
}
void dfs(string exp, int hasDo,const long long llValue, long long endMulValue,long long endValue)
{
if (hasDo == m_strNum.length())
{
if (llValue == m_iTarget)
{
m_vRet.emplace_back(exp);
}
return ;
}
const auto& chBit = m_strNum[hasDo] ;
const auto& iBit = chBit - ‘0’;
//1+23 llValue=7 endMulValue=6 endValue=3 exincludeEnd=1 preMul=2
long long exincludeEnd = llValue - endMulValue;
long long preMul = (0== endValue)? 0 : endMulValue / endValue;
#define NEW_END_MUL (preMul
llNewEnd)
//直接连接
//1+234 llValue=69 endMulValue=68 endValue=34 exincludeEnd=1 preMul=2
long long llNewEnd = endValue * 10 + ((endValue<0) ? -iBit : iBit);
if (0 != endValue )
{
dfs(exp + chBit, hasDo + 1, exincludeEnd + NEW_END_MUL, NEW_END_MUL, llNewEnd);
}
//乘以
llNewEnd = iBit;
preMul = endMulValue;
dfs(exp + '
'+ chBit, hasDo + 1, exincludeEnd + NEW_END_MUL, NEW_END_MUL, llNewEnd);
preMul = 1;
exincludeEnd = llValue;
dfs(exp + ‘+’ + chBit, hasDo + 1, exincludeEnd + NEW_END_MUL, NEW_END_MUL, llNewEnd);
llNewEnd = -iBit;
dfs(exp + ‘-’ + chBit, hasDo + 1, exincludeEnd + NEW_END_MUL, NEW_END_MUL, llNewEnd);
}
string m_strNum;
int m_iTarget;
vector m_vRet;
};

扩展阅读

视频课程

有效学习:明确的目标 及时的反馈 拉伸区(难度合适),可以先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771

如何你想快

速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

相关下载

想高屋建瓴的学习算法,请下载《闻缺陷则喜算法册》doc版
https://download.csdn.net/download/he_zhidan/88348653

鄙人想对大家说的话
闻缺陷则喜是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
墨家名称的来源:有所得以墨记

之。 |
|如果程序是一条龙,那算法就是他的是睛|

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻缺陷则喜何志丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值