282. 给表达式添加运算符

给定一个仅包含数字 0-9 的字符串和一个目标值,在数字之间添加二元运算符(不是一元)+、- 或 * ,返回所有能够得到目标值的表达式。

示例 1:

输入: num = “123”, target = 6

输出: [“1+2+3”, “123”]

示例 2:

输入: num = “232”, target = 8

输出: [“23+2", "2+32”]

示例 3:

输入: num = “105”, target = 5

输出: [“1*0+5”,“10-5”]

示例 4:

输入: num = “00”, target = 0

输出: [“0+0”, “0-0”, “0*0”]

示例 5:

输入: num = “3456237490”, target = 9191

输出: []

class Solution {
char[] num;
char[] exp;
int target;
List<String> res;
public List<String> addOperators(String num, int target) {
this.res = new ArrayList<>();
this.num = num.toCharArray();
this.target = target;
this.exp = new char[num.length()*2];
dfs(0,0,0,0);
return res;
} private void dfs(int pos,int len,long prev,long curr) {
if(pos == num.length) {
if(curr==target) {
res.add(new String(exp,0,len));
}
return;
}
/**
* s 记录该次 dfs的起始位置
* pos是num的位置
*/
int s = pos;
/**
* len是 放数字 的位置
* l是 放运算符 的位置
*/
int l = len;
if(s!=0) {
len++;
}
long n = 0;
while (pos < num.length){
if(num[s] =='0' && pos-s>0) {
break;
}
n = n*10+(int)(num[pos] -'0');
if(n > Integer.MAX_VALUE){
break;
}
exp[len++] = num[pos++];
if(s==0) {
dfs(pos,len,n,n);
continue;
}
exp[l] = '+';
dfs(pos,len,n,curr+n);
exp[l] = '-';
dfs(pos,len,-n,curr-n);
exp[l] = '*';
dfs(pos,len,prev*n,curr-prev+prev*n);
}
}
}

Java实现 LeetCode 282 给表达式添加运算符的更多相关文章

  1. leetcode 282. 给表达式添加运算符

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

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

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

  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. [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators

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

  5. Java实现 LeetCode 241 为运算表达式设计优先级

    241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...

  6. JavaWeb学习之JSP常用标签、EL表达式的运算符、JSTL标签库(6)

    1.JSP常用标签 * 只要支持JSP文件,常用标签有可以直接使用 * 格式: jsp:xxxx * jsp:forward ,完成jsp页面的转发 * page属性:转发的地址 <% requ ...

  7. Java 终于有 Lambda 表达式啦~Java 8 语言变化——Lambda 表达式和接口类更改【转载】

    原文地址 en cn 下载 Demo Java™ 8 包含一些重要的新的语言功能,为您提供了构建程序的更简单方式.Lambda 表达式 为内联代码块定义一种新语法,其灵活性与匿名内部类一样,但样板文件 ...

  8. Java for LeetCode 057 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  9. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. Java中Error和Exception的异同以及运行时异常(Runtime exception)与检查型异常(checked exception)的区别

    一:Error和Exception的基本概念: 首先Exception和Error都是继承于Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕 ...

  2. 线程和Python—Python多线程编程

    线程和Python 本节主要记录如何在 Python 中使用线程,其中包括全局解释器锁对线程的限制和对应的学习脚本. 全局解释器锁 Python 代码的执行是由 Python 虚拟机(又叫解释器主循环 ...

  3. ABP框架踩过的坑系列6

    ABP框架踩过的坑系列6 应是无事.齐侯方才的确到了吴纠庭院https://www.mixcloud.com/ltTFvU888smi8jS/几日行军劳顿其实齐侯本应该睡下了https://www.m ...

  4. PAT 1011 World Cup Betting (20分) 比较大小难度级别

    题目 With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly exc ...

  5. Unity3D UGUI Image与父级保持比例缩放

    using UnityEngine; using System.Collections; using UnityEngine.UI; public class X_RectAutoSize : Mon ...

  6. LAMP搭建wordpress

    centos7安装Apache centos7安装mysql8 centos7安装php7 先登录mysql创建一个wordpress的数据库 create database wordpress 下载 ...

  7. JavaDoc文件如何生成

    目录 如何使用 1.通过命令行生成JavaDoc文档 2.IDEA如何配置后生成javadoc文档 javadoc命令是用来生成自己的API文档的 参考信息: @author 作者名 @version ...

  8. JS轮播图带序号小点和左右按钮

    轮播图作为前端比较简易的动画,使用非常频繁,这里记录以便使用 此轮播图为最简易自动播放,非无缝,但有按钮,有序号跳转小点 想看全套轮播图可以查看我的分类轮播图全套 html布局 <div sty ...

  9. Spring Boot集成Shrio实现权限管理

    Spring Boot集成Shrio实现权限管理   项目地址:https://gitee.com/dsxiecn/spring-boot-shiro.git   Apache Shiro是一个强大且 ...

  10. Redis数据迁移同步工具(redis-shake)

    前言 最近线上一台自建redis服务的服务器频繁报警,内存使用率有点高,这是一台配置比较简陋(2C8G)的机子了,近期也打算准备抛弃它了.抛弃之前需对原先的数据进行迁移,全量数据,增量数据都需要考虑, ...