原题链接在这里: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 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 表达式添加运算符

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

  3. 【LeetCode】282. Expression Add Operators

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

  4. 282. Expression Add Operators

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

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

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

  6. LeetCode Expression Add Operators

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

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

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

  8. [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators

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

  9. Expression Add Operators

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

随机推荐

  1. [转帖]Linux内核系统体系概述

    Linux内核系统体系概述 https://www.cnblogs.com/alantu2018/p/8447369.html Linux 内核主要由 5 个模块构成,它们分别是: 进程调度模块 用来 ...

  2. luoguP1823 [COI2007] Patrik 音乐会的等待

    题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么他们是可以互相看得见的. ...

  3. go 食用指南

    Golang高效食用秘籍 一.关于构建 1.1 go环境变量 $ go env // 查看go 的环境变量 其中 GOROOT 是golang 的安装路径 GOPATH 是go命令依赖的一个环境变量 ...

  4. 在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串)

    原文:在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  5. dapper 参数不定时用这种方法动态参数

    string where = null; var p = new DynamicParameters(); if (classId != null) { where = " and clas ...

  6. C#基础之结构和类

    大家在平时的工作中对类的使用应该是比较多的,但是在结构使用方面可能稍微少点,这里我就总结一下结构和类的一些异同之处,如有错误之处,还请指正. 结构是值类型,类是引用类型,结构通常用来封装小型相关变量组 ...

  7. Jenkins 发邮件的Job

    Jenkins要做到构建失败的时候发送邮件,常规做法是加个全局的post failure,类似这样的代码 pipeline { agent any stages { stage('deploy') { ...

  8. SQL根据指定节点ID获取所有父级节点和子级节点(转载)

    --根据指定节点ID获取所有子节点-- WITH TEMP AS ( ' --表的主键ID UNION ALL SELECT T0.* FROM TEMP,table_name T0 WHERE TE ...

  9. 基于NetCore+SqlSugar+Layui开发出来的开源框架项目FytSoaCms问题处理

    最近刚好在学习NetCore框架所以就在网上搜索了一下相关的开源框架项目,正好在Github上找到了一个不错的开源框架所以推荐给大家看看哈哈哈. 1:项目相关技术 运行NetCore SDK版本为2. ...

  10. C#判断字符串中含有多少个汉字

    private void button1_Click(object sender, EventArgs e) { ArrayList itemList = new ArrayList(); CharE ...