[leetcode] 282. Expression Add Operators 解题报告

leetcode全套解答python版本 leetcode全套解答python版本。包括更新到10月份的的leetcode 立即下载

题目链接:https://leetcode.com/problems/expression-add-operators/

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 -> []


思路:今天看到我的有些文章被csdn收入算法和数据结构知识库,我真的很高兴也很惶恐.当初写解题报告只是为了让自己记住当初自己的想法,方便以后回顾,能够帮助别人我自己很高兴,又怕写的不好误导别人.算起来自去年十月份开始写解题报告,到现在leetcode总共331道题已经几乎刷完了,也写了300+的解题报告,我的刷题大业也已经持续了将近八个月之久,有时候想想除了当时考研,我再也没有投入这么多精力到一件事情上.在国外如此繁重的课程压力下我还是一直在坚持.中间找实习除了facebook几乎都没有回音,而facebook也在二面的时候跪了,当时非常沮丧,也认识到自己还差很多.但是生活还是要继续,所以我只能不停的刷题.而在这么长的时间里,我也感到我一直在进步,一方面是算法,另一方面是语言本身.我对c++的理解到现在也比一年前深了很多,马上要开始找全职了,希望我利用好这最后一个暑假拿到一个好的offer.


本题也是一个DFS+回溯的题目,在进行搜索的时候当前一位可以和后面的数字组成一个数,也可以直接从这里分割和后面的数字进行运算.在进行运算的时候需要二个数字,我们可以先保存路径,将当前运算符记录下来,等待下一个数字的时候的把运算符和下一个数字进行运算.即如果运算符为加号,我们只需要保存一个1,在和下一个数字运算只要乘1就行,然后将其值保存在结果中.同理对于减只要和下一个数字乘以-1.对于乘号我们需要保存一个用于乘的当前数字.

代码如下:

class Solution {
public:
    void DFS(string num, long sum, string path, int pos, long pre, int val)
    {//pre是未计算的前面累积下来的值,val是表示前面是什么运算,前面如果是加则为1,减-1,乘要累计乘积
        pre = pre*10 + num[pos]-'0';
        if(pre > INT_MAX) return;
        if(sum+pre*val ==0 && pos== num.size()-1) result.push_back(path+num[pos]);
        if(pos== num.size()-1) return;
        if(pre) DFS(num, sum, path+num[pos], pos+1, pre, val);//非0开头的数字可以保留下来
        DFS(num, sum+pre*val, path+num[pos]+'+', pos+1, 0, 1);
        DFS(num, sum+pre*val, path+num[pos]+'-', pos+1, 0, -1);
        DFS(num, sum, path+num[pos]+'*', pos+1, 0, val*pre);
    }

    vector<string> addOperators(string num, int target) {
        if(num.size() ==0) return {};
        DFS(num, -target, "", 0, 0, 1);
        return result;
    }
private:
    vector<string> result;
};

参考:https://leetcode.com/discuss/96257/shortest-c-solution


下面一个更好理解一些.

class Solution {
public:
    void DFS(string num, long sum, string path, int k, int pre)
    {
        if(k == num.size() && sum ==0) return ans.push_back(path);
        for(int i = k, val =0; i < num.size(); i++)
        {
            val = val*10 + (num[i]-'0');
            if(val > INT_MAX) break;
            if(k==0) DFS(num, sum+val, path+to_string(val), i+1, val);
            else
            {
                DFS(num, sum+val, path+"+"+to_string(val), i+1, val);
                DFS(num, sum-val, path+"-"+to_string(val), i+1, -val);
                DFS(num, sum-pre+pre*val, path+'*'+to_string(val), i+1, pre*val);
            }
            if(num[k]=='0') break;
        }
    }
    
    vector<string> addOperators(string num, int target) {
        if(num.size() ==0) return {};
        DFS(num, -target, "", 0, 0);
        return ans;
    }
private:
    vector<string> ans;
};





从原理到实践:详解双目散斑结构光的生成与优化 本文深入解析了双目散斑结构光的生成原理与优化实践,详细介绍了散斑结构光的抗干扰性、空间编码技术及其在三维视觉中的应用。通过对比不同生成方法和技术路线,提供了参数调优指南和工程实现细节,帮助开发者掌握高效、精准的散斑生成技术。 阅读详情

