两个栈实现一队列
class Queue
{
void Push(int data)
{
inStack.push(data);
}
int Pop()
{
if (inStack.empty() && outStack.size())
return -1;
if (!outStack.empty())
{
while (!inStack.empty())
{
outStack.push(inStack.top());
inStack.pop();
}
int top = outStack.top();
outStack.pop();
return ret;
}
}
private:
stack<int> inStack;
stack<int> outStack;
};
两个队列实现一个栈
class Stack
{
void Push(int data)
{
inQueue.push(data);
}
int Pop()
{
while (inQueue.size() > 0)
{
outQueue.push(inQueue.front());
inQueue.pop();
}
int Front = outQueue.front();
outQueue.pop();
return Front;
}
protected:
queue<int> inQueue;
queue<int> outQueue;
};
空格替换
void ReplaceBlack(char* str,size_t n)
{
if (str == NULL)
return;
int count = 0;
for (size_t i = 0; i < n; i++)
{
if (*str == ' ')
++count;
++str;
}
int newLen = count * 2 + n;
while (n >= 0 && newLen > n)
{
if (str[n - 1] == ' ')
{
str[newLen--] = '$';
str[newLen--] = '$';
str[newLen--] = '$';
}
else
{
str[newLen--] = str[n--];
}
}
}