Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // you can write to stdout for debugging purposes, e.g.
- // console.log('this is a debug message');
- console.log(solution('13000000000 DUP 4 POP 5 DUP + DUP + +'));
- console.log(Number.MAX_VALUE);
- function solution(S) {
- //split S
- var res = S.split(' ');
- var stack = []; // stack is now []
- /*stack.push(2); // stack is now [2]
- stack.push(5); // stack is now [2, 5]
- var i = stack.pop(); // stack is now [2]*/
- for (var i = 0; i < res.length; i++) {
- console.log(stack.toString());
- switch (res[i]) {
- case 'POP':
- if(stack.length==0){
- return -1;
- }else{
- stack.pop();
- }
- break;
- case 'DUP':
- if(stack.length==0){
- return -1;
- }else{
- // duplicate last value
- stack.push(stack[stack.length-1]);
- }
- break;
- case '-':
- if(stack.length<=1 || (stack[stack.length-2]>stack[stack.length-1])){
- return -1;
- }else{
- var firstLastValue = stack.pop();
- var secondLastValue = stack.pop();
- stack.push(firstLastValue-secondLastValue);
- }
- break;
- case '+':
- if(stack.length<=1){
- return -1;
- }else{
- var firstLastValue = stack.pop();
- var secondLastValue = stack.pop();
- stack.push(firstLastValue+secondLastValue);
- }
- break;
- default:
- // there is no 'long' in javascript.
- stack.push(parseInt(res[i]));
- break;
- }
- }
- return stack.pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement