generated from rayraydejesus/LeetCode-CSharp-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_1.cs
113 lines (99 loc) · 3.29 KB
/
Problem_1.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
public sealed class Problem_1 : LeetCodeProblem
{
public Problem_1(
int key = 1,
string title = "Two Sum",
string url = "https://leetcode.com/problems/two-sum/",
string description =
"Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target." +
"\n\nYou may assume that each input would have exactly one solution, and you may not use the same element twice." +
"\n\nYou can return the answer in any order." +
"\n\n \n\nExample 1:" +
"\n\nInput: nums = [2,7,11,15], target = 9" +
"\nOutput: [0,1]" +
"\nExplanation: Because nums[0] + nums[1] == 9, we return [0, 1]." +
"\nExample 2:" +
"\n\nInput: nums = [3,2,4], target = 6" +
"\nOutput: [1,2]" +
"\nExample 3:" +
"\n\nInput: nums = [3,3], target = 6" +
"\nOutput: [0,1]" +
"\n \n\nConstraints:" +
"\n\n2 <= nums.length <= 104" +
"\n-109 <= nums[i] <= 109" +
"\n-109 <= target <= 109" +
"\nOnly one valid answer exists."
) : base(key, title, url, description) {}
protected override void retrieveInput()
{
while(true)
{
base.retrieveInput();
string input = Console.ReadLine();
if (input == "")
Console.WriteLine("No input recieved.");
else
{
int leftBracket = -1;
int rightBracket = -1;
int targetEquals = -1;
// NOTE: Finds the brackets for array substring
for(int i = 0; i < input.Length; ++i)
{
if(input[i] == '[')
leftBracket = i + 1;
if (input[i] == ']')
{
rightBracket = i - leftBracket;
break;
}
}
// NOTE: Light error checking
if(leftBracket == -1 || rightBracket == -1)
{
if (!Prompts.invalidInput())
break;
else
continue;
}
// NOTE: Creates nums string for conversion
string numsString = input.Substring(leftBracket, rightBracket);
int[] nums = Array.ConvertAll(numsString.Split(','), int.Parse);
//Console.WriteLine(numsString);
//Console.WriteLine("[{0}]", string.Join(", ", nums));
// NOTE: Creates target string for conversion
string targetString = input.Substring(leftBracket + rightBracket);
targetEquals = targetString.IndexOf('=');
// NOTE: Light error checking
if (targetEquals == -1)
{
if (!Prompts.invalidInput())
break;
else
continue;
}
targetString = targetString.Substring(targetEquals + 1).Trim();
int target = int.Parse(targetString);
//Console.WriteLine(targetString);
//Console.WriteLine(target);
Input_1 parsedInput = new Input_1(nums, target);
this.runSolution(parsedInput);
if (!Prompts.anotherSolution())
break;
}
}
}
protected override void runSolution(object input)
{
Input_1 parsedInput = input as Input_1;
ISolution_1 solution = new Solution_1_Hashmap();
int[] result = solution.TwoSum(parsedInput.nums, parsedInput.target);
this.printSolution(result);
}
protected override void printSolution(object solution)
{
base.printSolution();
int[] result = solution as int[];
Console.WriteLine("[{0}]", string.Join(", ", result));
}
}