题目:

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. freemark2pdf

    freemarker+ITextRenderer 生成html转pdf 博客分类: ITextRenderer ITextRenderer  网上已经有比较多的例子 写这个 但是很多都是简单的 dem ...

  2. HDFS源码分析之DataXceiverServer

    DataXceiverServer是Hadoop分布式文件系统HDFS的从节点--数据节点DataNode上的一个后台工作线程,它类似于一个小型的服务器,被用来接收数据读写请求,并为每个请求创建一个工 ...

  3. linux下网卡绑定

    网卡绑定的作用:1.冗余,防止单点故障 2.防止传输瓶颈 1.交换机端口绑定: system-view link-aggregation group 1 mode manual 比如把端口1和2进行绑 ...

  4. sublime3 支持 jsx 语法

    添加几个插件即可在js中快速写html babel 可以识别React,并高亮显示ES6 command+shift+p -> install package -> babel 使用 在打 ...

  5. android的Environment类 Android存储访问及目录

    http://www.cnblogs.com/mengdd/p/3742623.html http://blog.csdn.net/barnett_zhubo/article/details/6832 ...

  6. Java 学习 day05

    01-面向对象(概述) 面向对象 -- 冰箱.打开:冰箱.存储:冰箱.关闭: 02-面向对象(举例) 使用和指挥 -- 对象,不需要关注过程,只关注结果: 一切皆对象,万物皆对象  -- 自<T ...

  7. EasyNVR摄像机H5流媒体服务器在windows上批处理脚本自动以管理员权限运行

    很多时候, 我们需要以管理员权限来运行批处理脚本, 比如操作 windows 服务. EasyNVR 中提供安装服务的批处理脚本, 运行这个bat文件, 自动将 EasyNVR 以 windows 服 ...

  8. 九度OJ 1021:统计字符 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5418 解决:3146 题目描述:     统计一个给定字符串中指定的字符出现的次数. 输入:     测试输入包含若干测试用例,每个测试用 ...

  9. JAVA Socket基础(简单实现)

    学习Socket需要了解的几个概念: Socket 指的是互联网连接中的各个终结点.互联网连接是怎么创建的,通过IP地址加端口号,进行互通. A电脑(192.168.3.125:80)>> ...

  10. Linux就该这么学--命令集合11(配置系统相关信息)

    1.配置主机名称: 查看主机名: hostname 修改主机名: vim /etc/hostname 2.配置网卡信息: 在红帽RHEL6系统中网卡配置文件的前缀为“ifcfg-eth”,第一块即为“ ...