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

(M) Evaluate Reverse Polish Notation (M) Basic Calculator (M) Basic Calculator II (M) Different Ways to Add Parentheses

class Solution {
public:
    void dfs(string nums,int nums_size,int start_pos,vector<string>& res, int target,string &one_res,long long int total_res,long long int exp_res,char pre_op)
    {
        for(int i=start_pos;i<nums_size;i++){
            if(i>start_pos && nums[start_pos]==‘0‘){
                return;
            }
            long long  int num = 0;
            for(int j=start_pos;j<=i;j++){
                num = num*10+(nums[j]-‘0‘);
            }
            int one_res_size = one_res.size();
            if(start_pos!=0){
                one_res.push_back(pre_op);
            }
            one_res += nums.substr(start_pos,i-start_pos+1);
            long long int tmp_exp_res = exp_res;
            switch(pre_op){
                case ‘*‘ : exp_res *= num; break;
                case ‘-‘ : exp_res = -num; break;
                case ‘+‘ : exp_res =  num; break;
            }
            if(i+1==nums_size){
                if(total_res+exp_res == target){
                    res.push_back(one_res);
                }
            }else{
                dfs(nums,nums_size,i+1,res,target,one_res,  total_res,         exp_res, ‘*‘);
                dfs(nums,nums_size,i+1,res,target,one_res,  total_res+exp_res, 0,       ‘+‘);
                dfs(nums,nums_size,i+1,res,target,one_res,  total_res+exp_res, 0,       ‘-‘);
            }
            one_res.erase(one_res.begin()+one_res_size,one_res.end());
            exp_res = tmp_exp_res;
        }
    }
    vector<string> addOperators(string nums, int target) {
        int nums_size = nums.size();
        vector<string> res;
        string one_res;
        dfs(nums,nums_size,0,res,target,one_res,0,0,‘+‘);
        return res;
    }
};

Next challenges: (M) Evaluate Reverse Polish Notation (M) Different Ways to Add Parentheses

时间: 2025-01-06 01:41:20

[string]Expression Add Operators的相关文章

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

[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

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

282. Expression Add Operators

解题思路:深搜 遍历所有可能的情况,终止条件为表达式计算的结果等于target.注意,这里要使用long 类型保存整数,否则会溢出.我在vs2013,win32模式下使用long同样不能通过 "3456237490", 9191 -> [] 这个案例,改用long long 才可以. 具体解析可以参考文末的这篇文章,讲的很好,主要要注意乘法情况下如何计算表达式的值. class Solution { public: void compute(vector<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"]&