题目:

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. Lua学习五----------Lua循环

    © 版权声明:本文为博主原创文章,转载请注明出处 1.循环类型 1.1 while循环 - 语法:while(condition) do ...<执行语句> end - 解析:判断cond ...

  2. 【强网杯2018】逆向hide

    这是事后才做出来的,网上没有找到现成的writeup,所以在这里记录一下 UPX加壳,而且linux下upx -d无法解,也无法gdb/ida attach 因为是64位,所以没有pushad,只能挨 ...

  3. js 中的 prototype 和 constructor

    var a=function(){ this.msg="aa"; } a.prototype.say=function(){ alert('this is say');} 1.只有 ...

  4. Android:实现两个Activity相互切换而都不走onCreate()

    本文要实现的目的是: 有3个Activity: A,B,C.从A中能够进入B,B中能够进入C.而且B和C之间可能须要多次相互切换,因此不能使用普通的startActivity-finish方式,由于又 ...

  5. 【BZOJ3065】带插入区间K小值 替罪羊树+权值线段树

    [BZOJ3065]带插入区间K小值 Description 从前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理 ...

  6. Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)

    sftp -b batchfile username@remote_host 报错:Permission denied (publickey,gssapi-keyex,gssapi-with-mic, ...

  7. SQLServer中游标实例介绍(转)

    引言 我们先不讲游标的什么概念,步骤及语法,先来看一个例子: 表一 OriginSalary                      表二 AddSalary 现在有2张表,一张是OriginSal ...

  8. mooc课程mit6.00.1x--problem set1解决方法

    counting vowels: 计算字符串中含有元音字母aeiou的数量 char = 'azcbobobegghakl' num = 0 #利用in方法直接查找字符串char中含有的元音字母数量 ...

  9. 【题解】P2602[JZOI2010]数字计数

    [题解][P2602ZJOI2010]数字计数 乍看此题,感觉直接从数字的位上面动手,感觉应该很容易. 但是仔细看数据范围,发现如果不利用计数原理,肯定会超时,考虑数码出现的特征: \(A000\)到 ...

  10. python数据分析之:数据清理,转换,合并,重塑(一)

    DataFrame合并: merge运算是将一个或多个键将行链接起来.来看下面的这个例子: In [5]: df1=DataFrame({'key':['b','b','a','c','a','a', ...