相关推荐

合规批量下载美国上市公司年报数据的一些经验和总结

本文总结了使用Python脚本批量下载和处理美国上市公司SEC报告的技术经验。通过CIK代码查询SEC EDGAR系统,生成特定日期和报告类型的URL,并使用Selenium和Requests库下载报告内容。流程包括数据获取、文件下载、Excel处理和多线程优化,确保高效处理几百条数据。关键技术挑战包括SEC访问限制、动态网页内容和数据完整性,解决方案包括设置合规的User-Agent、使用Selenium模拟浏览器行为以及错误日志记录。最佳实践包括模块化设计、多线程优化和严格遵守SEC合规性。该自动化流程

jyfool的博客 1649

【LeetCode-282】给表达式添加运算符 [难度:困难]

给定一个仅包含数字 0-9 的字符串 num 和一个目标值整数 target ,在 num 的数字之间添加 二元 运算符(不是一元)+、- 或 * ,返回 所有 能够得到 target 的表达式。我们可以用「回溯法」来模拟这个过程。从左向右构建表达式,并实时计算表达式的结果。由于乘法运算优先级高于加法和减法运算,我们还需要保存最后一个连乘串(如 2*3*4)的运算结果。设字符串 num 的长度为 n,为构建表达式,我们可以往 num中间的 n−1 个空隙添加 + 号、- 号或 * 号,或者不添加符号。

A-Itfuture的博客 360

寻迹Arduino智能小车

Arduino巡线寻迹智能小车

Rachael_Wang的博客 1万+

Expression Add Operators

Expression Add Operators

欧阳航凯 416

python怎样按某一列值拆分Excel表格

pd.ExcelWriter@python #用python按某一列拆分Excel工作表,将内容写入一个工作簿 本次以“按照表中的某一列,将一张表格拆分为多张表”的需求为例,简单介绍这个过程中用到的pd.ExcelWriter,及每一句的作用。 如上表《学生.xlsx》,假设有14万行28列,请按照学校名,拆分表格(不想用复制粘贴)。 ##代码及解释如下 import pandas as pd from time import time#用来计算时间,展示进度,减少焦虑 in_ws='学生.xlsx'#

learn_get的博客 6792

大模型驱动时序量化因子自动优化:从WorldQuant回测到实战策略

本文探讨了如何利用大模型驱动时序量化因子的自动优化,构建从创意生成到实战验证的完整流程。通过结合WorldQuant回测平台,搭建自动化流水线,详细拆解了因子生成、回测验证与迭代优化的核心模块。文章分享了从初版实现到进阶优化的实践经验,并指出提示词工程与领域知识注入是关键,旨在实现人机智能协作,提升量化策略研发效率。

wdx012345的博客 822

leetcode:282. 给表达式添加运算符

5 的前一个操作数是 *4 ,我们应该记录的值是 3 * 4 = 12。也就是 1 +2 *3 = 9,很明显是错的,原因就在于乘法的优先级较高,并不能直接将前边的结果乘上当前的数。而是用当前数乘以前一个操作数。算的话,我们可以用之前的值(3)减去前一个操作数(2),然后再加上当前数(3)乘以前一个操作数(2)。如果是连乘的情况,比如对于 2345,假设之前已经进行到了 2 +3 *4 (14),现在到 5 了。用之前的值(14)减去前一个操作数(12),然后再加上当前数(5)乘以前一个操作数(12)。

OceanStar的博客 387

PyCharm配置及使用Git教程

平时习惯在Windows下开发,但是我们又需要实时将远方仓库的代码clone到本地,也需要将自己修改的代码push到远端服务器。

酒酿小圆子呀~ 4万+

LeetCode 第282题 给表达式添加运算符

LeetCode 第282题

sakuraSD的博客 546

英飞凌TC3xx实战:PWM中点触发ADC采集+DMA自动搬运完整配置流程(基于ILLD库)

