oral

编程入门 行业动态 更新时间:2024-10-25 10:29:20

<a href=https://www.elefans.com/category/jswz/34/1746086.html style=oral"/>

oral

2019独角兽企业重金招聘Python工程师标准>>>

#include <stdio.h>
#include <stack>
#include <exception>bool MyIsPopOrder(int* InSeq, int* OutSeq, int length) {if(InSeq == NULL || OutSeq == NULL || length <= 0)return false;std::stack<int> stackAid;int index = 0;for(int i=0; i<length; ++i) {if(!stackAid.empty() && OutSeq[i] == stackAid.top()) {stackAid.pop();continue;}while(index < length && InSeq[index] != OutSeq[i]) {stackAid.push(InSeq[index++]);}//pushif(index < length && InSeq[index] == OutSeq[i])stackAid.push(InSeq[index]);if(!stackAid.empty() && OutSeq[i] != stackAid.top() && index >= length)return false;//popif(index < length && InSeq[index] == OutSeq[i]) {stackAid.pop();++index;}}return true;
}//============================hht's=========================
bool IsPopOrder(const int* pPush, const int* pPop, int nLength) {bool bPossible = false;if(pPush != NULL && pPop != NULL && nLength > 0) {const int* pNextPush = pPush;const int* pNextPop = pPop;std::stack<int> stackData;while(pNextPop - pPop < nLength) {while(stackData.empty() || stackData.top() != *pNextPop) {if(pNextPush - pPush == nLength)break;stackData.push(*pNextPush);++pNextPush;}if(stackData.top() != *pNextPop)break;stackData.pop();++pNextPop;}if(stackData.empty() && pNextPop - pPop == nLength)bPossible = true;}return bPossible;
}void Test(const char* testName, int* InSeq, int* OutSeq, int length, bool expected) {if(testName != NULL)printf("%s begins: \n", testName);
//	if(expected == MyIsPopOrder(InSeq, OutSeq, length))if(expected == IsPopOrder(InSeq, OutSeq, length))printf("passed.\n");elseprintf("failed.\n");
}//***********multi number***************
//1.yes
void Test1() {int InSeq[] = {1,2,3,4,5};int OutSeq[] = {3,5,4,2,1};Test("Test1", InSeq, OutSeq, 5, true);
}
//2.no
void Test2() {int InSeq[] = {1,2,3,4,5};int OutSeq[] = {4,3,5,1,2};Test("Test2", InSeq, OutSeq, 5, false);
}//***********single number***************
//1.yes
void Test3() {int InSeq[] = {2};int OutSeq[] = {2};Test("Test3", InSeq, OutSeq, 1, true);
}
//2.no
void Test4() {int InSeq[] = {5};int OutSeq[] = {4};Test("Test4", InSeq, OutSeq, 1, false);
}//NULL
void Test5() {Test("Test5", NULL, NULL, 0, false);
}int main(int argc, char* argv[]) {Test1();Test2();Test3();Test4();Test5();return 0;
}



转载于:

更多推荐

oral

本文发布于:2024-02-27 12:01:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1706440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:oral

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!