本文整理汇总了C#中Stack.Max方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Max方法的具体用法?C# Stack.Max怎么用?C# Stack.Max使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.Max方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MaxArea
public int MaxArea(int[] height)
{
var maxArea = 0;
if (height.Length > 0)
{
var startIndexes = new Stack<int>();
startIndexes.Push(0);
for (var index = 1; index < height.Length; index++)
if (height[index] > height[startIndexes.Peek()])
startIndexes.Push(index);
var endIndex = height.Length - 1;
maxArea = startIndexes.Max(startIndex => (endIndex - startIndex) * (height[endIndex] > height[startIndex] ? height[startIndex] : height[endIndex]));
for (var index = endIndex - 1; index >= 0; index--)
if (height[index] > height[endIndex])
{
endIndex = index;
while (startIndexes.Peek() > index)
startIndexes.Pop();
foreach (var startIndex in startIndexes)
{
var area = (index - startIndex) * (height[index] > height[startIndex] ? height[startIndex] : height[index]);
if (area > maxArea)
maxArea = area;
}
}
}
return maxArea;
}
示例2: Main
public static void Main()
{
Stack<int> numbers = new Stack<int>();
Stack<int> maxNumbers = new Stack<int>();
int numberOfQueries = int.Parse(Console.ReadLine());
int maxNumber = int.MinValue;
for (int i = 0; i < numberOfQueries; i++)
{
int[] query =
Console.ReadLine()
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToArray();
if (query[0] == 1)
{
if (query[1] > maxNumber)
{
maxNumber = query[1];
maxNumbers.Push(maxNumber);
}
numbers.Push(query[1]);
} else if (query[0] == 2)
{
if (query[0] == 1)
{
if (numbers.Peek() == maxNumber)
{
maxNumbers.Pop();
}
numbers.Pop();
} else if (query[0] == 3)
{
maxNumbers.Push(numbers.Max());
}
}
foreach (int outputNumber in maxNumbers)
{
Console.WriteLine(outputNumber);
}
}
}
示例3: Main
public static void Main()
{
int numberOfLines = int.Parse(Console.ReadLine());
Stack<long> stack = new Stack<long>();
long max = long.MinValue;
for (int i = 0; i < numberOfLines; i++)
{
int[] args = Console.ReadLine()
.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToArray();
int type = args[0];
switch (type)
{
case 1:
int numberToPush = args[1];
stack.Push(numberToPush);
if (numberToPush > max)
{
max = numberToPush;
}
break;
case 2:
stack.Pop();
if (!stack.Contains(max))
{
max = stack.Max();
}
break;
case 3:
Console.WriteLine(max);
break;
}
}
}
示例4: StackExtensions_Max_ThrowsExceptionIfStackIsEmpty
public void StackExtensions_Max_ThrowsExceptionIfStackIsEmpty()
{
var stack = new Stack<Int32>();
Assert.That(() => stack.Max(),
Throws.TypeOf<InvalidOperationException>());
}
示例5: StackExtensions_Max_ReturnsMaxValue
public void StackExtensions_Max_ReturnsMaxValue()
{
var stack = new Stack<Int32>();
stack.Push(4);
stack.Push(5);
stack.Push(6);
stack.Push(99);
stack.Push(10);
stack.Push(1);
stack.Push(12);
stack.Push(45);
var result = stack.Max();
TheResultingValue(result).ShouldBe(99);
}
示例6: StackExtensions_Max_ThrowsExceptionIfStackIsEmpty
public void StackExtensions_Max_ThrowsExceptionIfStackIsEmpty()
{
var stack = new Stack<Int32>();
stack.Max();
}