package com.zmz.algorithm.array;

/**
 * @author 张明泽
 * Create by 2022/5/25 12:02
 * 寻找数组的中心下标
 * LeetCode-724
 */
public class FindCenterIndex {
    public static void main(String[] args) {
        int[] nums = {1, 7, 3, 6, 5, 6};
        int result = pivotIndex(nums);
        System.out.println(result);
    }
    public static int pivotIndex(int[] nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++){
            sum += nums[i];
        }
        int leftSum = 0;
        for (int i = 0; i < nums.length; i++){
            sum -= nums[i];
            if (leftSum == sum){
                return i;
            }
            leftSum += nums[i];
        }
        return -1;
    }
}
最后修改:2022 年 05 月 25 日 12 : 16 PM
赏杯咖啡喝 谢谢您~