当前位置:网站首页>LeetCode 724. Find the central subscript of the array
LeetCode 724. Find the central subscript of the array
2022-06-24 02:42:00 【freesan44】
Title address (724. Find the central subscript of the array )
https://leetcode-cn.com/problems/find-pivot-index/
Title Description
Give you an array of integers nums , Please calculate the of the array Center subscript . Array Center subscript Is a subscript of the array , The sum of all elements on the left is equal to the sum of all elements on the right . If the central subscript is at the leftmost end of the array , Then the sum of the numbers on the left is regarded as 0 , Because there is no element to the left of the subscript . This also applies to the fact that the central subscript is at the rightmost end of the array . If the array has multiple central subscripts , Should return to Closest to the left The one of . If the array does not have a central subscript , return -1 . Example 1: Input :nums = [1, 7, 3, 6, 5, 6] Output :3 explain : The central subscript is 3 . The sum of the numbers on the left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 , The sum of the numbers on the right sum = nums[4] + nums[5] = 5 + 6 = 11 , Two equal . Example 2: Input :nums = [1, 2, 3] Output :-1 explain : There is no central subscript in the array that satisfies this condition . Example 3: Input :nums = [2, 1, -1] Output :0 explain : The central subscript is 0 . The sum of the numbers on the left sum = 0 ,( Subscript 0 There is no element on the left ), The sum of the numbers on the right sum = nums[1] + nums[2] = 1 + -1 = 0 . Tips : 1 <= nums.length <= 104 -1000 <= nums[i] <= 1000 Be careful : This topic and the main station 1991 The question is the same :https://leetcode-cn.com/problems/find-the-middle-index-in-array/
Ideas
Sum up first , Then deduct from the left
Code
- Language support :Python3
Python3 Code:
class Solution:
def pivotIndex(self, nums: List[int]) -> int:
rRes = sum(nums)
lRes = 0
for index,val in enumerate(nums):
rRes -= val
# print(lRes,rRes)
if rRes == lRes:
return index
lRes += val
return -1Complexity analysis
Make n Is array length .
- Time complexity :$O(n)$
- Spatial complexity :$O(1)$
边栏推荐
- Offline store + online mall, why do you want to be an online mall
- LeetCode 599. Minimum index sum of two lists
- What about registered domain names? How long does it take to register a domain name?
- Gartner released the magic quadrant of enterprise low code platform in 2021. Low code integrated platform becomes a trend!
- Objective-C downloads pictures from the network, saves them, and displays them from the save with uiimageview
- NFT metauniverse and the relationship between Games Golden Finance
- How to log in the remote server of Fortress machine working principle of Fortress machine
- Coding helps Jiangsu high-speed information to achieve organizational agility and R & D agility, leading the new infrastructure of Intelligent Transportation
- What are the main functions of DNS? What are the benefits of IP address translation
- Go language starts again, go modules' past life, present life and basic use
随机推荐
How to handle the abnormal state of easycvr national standard cascading superior display?
Is a trademark domain name useful? How long does it take to register a domain name?
Create and mount large files
How to quickly handle third-party login and easy to expand?
Tencent cloud temporary secret key scheme - character recognition example
[supply chain • case] Tianneng group: the key to understanding the leading battery manufacturer to achieve the first profit fault
How to enable IPv6 network access for personal broadband
Go language starts again, go modules' past life, present life and basic use
Live broadcast Reservation: a guide to using the "cloud call" capability of wechat cloud hosting
Using the database middleware MYCAT to realize read-write separation (dual master and dual slave)
The easydss on demand file upload interface calls postman to report an error. Failed to upload the file?
LeetCode 599. Minimum index sum of two lists
Deep and shallow copy
How to bind EIP to access public network in tke cluster fixed IP mode pod
How does easydss handle the problem that the sharing page cannot be opened due to cache problems?
Is the server connected to the fortress machine a virtual machine? What if the fortress machine IP is not connected
IPhone sending SMS implementation
How to calculate the trademark registration fee? How much does it cost to register a trademark?
How to batch output ean 13 code to pictures
What is data matrix code
