# My accepted 64ms C++ solution

• ``````class Solution {
private:
vector<string> parse(string s)
{
int n = s.length();
int i=0, j=0;
vector<string> result;
char cs[n+1];
strcpy(cs, s.c_str());
while(i<n)
{
if(cs[i] == ' ')
{
i = i + 1;
}
else if(cs[i] == '+' || cs[i] == '-' || cs[i] == '*' || cs[i] == '/')
{
string ts;
ts += cs[i];
result.push_back(ts);
i = i + 1;
}
else
{
string s;
j=i;
while(cs[j] != '+' && cs[j] != '-' && cs[j] != ' ' && j<n && cs[j]!='*' && cs[j]!='/')
{
s = s + cs[j];
j = j + 1;
}
result.push_back(s);
i=j;
}
}
return result;
}
public:
int calculate(string s) {
vector<string> str = parse(s);
int n = str.size();
int i=1;
int previous = 0, current = atoi(str[0].c_str());
int result;
while(i<n)
{
if(str[i] == "+")
{
previous = previous+current;
current = atoi(str[i+1].c_str());
i=i+2;
}
else if(str[i] == "-")
{
previous = previous+current;
current = -1*atoi(str[i+1].c_str());
i = i+2;
}
else if(str[i] == "*")
{
current = current*atoi(str[i+1].c_str());
i = i + 2;
}
else if(str[i] == "/")
{
current = current/atoi(str[i+1].c_str());
i = i + 2;
}
else
exit(1);
}
result = previous+current;
return result;
}
};``````

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