亚马逊笔试题目_亚马逊在线笔试题目

该篇博客主要介绍如何解决亚马逊在线笔试中关于栈的题目。通过给出的例子,解释了如何从原始数组通过一系列push和pop操作得到目标数组,并提供了C++实现的算法来计算操作序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

亚马逊在线笔试题目

Question 2 / 2

Question:

As you know, two operations of Stack are push and pop. Now give you two integer arrays, one is the original array before

push and pop operations, the other one is the result array after a series of push and pop operations to the first array. Please

give the push and pop operation sequence.

For example:

If the original array is a[] = {1,2,3}, and the result array is b[] = {1,3,2}.

Then, the operation sequence is “push1|pop1|push2|push3|pop3|pop2”(operations are split by ‘|’ and no space).

Rules:

1. The push and pop operations deal with the original int array from left to right.

2. The input is two integer array. They are the original array and the result array. These interger array is split by space.

3. The output is the operation sequence.

4. If the original array cannot make to the result array with stack push and pop, The output should be 'None'.

5. The operation "push1" means push the first element of the original array to the stack.

6. The operation "pop1" means pop the first element of the original array from the stack, and add this element to the tail

of the result array.

7. Please don't include any space in the output string.

Sample1:

Input:

1 2 3 4

1 2 3 4

Output:

push1|pop1|push2|pop2|push3|pop3|push4|pop4

Sample2:

Input:

1 2 3 4

4 3 2 1

Output:

push1|push2|push3|push4|pop4|pop3|pop2|pop1

#include

#include

#include

#include

#include

using namespace std;

char* calculateOperationSequence(int *originalArray, int *resultArray, int length);

inline bool isSpace(char x){

return x == ' ' || x == '\r' || x == '\n' || x == '\r' || x == '\b' || x == '\t';

}

char * rightTrim(char *str){

int len = strlen(str);

while(--len>=0){

if(isSpace(str[len])){

str[len] = '\0';

}else{

break;

}

}

return str;

}

char * getInputLine(char *buffer, int length){

if(fgets(buffer,length, stdin)==NULL){

return NULL;

}

rightTrim(buffer);

if(strlen(buffer)<=0){

return NULL;

}

return buffer;

}

int splitAndConvert(char* strings,int *array){

char*tokenPtr = strtok(strings,",");

int i=0;

while(tokenPtr!=NULL){

array[i] = atoi(tokenPtr);

i++;

tokenPtr=strtok(NULL,",");

}

return i;

}

int main(){

char line[1000] = {0} ;

while(getInputLine(line,1000)){

int originalArray[30] = {0};

int originalArrayLength = splitAndConvert(line,originalArray);

if(originalArrayLength==0){

break;

}

getInputLine(line, 1000);

int resultArray[30] = {0};

int resultArrayLength = splitAndConvert(line,resultArray);

if(resultArrayLength==0){

break;

}

char *operationSequence = calculateOperationSequence(originalArray, resultArray, resultArrayLength);

if (NULL != operationSequence)

{ // 原来系统提供的代码。这里没有NULL判断

cout<< operationSequence <

free(operationSequence); // 自己加的

}

else

cout<< "None" <

}

return 0;

}

//your code is here

//下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。

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("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;

}

【亚马逊在线笔试题目】相关文章:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值