int n = a.length; 把数组的 长度 交给 int类型 n 保存
for (int i = 10; i < n/2 ; i++) {
int j = n - 1 -i ;
int temp = a[i]; 值交换
a[i] = a[j];
a[j] = temp;
}
它 这里 i < n/2 表示了 啥意思? 这个有什么作用?
这里 n - 1 -i 表示了 啥意思? 这个有什么作用?
求大佬救救 迷茫的我
已解决
评论 (2)
- 受欢迎
- 新
- 旧
你必须先登录才能发表评论
Lisa
11 二月 2022, 15:20解决方法
n / 2 -> stops when the loop reaches the mid of the array, cause...
example array 1, 2, 3, 4, 5, 6
i = 0: index i(0) = 1, index array.length - 1 - i(0) = 6 // the last index of an array is length - 1
i = 1: index i(1) = 2, index array.length - 1 - i(1) = 5
i = 2: index i(2) = 3, index array.length - 1 - i(2) = 4
in each iteration you flip i and array.length - 1 - i values ==> the array is reversed 6, 5, 4, 3, 2, 1
+2
代码没写完啊~
14 二月 2022, 00:48
感谢大佬 ,虽然有好多都不清楚,如阵列、迭代这些知识点都没了解过
0