【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.
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的更多相关文章
- [LeetCode] 282. Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- LeetCode 282. Expression Add Operators
原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...
- [leetcode]282. Expression Add Operators 表达式添加运算符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- 282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
- 282 Expression Add Operators 给表达式添加运算符
给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ...
- 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- jQuery--基础(查询标签)
浅谈jQuery使用背景 jQuery是使用原生js写成的一个库,使用简单,提高开发效率.在用js冗杂的代码解决的问题中,大部分都可以用jQuery来快速解决. 例如: js中查询网页中ID为&quo ...
- dede内容页调用点击数
<script src="{dede:field name='phpurl'/}/count.php?view=yes&aid={dede:field name='id'/} ...
- 关于安装oracle 11g client 出现安装先决条件检查全部失败
本文转自:https://blog.csdn.net/iloli/article/details/45244159 今天我在安装Oracle11gClient时,全部显示成N/A,Oracle无法执行 ...
- 贷前系统ElasticSearch实践总结
贷前系统负责从进件到放款前所有业务流程的实现,其中涉及一些数据量较大.条件多样且复杂的综合查询,引入ElasticSearch主要是为了提高查询效率,并希望基于ElasticSearch快速实现一个简 ...
- python学习(一)运行第一个python脚本
当然这里指的是在linux或者unix下,像写bash脚本那样 #!/usr/bin/python print('The Bright Side ' + 'of Life...') 反正我建议就算一开 ...
- 架构 -- java
@.sql写在dao层 原文:http://blog.csdn.net/y_dzaichirou/article/details/53673528 @.Java Web项目需要掌握的技能 原文:htt ...
- grep命令:查看配置文件未注释行(转)
FROM: https://linux.cn/article-6958-1.html 可以使用 UNIX/BSD/OS X/Linux 这些操作系统自身提供的 grep,sed,awk,perl或者其 ...
- TCP的四种定时器简单记录
TCP管理的4个不同的定时器: 1.重传定时器:用于当希望收到另一端的确认. 2.坚持定时器:使窗口大小信息保持不断流动. 3.保活定时器:检测TCP空闲连接的另一端何时崩溃或重启. 4.2MSL定时 ...
- 【BZOJ2083】[Poi2010]Intelligence test 二分
[BZOJ2083][Poi2010]Intelligence test Description 霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列.Lyx很渴望成为霸 ...
- 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
目录: 一.概述 最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个 ...