leetcode_c++:Divide and Conquer: Expression Add Operators(282)

原创 2016年08月28日 15:42:24

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.

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

class Solution {
public:
    vector<string> addOperators(string num, int target) {
        vector<string> result;
        if (num.size() == 0) return result;
        helper(num, target, result, "", 0, 0, 0, ' ');
        return result;        
    }

    //DFS algorithm
    void helper(const string &num, const int target, //`num` and `target` never change
                vector<string>& result, // the array store all of the answers
                string solution, //the current potential answer.
                int idx, // the current index of `num` array
                long long val, // the current value we calculated so far
                long long prev, // the lastest value we used for calculation, which used for operation prioirty adjustment
                char preop ) // the latest "+" or "-" operation, which used for operation prioirty adjustment 
    {

        if (target == val && idx == num.size()){
            result.push_back(solution);
            return;
        }
        if (idx == num.size()) return;

        string n;
        long long v=0;
        for(int i=idx; i<num.size(); i++) {
            //get rid of the number which start by "0"
            //e.g.  "05" is not the case.
            if (n=="0") return;

            n = n + num[i];
            v = v*10 + num[i]-'0';

            if (solution.size()==0){ 
                // the first time for initialization
                helper(num, target, result, n, i+1, v, 0, ' ');
            }else{
                // '+' or '-' needn't to adjust the priority
                helper(num, target, result, solution + '+' + n, i+1, val+v, v, '+');
                helper(num, target, result, solution + '-' + n, i+1, val-v, v, '-');

                //if we meet multiply operation, we need adjust the calcualtion priority
                // e.g. if the previous value is calculated by 2+3=5, 
                //      then if we need to multipy 4, it is not 5*4, it is 2+3*4=2+12=24
                //      we need be careful about multiply again, such as: 2+3*4*5
                if (preop=='+') {
                    helper(num, target, result, solution + '*' + n, i+1, (val-prev)+prev*v, prev*v, preop);
                }else if (preop=='-'){
                    helper(num, target, result, solution + '*' + n, i+1, (val+prev)-prev*v, prev*v, preop);
                }else {
                    helper(num, target, result, solution + '*' + n, i+1, val*v, v, '*');
                }
            }
        }

    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

算法导论学习笔记之二--分而治之(divide-and-conquer approach)

如果一个问题当它的规模缩小的时候,问题性质不变,并且问题的规模最小的时候简单可解,就可以采用divide-and-conquer 方法。 divide-and-conquer 分以下4步进行: 直接...
  • institute
  • institute
  • 2014年03月02日 17:21
  • 2772

分治算法(divide and conquer)

0) 引论 正如名字divide and conquer所言,分治算法分为两步,一步是divide,一步是conquer。 Divide:Smaller Problems are solved rec...
  • changyuanchn
  • changyuanchn
  • 2013年12月09日 12:07
  • 4761

OpenCV 学习(图像的基本运算)

OpenCV 学习(图像的基本运算)图像的基本运算有很多种,比如两幅图像可以相加、相减、甚至可以相乘、相除。图像可以放大、缩小、旋转,还可以截取中间的一副子图,各个颜色通道还可以分别提取。总之,对于图...
  • liyuanbhu
  • liyuanbhu
  • 2015年10月05日 20:53
  • 14300

Leetcode 282. Expression Add Operators

282. Expression Add Operators Total Accepted: 17890 Total Submissions: 65879 Difficulty: Hard ...
  • fantasiasango
  • fantasiasango
  • 2016年10月07日 23:19
  • 231

LeetCode 282. Expression Add Operators(表达式)

原题网址:https://leetcode.com/problems/expression-add-operators/ Given a string that contains only ...
  • jmspan
  • jmspan
  • 2016年04月14日 09:11
  • 683

Leetcode 282. Expression Add Operators[hard]

题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to ad...
  • qq379548839
  • qq379548839
  • 2016年09月24日 10:50
  • 132

LeetCode-282. Expression Add Operators (JAVA)表达式求值

LeetCode-282. Expression Add Operators (JAVA)表达式求值
  • mine_song
  • mine_song
  • 2017年04月30日 17:35
  • 259

[LeetCode]282. Expression Add Operators

282. Expression Add Operators Given a string that contains only digits 0-9 and a target value, ret...
  • Johnkui
  • Johnkui
  • 2016年04月13日 01:29
  • 452

282. Expression Add Operators

解题思路:深搜 遍历所有可能的情况,终止条件为表达式计算的结果等于target。注意,这里要使用long 类型保存整数,否则会溢出。我在vs2013,win32模式下使用long同样不能通过 "3...
  • JANESTAR
  • JANESTAR
  • 2016年08月08日 19:08
  • 505

Divide and Conquer (1) -- Kth Largest Element in an Array,Different Ways to Add Parentheses

Kth Largest Element in an Array,Different Ways to Add Parentheses
  • dwt0317
  • dwt0317
  • 2017年01月12日 22:59
  • 122
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:Divide and Conquer: Expression Add Operators(282)
举报原因:
原因补充:

(最多只允许输入30个字)