-
Notifications
You must be signed in to change notification settings - Fork 1
/
241DifferentWaysToAddParentheses.cs
149 lines (123 loc) · 4.1 KB
/
241DifferentWaysToAddParentheses.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _241DifferentWaysToAddParentheses
{
class Program
{
static void Main(string[] args)
{
}
/*
* Leetcode 241:
* Different ways to add parentheses
* Given a string of numbers and operators, return all possible results from computing all
* the different possible ways to group numbers and operators. The valid operators are +, - and *.
*
*/
public static int compute(int a, int b, char op)
{
switch (op)
{
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
}
return 0; // do not return anything meaningful
}
public static IList<int> diffWays_MissingMemorization(string s)
{
if (s==null)
return null;
int n1 = 0;
int n2 = s.Length;
int i = 0;
for (; i < s.Length && isDigit(s[i]); i++)
{
n1 = n1 * 10 + s[i] - '0';
}
IList<int> list = new List<int>();
// if pure number, just return
bool isPureNumber = i==n2;
if (isPureNumber)
{
list.Add(n1);
return list;
}
IList<int> lefts = new List<int>();
IList<int> rights = new List<int>();
lefts = diffWays_MissingMemorization(s.Substring(0, i));
rights = diffWays_MissingMemorization(s.Substring(i + 1, n2 - i));
for (int j = 0; j < lefts.Count; j++)
for (int k = 0; k < rights.Count; k++)
{
int a = lefts[j];
int b = rights[k];
char op = s[i];
int res = compute(a, b, op);
list.Add(res);
}
return list;
}
public static bool isDigit(char c)
{
int no = c - '0';
if (no >= 0 && no <= 9)
return true;
return false;
}
public static string getKey(int start, int end)
{
return start.ToString() + "_" + end.ToString();
}
public static IList<int> diffWaysToCompute(string s)
{
return diffWaysToComputeWithMemo(ref s, 0, s.Length - 1);
}
public static Hashtable memo = new Hashtable();
/*
* julia's comment:
* 1.
*/
public static IList<int> diffWaysToComputeWithMemo(ref string s, int start, int end)
{
string cache_key = getKey(start, end);
if (memo.Contains(cache_key))
return (IList<int>)memo[cache_key];
int n1 = 0;
int n2 = s.Length;
int i = start;
for (; i < end && isDigit(s[i]); i++)
{
n1 = n1 * 10 + s[i] - '0';
}
IList<int> list = new List<int>();
// if pure number, just return
bool isPureNumber = i == end;
if (isPureNumber)
{
list.Add(n1);
memo.Add(cache_key, list);
return list;
}
IList<int> lefts = new List<int>();
IList<int> rights = new List<int>();
lefts = diffWaysToComputeWithMemo(ref s, start, i-1);
rights = diffWaysToComputeWithMemo(ref s, i+1, end);
for (int j = 0; j < lefts.Count; j++)
for (int k = 0; k < rights.Count; k++)
{
int a = lefts[j];
int b = rights[k];
char op = s[i];
int res = compute(a, b, op);
list.Add(res);
}
memo.Add(cache_key, list);
return list;
}
}
}