原题链接在这里: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. Spring之2:HierarchicalBeanFactory接口

    HierarchicalBeanFactory:HierarchicalBeanFactory继承BeanFactory并扩展使其支持层级结构.getParentBeanFactory()方法或者父级 ...

  2. Git操作入门

    生成ssh key: ​ ssh-keygen -t rsa -C "lkt@temp.com" ​ 按三次回车,最后在.ssh文件夹下得到id_rsa和id_rsa.pub两个文 ...

  3. Python 数据结构理解分享

    摘要:分享学习Python数据结构的一些理解,主要包含序列(如列表和元组),映射(如字典)以及集合3中基本的数据结构,以及可变和不可变数据类型. Python 中的数据结构是根据某种方式将数据元素组合 ...

  4. 小白入门.有函数F(x)=(x+1)^2和G(x)=2x+1.输入x值计算F(G(x))。

    #include<stdio.h> #include<math.h> void main() { float x,y; printf("Please input x\ ...

  5. ALV报表——点击事件(二)

    目录 一.ALV点击事件(双击) 一.ALV点击事件(双击) 代码: *Report ZRFI001_XFL_TEST REPORT ZRFI001_XFL_TEST . *定义ALV所需要用到的类型 ...

  6. 淘宝API总结

    1. 淘宝客API https://open.alimama.com/?spm=a219t.11816995.1998910419.d8546b700.2a8f75a5C0NajI#!/documen ...

  7. Math.random()的加密安全替换方法window.crypto.getRandomValues

    Math.random() 返回介于 0(包含) ~ 1(不包含) 之间的一个随机数. Math.random()函数不是加密安全的随机数生成器. window.crypto.getRandomVal ...

  8. iOS - 基础知识总结(OC版) 面试必看 再不看就要用swift了

    OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...

  9. 【转】SpringCloud学习笔记(一)——基础

    什么是微服务架构 简单地说,微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进 ...

  10. HTML中的图片标签的用法!

    在HTML中<img>这个标签是定义文本中的图片标签,它的作用就比如说可以提供图片的名字.提供图片的尺寸大小和提供图片的一些图片属性,比如Alt这个属性,可以给图片一个名称来告诉朋友们.这 ...