亚马逊在线笔试题目

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;

}

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

亚马逊笔试题目_亚马逊在线笔试题目相关推荐

  1. 前端实习生笔试_阿里巴巴前端实习生在线笔试后经验分享

    导读:还是太年轻,第一次在线笔试有些紧张了, 一.2015题目 我遇到的题目:6个选择其中3个多选,1个填空,6个大题.客服姐姐说题目是随机给的(因为给了一个时段考试,而不是统一时间点开考),不过题型 ...

  2. 亚马逊笔试题目_亚马逊笔试题目

    亚马逊笔试题目 总共两道编程题,全英文描述,时间为两个小时(可以用任何一种语言实现) 第一题:对于一个给定的物品名称X以及顾客的购物清单数组,找出所有购买过X的客户中所买的其他的最多的商品种类.如已知 ...

  3. 亚马逊笔试题目_亚马逊运营试题,你知道多少?

    你觉得亚马逊运营经理的主要工作是什么? 如果有曝光率,有点击率,但是转化率很低有可能是什么原因? 写出你的产品成本&利润计算公式? 如果FBA绩效评分低于350 你会如何处理? 如何防止VC跟 ...

  4. python亚马逊销量预测_亚马逊卖家如何预测一款产品的销量?

    在亚马逊选品过程中,通过对销量的评估来为自己选品与否做决策辅助是非常有必要的手段,可是我们该怎样去判断一个产品的销量呢? 一般来说,可以通过以下四方面的结合: 一.添加购物车 把自己做参考的Listi ...

  5. python 隐马尔科夫_隐马尔可夫模型原理和python实现

    隐马尔可夫模型(Hidden Markov Model,HMM)是统计模型,它用来描述一个含有隐含未知参数的马尔可夫过程.其难点是从可观察的参数中确定该过程的隐含参数. 隐马尔可夫模型注意包含以下参数 ...

  6. 面试题目_总结面试中 promise 相关题目的套路

    Promise 作为当下主流的异步解决方案,在工作中和面试中常常出现,尤其是在面试中,会弄个场景让你手写代码,这里给大家介绍五道比较有代表性的题目,以便熟悉一些套路. promise 简单介绍 先简单 ...

  7. 面试题目_数据分析之hive sql面试题目

    相信使用过mysql数据库的同学都应该知道,对于一些复杂的逻辑,mysql写起来是比较麻烦的,但是对于hive 来说是比较方便的,这是因为hive中可以使用窗口函数.如果大家觉得自己安装hive费时费 ...

  8. 迈克尔逊干涉仪仿真程序_使用迈克尔逊编程语言在Tezos上编写智能合约[操作指南]-第2部分...

    迈克尔逊干涉仪仿真程序 我们将使用用户传入的参数在Michelson中编写一个新的智能合约,在将新字符串保存到存储中之前,我们将其连接到存储中已经存在的字符串. (第1部分 在这里 .) 在上一篇文章 ...

  9. 华为博士招聘上机考试题目_华为校园招聘上机考试题目

    第一题和答案 题目1.选秀节目打分,分为专家评委和大众评委,score[]数组里面存储每个评委打的分数,judge_type[]里存储与score[]数组对应的评委类别,judge_type[i] = ...

最新文章

  1. 最早接触到的计算机编程语言——c语言
  2. C/C++如何检查系统内存泄露与使用情况?
  3. 【RS】如何从USGS上下载LANDSAT数据
  4. python【Matlibplot绘图库】绘制用于学术论文投稿的黑白图片
  5. 分享:根据svg节点对象类型和路径值转换坐标值
  6. 使用nginx动静分离后,druid被拦截的解决方法
  7. 英语语法---主语详解
  8. 开启真我新格调 期待绚丽的未知
  9. JS的三大组成(ES,DOM,BOM)
  10. 使用OpenCV4Android打开相机
  11. 功率因数cosφ仪表盘
  12. 超级经典的Word技巧
  13. iOS开发:使用大图+脚本,生成各种size的app icon和图片素材
  14. uniapp app内使用微信H5支付
  15. 区分event对象中的[clientX,offsetX,screenX,pageX]
  16. SELinux,查看 SELinux状态及关闭SELinux
  17. 如何防御DDoS攻击和CC攻击
  18. h5 android 滚动条卡顿,h5页面滑动卡顿解决方法
  19. 站内搜索 迅搜xunsearch 中小型网站的福音
  20. 贷前审批策略的6个搭建思路

热门文章

  1. 【LQ系列】 BASIC-16~BASIC-19
  2. 智慧物流车联网远程管理终端设备横向评估与优劣势对比
  3. Linux下简单创建ThinkPHP 6.0的网站 - 简单前后端 (未完待续)
  4. android view设置按钮颜色_建议收藏!最全 Android 常用开源库总结!
  5. <C++> 通讯录管理系统(纯手写含源码)
  6. 模拟语音加密的置乱技术简介
  7. 笔记本电源适配器的工作原理及其类型简介
  8. 揭秘2023年高新软件技术
  9. java.lang.ClassNotFoundException: org.apache.ibatis.session.SqlSession
  10. 小白从0开始学习推荐系统