`a[k++] = l[i++]` 表示什么意思

huangapple go评论104阅读模式
英文:

What does a[k++] = l[i++] notation mean

问题

public static void merge(int[] a, int[] l, int[] r, int left, int right) {
 
    int i = 0, j = 0, k = 0;
    while (i < left && j < right) {
        if (l[i] <= r[j]) {
            a[k++] = l[i++];
        }
        else {
            a[k++] = r[j++];
        }
    }
    while (i < left) {
        a[k++] = l[i++];
    }
    while (j < right) {
        a[k++] = r[j++];
    }
}
英文:

I'm trying to figure out this merge function for merge sort, but I don't understand the line a[k++] = l[i++]. Can someone give me the non-reduced version of what that would look like?

public static void merge(int[] a, int[] l, int[] r, int left, int right) {
 
    int i = 0, j = 0, k = 0;
    while (i &lt; left &amp;&amp; j &lt; right) {
        if (l[i] &lt;= r[j]) {
            a[k++] = l[i++];
        }
        else {
            a[k++] = r[j++];
        }
    }
    while (i &lt; left) {
        a[k++] = l[i++];
    }
    while (j &lt; right) {
        a[k++] = r[j++];
    }
}

答案1

得分: 1

a[k++] = l[i++]; 等同于

a[k] = l[i];
k = k + 1;
i = i + 1;


假设 k = 3i = 5

a[k++] = l[i++];
print(k);
print(i);

输出结果: 4 6

等同于

a[k] = l[i];
k = k + 1;
i = i + 1;
print(k);
print(i);

输出结果: 4 6

英文:
a[k++] = l[i++];

is the same as

a[k] = l[i];
k = k + 1;
i = i + 1;

Suppose k = 3 and i = 5.

a[k++] = l[i++];
print(k);
print(i);

>OUTPUT: 4 6

is the same as

a[k] = l[i];
k = k + 1;
i = i + 1;
print(k);
print(i);

>OUTPUT: 4 6

huangapple
  • 本文由 发表于 2020年8月19日 07:51:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63478179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定