码农知识堂 - 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

  • 相关阅读:
    Centos7 yum报错 Could not resolve host: mirrorlist.centos.org
    GIS前端编程-Leaflet插件扩展
    LeetCode刷题——等差数列划分#413#Medium
    QT-day4
    汇编语言程序设计·RB(AT&T汇编)_笔记_第12章:使用Linux系统调用
    对话PostgreSQL作者Bruce:“转行”是为了更好地前行
    制造业行业现状及智能生产管理系统一体化解决方案
    【Java】安装JDK开发者工具包并编写第一个程序“Hello World.java”
    《HarmonyOS开发 – OpenHarmony开发笔记(基于小型系统)》第6章 环境监测系统
    Scala基础【异常、隐式转换、泛型】
  • 原文地址: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号