LeetCode 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) +, -, 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 -> []
题解:
It needs all the possible combinations. Thus it needs to use DFS.
DFS states need rest string, target, current accumlated value, the diff added last time, current combination path and res.
每次把新的num.substring按照三种方式添加到当前结果cur中,若是cur==target时, num string 也走到尾部,就把这个item结果加到res中.
从num string 中依次取出长度大于1的string来生成一个数字, cur 记录当前运算后的结果,preCurDiff用来记录最后变化. e.g. 2+3*2,即将要运算到乘以2的时候,上次循环的cur = 5, 是2 加上 3 得到的,所以preCurDiff = 3, 而要算这个乘2的时候,需要把preCurDiff先从cur中减掉,在加上新的diff. 新的 diff 是3*2=6.
数字是可以合并出现的,比如"123", 15能返回"12+3".
若是出现 2*05 这种情况,要排除.
用Long型是防止溢出.
Time Complexity: exponential.
Space: O(n). n是num长度.
AC Java:
public class Solution {
public List<String> addOperators(String num, int target) {
List<String> res = new ArrayList<String>();
dfs(num, target, 0, 0, "", res);
return res;
}
private void dfs(String num, int target, long cur, long preCurDiff, String item, List<String> res){
if(cur == target && num.length() == 0){ //cur加到了target 并且没有剩余的num string
res.add(item);
return;
}
//从头开始,每次从头取不同长度的string 作为curStr, 作为首个数字
for(int i = 1; i<=num.length(); i++){
String curStr = num.substring(0,i);
if(curStr.length() > 1 && curStr.charAt(0) == '0'){ //去掉corner case 1*05
break;
}
String nextStr = num.substring(i);
if(item.length() == 0){ //当前item为空,说明第一个数字
dfs(nextStr, target, Long.valueOf(curStr), Long.valueOf(curStr), curStr, res);
}else{
dfs(nextStr, target, cur + Long.valueOf(curStr), Long.valueOf(curStr), item + "+" + curStr, res);
dfs(nextStr, target, cur - Long.valueOf(curStr), -Long.valueOf(curStr), item + "-" + curStr, res);
dfs(nextStr, target, cur-preCurDiff + preCurDiff*Long.valueOf(curStr), preCurDiff*Long.valueOf(curStr), item + "*" + curStr, res);
}
}
}
}
LeetCode Expression Add Operators的更多相关文章
- [LeetCode] Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...
- [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 ...
- 【LeetCode】282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- 282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- LeetCode282. Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
随机推荐
- 清除 WD MyCloud 自动生成的 .wdmc 目录
1. 先 SSH,停止相应服务 /etc/init.d/wdmcserverd stop/etc/init.d/wdphotodbmergerd stop 2. 禁止服务自启动 update-rc.d ...
- QQ空间分享功能(二)
http://sns.z.qq.com/tools/share/demo_html.jsp 手机QQ空间分享功能接入指引: 1.请求地址: http://sns.z.qq.com/share 2.请 ...
- 用MyEclipse搭建SSH框架(Struts2 Spring Hibernate)
1.new一个web project. 2.右键项目,为项目添加Struts支持. 点击Finish.src目录下多了struts.xml配置文件. 3.使用MyEclipse DataBase Ex ...
- Phaser提供了Button对象简单的实现一个按钮
Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希 ...
- APT 常用功能
apt-get install package 安装包 apt-get reinstall package 重新安装包 apt-get upgrade 更新已安装的包 #apt-get update ...
- Mac 配置环境变量
系统:mac OS 10.10.3 or later 1.如果不能使用一些常用终端命令,如ls,open.可能是环境变量配置不当导致的.尤其是手动修改 输入以下命令: export PATH=/us ...
- [Albert 朗读行动记录贴]采纳Scalers方法:口语成长行动
目标:100小时成长计划,持续朗读录音100小时. 行动计划:每天点评美音3个人的朗读,英音1个. 完成朗读计划,录一段.附录一段.1分半左右. 开始日期:3月18日 原帖: [335][合辑]Sca ...
- centos7 设置中文
查看系统版本[root@webtest76 ~]# cat /etc/redhat-releaseCentOS Linux release 7.0.1406 (Core) [root@localhos ...
- 推荐给开发者的20个优秀PHP框架
推荐给开发者的20个优秀PHP框架 来源:developerslane 时间:2015-01-13 19:48:06 阅读数:111916 分享到:14 [导读] PHP是非常受欢迎并且很有影 ...
- Support vector machine
https://en.wikipedia.org/wiki/Support_vector_machine In machine learning, support vector machines (S ...