724. Find Pivot Index Easy

@problem@discussion
#Array#Prefix Sum



1/**
2 * [724] Find Pivot Index
3 *
4 * Given an array of integers nums, calculate the pivot index of this array.
5 * 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.
6 * 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.
7 * Return the leftmost pivot index. If no such index exists, return -1.
8 *  
9 * Example 1:
10 * 
11 * Input: nums = [1,7,3,6,5,6]
12 * Output: 3
13 * Explanation:
14 * The pivot index is 3.
15 * Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
16 * Right sum = nums[4] + nums[5] = 5 + 6 = 11
17 * 
18 * Example 2:
19 * 
20 * Input: nums = [1,2,3]
21 * Output: -1
22 * Explanation:
23 * There is no index that satisfies the conditions in the problem statement.
24 * Example 3:
25 * 
26 * Input: nums = [2,1,-1]
27 * Output: 0
28 * Explanation:
29 * The pivot index is 0.
30 * Left sum = 0 (no elements to the left of index 0)
31 * Right sum = nums[1] + nums[2] = 1 + -1 = 0
32 * 
33 *  
34 * Constraints:
35 * 
36 * 	1 <= nums.length <= 10^4
37 * 	-1000 <= nums[i] <= 1000
38 * 
39 *  
40 * Note: This question is the same as 1991: <a href="https://leetcode.com/problems/find-the-middle-index-in-array/" target="_blank">https://leetcode.com/problems/find-the-middle-index-in-array/</a>
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/find-pivot-index/
46// discuss: https://leetcode.com/problems/find-pivot-index/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn pivot_index(nums: Vec<i32>) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_724() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.