python - 有什么办法可以避免“在第一个空索引处插入”?
问题描述
我正在使用 Python3,我想使用 insert() 函数在特定索引处插入 Int。我知道该函数将首先尝试将其插入到我指定索引之前的第一个空索引处。有没有办法避免这种行为并首先插入我指定的索引?尝试执行以下操作:
[18, 6, 6, 6, 1, -1]
但实际结果如下:
[18, -1, 6, 1, 6, 6]
def replaceElements(self, arr: List[int]) -> List[int]:
res = []
temp = -1
for index in range(len(arr)-1, -1, -1):
res.insert(index, temp)
if arr[index] > temp:
temp = arr[index]
return res
解决方案
推荐阅读
- node.js - Mongoose,检查数组中是否存在,然后推拉
- batch-file - 只循环一个批处理文件 5 次
- node.js - 无法使用 node.js 执行 Exe 或批处理(lolminer)
- javascript - 如何从javascript中的本地存储中获取嵌套数组
- python - 在 IBM 云上连接 DB2 实例时出错
- webpack - 无效的合并操作员 webpack/bundle 问题
- ios - SwiftUI 使用 onTapGesture 和 onLongPressGesture 处理按钮/视图以及释放操作
- html - 当手机水平翻转时处理响应式css
- react-native - 初始化了一个新的 react-native 应用程序,但没有安装到设备上
- ethereum - 如何与已部署的 ERC20 代币与另一个智能合约进行交互?