注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
40 分钟写两道算法题,还是有点恶心,Codility代码区域这个好像不能复制粘贴,同时这些题目题干都比较长,理解题意都要几分钟,如果不提前准备的话,很危险
1.
A stack machine is a simple system that performs arithmetic operations on an input string of numbers and operators. It contains a stack that can store an arbitrary number of 12-bit unsigned integers. Initially the stack is empty. The machine processes a string of characters in the following way:
the characters of the string are processed one by one;
if the current character is a digit ('0'-'9'), the machine pushes the value of that digit onto its stack;
if the current character is '+', the machine pops the two topmost values from its stack, adds them and pushes the result onto the stack;
if the current character is '*', the machine pops the two topmost values from its stack, multiplies them and pushes the result onto the stack;
after the machine has processed the whole string it returns the topmost value of its stack as the result;
the machine reports an error if any operation it performs its magnitude poles. The function should return −1 if array A does not have a magnitude pole.
For example, given array A consisting of ten elements such that:
A[0] = 4
A[1] = 2
A[2] = 2
A[3] = 3
A[4] = 1
A[5] = 4
A[6] = 7
A[7] = 8
A[8] = 6
A[9] = 9
the function may return 5 or 9, as explained above.
Assume that:
N is an integer within the range [0..100,000];
each element of array A is an integer within the range [−2147483648..2147483647].
Complexity:
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
|