从顺序表L中删除所有值为x的元素,要求时间复杂为O(n),空间复杂度为O(1)
public class Delx {
public static void main(String[] args) {
int[] a = new int[]{1,2,3,4,1,2,3,4,5,6,1,1,2,3,4,5,3,3,1,1,3};
int del_position = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == 1){
if (del_position == -1)
del_position = i;
}else {
a[del_position] = a[i];
del_position++;
}
}
for (int i = 0; i <del_position; i++) {
System.out.print(""+a[i]);
}
}
}
网友评论