-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
57 lines (52 loc) · 1.44 KB
/
Solution.cs
File metadata and controls
57 lines (52 loc) · 1.44 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
public class NumArray
{
long[] sum;
public NumArray(int[] nums)
{
int n = nums.Length;
sum = new long[n + 1];
for (int i = 0; i < n; ++i)
{
sum[i + 1] = sum[i] + nums[i];
}
}
public int SumRange(int left, int right)
{
long ret = sum[right + 1] - sum[left];
return (int)ret;
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.SumRange(left,right);
*/
public class Solution
{
public List<dynamic> Execute(string[] actions, dynamic values)
{
List<dynamic> result = [];
object[] objectList = JsonConvert.DeserializeObject<object[]>(values);
NumArray numArr = null;
for (int i = 0; i < actions.Length; i++)
{
switch (actions[i])
{
case "NumArray":
numArr = new NumArray(CastType<int[]>(objectList[i])[0]);
result.Add(null);
break;
case "sumRange":
result.Add(numArr.SumRange(CastType<int>(objectList[i])[0], CastType<int>(objectList[i])[1]));
break;
default:
break;
}
}
return result;
}
private T[] CastType<T>(object value)
{
return JsonConvert.DeserializeObject<T[]>(JsonConvert.SerializeObject(value));
}
}