10000 2 problems · hitzzc/go-leetcode@e14c84d · GitHub
[go: up one dir, main page]

Skip to content

Commit e14c84d

Browse files
committed
2 problems
1 parent 6579301 commit e14c84d

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
206206
#### [268. Missing Number](https://github.com/hitzzc/go-leetcode/tree/master/missing_number)
207207
#### [278. First Bad Version](https://github.com/hitzzc/go-leetcode/tree/master/missing_number)
208208
#### [279. Perfect Squares](https://github.com/hitzzc/go-leetcode/tree/master/perfect_squares)
209+
#### [282. Expression Add Operators](https://github.com/hitzzc/go-leetcode/tree/master/expression_add_operators)
210+
#### [283. Move Zeroes](https://github.com/hitzzc/go-leetcode/tree/master/expression_add_operators)
209211

210212

10000 211213

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
vector<string> addOperators(string num, int target) {
4+
vector<string> results;
5+
DFS(num, target, 0, "", 0, " ", 0, results);
6+
return results;
7+
}
8+
9+
void DFS(string& num, int target, int idx, string solution, long long current_val, string pre_op, long long pre_val, vector<string>& results) {
10+
if (current_val == target && idx == num.size()) {
11+
results.push_back(solution);
12+
return;
13+
}
14+
if (idx == num.size()) return;
15+
string n;
16+
long long v = 0;
17+
for (int i = idx; i < num.size(); ++i) {
18+
if (n =="0") {
19+
return;
20+
}
21+
n += num[i];
22+
v = 10*v + num[i] - '0';
23+
if (solution.size() == 0) {
24+
DFS(num, target, i+1, n, v, " ", 0, results);
25+
}else {
26+
DFS(num, target, i+1, solution + "+" + n, current_val + v, "+", v, results);
27+
DFS(num, target, i+1, solution + "-" + n, current_val - v, "-", v, results);
28+
if (pre_op == "+") {
29+
DFS(num, target, i+1, solution + "*" + n, current_val-pre_val+pre_val*v, pre_op, pre_val*v, results);
30+
}else if (pre_op == "-") {
31+
DFS(num, target, i+1, solution + "*" + n, current_val+pre_val-pre_val*v, pre_op, pre_val*v, results);
32+
}else {
33+
DFS(num, target, i+1, solution + "*" + n, current_val*v, "*", v, results);
34+
}
35+
}
36+
}
37+
return;
38+
}
39+
};

move_zeroes/move_zeroes.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
void moveZeroes(vector<int>& nums) {
4+
int j = 0;
5+
int tmp;
6+
for (int i = 0; i < nums.size(); ++i){
7+
if (nums[i] != 0){
8+
tmp = nums[i];
9+
nums[i] = nums[j];
10+
nums[j] = tmp;
11+
j++;
12+
}
13+
}
14+
return;
15+
}
16+
};

0 commit comments

Comments
 (0)
0