首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
计算器(一)
[编程题]计算器(一)
热度指数:1865
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个字符串形式的表达式 s ,请你实现一个计算器并返回结果。
数据范围:表达式长度满足
,字符串中包含 + , - , ( , ) ,保证表达式合法。
示例1
输入
"1+2"
输出
3
示例2
输入
"1-1"
输出
0
示例3
输入
"-0"
输出
0
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(35)
分享
纠错
提交结果有问题?
2个回答
6篇题解
开通博客
godhands
发表于 2022-01-11 13:37:18
描述 题目描述 首先给我们一个字符串,这个字符串里面含有+,−,(,)+, -, (, )+,−,(,),然后运算的优先级跟我们正常算数的运算优先级一样,让我们求出最后的值 样例解释 "1+2" 这个我们直接计算就可以,得到333 所以最后的输出是 3 需要注意 这里我们会有括号嵌套的情况,这里
展开全文
太阳hxy
发表于 2023-08-30 08:52:52
栈(计算表达式) import java.util.*; public class Solution { public int calculate (String s) { //创建一个栈,用于存每个数字的数值和符号位 Stack<Integer>sta
展开全文
球球了给孩子一个offer吧
发表于 2022-02-10 15:41:48
题目: 给定一个字符串形式的表达式 s ,请你实现一个计算器并返回结果。 字符串中包含 + , - , ( , ) ,保证表达式合法。 方法一:去括号 本题只有4种符号'+','-','(',')',当我们计算有括号的字符串时首先想到如何处理括号,比如'3+(5-(3-1))',可以去掉括号,变成
展开全文
CroMarmot
发表于 2022-01-03 09:29:49
题意 给定一个包含加减括号的表达式,计算表达式的值 限制: 表达式长度不大于100000,计算过程保证在int内,只包含 数字 加减乘除 和 括号,保证表达式正确 方法 递归同时计算 考虑表达式,其实是由数值 运算符 数值 运算符 数值 运算符构成的 其中第一个数值可能是负号开始,可以通过判断起始字
展开全文
牛客857222939号
发表于 2022-06-24 17:34:22
//一个递归的函数,遇到一对括号,就调用自身 //计算()整体所代表的值 static int GlobalIndex=0; //MyCalulate单纯地以为这个世界没有括号,遇到括号就递归 //正常的情况是,取一个操作,然后取一个操作数,迭代 //ret +value -value +vale
展开全文
代码界的小白
发表于 2022-01-10 10:01:49
题目主要信息 1、给定一个表达式求值 2、字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’,‘(’, ‘)’ 3、字符串一定合法 4、可能出现多个数字连在一起组成多位数 5、可能出现负数 方法一:利用栈求解 具体方法 我们可以用o1来储存当前符号,具体定义为 +,o1=1,-,o1=-1 我
展开全文
问题信息
字符串
栈
基础数学
递归
难度:
2条回答
35收藏
2247浏览
热门推荐
通过挑战的用户
查看代码
Asphyxiay
2022-09-14 01:41:12
ৡ晨ꦿ᭄࿐
2022-09-13 17:35:26
蒋燊
2022-09-13 08:52:15
亮里格亮
2022-09-12 21:33:27
初学者-L
2022-09-10 18:33:25
相关试题
线段树编号问题
基础数学
评论
(2)
牛牛的超市
动态规划
基础数学
评论
(5)
执行完下列语句段后,i值为()
递归
评论
(15)
之前的经历中单品数据分析的经验丰富...
评论
(1)
什么样的人适合做数据分析
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ public int calculate (String s) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ int calculate(string s) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @return int整型 # class Solution: def calculate(self , s ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ public int calculate (string s) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ function calculate( s ) { // write code here } module.exports = { calculate : calculate };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @return int整型 # class Solution: def calculate(self , s: str) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ func calculate( s string ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ int calculate(char* s ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @return int整型 # class Solution def calculate(s) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ def calculate(s: String): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ fun calculate(s: String): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ public int calculate (String s) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ export function calculate(s: string): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ func calculate ( _ s: String) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ pub fn calculate(&self, s: String) -> i32 { // write code here } }
"1+2"
3
"1-1"
0
"-0"
0