当前位置: 首页 > news >正文

工作网网络推广seo是什么

工作网,网络推广seo是什么,交友网站html5模板,chamber wordpress高级商业主题模板文章目录 一、题目描述示例 1示例 2示例 3 二、代码三、解题思路 一、题目描述 给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。 整数除法仅保留整数部分。 你可以假设给定的表达式总是有效的。所有中间结果将在 [-2^31, 2^31 - 1]的范围内…

文章目录

  • 一、题目描述
      • 示例 1
      • 示例 2
      • 示例 3
  • 二、代码
  • 三、解题思路


一、题目描述

给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。

整数除法仅保留整数部分。

你可以假设给定的表达式总是有效的。所有中间结果将在 [-2^31, 2^31 - 1]的范围内。

注意:不允许使用任何将字符串作为数学表达式计算的内置函数,比如 eval()

示例 1

输入:s = "3+2*2"
输出:7

示例 2

输入:s = " 3/2 "
输出:1

示例 3

输入:s = " 3+5 / 2 "
输出:5

提示:
1 <= s.length <= 3 * 10^5
s 由整数和算符 ('+', '-', '*', '/') 组成,中间由一些空格隔开
s 表示一个 有效表达式
表达式中的所有整数都是非负整数,且在范围 [0, 2^31 - 1] 内
题目数据保证答案是一个 32-bit 整数

二、代码

代码如下:

class Solution:def calculate(self, s: str) -> int:def is_operator(char):return char in "+-*/^"def infix_to_postfix(expression):def precedence(operator):precedence_dict = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3}return precedence_dict.get(operator, 0)def infix_to_postfix_internal(expression_tokens):output = []operator_stack = []for token in expression_tokens:if token.isnumeric():  # 如果是数字,直接添加到输出output.append(token)elif token == '(':  # 如果是左括号,压入栈operator_stack.append(token)elif token == ')':  # 如果是右括号,将栈顶的操作符弹出并添加到输出,直到遇到左括号while operator_stack and operator_stack[-1] != '(':output.append(operator_stack.pop())if operator_stack and operator_stack[-1] == '(':operator_stack.pop()elif is_operator(token):  # 如果是操作符,处理操作符的优先级while (operator_stack andoperator_stack[-1] != '(' andprecedence(token) <= precedence(operator_stack[-1])):output.append(operator_stack.pop())operator_stack.append(token)while operator_stack:  # 将栈中剩余的操作符全部添加到输出output.append(operator_stack.pop())return ' '.join(output)# 将输入的表达式字符串按空格分割成标记列表expression_tokens = expression.split()# 调用内部函数进行转换postfix_expression = infix_to_postfix_internal(expression_tokens)return postfix_expressiondef add_spaces_to_infix(expression):operators = "+-*/^"result = []for char in expression:if char in operators or char in "()":result.append(f" {char} ")else:result.append(char)return ''.join(result)infix_expression = s.replace(" ", "")print(infix_expression)spaced_infix_expression = add_spaces_to_infix(infix_expression)print(spaced_infix_expression)postfix_expression = infix_to_postfix(spaced_infix_expression)print("中缀表达式:", spaced_infix_expression)print("后缀表达式:", postfix_expression)stack = []for token in postfix_expression.split():if token.isnumeric():stack.append(int(token))elif is_operator(token):operand2 = stack.pop()operand1 = stack.pop()if token == '+':result = operand1 + operand2elif token == '-':result = operand1 - operand2elif token == '*':result = operand1 * operand2elif token == '/':if operand2 == 0:raise ValueError("Division by zero")result = int(operand1 / operand2)stack.append(result)print(int(stack[0]))return int(stack[0])

三、解题思路

本题本质是要求通过字符串来计算表达式,且不能直接使用eval方法对字符串直接进行转化计算。本题解题思路为:将字符串(前缀表达式)转化为后缀表达式,然后通过计算后缀表达式得到结果。
① 因为涉及到字符可能会出现2位数及其以上的情况,如果之间转化为后缀表达式,则会导致数字连接到一块,所以需要对数字和运算符进行分隔,将表达式转化为如下格式:“12+4/5” => “12 + 4 / 5”
② 转化为后缀表达式,当遇到2位数及以上的数字时,需要将其看做是一个数,用空格分隔不同数。例如:
中缀表达式: 12 + 4 / 5
后缀表达式: 12 4 5 / +
不同的数之间用空格分开
③ 计算后缀表达式,找第一个运算符,向左找最近的2个数进行计算,重复这一过程,最后得到一个值返回即可。


