鱼C论坛

 找回密码
 立即注册
查看: 1413|回复: 3

[技术交流] C++刷leetcode(面试题 16.26. 计算器)【逆波兰】

[复制链接]
发表于 2020-4-30 15:51:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-4-30 15:51 编辑

题目描述:

  1. 给定一个包含正整数、加(+)、减(-)、乘(*)、除(/)的算数表达式(括号除外),计算其结果。

  2. 表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  。 整数除法仅保留整数部分。

  3. 示例 1:

  4. 输入: "3+2*2"
  5. 输出: 7
  6. 示例 2:

  7. 输入: " 3/2 "
  8. 输出: 1
  9. 示例 3:

  10. 输入: " 3+5 / 2 "
  11. 输出: 5
  12. 说明:

  13. 你可以假设所给定的表达式都是有效的。
  14. 请不要使用内置的库函数 eval。
复制代码


  1.     //先将中缀表达式变为后缀表达式
  2.     queue<string> trans(string input){
  3.         queue<string> res;
  4.         map<char, int> store ={{'+',1},{'-',1},{'*',2},{'/',2}};
  5.         stack<char> temp;
  6.         for(int i = 0; i < input.size(); i++){
  7.             if(input[i] >= '0' && input[i] <= '9'){
  8.                 string total;
  9.                 while(input[i] >= '0' && input[i] <= '9'){
  10.                     total += input[i];
  11.                     i++;
  12.                 }
  13.                 i--;
  14.                 res.push(total);
  15.             }
  16.             else if(input[i] == ' ')continue;
  17.             else{

  18.                 if(temp.empty()) temp.push(input[i]);
  19.                 else if(store[input[i]] > store[temp.top()]){
  20.                     temp.push(input[i]);
  21.                 }
  22.                 else if(store[input[i]] <= store[temp.top()]){//注意这里有等号!!!!
  23.                     while(!temp.empty() &&store[input[i]] <= store[temp.top()]){  
  24.                         string s(1, temp.top());      
  25.                         res.push(s);
  26.                         temp.pop();
  27.                     }
  28.                     temp.push(input[i]);
  29.                 }
  30.             }
  31.         }
  32.         if(!temp.empty()){
  33.             while(!temp.empty()){
  34.                 string a(1,temp.top());
  35.                 res.push(a);
  36.                 temp.pop();
  37.             }
  38.         }
  39.         queue<string> temp4 = res;
  40.         // while(!temp4.empty()){
  41.         //     cout << temp4.front() << " ";
  42.         //     temp4.pop();
  43.         // }
  44.         // cout << endl;
  45.         return res;
  46.     }
  47.     int comput(queue<string> input){
  48.         stack<int> temp;
  49.         while(!input.empty()){
  50.             string cha = input.front();
  51.             // cout << cha << endl;
  52.             if(temp.empty()){
  53.                 temp.push(stoi(cha));
  54.             }
  55.             else if(cha != "+" && cha != "-" && cha != "*" && cha != "/"){
  56.                 temp.push(stoi(cha));
  57.             }
  58.             else{
  59.                 int temp1 = temp.top();
  60.                 temp.pop();
  61.                 int temp2 = temp.top();
  62.                 temp.pop();
  63.                 int temp3;
  64.                 if(cha == "*")  temp3 = temp1 * temp2;
  65.                 else if(cha == "-") temp3 = temp2 - temp1;
  66.                 else if(cha == "+") temp3 = temp1 + temp2;
  67.                 else if(cha == "/") temp3 = temp2 / temp1;
  68.                 // cout << temp3 << endl;
  69.                 temp.push(temp3);
  70.                
  71.             }
  72.             input.pop();
  73.         }
  74.         return temp.top();
  75.     }
  76.     int calculate(string s) {
  77.         int res = 0;
  78.         queue<string> temp = trans(s);
  79.         res = comput(temp);
  80.         return res;

  81.     }
复制代码



