力扣:寻找数组的中心索引[1,2,3]

请问一下大神,为什么输入[1,2,3],输出结果为0?

答案是0吗 不应该是-1吗


public class Solution724 {
    public int pivotIndex(int[] nums) {

        int out = -1;
        int countleft = 0;
        int countright = 0;

        for (int i = 0; i < nums.length; i++) {
            countleft = 0;
            countright = 0;
            for (int j = 0; j < i; j++) {

                countleft = countleft + nums[j];

            }
            // System.out.println(i+":countleft:"+countleft);
            for (int j = i + 1; j < nums.length; j++) {

                countright = countright + nums[j];

            }
            // System.out.println(i+":countright:"+countright);
            if (countleft == countright) {
                out = i;
                // System.out.println(i+":"+i);
            }

        }

        return out;
    }

    public static void main(String[] args) {

        Solution724 s = new Solution724();

        int[] nums = { 1,2,3 };

        System.out.println(s.pivotIndex(nums));
    }
}
 

out:

-1
 

 

    public int pivotIndex(int[] nums) {

        int out = -1;
        int countleft = 0;
        int countright = 0;

        for (int i = 0; i < nums.length; i++) {
            countleft = 0;
            countright = 0;
            for (int j = 0; j < i; j++) {

                countleft = countleft + nums[j];

            }
            // System.out.println(i+":countleft:"+countleft);
            for (int j = i + 1; j < nums.length; j++) {

                countright = countright + nums[j];

            }
            // System.out.println(i+":countright:"+countright);
            if (countleft == countright) {
                out = i;
                return out;
                // System.out.println(i+":"+i);
            }

        }

        return out;
    }