概要:char* calculateOperationSequence(int * originalArray, int * resultArray, int length){if (NULL == originalArray || NULL == resultArray || length <= 0)return NULL;//使用一个栈模拟入栈和出栈操作就ok了。string str;stack st;int i = 0;int j = 0;st.push(originalArray[i]);char tmp[5] = "\0";str.append("push");sprintf(tmp, "%d", originalArray[i]);str.append(tmp);str.append("|");i++;while (!st.empty()){if (j < length && st.top() == resultArray[j]){str.append("p
亚马逊在线笔试题目,标签:笔试大全,http://www.88haoxue.comchar* calculateOperationSequence(int * originalArray, int * resultArray, int length)
{
if (NULL == originalArray || NULL == resultArray || length <= 0)
return NULL;
//使用一个栈模拟入栈和出栈操作就ok了。
string str;
stack st;
int i = 0;
int j = 0;
st.push(originalArray[i]);
char tmp[5] = "\0";
str.append("push");
sprintf(tmp, "%d", originalArray[i]);
str.append(tmp);
str.append("|");
i++;
while (!st.empty())
{
if (j < length && st.top() == resultArray[j])
{
str.append("pop");
sprintf(tmp, "%d", resultArray[j]);
str.append(tmp);
str.append("|");
st.pop();
j++;
if (i < length)
{
st.push(originalArray[i]);
str.append("push");
sprintf(tmp, "%d", originalArray[i]);
str.append(tmp);
str.append("|");
i++;
}
}
else
{
if (i < length)
{
st.push(originalArray[i]);
str.append("push");
sprintf(tmp, "%d", originalArray[i]);
str.append(tmp);
str.append("|");
i++;
}
else
break;
}
}
if (!st.empty())
return NULL;
char *p = (char *)malloc(1 + str.length());
if (NULL != p)
{
strcpy(p, str.c_str());
p[str.length() - 1] = '\0';
return p;
}
return NULL;
}
上一篇:iphone笔试题目
最新更新