向光而行的布莱克很想去广西嗦粉 level
获赞
0
粉丝
0
关注
0
看过 TA
4
西安电子科技大学
2024
人工智能
IP属地:上海
暂未填写个人简介
私信
关注
1、给一个字符串str='10+10*5-8',实现加减乘除,输出结果---PDDdef calculate(s):s = s.replace(' ', '')stack = []num = 0op = '+'  # 初始操作符设为 '+'for i, char in enumerate(s):# 判断字符串是否只包含数字字符(0 - 9)if char.isdigit():# 多位数处理num = num * 10 + int(char)# 遇到运算符或到达末尾时,处理前一个操作符if char in '+-*/' or i == len(s) - 1:if op == '+':stack.append(num)elif op == '-':stack.append(-num)elif op == '*':stack.append(stack.pop() * num)elif op == '/':prev = stack.pop()if prev // num < 0 and prev % num != 0:stack.append(prev // num + 1)  # 向零取整else:stack.append(prev // num)op = charnum = 0return sum(stack)str_expr = '10+10*5-8'result = calculate(str_expr)print(result)2、给定一个二维数组(矩阵)要求按照螺旋顺序输出所有元素def spiral_order(matrix):if not matrix or not matrix[0]:return []result = []top, bottom = 0, len(matrix) - 1left, right = 0, len(matrix[0]) - 1while top <= bottom and left <= right:# 1. 从左到右遍历上边界for col in range(left, right + 1):result.append(matrix[top][col])top += 1  # 上边界下移# 2. 从上到下遍历右边界for row in range(top, bottom + 1):result.append(matrix[row][right])right -= 1  # 右边界左移# 3. 从右到左遍历下边界(需检查 top <= bottom)if top <= bottom:for col in range(right, left - 1, -1):result.append(matrix[bottom][col])bottom -= 1  # 下边界上移# 4. 从下到上遍历左边界(需检查 left <= right)if left <= right:for row in range(bottom, top - 1, -1):result.append(matrix[row][left])left += 1  # 左边界右移return result3、给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素(前 K 个高频元素)---阿里4、给你一个非负整数数组 nums 和一个整数 target 。向数组中的每个整数前添加 '+' 或 '-' ,然后串联起所有整数,可以构造一个 表达式 :(目标和)---阿里
0 点赞 评论 收藏
分享

创作者周榜

更多
关注他的用户也关注了:
牛客网
牛客网在线编程
牛客网题解
牛客企业服务