-
Notifications
You must be signed in to change notification settings - Fork 115
/
0155-MinStack.cs
59 lines (49 loc) · 1.29 KB
/
0155-MinStack.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
//-----------------------------------------------------------------------------
// Runtime: 132ms
// Memory Usage: 34.8 MB
// Link: https://leetcode.com/submissions/detail/322878007/
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
public class _0155_MinStack
{
private Node head;
/** initialize your data structure here. */
public _0155_MinStack()
{
head = null;
}
public void Push(int x)
{
if (head == null)
head = new Node(x, x);
else
head = new Node(x, Math.Min(x, head.MinValue), head);
}
public void Pop()
{
head = head.Next;
}
public int Top()
{
return head.Value;
}
public int GetMin()
{
return head.MinValue;
}
private class Node
{
public Node(int value, int minValue, Node next = null)
{
Value = value;
MinValue = minValue;
Next = next;
}
public int Value { get; set; }
public int MinValue { get; set; }
public Node Next { get; set; }
}
}
}