leetcode 1480+1672+45

本文介绍了在LeetCode中解决数组问题的两种不同方法,包括求解1D数组的运行总和及寻找目标插入位置。通过对比个人与他人简洁的Python代码实现,展示了动态规划和数组操作的高效技巧。同时,讨论了寻找客户最大财富的方法,强调了一行代码在解决问题中的力量。
摘要由CSDN通过智能技术生成

leetcode 1480

  1. 我的
    class Solution:
    def runningSum(self, nums: List[int]) -> List[int]:
    sums = []
    tmp = 0
    for i in range(len(nums)):
    tmp += nums[i]
    sums.append(tmp)
    return sums

  2. 别人的解法
    class Solution:
    def runningSum(self, nums):
    newnums = nums
    for i in range(1,len(nums)):
    newnums[i] = newnums[i-1] + nums[i]
    return newnums

作者:superpangbao-m
链接:https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/python3-by-superpangbao-m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

leetcode 1672

  1. 我的
    class Solution:
    def maximumWealth(self, accounts: List[List[int]]) -> int:
    sums = []
    for i in range(len(accounts)):
    for j in range(len(accounts)):
    sums.append(sum(accounts[j]))
    return max(sums)

  2. 其他作者的 一行简洁代码
    class Solution:
    def maximumWealth(self, accounts: List[List[int]]) -> int:
    return max(sum(accounts[i]) for i in range(len(accounts)))

作者:JamLeon
链接:https://leetcode-cn.com/problems/richest-customer-wealth/solution/yi-xing-dai-ma-si-lu-jian-dan-xing-neng-iwjw7/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

35

  1. 我的
    class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
    if nums[-1]<target:
    return len(nums)
    if nums[0]>target:
    return 0
    else:
    for i in range(len(nums)):
    if (nums[i] == target):
    return nums.index(target)
    if (nums[i]<target) & (nums[i+1]>target):
    return i+1
  2. 优秀答案
    class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
    # 不管这个数在不在里面,直接append
    nums.append(target)
    # 然后再排序
    nums.sort()
    # 最后返回查找的index
    return nums.index(target)

作者:nbcoder
链接:https://leetcode-cn.com/problems/search-insert-position/solution/pythonnei-zhi-fang-fa-chao-jian-dan-by-ljzecg/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值