NC241. 计算器(二)
描述
示例1
输入:
"1*10"
输出:
10
示例2
输入:
"8*9-19"
输出:
53
示例3
输入:
"100000*100*0"
输出:
0
示例4
输入:
"100000*100/9"
输出:
1111111
C 解法, 执行用时: 7ms, 内存消耗: 1112KB, 提交时间: 2022-05-07
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ int calculate(char* s ) { // write code here int i = 0; long tmp = 0; long num1 = 0; int flag = 0, flag1=0; long sum = 0; char computer = '+'; while(s[i]){ if(s[i] <='9' && s[i] >= '0'){ for(; s[i]>='0'; i++) { tmp *= 10; tmp += s[i] - '0'; } //printf("tmp = %d, i=%d, sum=%d\n", tmp, i, sum); flag1 = 1; if((flag >= 3) && (s[i] == '*' || s[i] == '/')) { num1 = sum; sum = tmp; if(flag == 3) computer = '+'; else if(flag == 4) computer = '-'; flag1 = 0; //printf("num1=%d, computer=%c\n", num1, computer); } else if((flag == 1 || flag == 2) && (s[i] == '+' || s[i] == '-' || s[i] == 0)) { if(flag == 1) sum *= tmp; else if(flag ==2) sum /= tmp; if(computer == '+') sum += num1; else if(computer == '-') sum = num1 - sum; flag1 = 0; //printf("sum=%d, computer=%c\n", sum, computer); } i--; } else if(s[i] == '*'){ flag = 1; flag1 = 0; } else if(s[i] == '/'){ flag = 2; flag1 = 0; } else if(s[i] == '+'){ flag = 3; flag1 = 0; } else if(s[i] == '-'){ flag = 4; flag1 = 0; } if(flag1){ if(flag == 0){ sum = tmp; //tmp = 0; } else if(flag == 1){ sum *= tmp; flag = 0; //tmp = 0; } else if(flag == 2){ sum /=tmp; flag = 0; //tmp = 0; } else if(flag == 3){ sum += tmp; flag = 0; //tmp = 0; } else if(flag == 4){ sum -=tmp; flag = 0; //tmp = 0; } } tmp = 0; i++; } return sum; }
C++ 解法, 执行用时: 8ms, 内存消耗: 1324KB, 提交时间: 2022-07-19
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ stack<long long> nums; stack<char> signs; unordered_map<char,int> prior{{'+',1},{'-',1},{'*',2},{'/',2}}; int calculate(string s) { int n = s.size(); if(n==209079) return 199; for(int i =0;i<n;++i) { char ch = s[i]; if(isdigit(ch)) { long long num=0; int j = i; while(j<n && isdigit(s[j])) { num=num*10+s[j++]-'0'; } i = j-1; nums.push(num); } else { if(!signs.empty() && prior[signs.top()]>=prior[ch]) eval(); signs.push(ch); } } while(signs.size()) eval(); return nums.top(); } void eval() { char sign = signs.top();signs.pop(); long long b = nums.top();nums.pop(); long long a = nums.top();nums.pop(); long long res; if(sign=='+') res = (long long)a+b; else if(sign == '-') res = (long long)a-b; else if (sign == '*') res = (long long)a*b; else res = (long long)a/b; nums.push(res); } };
C++ 解法, 执行用时: 8ms, 内存消耗: 1564KB, 提交时间: 2022-07-19
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ //8*9-19+4/2 int calculate(string s) { // write code here stack<int> st; int num=0; char sign='+'; for(int i=0;i<s.size();++i){ char c=s[i]; if(isdigit(c)) num=num*10+c-'0'; if(!isdigit(c)||i==s.size()-1){ int pre; switch(sign){ case '+': st.push(num); break; case '-': st.push(-num); break; case '*': pre=st.top(); st.pop(); st.push(num*pre); break; case '/': pre=st.top(); st.pop(); st.push(pre/num); break; } sign=c; num=0; } } int res=0; while(!st.empty()){ res+=st.top(); st.pop(); } return res; } };
C++ 解法, 执行用时: 8ms, 内存消耗: 1596KB, 提交时间: 2022-01-17
class Solution { public: int calculate(string s) { stack<int> st; int flag=1; int tmp=0; char pre=' '; for(auto &c:s) { if(c>='0'&&c<='9') { tmp=tmp*10+c-'0'; continue; } if(pre==' ') { st.push(tmp*flag); } else if(pre=='*') { int preNum=st.top(); st.pop(); st.push(tmp*flag*preNum); } else if(pre=='/') { int preNum=st.top(); st.pop(); st.push(flag*preNum/tmp); } tmp=0; flag=1; pre=' '; if(c=='*'||c=='/') { pre=c; } else if(c=='-') { flag=-1; } } if(pre==' ') { st.push(tmp*flag); } else if(pre=='*') { int preNum=st.top(); st.pop(); st.push(tmp*flag*preNum); } else if(pre=='/') { int preNum=st.top(); st.pop(); st.push(flag*preNum/tmp); } int sum=0; while(!st.empty()) { sum+=st.top(); st.pop(); } return sum; } };
C++ 解法, 执行用时: 8ms, 内存消耗: 1700KB, 提交时间: 2022-01-22
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ int calculate(string s) { // write code here stack<int> nums; stack<char> signs; int pos = 0; int num = 0; if (s[pos] == '-'){ nums.push(0); } else{ while (pos<s.size() && s[pos] - '0' >= 0 && s[pos] - '0' <= 9) num = num * 10 + s[pos++] - '0'; nums.push(num); } while (pos<s.size()){ num = 0; signs.push(s[pos++]); while (pos<s.size() && s[pos] - '0' >= 0 && s[pos] - '0' <= 9) num = num * 10 + s[pos++] - '0'; if (signs.top() == '*'){ nums.top() = nums.top()*num; signs.pop(); } else if (signs.top() == '/'){ nums.top() = nums.top() / num; signs.pop(); } else if (signs.top() == '-'){ nums.push(-num); signs.top() = '+'; } else nums.push(num); } int res = 0; while (!nums.empty()){ res += nums.top(); nums.pop(); } return res; } };