17 lines C++, easy, 20 ms

• If you don't like the `44 - op` ASCII trick, you can use `op == '+' ? 1 : -1` instead. And wow, I didn't know C++ has `or`. I'm a Python guy and wrote that out of habit and only realized it after getting this accepted :-)

``````int calculate(string s) {
istringstream in('+' + s + '+');
long long total = 0, term = 0, n;
char op;
while (in >> op) {
if (op == '+' or op == '-') {
total += term;
in >> term;
term *= 44 - op;
} else {
in >> n;
if (op == '*')
term *= n;
else
term /= n;
}
}
}``````

• Aha! StefanPochmann, I see you again! I have continuously seen you these days. Every time I saw you, the 'asked' time is closer. Maybe we are practicing similar targets :)

And thanks for sharing your clean and short code!

A more C style version

• Yeah, I've been quite active here recently. I should better stop, though, got more important stuff to do. But it's addictive.

• This is so neat! But i'm a little confused. How does it deal with whitespace and parenthese? Maybe I don't understand >> operator well?

• Whitespace is skipped by `>>`, and the input for this problem doesn't have parentheses.

• incredible solution...

• learn a lot :) thank you

• Unbelievable solution!!!

• can someone explain the code a little? what is the use of n, term? thanks

• why you put s like '+'+s+'s'?

• As someone who knows very little cpp it took me a while to figure out what is going on in Stefan's solution. Here is the same solution in JavaScript purely for explanation purposes:

``````var calculate = function(str) {
str = '+' + str + '+';
let total = 0;
let term = 0;
let n;
let op;
for (let ii = 0; ii < str.length;) {
//in >> op
while((op = str[ii++]) === ' ') {}

if (op === '+' || op === '-') {
total += term;
//in >> term
term = '';
while((/[0-9\s]/).test(str[ii])) {
term += str[ii++];
}
term = parseInt(term);

term *= 44 - op.charCodeAt(0);
} else {
//in >> n
n = '';
while((/[0-9\s]/).test(str[ii])) {
n += str[ii++];
}
n = parseInt(n);

if (op === '*') {
term *= n;
} else {
term /= n;
term = Math.trunc(term);
}
}
}
};
``````

• Though Stephan's C++ solution is very neat, yet it is not easy to understand for people (like me) who are not familiar with sstream. So I rewrote the code for easy understanding.

``````class Solution {
public:
int calculate(string ss) {
int total = 0, preVal = 0;
string s = "+" + ss + "+";
for(int i = 0; i < s.size(); i++) {
if(isspace(s[i])) continue;
char op = s[i];
if(op == '+' || op == '-') {
total += preVal;
preVal = parse(s, ++i);
preVal *= (op == '+') ? 1 : -1;
}
else if(op == '*')
preVal *= parse(s, ++i);
else if(op == '/')
preVal /= parse(s, ++i);
}
}

private:
int parse(string& s, int& pos) {
while(pos < s.size() && isspace(s[pos])) pos++;
int num = 0;
while(pos < s.size() && isdigit(s[pos]))
num = 10 * num + (s[pos++] - '0');
pos--;
return num;
}
};
``````

• excellent solution.
it's very smart to use istringstream

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.