大模型社招(RAG、Agent)算法题记录
1、给一个字符串str='10+10*5-8',实现加减乘除,输出结果---PDD
def calculate(s):
s = s.replace(' ', '')
stack = []
num = 0
op = '+' # 初始操作符设为 '+'
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 = char
num = 0
return 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) - 1
left, right = 0, len(matrix[0]) - 1
while 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 result
3、给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素(前 K 个高频元素)---阿里
4、给你一个非负整数数组 nums 和一个整数 target 。向数组中的每个整数前添加 '+' 或 '-' ,然后串联起所有整数,可以构造一个 表达式 :(目标和)---阿里
def calculate(s):
s = s.replace(' ', '')
stack = []
num = 0
op = '+' # 初始操作符设为 '+'
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 = char
num = 0
return 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) - 1
left, right = 0, len(matrix[0]) - 1
while 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 result
3、给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素(前 K 个高频元素)---阿里
4、给你一个非负整数数组 nums 和一个整数 target 。向数组中的每个整数前添加 '+' 或 '-' ,然后串联起所有整数,可以构造一个 表达式 :(目标和)---阿里
全部评论
相关推荐