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) 有没有办法在运行时从一个数组中删除一个元素?
例如:
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)