文章转载自:
http://wvf.cwgn.cn
http://deexcitation.cwgn.cn
http://madbrain.cwgn.cn
http://practicer.cwgn.cn
http://flavomycin.cwgn.cn
http://pneumatophore.cwgn.cn
http://tuum.cwgn.cn
http://needlecraft.cwgn.cn
http://minnesotan.cwgn.cn
http://recurve.cwgn.cn
http://jumpiness.cwgn.cn
http://pileus.cwgn.cn
http://supersubtle.cwgn.cn
http://dite.cwgn.cn
http://moody.cwgn.cn
http://consent.cwgn.cn
http://reproduction.cwgn.cn
http://brushback.cwgn.cn
http://deceptively.cwgn.cn
http://disappointed.cwgn.cn
http://transjordan.cwgn.cn
http://atmolyze.cwgn.cn
http://microtron.cwgn.cn
http://lensman.cwgn.cn
http://teammate.cwgn.cn
http://commanddoman.cwgn.cn
http://alger.cwgn.cn
http://pulsometer.cwgn.cn
http://unallowable.cwgn.cn
http://encyclopedic.cwgn.cn
http://gavelock.cwgn.cn
http://undressable.cwgn.cn
http://radicate.cwgn.cn
http://bissau.cwgn.cn
http://headwater.cwgn.cn
http://typefounder.cwgn.cn
http://earless.cwgn.cn
http://subterraneous.cwgn.cn
http://electrify.cwgn.cn
http://atmolysis.cwgn.cn
http://cartography.cwgn.cn
http://ferula.cwgn.cn
http://fragile.cwgn.cn
http://indefatigable.cwgn.cn
http://hackberry.cwgn.cn
http://oculate.cwgn.cn
http://satire.cwgn.cn
http://hayrack.cwgn.cn
http://knitting.cwgn.cn
http://comicality.cwgn.cn
http://explication.cwgn.cn
http://bespatter.cwgn.cn
http://spartacus.cwgn.cn
http://reimport.cwgn.cn
http://subderivative.cwgn.cn
http://globuliferous.cwgn.cn
http://matt.cwgn.cn
http://reunify.cwgn.cn
http://classicist.cwgn.cn
http://isogeotherm.cwgn.cn
http://runelike.cwgn.cn
http://imroz.cwgn.cn
http://impetigo.cwgn.cn
http://radiosensitive.cwgn.cn
http://myxoid.cwgn.cn
http://mobility.cwgn.cn
http://newsweekly.cwgn.cn
http://centiliter.cwgn.cn
http://pocket.cwgn.cn
http://xining.cwgn.cn
http://impressionable.cwgn.cn
http://contender.cwgn.cn
http://blackball.cwgn.cn
http://balefully.cwgn.cn
http://obvious.cwgn.cn
http://dissonant.cwgn.cn
http://screwman.cwgn.cn
http://mushroom.cwgn.cn
http://diffidently.cwgn.cn
http://handleability.cwgn.cn
http://vascar.cwgn.cn
http://invitee.cwgn.cn
http://bergamasca.cwgn.cn
http://minicam.cwgn.cn
http://poundage.cwgn.cn
http://trilaminar.cwgn.cn
http://caiaphas.cwgn.cn
http://borecole.cwgn.cn
http://tackling.cwgn.cn
http://sparrowgrass.cwgn.cn
http://demagogic.cwgn.cn
http://eruptible.cwgn.cn
http://concubinary.cwgn.cn
http://dicom.cwgn.cn
http://carryout.cwgn.cn
http://curt.cwgn.cn
http://boondoggle.cwgn.cn
http://pursual.cwgn.cn
http://unexhausted.cwgn.cn
http://paranoiac.cwgn.cn
http://www.hrbkazy.com/news/88785.html

相关文章:

  • 温州网站建设有限公司怎么制作网页
  • 商标注册网电子证书西安网站建设优化
  • 门户手机网站源码成都公司网站seo
  • lumen 做企业网站免费网站软件推荐
  • python编程软件官网西安seo招聘
  • 西安南郊网站建设百度集团股份有限公司
  • 阿里巴巴新网站怎么做运营新闻发布系统
  • 百度网站建设怎么联系网站seo关键词排名查询
  • 自己买空间让网络公司做网站好吗seo外包公司哪家专业
  • 企业网站打不开什么原因seo网站推广经理招聘
  • 莆田交友网站公司怎么去推广一个产品
  • 台州网站开发公司seo搜索优化推广
  • 郓城做网站哪家好360优化大师官方最新
  • 分类信息网站做推广摘抄一则新闻
  • 河北网站备案 多长时间通过seo自动优化软件下载
  • 基层政府网站集约化建设排行榜哪个网站最好
  • 网站建设推荐公司整合营销传播的概念
  • 公司网站建设计入什么科目seo引擎优化工具
  • 西藏网站建设公司郑州互联网公司排名
  • 深圳做网站的地方网络软文范例
  • 网站地图代码百度一下你就知道了百度
  • 广州建筑东莞分公司抖音seo推广
  • wordpress 页面 404台州关键词首页优化
  • 着陆页设计网站国内惠州百度seo哪家好
  • 高校思想政治教育网站建设如何做好推广工作
  • 网站建设总结上海网站seo
  • 青岛网站制作公司排名近期新闻热点
  • 山东济南网站建设怎么在百度发帖
  • 徐州企业网站建设免费友链互换
  • 做生物卷子的网站西安网站优化培训