forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
123 lines (99 loc) · 3.52 KB
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/// Source : https://leetcode.com/problems/expression-add-operators/description/
/// Author : liuyubobobo
/// Time : 2018-09-07
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
/// Backtracking to split num and eval
/// Evaluate the expression on the fly :)
///
/// Time Complexity: O(3^n)
/// Space Complexity: O(3^n)
class Solution {
public:
vector<string> addOperators(string num, int target) {
if(num == "")
return {};
vector<string> ret;
split(num, 0, target, "", ' ', -1, 0ll, ret);
return ret;
}
private:
void split(const string& num, int index, int target, const string& expr,
char lastop, long long pre, long long res, vector<string>& ret){
if(index == num.size()){
if(res == (long long)target)
ret.push_back(expr);
return;
}
int end = num.size();
if(num[index] == '0')
end = index + 1;
for(int i = index + 1; i <= end; i ++){
int len = i - index;
string cur_num_s = num.substr(index, len);
long long cur = atol(cur_num_s.c_str());
char next_lastop = ' ';
long long next_pre = cur;
long long next_res = res;
if(expr[expr.size() - 1] == '*' && (lastop == '+' || lastop == '-')){
assert(pre != -1);
if(lastop == '+')
next_res -= pre, next_res += pre * cur;
else
next_res += pre, next_res -= pre * cur;
next_pre = pre * cur;
next_lastop = lastop;
}
else if(expr != ""){
switch(expr[expr.size() - 1]){
case '+': next_res += cur; break;
case '-': next_res -= cur; break;
case '*': next_res *= cur; break;
default:assert(false); break;
}
next_lastop = expr[expr.size() - 1];
}
else
next_res = cur;
if(index + len == num.size())
split(num, index + len, target, expr + cur_num_s,
' ', next_pre, next_res, ret);
else{
split(num, index + len, target, expr + cur_num_s + "*",
next_lastop, next_pre, next_res, ret);
split(num, index + len, target, expr + cur_num_s + "+",
next_lastop, next_pre, next_res, ret);
split(num, index + len, target, expr + cur_num_s + "-",
next_lastop, next_pre, next_res, ret);
}
}
}
};
void print_vec(const vector<string>& vec){
cout << "[ ";
for(const string& e: vec)
cout << e << " ";
cout << "]" << endl;
}
int main() {
string nums1 = "123";
print_vec(Solution().addOperators(nums1, 6)); //2
string nums2 = "232";
print_vec(Solution().addOperators(nums2, 8)); // 2
string nums3 = "105";
print_vec(Solution().addOperators(nums3, 5)); // 2
string nums4 = "00";
print_vec(Solution().addOperators(nums4, 0)); // 3
string nums5 = "3456237490";
print_vec(Solution().addOperators(nums5, 9191)); // 0
string nums6 = "2147483647";
print_vec(Solution().addOperators(nums6, 2147483647)); // 1
string nums7 = "2147483648";
print_vec(Solution().addOperators(nums7, -2147483648)); // 0
string nums8 = "123456789";
print_vec(Solution().addOperators(nums8, 45));
return 0;
}