zoukankan      html  css  js  c++  java
  • Find Pivot Index之Python实现

    一、题目

      Given an array of integers nums, write a method that returns the "pivot" index of this array.

      We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

      If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

      Example 1:

        Input: nums = [1, 7, 3, 6, 5, 6]

        Output: 3

        Explanation: The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.Also, 3 is the first index where this occurs.

      Example 2:

        Input: nums = [1, 2, 3]

        Output: -1

        Explanation: There is no index that satisfies the conditions in the problem statement.

      Note:

      • The length of nums will be in the range [0, 10000].
      • Each element nums[i] will be an integer in the range [-1000, 1000].

    二、题目理解

      给定一个整型数组(数组中的值可正可负),寻找数组中的一个值,使得该值左边的所有值之和等于其右边所有值之和,并且该值的下标是所有满足条件的下标中最左边的一个,并返回该值的下标。如果找不到该值,则返回-1。

    三、实现代码

    1、第一种实现方式

      使用循环遍历数组,每次都计算当前下标左边所有值之和以及右边所有值之和,并判断两个和是否相等,若相等,则返回下标,否则返回-1。实现代码如下:

    def solution01(self, nums):
            startTime = time.time()     # 求当前时间
            index = -1
            for i in range(0, len(nums)):
                sum_left = 0
                sum_right = 0
                # 求i左边所有值的和
                for j in range(0, i):
                    sum_left += nums[j]
                # 求i右边所有值的和
                for k in range(i + 1, len(nums)):
                    sum_right += nums[k]
                # 判断两个和是否相等,相等则跳出循环
                if sum_left == sum_right:
                    index = i
                    break
            endTime = time.time()       # 求当前时间
            print("程序执行时间:", (endTime - startTime))      #打印程序运行时间(单位:秒)
            return index
    

    2、第二种实现方式

      以上的第一种方法的执行时间过长,不是一种好的解决方法,下面是第二种解决方案。

      首先求出整个数组所有值的总和sum,并定义一个变量用于存放当前遍历过的所有值之和cur_sum,再遍历数组,每遍历一个值,都用sum减去当前值,并判断减后的值是否等于cur_sum的2倍,如果是,等当前下标即为所求下标,否则返回-1。

      由于这个方法的循环嵌套较少,运行速度比第一种方法要快。

      实现代码如下:

    def solution02(self, nums):
            startTime = time.time()
            index = -1
            sum = 0
            cur_sum = 0
            # 求数组总和
            for i in range(0, len(nums)):
                sum += nums[i]
            for i in range(0, len(nums)):
                temp = sum - nums[i]        # 求总和减去当前值所得的结果
                # 如果temp等于cur_sum的2倍,则返回该下标,否则,算cur_sum的新值
                if temp == cur_sum * 2:
                    index = i
                    break
                else:
                    cur_sum += nums[i]
            endTime = time.time()
            print("程序执行时间:", (endTime - startTime))
            return index
    

    源码链接

  • 相关阅读:
    Python图形图像处理库的介绍之Image模块
    python re.sub
    eclipse 安装git插件
    一组神奇的 3D Gif 动图
    互联网颠覆房地产
    一位IT行业高收入者的理财规划方案
    阿里核心系统团队介绍
    大规模SNS中兴趣圈子的自动挖掘
    关于 MySQL LEFT JOIN 你可能需要了解的三点
    Could not connect to SMTP host: localhost, port: 25;
  • 原文地址:https://www.cnblogs.com/xiezh-it/p/9711710.html
Copyright © 2011-2022 走看看