-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReverseString.cs
58 lines (51 loc) · 1.75 KB
/
ReverseString.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
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 344. Reverse String
/// Write a function that takes a string as input and returns the string reversed.
/// IE) Input: "hello"
/// Output: "olleh"
/// </summary>
public class ReverseString : Solution
{
private string _inputString;
public ReverseString(string inputString)
{
this._inputString = inputString;
}
private string reverseString(string inputString)
{
// Loop backwards from the end of the string.
// Time Complexity : O(n)
// Space Complexity: O(n) where n is the length of the inputString
var reverse = inputString.ToCharArray();
Array.Reverse(reverse);
return new string(reverse);
/* Passes 475/476 Test Cases: Time Limit Exceeded
if (inputString.Length<=1)
{
return _inputString;
}
string reversedString = "";
for (int i = inputString.Length - 1; i >= 0; i--)
{
reversedString += inputString[i];
}
return reversedString;
*/
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = reverseString(this._inputString);
watch.Stop();
Console.WriteLine($"344. Reverse String\n" +
$"Input String = {this._inputString} \n" +
$"Result: [{results}] \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}