我正在尝试执行 "Count Reverse Pairs" 但我不明白为什么在这一行需要 parentesis()

I am trying out to do the "Count Reverse Pairs" and I can't understand why parentesis() are necessary on this line

我知道加法是可交换的,因此我想在第 46 行用 shorthand 操作 += 执行存储操作,但只有当我放括号时答案才会不同,当我不放括号时也是如此'放括号我得到错误的答案。

该行在合并函数中。

代码:

public class ProblemA {

    public static void main(String[] args) {
        ArrayList<Integer> arr = new ArrayList<Integer> (Arrays.asList(1, 3, 2, 3, 1));
        int n = arr.size();
        //calling mergesort 
        int total = countPair(arr, n);
        System.out.println(total);
    }

    public static int countPair(ArrayList<Integer> arr, int n) {
        int total = mergeSort(arr, 0, n - 1);
        return total;
    }

    public static int mergeSort(ArrayList<Integer> arr, int low, int high) {
        //termination condition
        if (low >= high) return 0;
        //firstly we'll disassociate the elements of array on elementary level
        int mid = (low + high) / 2;
        //we'll store our count value inside the counter variable 
        int inv = mergeSort(arr, low, mid);
        inv += mergeSort(arr, mid + 1, high);
        inv += merge(arr, low, mid, high);
        return inv;
    }
    public static int merge(ArrayList<Integer> arr, int low, int mid, int high) {
        //AIM: make a double loop and traverse through the elements and increase the right side pointer
        //whenever condition (arr[i]>2*arr[j] is satisfied)
        //int i = 0;
        int total = 0;
        int j = mid + 1;
        for (int i = low; i<= mid; i++) {
            //looping till we run out the right hand side or condition is not satisfied
            while (j<= high && arr.get(i) > 2 * arr.get(j)) {
                j++;
            } **
            * total += (j - (mid + 1)); ** * //parenthesis error here
        }

        //Now we can move to merging part
        ArrayList<Integer> temp = new ArrayList<Integer> ();
        int left = low, right = mid + 1;
        while (left<= mid && right<= high) {
            if (arr.get(left)<= arr.get(right)) {
                temp.add(arr.get(left++));
            } else {
                temp.add(arr.get(right++));
            }
        }

        //for the last right or left remaining element 
        while (left<= mid) {
            temp.add(arr.get(left++));
        }
        while (right<= high) {
            temp.add(arr.get(right++));
        }

        //now we can copy the remaining elements from temp list to arr
        for (int i = low, k = 0; i<= high; i++) {
            arr.set(i, temp.get(k++));
        }
        return total;
    }
}

OUTPUT(带括号) "total += (j-(mid+1))":

2

OUTPUT(无括号) "total += j-mid+1":

16

发生这种情况是因为如果您使用括号,java 将决定您要首先计算哪个表达式。如果你不使用括号,那么一切都会从左到右执行。 示例:

int total=1;
int total1=1;
int total2=1;
total+=total1-total2+1;
System.out.println(total); 

输出:2

原因就在这里,表达式被计算为total=total+total1-total2+1; 即总计=1+1-1+1=2;

对于基于括号的表达式,它首先被评估然后其他:-

int total=1;
int total1=1;
int total2=1;
total+=(total1-(total2+1));
System.out.println(total);

输出:0,评估如下:-

total=total+(1-(1+1));
total=1+(1-(2));
total=1+(-1);
total=0;

参考:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html