7-8 jmu-ds-顺序表区间元素删除 (15 分)
若一个线性表L采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1)。
输入格式:
三行数据,第一行是顺序表的元素个数,第二行是顺序表的元素,第三行是x和y。
输出格式:
删除元素值在[x,y]之间的所有元素后的顺序表。
输入样例:
10
5 1 9 10 67 12 8 33 6 2
3 10
输出样例:
1 67 12 33 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int g=0,t=0;
int x,y;
int L[]={1,2,3,4,5,6,7,8,9,21213,123,1123,1,132123,123
,123,132,23,123,12,32,123,123,12,132,123,12,32
,123,12,132,123,1,23,132,123,312,321
,12312,32,123,123,1,1231,231,23,123,1321,32
,12,132,123,132,123,123,132,12,123,
123,123,123,1,231,231,231,231,231,21,321,231,231,3
,1,1,3,1321,1,1,132,123,1,1321,1,32,32,
123,123,123,132,23,1321,321,1,23,1
,123,13,1231,32,231,1,321,
123,1,132,123,123,132,123,132,12,132,123,132,132,321,132
,123,23,132,132,132,132,13,213,2,132,1,231,32,132,132,132,132
,23,132,132,132,132,13,213,2,132,1,231,32,132,132,132,13
,23,132,132,132,132,13,213,2,132,1,231,32,132,132,132,13};
int i,j;
for(i=0;i<n;i++)
L[i]=scanner.nextInt();
x = scanner.nextInt();
y = scanner.nextInt();
for(i=0;i<n;i++)
{
if(L[i]>=x && L[i]<=y)
{ L[i]=0;}
}
for(i=0;i<n;i++)
if(L[i]!=0)
g++;
for(i=0;i<n;i++)
{
if(L[i]!=0){
if(t!=g-1)
System.out.print(L[i] + " ");
else
System.out.print(L[i]);
t++;
}
}
}
}