码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Leetcode 724. Find Pivot Index (Python)


    Given an array of integers nums, calculate the pivot index of this array.

    The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.

    If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.

    Return the leftmost pivot index. If no such index exists, return -1.

    Example 1:

    Input: nums = [1,7,3,6,5,6]
    Output: 3
    Explanation:
    The pivot index is 3.
    Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
    Right sum = nums[4] + nums[5] = 5 + 6 = 11
    

    Example 2:

    Input: nums = [1,2,3]
    Output: -1
    Explanation:
    There is no index that satisfies the conditions in the problem statement.

    Example 3:

    Input: nums = [2,1,-1]
    Output: 0
    Explanation:
    The pivot index is 0.
    Left sum = 0 (no elements to the left of index 0)
    Right sum = nums[1] + nums[2] = 1 + -1 = 0
    

    Constraints:

    • 1 <= nums.length <= 104
    • -1000 <= nums[i] <= 1000
    1. class Solution(object):
    2. def pivotIndex(self, nums):
    3. """
    4. :type nums: List[int]
    5. :rtype: int
    6. """
    7. if len(nums) == 1:
    8. return nums[0]
    9. for i in range(len(nums)):
    10. if i == 0:
    11. left = 0
    12. else:
    13. left = sum(nums[0:i])
    14. right = sum(nums[i+1:len(nums)+1])
    15. if left == right:
    16. a = True
    17. return i
    18. else:
    19. a = False
    20. if a == False:
    21. return -1

  • 相关阅读:
    TCP的拥塞控制 (Tahoe Reno NewReno SACK)
    EasyCVR国标GB28181协议接入下的TCP和UDP模式说明及差异
    javaScript 中的宏任务、微任务
    Linux查看开机启动的服务
    【代码】js闭包
    springboot logback-spring.xml 整合apollo实现动态配置日志级别
    在这个技术浮躁的时代也要注重架构的本质,多位大佬联袂推荐小团队构建大网站,让你少走弯路,甚至实现速成,成为优秀架构师!
    java“俄罗斯方块”
    【669. 修剪二叉搜索树】
    C++11 list::splice()函数的使用
  • 原文地址:https://blog.csdn.net/weixin_58080442/article/details/125541513
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号