[LeetCode By Go 724] Easy, Array, 724. Find Pivot Index

题目

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].

解题思路

要求一个数组值的下标,使该值左边的和 = 该值右边的和。
暴力一点,从0开始遍历数组下标,每次计算该值左右两侧的和是否相等,相等则返回该下标;如果遍历完还没找到该下标,就返回-1。
这样最多要进行2n次求和操作,算法复杂度O(2n*n) = O(n2),如果每次下标i右移, 左边和totalLeft += nums[i], 右边和totalRight -= nums[i+1], 只用进行一次操作就能完成遍历,算法复杂度O(n+2n) = O(n).

代码

请参考我的GitHub获得更多使用go解答LeetCode的代码

pivotIndex.go

package _724_Find_Pivot_Index

import "fmt"

func pivotIndex(nums []int) int {
    length := len(nums)
    if length < 1 {
        return -1
    } else if 1 == length {
        return 0
    }

    i := 0
    var totalLeft, totalRight int
    for _, num := range nums {
        totalRight += num
    }
    totalRight -= nums[0]

    for ; i < length; i++ {
        fmt.Printf("i:%v, totalLeft:%v, totalRight:%v\n", i, totalLeft, totalRight)
        if totalLeft == totalRight {
            return i
        }
        totalLeft += nums[i]
        if i < length-1 {
            totalRight -= nums[i+1]
        }

    }

    return -1
}

测试

pivotIndex_test.go

package _724_Find_Pivot_Index

import (
    "testing"
)

type Input struct {
    nums []int
    want int
}

var inputs = []Input{
    Input{
        nums: []int{1, 7, 3, 6, 5, 6},
        want: 3,
    },
    Input{
        nums: []int{1, 2, 3},
        want: -1,
    },
    Input{
        nums: []int{},
        want: -1,
    },
    Input{
        nums: []int{3},
        want: 0,
    }, Input{
        nums: []int{-1, -1, -1, -1, -1, 0},
        want: 2,
    },
}

func Test_PivotIndex1(t *testing.T) {
    for _, input := range inputs {
        nums := input.nums
        t.Logf("input:%v\n", nums)
        ret := pivotIndex(nums)

        want := input.want
        if want == ret {
            t.Logf("pass")
        } else {
            t.Errorf("fail, want %+v, get %+v", want, ret)
            return
        }
    }

}

func Test_PivotIndex2(t *testing.T) {
    nums := inputs[4].nums
    t.Logf("input:%v\n", nums)
    ret := pivotIndex(nums)

    want := 2
    if want == ret {
        t.Logf("pass")
    } else {
        t.Errorf("fail, want %+v, get %+v", want, ret)
    }
}

    原文作者:miltonsun
    原文地址: https://www.jianshu.com/p/14a87656a45e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