The sword refers to offer31

#include<iostream>
#include<stack>
using namespace std;

int check(const int* a,const int* b,int len){
	stack<int> v;
	int i = 0, j = len-1;
	while (i<len) {
		if (b[i] == a[j]){
			i++;
			j--;
		}
		else if (!v.empty()&&(v.top() == a[j])) {
			v.pop();
			j--;
		}
		else{
			v.push(b[i]);
			i++;
		}
	}
	return v.empty() ? 1 : 0;
}

int main() {
	int a[] = { 1,2,3,4,5 };
	int b[] = { 4,5,3,2,1 };
	cout << (check(a,b,5)==1) << endl;
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324720898&siteId=291194637