码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]Expression Add Operators

时间:2015-09-21 01:27:19      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

 

 

 

[LeetCode][JavaScript]Expression Add Operators

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4824775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!