注意事项:
1.逆波兰参考《大话数据结构》栈和队列章节内容。

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-30 15:51:29 | 显示全部楼层
224. 基本计算器
题目描述:
  1. 实现一个基本的计算器来计算一个简单的字符串表达式的值。

  2. 字符串表达式可以包含左括号&#160;(&#160;,右括号&#160;),加号&#160;+&#160;,减号&#160;-,非负整数和空格&#160;&#160;。

  3. 示例 1:

  4. 输入: "1 + 1"
  5. 输出: 2
  6. 示例 2:

  7. 输入: " 2-1 + 2 "
  8. 输出: 3
  9. 示例 3:

  10. 输入: "(1+(4+5+2)-3)+(6+8)"
  11. 输出: 23
  12. 说明:

  13. 你可以假设所给定的表达式都是有效的。
  14. 请不要使用内置的库函数 eval。

  15. 来源:力扣(LeetCode)
  16. 链接:https://leetcode-cn.com/problems/basic-calculator
  17. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码


  1. class Solution {
  2. public:
  3.     queue<string> trans(string input){
  4.         queue<string> res;
  5.         stack<char> temp;
  6.         map<char, int> store = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
  7.         for(int i = 0; i < input.size(); i++){
  8.             if(input[i] == ' ')continue;
  9.             else if(input[i] == '(') temp.push(input[i]);
  10.             else if(input[i] == ')'){
  11.                 while(!temp.empty()&&temp.top()!= '('){
  12.                     string a (1, temp.top());
  13.                     res.push(a);
  14.                     temp.pop();
  15.                 }
  16.                 temp.pop();
  17.             }
  18.             else if(input[i] >= '0' && input[i] <= '9'){
  19.                 string b;
  20.                 while(i < input.size() &&input[i] >= '0' && input[i] <= '9'){
  21.                     b+=input[i];
  22.                     i++;
  23.                 }
  24.                 i--;
  25.                 res.push(b);
  26.             }
  27.             else{
  28.                 if(temp.empty()) temp.push(input[i]);
  29.                 else if(store[input[i]] > store[temp.top()]) temp.push(input[i]);
  30.                 else{
  31.                     while(!temp.empty() && store[input[i]] <= store[temp.top()]){
  32.                         string c(1, temp.top());
  33.                         res.push(c);
  34.                         temp.pop();
  35.                     }
  36.                     temp.push(input[i]);
  37.                 }

  38.             }
  39.         }
  40.         while(!temp.empty()){
  41.             string d (1, temp.top());
  42.             temp.pop();
  43.             res.push(d);
  44.         }
  45.         // queue<string> temp4 = res;
  46.         // while(!temp4.empty()){
  47.         //     cout << temp4.front() << " ";
  48.         //     temp4.pop();
  49.         // }
  50.         // cout << endl;
  51.         return res;
  52.     }

  53.     int comput(queue<string> input){
  54.         stack<int> temp;
  55.         while(!input.empty()){
  56.             string cha = input.front();
  57.             if(temp.empty()){
  58.                 temp.push(stoi(cha));
  59.             }
  60.             else if(cha != "+" && cha != "-" && cha != "*" && cha != "/"){
  61.                 temp.push(stoi(cha));
  62.             }
  63.             else{
  64.                 int temp1 = temp.top();
  65.                 temp.pop();
  66.                 int temp2 = temp.top();
  67.                 temp.pop();
  68.                 int temp3;
  69.                 if(cha == "*")  temp3 = temp1 * temp2;
  70.                 else if(cha == "-") temp3 = temp2 - temp1;
  71.                 else if(cha == "+") temp3 = temp1 + temp2;
  72.                 else if(cha == "/") temp3 = temp2 / temp1;
  73.                 temp.push(temp3);
  74.                
  75.             }
  76.             input.pop();
  77.         }
  78.         return temp.top();
  79.     }

  80.     int calculate(string s) {
  81.         int res = 0;
  82.         queue<string> input = trans(s);
  83.         res = comput(input);
  84.         return res;

  85.     }
  86. };
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 11:00:17 | 显示全部楼层
糖逗 发表于 2020-4-30 15:51
224. 基本计算器
题目描述:

C++ 有 eval() 吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-7 11:14:54 | 显示全部楼层
_2_ 发表于 2020-5-7 11:00
C++ 有 eval() 吗?

貌似是没有
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 12:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表