public class Solution
{
public int[] MaxSlidingWindow(int[] nums, int k)
{
MyCircularDeque deque=new MyCircularDeque(k);
int[] result = new int[nums.Length-k+1];
int j = 0;
for (int i = 0; i < nums.Length; i++)
{
if (i < k - 1)
deque.push(nums[i]);
else
{
int a= deque.push(nums[i]);
result[j++]=a;
if (!deque.IsEmpty() && deque.GetFront() == nums[i - k + 1])
deque.DeleteFront();
}
}
return result;
}
}
public class MyCircularDeque
{
private int[] data;
private int front;
private int rear;
public int MaxSize { get; }
public int Length { get; set; }
public MyCircularDeque(int k)
{
data = new int[k];
MaxSize = k;
Length = 0;
rear = front = -1;
}
public void InsertFront(int value)
{
if (Length == MaxSize)
throw new Exception("队列为空");
if (Length == 0)
{
data[0] = value;
front = rear = 0;
}
else if (front == 0)
{
front = MaxSize - 1;
data[front] = value;
}
else
data[--front] = value;
Length++;
}
public void InsertLast(int value)
{
if (Length == MaxSize)
throw new Exception("队列已满");
if (Length == 0)
{
data[0] = value;
front = rear = 0;
}
else if (rear == MaxSize - 1)
{
rear = 0;
data[rear] = value;
}
else
data[++rear] = value;
Length++;
}
public void DeleteFront()
{
if (Length == 0)
throw new Exception("队列为空");
if (Length == 1)
front = rear = -1;
else if (front == MaxSize - 1)
front = 0;
else
front++;
Length--;
}
public void DeleteLast()
{
if (Length == 0)
throw new Exception("队列为空");
if (Length == 1)
front = rear = -1;
else if (rear == 0)
rear = MaxSize - 1;
else
rear--;
Length--;
}
public int GetFront()
{
if (Length == 0)
return -1;
return data[front];
}
public int GetRear()
{
if (Length == 0)
return -1;
return data[rear];
}
public bool IsEmpty()
{
return Length == 0;
}
public int push(int value)
{
while (Length != 0 && value > GetRear())
DeleteLast();
InsertLast(value);
return GetFront();
}
}
题目C#代码public class Solution { public int[] MaxSlidingWindow(int[] nums, int k) { MyCircularDeque deque=new MyCircularDeque(k); int[] result = new int[nums.Length-k+1]; int j = 0; for (i