-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.hpp
More file actions
114 lines (85 loc) · 2.4 KB
/
Solution.hpp
File metadata and controls
114 lines (85 loc) · 2.4 KB
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
//
// Created by CFWLoader on 12/29/17.
//
#ifndef PROBLEM282_SOLUTION_HPP
#define PROBLEM282_SOLUTION_HPP
#include <vector>
using std::vector;
using std::string;
using std::cout;
using std::endl;
class Solution {
public:
vector<string> addOperators(string num, int target) {
if(num.size() < 1)
{
return vector<string>();
}
vector<vector<int>> combinations;
for(int len = 1; len <= num.size(); ++len)
{
vector<int> preSeq;
preSeq.push_back(this->convertInt(num, 0, len));
this->generateCombinations(combinations, preSeq, num, len);
}
// cout << "-----------Result-----------" << endl;
// for(auto seq : combinations)
// {
// printIntVec(seq);
// }
int sum = 0;
vector<string> resultStrings;
for(auto combi : combinations)
{
sum = combi[0];
if(combi.size() == 1)
{
if(sum == target)
{
}
}
}
return vector<string>();
}
void generateCombinations(vector<vector<int>>& combinations, const vector<int>& preSeq, string num, int startIdx)
{
if(startIdx >= num.size())
{
combinations.push_back(vector<int>(preSeq.begin(), preSeq.end()));
return;
}
for(int len = 1; startIdx + len <= num.size(); ++len)
{
vector<int> curPreSeq(preSeq.begin(), preSeq.end());
// cout << "Getting: " << convertInt(num, startIdx, len) << endl;
curPreSeq.push_back(this->convertInt(num, startIdx, len));
this->generateCombinations(combinations, curPreSeq, num, startIdx + len);
}
}
int convertInt(const string& num, int startIdx, int len)
{
int val = 0;
for(int idx = startIdx; idx < startIdx + len; ++idx)
{
val = val * 10 + num[idx] - '0';
}
return val;
}
void printIntVec(const vector<int>& vec)
{
cout << "[";
for(auto val :vec)
{
cout << val << " ";
}
cout << "]" << endl;
}
// void generateCombinations(vector<vector<int>>& combinations, string num, int startIdx)
// {
// if(startIdx == nums.size())
// {
// return;
// }
// }
};
#endif //PROBLEM282_SOLUTION_HPP