java - 我正在尝试做“Count Reverse Pairs”,但我不明白为什么在这条线上需要 parentesis()

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

该行在合并功能中。

代码:

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;
    }
}

输出(带括号)“总计 += (j-(mid+1))”:

2

输出(不带括号)“total += j-mid+1”:

16

回答1

发生这种情况是因为如果您使用括号, 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

相似文章

algorithm - 长度为 5 的回文数

给定一个二进制字符串S,找出长度为5的回文子序列的数量。长度为5的回文子序列是数组a<b<c<d<e的5个递增索引的列表,使得S[a]S[b]S[c]S[d]S[e]的串联形成回文。如果两个回文子序列...

随机推荐

最新文章