[LeetCode][JavaScript]Expression Add Operators

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 -> []

https://leetcode.com/problems/expression-add-operators/



NP完全问题,还是要一个个试,与前一题思路相近:http://www.cnblogs.com/Liok3187/p/4823785.html

每一轮都要拿一位,拿两位...直到最后(1+2+3+4, 12+3+4, 123+4),每个数都要试三种符号。

麻烦的是四则元算有优先级,如果之前是加法(2+3),再碰到乘法的话(2+3*2)需要回退。

开一个back的变量记录需要回退的数,分三种符号来处理。

1.加法(1+2,之前的val是1这一轮做+2),如果之后碰到乘法,需要回退当前的数(2)。

2.减法(1-2,之前的val是1这一轮做-2),如果之后碰到乘法,需要回退当前的数的相反数(-2)。

3.乘法(1+2*3*4,之前的val是1+2*3=7, 这一轮做*4),需要回退的数是上一轮传过来的(2*3),这一轮的val是7 - (2*3) + (2*3) * 4。

最后还要注意去掉以0开始的大于两位的数。

 1 /**
 2  * @param {string} num
 3  * @param {number} target
 4  * @return {string[]}
 5  */
 6 var addOperators = function(num, target) {
 7     var res = [];
 8     compute(num, "", 0, 0);
 9     return res;
10
11     function compute(nums, exp, val, back){
12         var tmp;
13         if(nums === "" && val === target){
14             res.push(exp);
15         }else{
16             for(var i = 1; i <= nums.length; i++){
17                 var head = nums.substring(0, i);
18                 var tail = nums.substring(i, nums.length);
19                 var curr = parseInt(head) || 0;
20                 if(head.length >= 2 && head[0] === "0"){
21                     return;
22                 }
23                 if(exp === ""){
24                     compute(tail, head, curr, curr);
25                 }else{
26                     compute(tail, exp + "+" + head, val + curr, curr);
27                     compute(tail, exp + "-" + head, val - curr, -1 * curr);
28                     compute(tail, exp + "*" + head, val - back + back * curr, back * curr);
29                 }
30             }
31         }
32     }
33 };

第一把想偷,只拼string不计算,最后调用恶名昭著的eval方法,妥妥的TLE,复杂度一样的,感觉是eval太花时间。

 1 /**
 2  * @param {string} num
 3  * @param {number} target
 4  * @return {string[]}
 5  */
 6 var addOperators_TLE = function(num, target) {
 7     var res = [];
 8     compute("", num);
 9     return res;
10
11     function compute(exp, str){
12         var tmp;
13         if(str === ""){
14             if(eval(exp) === target){
15                 res.push(exp);
16             }
17         }else{
18             for(var i = 1; i <= str.length; i++){
19                 var head = str.substring(0, i);
20                 var tail = str.substring(i, str.length);
21                 if(head.length >= 2 && head[0] === "0"){
22                     return;
23                 }
24                 if(exp === ""){
25                     compute(head, tail);
26                 }else{
27                     compute(exp + "+" + head, tail);
28                     compute(exp + "-" + head, tail);
29                     compute(exp + "*" + head, tail);
30                 }
31             }
32         }
33     }
34 };
时间: 2024-11-18 21:59:47

[LeetCode][JavaScript]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 operators +, -, or * between the digits so they evaluate to the target value. Examples: "123", 6 -> ["1+2+3", "1*2*3"] "

【LeetCode】282. 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", &q

[LeetCode] Expression Add Operators

This post shares a very nice solution, which is rewritten below. 1 class Solution { 2 public: 3 vector<string> addOperators(string num, int target) { 4 int n = num.length(); 5 if (!n) return {}; 6 vector<string> ans; 7 for (int i = 1; i <=

[string]Expression Add Operators

Total Accepted: 5561 Total Submissions: 26048 Difficulty: Hard 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

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

leetcode282 - Expression Add Operators - hard

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.Example 1:Input: num = "123", target = 6Output: [&quo

LeetCode282. 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. Example 1: Input: num = "123", target = 6 Output: [&

LeetCode 282: Expression Add Operation

Note: 1. When real index is 0. It will be only add as first element without any opeartors. 2. When the starting char of one layer is '0', it should break since it could not be 01, 02, 03...... class Solution { public List<String> addOperators(String

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

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