ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

Õ»ºÍ¶ÓÁÐ(¶þ)
2014-11-24 02:20:18 ¡¾´ó ÖРС¡¿ ä¯ÀÀ:1224´Î
Tags£º¶ÓÁÐ
e queue2;
public NewStack()
{
queue1 = new Queue();
queue2 = new Queue();
}
public void Push(int element)
{
if (queue1.Count == 0)
queue2.Enqueue(element);
else
queue1.Enqueue(element);
}
public int Pop()
{
if (queue1.Count == 0 && queue2.Count == 0)
throw new Exception(¡°The stack is empty¡±);
if (queue1.Count > 0)
{
while (queue1.Count > 1)
{
queue2.Enqueue(queue1.Dequeue());
}
//»¹Ê£Ò»¸ö
return (int)queue1.Dequeue();
}
else //queue2.Count > 0
{
while (queue2.Count > 1)
{
queue1.Enqueue(queue2.Dequeue());
}
//»¹Ê£Ò»¸ö
return (int)queue2.Dequeue();
}
}
public int Peek()
{
if (queue1.Count == 0 && queue2.Count == 0)
throw new Exception(¡°The stack is empty¡±);
int result = 0;
if (queue1.Count > 0)
{
while (queue1.Count > 1)
{
queue2.Enqueue(queue1.Dequeue());
}
//»¹Ê£Ò»¸ö
result = (int)queue1.Dequeue();
queue2.Enqueue(result);
}
else //queue2.Count > 0
{
while (queue2.Count > 1)
{
queue1.Enqueue(queue2.Dequeue());
}
//»¹Ê£Ò»¸ö
result = (int)queue2.Dequeue();
queue1.Enqueue(result);
}
return result;
}
}


5.Õ»µÄpush¡¢popÐòÁÐÊÇ·ñÒ»ÖÂ
ÊäÈëÁ½¸öÕûÊýÐòÁС£ÆäÖÐÒ»¸öÐòÁбíʾջµÄpush˳Ðò£¬ÅжÏÁíÒ»¸öÐòÁÐÓÐûÓпÉÄÜÊǶÔÓ¦µÄpop˳Ðò¡£ÎªÁ˼òµ¥Æð¼û£¬ÎÒÃǼÙÉèpushÐòÁеÄÈÎÒâÁ½¸öÕûÊý¶¼ÊDz»ÏàµÈµÄ¡£
±ÈÈçÊäÈëµÄpushÐòÁÐÊÇ1¡¢2¡¢3¡¢4¡¢5£¬ÄÇô4¡¢5¡¢3¡¢2¡¢1¾ÍÓпÉÄÜÊÇÒ»¸öpopϵÁС£ÒòΪ¿ÉÒÔÓÐÈçϵÄpushºÍpopÐòÁУºpush 1£¬push 2£¬push 3£¬push 4£¬pop£¬push 5£¬pop£¬pop£¬pop£¬pop£¬ÕâÑùµÃµ½µÄpopÐòÁоÍÊÇ4¡¢5¡¢3¡¢2¡¢1¡£µ«ÐòÁÐ4¡¢3¡¢5¡¢1¡¢2¾Í²»¿ÉÄÜÊÇpushÐòÁÐ1¡¢2¡¢3¡¢4¡¢5µÄpopÐòÁС£


ÍøÉϵÄÈô¸ÉËã·¨¶¼Ì«¸´ÔÓÁË£¬ÏÖÌá³ö°üÊÏËã·¨ÈçÏ£º
ÏÈforÑ­»·°Ñarr1ÖеÄÔªËØÈëÕ»£¬²¢ÔÚÿ´Î±éÀúʱ£¬¼ìË÷arr2ÖпÉÒÔpopµÄÔªËØ¡£Èç¹ûÑ­»·½áÊø£¬¶østackÖл¹ÓÐÔªËØ£¬¾Í˵Ã÷arr2ÐòÁв»ÊÇpopÐòÁС£
static bool
JudgeSequenceIsPossible(int[] arr1,int[] arr2)
{
Stack stack = new Stack();
for (int i = 0, j = 0; i < arr1.Length; i++)
{
stack.Push(arr1[i]);
while(stack.Count > 0 && (int)stack.Peek() == arr2[j])
{
stack.Pop();
j++;
}
}
return stack.Count == 0;
}


