当前位置:网站首页>菜菜的刷题日记 | 238.除自身以外数组的乘积
菜菜的刷题日记 | 238.除自身以外数组的乘积
2022-04-23 06:19:00 【猿知】

系列索引:菜菜的刷题日记 | 被LeetCode用Python狂虐的那段日子
菜鸡的修仙之路——2022/1/12
【题目】
给你一个长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。
示例:
输入: [1,2,3,4]
输出: [24,12,8,6]
提示:题目数据保证数组之中任意元素的全部前缀元素和后缀(甚至是整个数组)的乘积都在 32 位整数范围内。
说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。
题目链接:https://leetcode-cn.com/problems/product-of-array-except-self/
【官方思路】
老规矩,看官方怎么解。不得不说还是得看大佬解题才有趣啊、

方法一: 构造两个列表分别存储前缀和后缀之积,anwser就等于二者再乘积,共遍历三次。

进阶:进阶反复借用anwser来节省空间复杂度,这里使用R来暂存后缀之积数据,得到一个新R后直接乘到anwser里了。起到“以一抵组”的作用。

代码可以研究一下,从视频中截取的。
【参考代码】
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
res = [1 for i in range(len(nums))]
left = 1
for i in range(len(nums)):
res[i] *= left
left *= nums[i]
right = 1
for i in range(len(nums)-1, -1, -1):
res[i] *= right
right *= nums[i]
return res
【思考】
不得不说,感觉自己读了快三年计算机这代码水平还是这么拉真是惭愧,虽说物联网偏向工程实践,但从个人规划来说还是要好好提升一下自己的算法水平,加油,学习这件事什么时候都不算晚!
Python力扣题解系列持续更新,欢迎
点赞收藏+关注
上一篇:菜菜的刷题日记 | 189. 轮转数组 rotate-array
下一篇:菜菜的刷题日记 | 118.杨辉三角
本人水平有限,文章中不足之处欢迎下方评论区批评指正~如果感觉对你有帮助,点个赞 支持一下吧 ~
不定期分享 有趣、有料、有营养内容,欢迎 订阅关注 我的博客 ,期待在这与你相遇 ~
版权声明
本文为[猿知]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Magic_Zsir/article/details/122460850
边栏推荐
猜你喜欢

学习资料

hql求一个范围内最大值

DMR system solution of Kaiyuan MINGTING hotel of Fengqiao University

Solution of emergency communication system for major security incidents

The people of Beifeng have been taking action

学习笔记6-几种深度学习卷积神经网络的总结

How to improve the service efficiency of the hotel without blind spots and long endurance | public and Private Integrated walkie talkie?

Typora操作技巧说明(一).md

el-select 中v-model绑定值,数据回显只显示value,不显示label

Beifeng communication helps Zhanjiang fire brigade build PDT wireless communication system
随机推荐
记录阿里云服务器挖矿程序处理
海康威视面经总结
go语言映射操作
Jiangning hospital DMR system solution
通过sparksql读取presto中的数据存到clickhouse
PyTorch 18. torch. backends. cudnn
记录一些npm 有关的问题(杂乱记录)
Unable to determine the device handle for GPU 0000:02:00.0: GPU is lost.
免费开源农业物联网云平台(Version:3.0.1)
Metro wireless intercom system
Gather, unsqueeze and other operators when PTH is converted to onnx
USO technology was invited to share the technical framework and challenges of AI synthetic virtual characters at lvson2020 conference
PyTorch 17. GPU concurrency
Draw margin curve in arcface
记录一个查询兼容性的网站,String.replaceAll()兼容性报错
Flexible blind patch of ad hoc network | Beifeng oil and gas field survey solution
go iris框架实现多服务Demo:通过(监听8083端口的)服务1中的接口启动(监听8084端口的)服务2
Are realrange and einsum really elegant
无盲区、长续航|公专融合对讲机如何提升酒店服务效率?
重大安保事件应急通信系统解决方案