スポンサーリンク

【LeetCode】724. Find Pivot Index 解答・解説【Python】

スポンサーリンク
スポンサーリンク
この記事は約3分で読めます。

 

問題

原文

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:

Example 2:

Example 3:

 

Constraints:

  • 1 <= nums.length <= 104
  • -1000 <= nums[i] <= 1000

 

内容

整数の配列numsが与えられるので、配列のpivot indexを計算してください。

pivot indexは、その左側にある要素の合計と、右側にある要素の合計が等しくなるインデックスです。

インデックスが配列の左端の要素の場合は、それより左側に要素はないので0です。右端の場合も同様です。

pivot indexを返してください。存在しない場合は-1を返してください。

 

 

※正しくない可能性があります。

 

解答

解答1:Python

 

 

解答2:

 

 

 

メモ・参考・感想

 

 

 

前:1480. Running Sum of 1d Array

次:205. Isomorphic Strings

LeetCode 解答・解説記事一覧

コメント