题目:

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

  

题解:

  想了半个小时还是想不出来,卡在乘上了,看了别人的代码,感觉这个处理很巧妙,基本思路和回溯递归差不多

Soution 1 ()

class Solution {
public:
void helper(string num, vector<string>& res, string s, int target, int pos, long cur, long pre) {
if(pos == num.size()) {
if(cur == target) res.push_back(s);
return;
}
for(int i=pos; i<num.size(); i++) {
//首字符为0且长度大于1,那么这个字符串不能代表数字
if(num[pos] == '' && i>pos) break;
string str = num.substr(pos, i-pos+);
long val = stol(str);
if(pos == )
helper(num, res, s+str, target, i+, val, val);
else {
helper(num, res, s+'+'+str, target, i+, cur+val, val);
helper(num, res, s+'-'+str, target, i+, cur-val, -val);
helper(num, res, s+'*'+str, target, i+, cur-pre+pre*val, pre*val);
}
}
}
vector<string> addOperators(string num, int target) {
vector<string> res;
if(num.size() == ) return res;
helper(num, res, "", target, , , );
return res;
}
};

【LeetCode】282. Expression Add Operators的更多相关文章

  1. [LeetCode] 282. Expression Add Operators 表达式增加操作符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  2. LeetCode 282. Expression Add Operators

    原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...

  3. [leetcode]282. Expression Add Operators 表达式添加运算符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  4. 282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

  5. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  6. 282 Expression Add Operators 给表达式添加运算符

    给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ...

  7. 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  8. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  9. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

随机推荐

  1. php程序执行过程--非宏观和微观而是写的程序一行一行的路径----利用xdebug了解一段程序的执行过程----覆盖率

    1.xdebug_start_code_coverage();//在需要开始跟踪程序执行路径时使用 2.var_dump(xdebug_get_code_coverage());//在结尾使用打印程序 ...

  2. 对Oracle的rownum生成时机的理解

    在Oracle中,rownum和rowid是平时经常用到的.比如rownum经常用于分页查询,rowid用于排重或者快速定位到记录. 对rownum跟order by配合下的生成时机一直没有具体研究过 ...

  3. nginx - KeepAlive详细解释

    最近工作中遇到一个问题,想把它记录下来,场景是这样的: 从上图可以看出,用户通过Client访问的是LVS的VIP, VIP后端挂载的RealServer是Nginx服务器. Client可以是浏览器 ...

  4. mongo 的逻辑存储和物理存储

    逻辑存储空间与物理存储空间有差距的主要原因 存储引擎存储时,需要记录一些额外的元数据信息,这会导致物理空间总和比逻辑空间略大 存储引擎可能支持数据压缩,逻辑的数据块存储到磁盘时,经过压缩可能比逻辑数据 ...

  5. 14 nginx 中配置 expires缓存提升网站负载

    一:nginx 中配置 expires缓存提升网站负载 对于网站的图片,尤其是新闻站, 图片一旦发布, 改动的可能是非常小的.我们希望 能否在用户访问一次后, 图片缓存在用户的浏览器端,且时间比较长的 ...

  6. 018 nginx与第三模块整合[一致性哈希模块整合]

    nginx第三方模块官网:http://wiki.nginx.org/HttpUpstreamConsistentHash nginx第三方模块下载地址:https://github.com/repl ...

  7. 8148之更换摄像头出现异常---REISZER OVERFLOW OCCURED: RESTARTING

    my iss config as: rsz_reg->SRC_VSZ  = 1079;//715;    rsz_reg->SRC_HSZ  = 1919;//1277; rszA_reg ...

  8. 早一天把生意做到de

    const isMobilePhoneNum = (params) => { const areaCode = params.areaCode || 'cn' const str = param ...

  9. PECL的安装和使用

    下载并安装pear脚本 cd /usr/local/php/bin/ curl -o go-pear.php http://pear.php.net/go-pear.phar ./php go-pea ...

  10. Flask:程序结构

    在Flask中需要配置各种各样的参数.比如设置秘钥,比如上一章介绍到的配置数据库类型. app.config['SECRET_KEY']=os.urandom(20) app.config['SQLA ...