小编rob*_*ert的帖子

Total of all numbers from 1 to N will always be zero

The problem is I have to print all combinations of a sequence of numbers from 1 to N that will always result to zero. It is allowed to insert "+" (for adding) and "-" (for subtracting) between each numbers so that the result will be zero.

//Output
N = 7

1 + 2 - 3 + 4 - 5 - 6 + 7 = 0
1 + 2 - 3 - 4 + 5 + 6 - 7 = 0 …
Run Code Online (Sandbox Code Playgroud)

java algorithm

6
推荐指数
1
解决办法
1436
查看次数

在Java中从运行时删除Array中的元素

有没有办法在运行时从一个数组中删除一个元素?

例如:

int[] num =  {8, 1, 4, 0, 5};

Output:
Enter the Index: 0
1, 4, 0, 5
Enter the Index: 3
1, 4, 0
Enter the Index: 1
4, 0;
Run Code Online (Sandbox Code Playgroud)

我知道一旦初始化你就无法调整数组的长度,并且在这种样本问题中,使用a ArrayList更加实用.但是,有没有办法通过仅使用数组来解决这类问题?

我设法删除了一个元素并通过创建新数组并在其中复制原始数组的值来显示数组-1.但问题是,在输出的下一次迭代中,我仍然可以删除一个元素但是大小不会改变.

这是发生的事情:

int[] num =  {8, 1, 4, 0, 5};

Output:
Enter the Index: 0
1, 4, 0, 5  // in the first loop it goes as I want it.
Enter the Index: 2
1, 4, 5, 5  // this time array's length is still …
Run Code Online (Sandbox Code Playgroud)

java arrays

4
推荐指数
1
解决办法
156
查看次数

标签 统计

java ×2

algorithm ×1

arrays ×1