leetcode_Basic Calculator
题目:
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" = 2
- " 2-1 + 2 " = 3
- "(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval
built-in
library function.
思路:栈的使用,括号的优先级最高。
- class Solution {
- public:
- int calculate(string s) {
- int len = s.length();
- stack<int> st;
- int i = 0;
- int result = 0;
- while(i<len)
- {
- int sum = 0;
- if(s.at(i)>='0'&&s.at(i)<='9')
- {
- int j = i+1;
- sum = s.at(i)-'0';
- while(j<=len-1&&s.at(j)>='0'&&s.at(j)<='9')
- {
- sum = (sum*10 + (s.at(j) - '0'));
- j++;
- }
- // cout<<sum<<endl;
- //以上计算数字字符串转化为数字
- if(!st.empty()&&(char)st.top()=='+')
- {
- st.pop();
- result = st.top()+sum;
- st.pop();
- st.push(result);
- }
- else if(!st.empty()&&(char)st.top()=='-')
- {
- st.pop();
- result = st.top()-sum;
- st.pop();
- st.push(result);
- }
- else
- {
- st.push(sum);
- }
- i = j;
- }
- else if(s.at(i)==' ')
- {
- i++;
- }
- else if(s.at(i)=='+'||s.at(i)=='-')
- {
- st.push((int)s.at(i));
- i++;
- }
- else if(s.at(i)=='(')
- {
- st.push((int)s.at(i));
- i++;
- }
- else if(s.at(i)==')')
- {
- int temp = st.top();
- if(!st.empty())
- st.pop();
- if(!st.empty())
- st.pop();
- if(!st.empty()&&st.top()=='+')
- {
- st.pop();//去掉
- temp = temp+(st.top());
- st.pop();
- st.push(temp);
- }
- else if(!st.empty()&&st.top()=='-')
- {
- st.pop();//去掉
- temp = (st.top())-temp;
- st.pop();
- st.push(temp);
- }
- else
- {
- st.push(temp);
- }
- i++;
- }
- }
- return st.top();
- }
- };
leetcode_Basic Calculator的更多相关文章
- leetcode_Basic Calculator II
题目: Implement a basic calculator to evaluate a simple expression string. The expression string conta ...
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Windows Universal 应用 – Tip Calculator
声明 以下内容取材于 Bob Tabor 的课程<Windows Phone 8.1 Development for Absolute Beginners>,链接地址为:http://ww ...
- Calculator(1.5)
Calculator(1.5) Github链接 ps.负数的处理未完成 解题过程中遇到的困难和解决 <stack>的使用: 认真研究了栈,基本上掌握了用法,与<queue>的 ...
- Calculator(1.0)
Calculator(1.0) Github链接 解题过程中遇到的困难 对于c++中类和对象的使用不够明确,看了c++的视频教程学会了用类和对象来写程序. 不会使用<queue>,在网上查 ...
- 数据结构与算法(1)支线任务2——Basic Calculator
题目:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a simple ...
- calculator
#include <stdio.h> #include <stdlib.h> typedef enum { FALSE = , TRUE }BOOL; void calcula ...
随机推荐
- chrome 插件地址 知乎
chrome运行内存过大:https://www.zhihu.com/question/20061319 chrome扩展程序:https://www.zhihu.com/question/19594 ...
- linux如何查看CPU,内存,机器型号,网卡信息
查看CPU信息(型号)# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CPU ...
- 基于jquery垂直缩略图切换相册
今天给大家分享一款垂直缩略图切换jQuery相册,这是一款垂直缩略图左右滚动切换响应式jQuery图片相册代码.该 插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Oper ...
- SQL查询优化联合索引 与 单一列的索引
目前WEB的普及太快,在实际的开发中,一旦遇到大数据量的时候就需要做到优化,让查询的更快,才能给客户更好的体验,也能够在程序上避免timeout. 部分转载自:https://www.cnblogs. ...
- Hbase分布式安装部署过程
系统 Red hat linux 6.4 Hadoop版本 1.2.1 Hbase版本 0.94.16 Hbase的完全分布式安装概述: 1. 配置hosts,确保涉及的主机名均可解析为ip 2. 编 ...
- kickstart安装步骤
1.1 环境说明 [root@test ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@test ~]# uname -r 2 ...
- GDI+学习笔记
7.1.1 GDI+概述 GDI+是微软在Windows 2000以后操作系统中提供的新的图形设备接口,其通过一套部署为托管代码的类来展现, 这套类被称为GDI+的“托管类接口”,GDI+主要提供了以 ...
- 逻辑斯特回归(logistic regression)与最大熵模型(maximum entropy model)
- ThinkPHP项目笔记之数据库配置篇
对于配置文件,有几点说明 common:公共配置,也就是前台,后台,都可以调用的文件,具有普遍性 前台/后台:就是针对前后台的配置文件,具有针对性. 如:(公共文件基本配置) <?php ret ...
- 通过代码注册COM、DLL组件
注册代码如下: C++ Code 1234567891011121314151617181920212223242526272829303132333435363738 // //====== ...