本文详细介绍了在英飞凌TC3xx系列微控制器上使用ILLD库实现PWM中点触发ADC采集并通过DMA自动搬运数据的完整配置流程。该方案适用于电机控制、电源管理等需要高精度定时采集的场景,通过GTM生成PWM信号、ADC采集和DMA传输实现全硬件自动化流程,显著提升系统效率。

weixin_42536058的博客 543

FPGA实现CAN接口(SJA1000)

学无止境,善于积累,每天积累一点点,成功就在眼前,加油! 1 CAN总线简介 CAN(Controller Area Network,控制器局域网 )总线是一个多主机异步串行总线,也是国际上应用最广泛的现场总线之一。在现场总线中,它是惟一被ISO国际标准化组织批准的现场总线。由于其成本低、容错能力强、支持分布式控制、通信速率高等优点在汽车、工业控制、航天等领域得到广泛应用。特别是由于CAN总线具有...

m0_46498597的博客 1万+

282. Expression Add Operators

目录第一步:dfs获得所有计算式第二步:计算结果先计算加减法 第一步:dfs获得所有计算式 class Solution { private String num; private int target; public List<String> addOperators(String num, int target) { this.num = num; this.target = target; dfs(0,"");

约定的博客 432

基于Simulink的无线充电系统LCC补偿网络建模与控制

本文详细介绍了基于Simulink的无线充电系统LCC补偿网络建模与控制方法。首先分析了无线充电系统面临的核心挑战,提出LCC补偿网络解决方案,阐述了其恒流输出和输入阻抗解耦原理。通过Simulink分步演示了系统建模过程,包括松耦合变压器、LCC+LC补偿网络、功率变换级搭建以及频率跟踪控制实现。仿真结果表明,LCC补偿系统在气隙变化和负载跳变下能保持输出电压稳定(波动±3%),效率达89%。文章还总结了工程实现要点,并探讨了动态充电、多负载系统等扩展应用。该教程为开发高效智能的无线充电系统提供了实用指导

amy_mhd的博客 69

[Leetcode] 282. Expression Add Operators 解题报告

题目: 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 v

魔豆(Magicbean)的博客 517

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

【LeetCode】282.Expression Add Operators(Hard)解题报告

【LeetCode】282.Expression Add Operators(Hard)解题报告题目地址:https://leetcode.com/problems/expression-add-operators/description/ 题目描述:   Given a string that contains only digits 0-9 and a target value, retu

郝春雨的博客 399

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode 282. Expression Add Operators(表达式)

原题网址:https://leetcode.com/problems/expression-add-operators/ Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -,

James Pan 1132

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

[leetcode 282]Expression Add Operators

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.

ER_Plough的专栏 1868

LeetCode //C - 282. Expression Add Operators

【代码】LeetCode //C - 282. Expression Add Operators。

Made in Code 1107

LeetCode:282. Expression Add Operators

LeetCode:282. Expression Add Operators 题目描述 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 ...

杨领well的专栏 167

LeetCode282. Expression Add Operators

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

李歇特冯-兹拜因巴哈的博客 243

Leetcode 282. Expression Add Operators

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

taoqick的专栏 238

[LeetCode]282. Expression Add Operators

282. Expression Add Operators 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 evalu

Johnkui技术博客 789

leetcode 282. Expression Add Operators

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

cmy203的博客 233

LeetCode 282 Expression Add Operators 题解

题意简述:给定一个数字串和一个目标值,允许在数字串中任意两个相邻数字之间插入+、-和*来组成表达式,求出所有这样的表达式,表达式求出的值恰好等于目标值。 输入:一个只由数字组成的字符串num,一个整数target。 输出:所有计算值为目标值的表达式。 示例:输入为(”105”,5),输出为[“1*0+5”,”10-5”]题解: 这应该是条搜索题,搜索所有可能的表达式并分别计算结果,储存所有结

MonoTali的博客 910

Leetcode282. Expression Add Operators

AnalysisOveiously, in order to solve this question, we have to iterate through all of the possible cases at least once. DFS can be used to solve the iteration process. The optimization we could made it

正西风落叶下长安 735
上一篇: [leetcode] 149. Max Points on a Line 解题报告
下一篇: [leetcode] 335. Self Crossing 解题报告
小榕流光
博客等级 码龄16年 135粉丝 424原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值