6.µÝ¹é·´×ªÒ»¸öÕ»£¬ÒªÇó²»µÃÖØÐÂÉêÇëÒ»¸öͬÑùµÄÕ»£¬¿Õ¼ä¸´ÔÓ¶Èo(1)
Ë㷨˼Ï룺ººÅµËþµÄ˼Ï룬·Ç³£¸´ÔÓ£¬Íæ¹ý¾ÅÁ¬»·µÄÈ˶¼ÏëµÃͨµÄ
static void ReverseStack(ref Stack stack)
{
if (stack.Count == 0)
return;
object top = stack.Pop();
ReverseStack(ref stack);
if (stack.Count == 0)
{
stack.Push(top);
return;
}
object top2 = stack.Pop();
ReverseStack(ref stack);
stack.Push(top);
ReverseStack(ref stack);
stack.Push(top2);
}


7.¸øջşöÐò
±¾ÌâÄ¿ÊÇÉÏÒ»ÌâÄ¿µÄÑÓÉì
static void Sort(ref Stack stack)
{
if (stack.Count == 0)
return;
object top = stack.Pop();
Sort(ref stack);
if (stack.Count == 0)
{
stack.Push(top);
return;
}
object top2 = stack.Pop();
if ((int)top > (int)top2)
{
stack.Push(top);
Sort(ref stack);
stack.Push(top2);
}
else
{
stack.Push(top2);
Sort(ref stack);
stack.Push(top);
}
}


8..ÈçºÎÓÃÒ»¸öÊý×éʵÏÖÁ½¸öÕ»
¼ÌÐøÎÒËùÌᳫµÄ¿ÙÃŶù˼Ï룬Ҳ²»Í÷ÎÒºÍÇà²ËÁ³Ïཻһ³¡¡£
ÍøÉÏÁ÷´«×ÅÁ½ÖÖ·½·¨£º
·½·¨1
²ÉÓý»²æË÷ÒýµÄ·½·¨
Ò»ºÅÕ»ËùÕ¼Êý×éË÷ÒýΪ0, 2, 4, 6, 8¡­¡­(K*2)


¶þºÅÕ»ËùÕ¼Êý×éË÷ÒýΪ1£¬3£¬5£¬7£¬9 ¡­¡­(K*2 + 1)


Ë㷨ʵÏÖÈçÏ£º
public class NewStack
{
object[] arr;
int top1;
int top2;
public NewStack(int capticy)
{
arr = new object[capticy];
top1 = -1;
top2 = -2;
}
public void Push(int type, object element)
{
if (type == 1)
{
if (top1 + 2 >= arr.Length)
throw new Exception(¡°The stack is full¡±);
else
{
top1 += 2;
arr[top1] = element;
}
}
else //type==2
{
if (top2 + 2 >= arr.Length)
throw new Exception(¡°The stack is full¡±);
else
{
top2 += 2;
arr[top2] = element;
}
}
}
public object Pop(int type)
{
object obj = null;
if (type == 1)
{
if (top1 == -1)
throw new Exception(¡°The stack is empty¡±);
else
{
obj = arr[top1];
arr[top1] = null;
top1 -= 2;
}
}
else //type == 2
{
if (top2 == -2)
throw new Exception(¡°The stack is empty¡±);
else
{
obj = arr[top2];
arr[top2] = null;
top2 -= 2;
}
}
return obj;
}
public object Peek(int type)
{
if (type == 1)
{
if (top1 == -1)
throw new Exception(¡°The stack is empty¡±);
return arr[top1];
}
else //type == 2
{
if (top2 == -2)
throw new Exception(¡°The stack is empty¡±);
return arr[top2];
}
}
}


·½·¨2£º
µÚÒ»¸öÕ»A£º

Ê×Ò³ ÉÏÒ»Ò³ 1 2 3 ÏÂÒ»Ò³ βҳ 2/3/3
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
ÉÏһƪ£ºOralceÊý¾Ý¿â±ÊÊÔÌâÒ»Ì× ÏÂһƪ£ºJava³ÌÐòÔ±³£¼û±ÊÊÔÌâÖ®Öм¶¼ò´ðÌâ

×îÐÂÎÄÕÂ

ÈÈÃÅÎÄÕÂ

Hot ÎÄÕÂ

Python

C ÓïÑÔ

C++»ù´¡

´óÊý¾Ý»ù´¡

linux±à³Ì»ù´¡

C/C++ÃæÊÔÌâÄ¿