顺序表——插入不重复元素

来源:互联网 发布:日本社交软件 编辑:程序博客网 时间:2024/05/16 02:18

顺序表——插入不重复元素

在顺序表中插入不重复元素。查找不成功时,尾插入。
public int insertDifferent(T x) {    if (x==null){throw new NullPointerException("x==null");     //抛出空对象异常    }    if(this.isEmpty()){   //空表,尾插入, O(1)    this.insert(x);    return 1;    }for(int i = 0; i<this.n; i++){if(x.compareTo(this.get(i)) == 0){     //已存在,不做操作return -1;}}this.insert(x);   //查找不成功,尾插入, O(1)return 1;    }


时间复杂度O(n)

原创粉丝点击