Untitled
user_2740508
plain_text
a year ago
1.4 kB
3
Indexable
Never
#include<bits/stdc++.h> using namespace std; int solution(string &s) { int min = 0, max = (1 << 20) - 1; stringstream stream(s); stack<int> numberStack; string token; while (stream >> token) { if (token == "DUP") { if (!numberStack.size()) { return -1; } int x = numberStack.top(); numberStack.push(x); continue; } if (token == "POP") { if (!numberStack.size()) { return -1; } numberStack.pop(); continue; } if (token == "+") { if (numberStack.size() < 2) { return -1; } int x = numberStack.top(); numberStack.pop(); x += numberStack.top(); numberStack.pop(); if (x > max) { return -1; } numberStack.push(x); continue; } if (token == "-") { if (numberStack.size() < 2) { return -1; } int x = numberStack.top(); numberStack.pop(); x = numberStack.top() - x; numberStack.pop(); if (x > max or x < min) { return -1; } numberStack.push(x); continue; } numberStack.push(stoi(token)); } }