-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPalindromeNumber.cs
72 lines (64 loc) · 2.6 KB
/
PalindromeNumber.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
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 9. Palindrome Number
/// Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backwards as forward.
/// IE) Input : -121
/// Output : false
/// Explanation: From left to right, it reads -121. From right to left, it reads 121-. Therefore it is NOT a palindrome.
/// </summary>
public class PalindromeNumber : Solution
{
private int _num;
/// <summary>
/// Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backwards as forward.
/// </summary>
/// <param name="num"></param>
public PalindromeNumber(int num)
{
this._num = num;
}
// Time Complexity: O(2n), n == number of digits in integer. Because we have to do a 2 loops, 1 for pushing and 1 for popping.
// Space Complexity: O(n), n == number of digits in integer. This is because we need to create a stack with enough space for each digit.
private bool isPalindrome(int num)
{
// 1) Create a stack and push each digit onto the stack.
// 2) At the end, pop each number off the stack
// see if resulting numbers reads the same forwards and backwards
if (num < 0)
{
return false;
}
// 1)
string numToString = num.ToString();
Stack<char> stack = new Stack<char>();
for (int i = 0; i <= numToString.Length - 1; i++)
{
//Console.WriteLine($"Stack Push: {numToString[i]}");
stack.Push(numToString[i]);
}
// 2)
string result = "";
for (int i = 0; i <= numToString.Length - 1; i++)
{
//Console.WriteLine($"Stack Pop: {stack.Peek()}");
result += stack.Pop();
}
return result == numToString;
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = isPalindrome(this._num);
watch.Stop();
Console.WriteLine($"9. Palindrome Number\n" +
$"Input Number = {this._num.ToString()}\n" +
$"Result: {results} \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}