Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the central index of an array by leetcode

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how leetcode to find the central index of the array, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. the content of the topic

Given an array nums of integer type, write a method that returns the array's "central index".

We define the central index of the array as follows: the sum of all elements on the left side of the central index of the array is equal to the sum of all elements on the right.

If the array does not have a central index, then we should return-1. If the array has more than one central index, then we should return the one closest to the left.

Example 1:

Enter:

Nums = [1,7,3,6,5,6]

Output: 3

Explanation:

The sum of the left number of index 3 (nums [3] = 6) (1 + 7 + 3 = 11) is equal to the sum of the right number (5 + 6 = 11).

At the same time, 3 is the first central index that meets the requirements.

Example 2:

Enter:

Nums = [1,2,3]

Output:-1

Explanation:

There is no central index in the array that meets this condition.

Description:

The length range of nums is [0, 10000].

Any nums [I] will be an integer in the range of [- 1000, 1000].

Second, the way to solve the problem

First calculate the sum of all elements in nums, and then calculate the left sum from left to right, if:

Left and * 2 + current element = all elements and

Returns the current element index, otherwise-1 is returned.

Code class Solution: def pivotIndex (self Nums: list)-> int: whole_sum = sum (nums) left_sum = 0 for i in range (len (nums)): if left_sum * 2 + nums [I] = whole_sum: return i else: left_sum + = nums [I] return-1if _ name__ = ='_ main__': S = Solution () nums1 = [1 7, 3, 6, 5, 6] ans1 = s.pivotIndex (nums1) print (ans1) nums2 = [1,2,3] ans2 = s.pivotIndex (nums2) print (ans2) nums3 = [- 1,-1,-1,-1,-1] ans3 = s.pivotIndex (nums3) print (ans3) nums4 = [- 1,-1,-1,-1,-1 0] ans4 = s.pivotIndex (nums4) print (ans4) these are all the contents of the article "how leetcode finds the central index of an array" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report