题目:

Implement a basic calculator to evaluate a simple expression string.

The expression string may contain open ( and closing parentheses ),
the plus + or minus sign -, non-negative integers
and empty spaces .

You may assume that the given expression is always valid.

Some examples:

  1. "1 + 1" = 2
  2. " 2-1 + 2 " = 3
  3. "(1+(4+5+2)-3)+(6+8)" = 23

Note: Do not use the eval built-in
library function.

思路:栈的使用,括号的优先级最高。

  1. class Solution {
  2. public:
  3. int calculate(string s) {
  4. int len = s.length();
  5. stack<int> st;
  6. int i = 0;
  7. int result = 0;
  8. while(i<len)
  9. {
  10. int sum = 0;
  11. if(s.at(i)>='0'&&s.at(i)<='9')
  12. {
  13. int j = i+1;
  14. sum = s.at(i)-'0';
  15. while(j<=len-1&&s.at(j)>='0'&&s.at(j)<='9')
  16. {
  17. sum = (sum*10 + (s.at(j) - '0'));
  18. j++;
  19. }
  20. // cout<<sum<<endl;
  21. //以上计算数字字符串转化为数字
  22. if(!st.empty()&&(char)st.top()=='+')
  23. {
  24. st.pop();
  25. result = st.top()+sum;
  26. st.pop();
  27. st.push(result);
  28. }
  29. else if(!st.empty()&&(char)st.top()=='-')
  30. {
  31. st.pop();
  32. result = st.top()-sum;
  33. st.pop();
  34. st.push(result);
  35. }
  36. else
  37. {
  38. st.push(sum);
  39. }
  40. i = j;
  41. }
  42.  
  43. else if(s.at(i)==' ')
  44. {
  45. i++;
  46. }
  47. else if(s.at(i)=='+'||s.at(i)=='-')
  48. {
  49. st.push((int)s.at(i));
  50. i++;
  51. }
  52. else if(s.at(i)=='(')
  53. {
  54. st.push((int)s.at(i));
  55. i++;
  56. }
  57. else if(s.at(i)==')')
  58. {
  59. int temp = st.top();
  60. if(!st.empty())
  61. st.pop();
  62. if(!st.empty())
  63. st.pop();
  64. if(!st.empty()&&st.top()=='+')
  65. {
  66. st.pop();//去掉
  67. temp = temp+(st.top());
  68. st.pop();
  69. st.push(temp);
  70. }
  71. else if(!st.empty()&&st.top()=='-')
  72. {
  73. st.pop();//去掉
  74. temp = (st.top())-temp;
  75. st.pop();
  76. st.push(temp);
  77. }
  78. else
  79. {
  80. st.push(temp);
  81. }
  82. i++;
  83. }
  84. }
  85. return st.top();
  86. }
  87. };

leetcode_Basic Calculator的更多相关文章

  1. leetcode_Basic Calculator II

    题目: Implement a basic calculator to evaluate a simple expression string. The expression string conta ...

  2. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  3. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  4. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  5. Windows Universal 应用 – Tip Calculator

    声明 以下内容取材于 Bob Tabor 的课程<Windows Phone 8.1 Development for Absolute Beginners>,链接地址为:http://ww ...

  6. Calculator(1.5)

    Calculator(1.5) Github链接 ps.负数的处理未完成 解题过程中遇到的困难和解决 <stack>的使用: 认真研究了栈,基本上掌握了用法,与<queue>的 ...

  7. Calculator(1.0)

    Calculator(1.0) Github链接 解题过程中遇到的困难 对于c++中类和对象的使用不够明确,看了c++的视频教程学会了用类和对象来写程序. 不会使用<queue>,在网上查 ...

  8. 数据结构与算法(1)支线任务2——Basic Calculator

    题目:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a simple ...

  9. calculator

    #include <stdio.h> #include <stdlib.h> typedef enum { FALSE = , TRUE }BOOL; void calcula ...

随机推荐

  1. chrome 插件地址 知乎

    chrome运行内存过大:https://www.zhihu.com/question/20061319 chrome扩展程序:https://www.zhihu.com/question/19594 ...

  2. linux如何查看CPU,内存,机器型号,网卡信息

    查看CPU信息(型号)# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c      8  Intel(R) Xeon(R) CPU     ...

  3. 基于jquery垂直缩略图切换相册

    今天给大家分享一款垂直缩略图切换jQuery相册,这是一款垂直缩略图左右滚动切换响应式jQuery图片相册代码.该 插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Oper ...

  4. SQL查询优化联合索引 与 单一列的索引

    目前WEB的普及太快,在实际的开发中,一旦遇到大数据量的时候就需要做到优化,让查询的更快,才能给客户更好的体验,也能够在程序上避免timeout. 部分转载自:https://www.cnblogs. ...

  5. Hbase分布式安装部署过程

    系统 Red hat linux 6.4 Hadoop版本 1.2.1 Hbase版本 0.94.16 Hbase的完全分布式安装概述: 1. 配置hosts,确保涉及的主机名均可解析为ip 2. 编 ...

  6. kickstart安装步骤

    1.1 环境说明 [root@test ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@test ~]# uname -r 2 ...

  7. GDI+学习笔记

    7.1.1 GDI+概述 GDI+是微软在Windows 2000以后操作系统中提供的新的图形设备接口,其通过一套部署为托管代码的类来展现, 这套类被称为GDI+的“托管类接口”,GDI+主要提供了以 ...

  8. 逻辑斯特回归(logistic regression)与最大熵模型(maximum entropy model)

  9. ThinkPHP项目笔记之数据库配置篇

    对于配置文件,有几点说明 common:公共配置,也就是前台,后台,都可以调用的文件,具有普遍性 前台/后台:就是针对前后台的配置文件,具有针对性. 如:(公共文件基本配置) <?php ret ...

  10. 通过代码注册COM、DLL组件

    注册代码如下:  C++ Code  1234567891011121314151617181920212223242526272829303132333435363738   // //====== ...