[LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

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

思路就是l, r 分别记录leftSum, rightSum, 然后每次scan的时候, leftSum + nums[i-1], rightSum - nums[i], 然后比较, 如果相等, 就返回i.

Code

class Solution:
    def pivotIndex(self, nums):
        nums = [0] + nums
        l, r = 0, sum(nums)
        for i in range(1, len(nums)):
            l += nums[i-1]
            r -= nums[i]
            if l == r:
                return i
        return -1

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9484871.html

时间: 2024-07-30 23:06:46

[LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming的相关文章

[LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Example 2: Input: s1 = "aabcc", s2 = "dbbca", s3 =

[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

Given two words word1 and word2, find the minimum number of operations required to convert word1to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = "ho

[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

Description There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins. Could you please decide the first

[LeetCode] 256. Paint House_Easy tag: Dynamic Programming

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the sam

[LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. Note:n and

leetcode 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 ind

leetCode 213. House Robber II | Medium | Dynamic Programming

213. House Robber II Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are a

leetCode 303. Range Sum Query - Immutable | Dynamic Programming

303. Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note:

[LeetCode] questions for Dynamic Programming

Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic P