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. 【hdu5100】棋盘覆盖

    http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目大意: 用1*k的木块铺n*n的棋盘,求多铺满多少个单位格. 方法: n < k,显然无解:n ...

  2. mysql 库表整体相关查询

    select table_schema,table_name from information_schema.columns where column_name = '字段名'; 查询某张表有几条记录 ...

  3. 黑马程序员_毕向东_Java基础视频教程——进制的相互转换(随笔)

    进制的相互转换 二进制转十进制: 原理对十进制数进行除2运算(余数不是0 就是1) 6 的二进制: 6 / 2 = 3--0 3 / 2 = 1--1 1 / 2 = 0--1 余数倒序排列输出:11 ...

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

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

  5. 基于 abp vNext 和 .NET Core 开发博客项目 - 统一规范API,包装返回模型

    上一篇文章(https://www.cnblogs.com/meowv/p/12916613.html)使用自定义仓储完成了简单的增删改查案例,有心的同学可以看出,我们的返回参数一塌糊涂,显得很不友好 ...

  6. js读取json

    Json字符串是: [{"n":"aaa","un":"aaa"},{"n":"yang& ...

  7. unity-消息的注册,监听,回调

    最近在空闲时间准备做个小游戏,先把一些基本框架搭建好,本次记录的是消息的注册,监听和回调等 其实这些就是基于C#的委托(delegate) 第一步:定义一些委托 namespace Common.Me ...

  8. Java并发编程3-抽象同步队列AQS详解

    AQS是AtractQueuedSynchronizer(队列同步器)的简写,是用来构建锁或其他同步组件的基础框架.主要通过一个int类型的state来表示同步状态,内部有一个FIFO的同步队列来实现 ...

  9. hdu4746莫比乌斯反演进阶题

    Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others)Total S ...

  10. A + B Problem(hdu1000)

    注意,认真读题目的Input要求,看看是输入一组测试数据还是输入多组测试数据.输入多组数据,不要忘记while(). #include<iostream> using namespace ...