Skip to content

Commit db26f2e

Browse files
committed
add 282 Expression Add Operators
1 parent bfc8da4 commit db26f2e

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-27 10:42:26
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
Once you can understand the solution space tree, you just get it.
10+
11+
Refer to:
12+
https://discuss.leetcode.com/topic/24523/java-standard-backtrace-ac-solutoin-short-and-clear
13+
*/
14+
vector<string> addOperators(string num, int target) {
15+
vector<string> ans;
16+
dfs_search(ans, "", num, target, 0, 0, 0);
17+
return ans;
18+
}
19+
20+
void dfs_search(vector<string> &ans, string path, const string &num,
21+
int target, int pos, long value, long pre_num){
22+
/*
23+
Put binary operator in pos, and then calculate the new value.
24+
25+
@pre_num: when process *, we need to know the previous number.
26+
*/
27+
if(pos == num.size()){
28+
if(value == target){
29+
ans.push_back(path);
30+
}
31+
return;
32+
}
33+
34+
for(int i=1; i+pos<=num.size(); i++){
35+
string cur_str = num.substr(pos, i);
36+
// Digit can not begin with 0 (01, 00, 02 are not valid), except 0 itself.
37+
if(i>1 && cur_str[0] == '0') break;
38+
long cur_d = stoll(cur_str);
39+
if(pos==0){
40+
dfs_search(ans, cur_str, num, target, pos+i, cur_d, cur_d);
41+
}
42+
// All three different binary operators: +, -, *
43+
else{
44+
dfs_search(ans, path+"+"+cur_str, num, target, pos+i, value+cur_d, cur_d);
45+
dfs_search(ans, path+"-"+cur_str, num, target, pos+i, value-cur_d, -cur_d);
46+
dfs_search(ans, path+"*"+cur_str, num, target, pos+i,
47+
value-pre_num+pre_num*cur_d, cur_d*pre_num);
48+
}
49+
}
50+
}
51+
};
52+
53+
/*
54+
"000"
55+
0
56+
"123"
57+
6
58+
"232"
59+
8
60+
"1005"
61+
5
62+
"3456237490"
63+
9191
64+
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# @Last Modified time: 2016-07-27 09:43:06
5+
6+
7+
class Solution(object):
8+
def addOperators(self, num, target):
9+
""" Once you can understand the solution space tree, you just get it.
10+
11+
Refer to:
12+
https://discuss.leetcode.com/topic/24523/java-standard-backtrace-ac-solutoin-short-and-clear
13+
"""
14+
ans = []
15+
self.dfs_search(ans, "", num, target, 0, 0, 0)
16+
return ans
17+
18+
def dfs_search(self, ans, path, num, target, pos, pre_num, value):
19+
""" Put binary operator in pos, and then calculate the new value.
20+
21+
@pre_num: when process *, we need to know the previous number.
22+
"""
23+
if pos == len(num):
24+
if value == target:
25+
ans.append(path)
26+
return
27+
28+
for i in range(pos + 1, len(num) + 1):
29+
cur_str, cur_n = num[pos: i], int(num[pos: i])
30+
# Digit can not begin with 0 (01, 00, 02 are not valid), except 0 itself.
31+
if i > pos + 1 and num[pos] == '0':
32+
break
33+
if pos == 0:
34+
self.dfs_search(ans, path + cur_str, num, target, i, cur_n, cur_n)
35+
# All three different binary operators: +, -, *
36+
else:
37+
self.dfs_search(ans, path + "+" + cur_str, num,
38+
target, i, cur_n, value + cur_n)
39+
self.dfs_search(ans, path + "-" + cur_str, num,
40+
target, i, -cur_n, value - cur_n)
41+
self.dfs_search(ans, path + "*" + cur_str, num,
42+
target, i, pre_num * cur_n, value - pre_num + pre_num * cur_n)
43+
44+
"""
45+
"000"
46+
0
47+
"123"
48+
6
49+
"232"
50+
8
51+
"1005"
52+
5
53+
"3456237490"
54+
9191
55+
"""

DepthFirstSearch/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ WikiPedia 里对 DFS 的说明如下:
3333

3434
[Clone Graph](https://leetcode.com/problems/clone-graph/) 是一个图的复制问题,可以用 DFS 来遍历图中所有的顶点。
3535

36+
# 例子,更好的理解
3637

37-
更多阅读
38+
## 282. Expression Add Operators
39+
40+
# 更多阅读
3841

3942
[Depth-first search](https://en.wikipedia.org/wiki/Depth-first_search)
4043

0 commit comments

Comments
 (0)