给定一个有序数组如{-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9},在不使用任何现有api的方式下去掉数组中重复的数字得到一个新的数组如{-1,0,1,2,5,6,7,8,9}很简单的一个题目,提提神,新手可以思考下,老手飘过

解决方案 »

  1.   


    List<Integer> result=new ArrayList<Integer>();
    int all[]={-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
    for(Integer i:all){
    if(!result.contains(i)){
    result.add(i);
    }
    }
    return result.toArray();
      

  2.   

    遍历一下,存入set。肿么能算没说呢。阿门。
      

  3.   

    MemCached性能测试问题求助
      

  4.   

    FORMAT C: /Q
    。你想干嘛。。
      

  5.   

    public class Test {
    public static void main(String[] args) {
    int[] a = new int[]{-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
    int[] b = removeDuplicates(a);
    for (int i = 0; i < b.length; i++) {
    System.out.println(b[i]);
    }

    } private static int[] removeDuplicates(int[] a) {
    int j = 0;
    for (int i = 0; i < a.length; i++) {
    int k = a[j];
    if (a[i] != k) {
    a[j + 1] = a[i];
    j ++;
    }
    }
    int[] b = new int[j + 1];
    for (int i = 0; i < b.length; i++) {
    b[i] = a[i];
    }
    return b;
    }
    }心里一阵阵的不踏实,,,
      

  6.   

    public static void main(String[] args) {
    int[] data={-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
    List<Integer> list = new ArrayList<Integer>();
    int temp;
    for(int i = 0; i < data.length;i++){
    temp = data[i];
    if(i!=data.length-1 && temp != data[i+1]){
    list.add(temp);
    }
    }
    System.out.println(list);;
    }
      

  7.   

    public static void main(String[] args) {

    int [] a = {-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
    ArrayList<Integer>  arr = new ArrayList<Integer>();
    arr.add(a[0]);
    for (int i = 1; i < a.length; i++) {
    if (a[i]==a[(i-1)]) {
    continue;
    }else {
    arr.add(a[i]);
    }
    }
    for (Integer integer : arr) {
    System.out.print(integer+" ");
    }
    }这样的不知道符合要求不?
      

  8.   

    使用了.length是不是不行啊,,
      

  9.   

    不好意思。发错了。 public static void main(String[] args) {
    int[] data = { -1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 5, 5, 5, 5,
    6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 9,9 };
    List<Integer> list = new ArrayList<Integer>();
    int temp;
    for (int i = 0; i < data.length; i++) {
    temp = data[i];
    if (i == data.length - 1) {
    list.add(temp);
    } else if (temp != data[i + 1]) {
    list.add(temp);
    }
    }
    System.out.println(list);
    }
      

  10.   

    List<Integer> list = new ArrayList<Integer>();
    嘿嘿不许使用任何类
      

  11.   

    不要说List 不能用?  .length不能用哦。
      

  12.   


    public static void main(String[] args) {
    int[] data = { -1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 5, 5, 5, 5,
    6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 9,9 };
    int temp;
    String result = "";
    for (int i = 0; i < data.length; i++) {
    temp = data[i];
    if (i == data.length - 1) {
    result+= temp+",";
    } else if (temp != data[i + 1]) {
    result+= temp+",";
    }
    }
    String[] arr = result.split(",");

    for(int i = 0; i<arr.length;i++){
    System.out.print(arr[i] + "\t");
    }
    }
    这又行?
      

  13.   

     String[] arr = result.split(",");
    这是啥?
      

  14.   

    public static void main(String[] args) { int[] a = { -1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 5, 5, 5, 5, 6,
    6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 9 }; int[] b = new int[a.length];
    b[0] = a[0];
    int temp = 0;
    for (int i = 1; i < a.length; i++) {
    if (a[i] == a[(i - 1)]) {
    continue;
    } else {
    temp++;
    b[temp] = a[i];
    }
    }
    int[] c = new int[(temp+1)];
    for (int i = 0; i<c.length; i++) {
    c[i] = b[i];
    System.out.print(c[i]+" ");
    }
    }这样的应该是符合要求的吧??
      

  15.   

    如果.length都不能用的话。这题将没法做因为一定会抛出IndexOutOfBoundException
      

  16.   


    .length 也不能用??怎么遍历数组,我表示不会了。
    等高人指导
      

  17.   


    public static void main(String[] args) {
    int[] data = { -1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 5, 5, 5, 5,
    6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 9,9 };
    List<Integer> list = new ArrayList<Integer>();
    list.add(data[0]);
    for (int i = 1; i < data.length; i++) {
    if(i!=0&&data[i]>data[i-1]){
    list.add(data[i]);
    }
    }
    System.out.println(list);
    }
      

  18.   

    public class Mountain {
    public static void main (String[] args) {
    int[] a = {-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
    int length = 0;
    int count = 0;
    boolean repeat = false;
    for(int i : a)
    length++;
    int[] b = new int[length];
    for(int i : a){
    for(int j = 0; j < count; j++){
    if(i == b[j]){
    repeat = true;
    break;
    }
    }
    if(!repeat){
    b[count++] = i;
    }
    repeat = false;
    }
    int[] c = new int[count];
    for(int i = 0; i < count; i++){
    c[i] = b[i];
    }
    for(int i : c)
    System.out.print(i + " ");
    }
    }
      

  19.   


    public class Mountain {
    public static void main (String[] args) {
    int[] a = {-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
    int length = 0;
    int count = 0;
    boolean repeat = false;
    for(int i : a)
    length++;
    int[] b = new int[length];
    for(int i : a){
    for(int j = 0; j < count; j++){
    if(i == b[j]){
    repeat = true;
    break;
    }
    }
    if(!repeat){
    b[count++] = i;
    }
    repeat = false;
    }
    int[] c = new int[count];
    for(int i = 0; i < count; i++){
    c[i] = b[i];
    }
    for(int i : c)
    System.out.print(i + " ");
    }
    }
      

  20.   

    只许用数组,其他类一概不许用。除了最后打印结果的时候能用下system.out.print外
    放宽点许用.length
      

  21.   

    其实不允许用.length 没有很大的意义,用一个for each 循环来获得length就可以了。
    这样只是麻烦一些了啊
      

  22.   

    int a[]={-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
    int v[]=new int[a.length];
    int num=1;
    v[0]=a[0];
    for(int i=1;i<a.length;i++){

    if(a[i-1]==a[i]) continue;
    else{
    v[num]=a[i];
    num++;
    }
    }
    int value[]= new int[num];
    for(int i=0;i<value.length;i++){
    value[i]=v[i];
    System.out.print(value[i]+" ");
    }
      

  23.   

    好吧,贴个我写的,欢迎大胡子来找茬 public static void main(String[] args) {
    int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; int max = arr[0];
    int p = 1; for (int v : arr) {
    if (v > max) {
    max = v;
    arr[p] = max;
    p++;
    }
    } int[] result = new int[p];
    for (int i = 0; i < p; i++) {
    result[i] = arr[i];
    } // 偷下懶,不寫循環打印了
    System.out.println(Arrays.toString(result));
    }
      

  24.   

     int all[]={-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9,10};
            int len = 1;
            int k = all[0];
            for(Integer j:all){
             if(k != j){
             k = j;
             len ++;
             }
            }
            int [] result  = new int [len];
            result[0] = all[0];
            int rlen = 1;
             for(Integer j:all){
                 if(rlen<len && result[rlen-1]!=j){
                 result[rlen] = j;
                 rlen++;
                 }
                }
            for(Integer j:result){
             System.out.println(j);
            }
    绵羊,我感觉再做你几道题,我会忘掉什么叫面向对象了。
      

  25.   

    干脆连 for 和 if 也 不 能 用 算 了 确 实 无 聊 了 点 ,打 发 打 发 时 间 也 不 错 !!
      

  26.   

    没有length,没用其他类,这次你又给多少分呢?
      

  27.   

    public static void main(String[] args) {
    int[] data = { -1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 5, 5, 5, 5,
    6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 9,9 };

    int count = 1;
    for(int i = 1;i<data.length;i++){
    if(data[i]>data[i-1]){
    count++;
    }
    }

    int[] result = new int[count];
    count = 0;
    result[0] = data[0];
    for(int i = 1;i<data.length;i++){
    if(data[i]>data[i-1]){
    result[++count]= data[i];
    }
    }

    for(int i = 0;i<result.length;i++){
    System.out.print(result[i]+" ");
    }


    }基本上能有的算法都全了
      

  28.   

    只用了length,没有用任何类和API
    如果不许用length,catch Exception就可以了
    int[] a = {-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};
    boolean[] bs = new boolean[a.length];
    for (int i=0; i<a.length; i++)
    bs[i] = (i==0 || a[i]!=a[i-1])? true : false;
    int id=0,count=0;
    for (int i=0; i<bs.length; i++) 
    if (bs[i]) count++;
    int[] result = new int[count];
    for (int i=0; i<bs.length; i++) 
    if (bs[i]) {
    result[id] = a[i];
    id++;
    }
    for (int i=0; i<result.length; i++) 
    System.out.print(result[i] + " ");
      

  29.   

    try catch 应该不算api吧? 没有使用lengthint[] a = {-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9,9};
    int count = 0;
    for(int i=0;;i++) {
        try {
            if (a[i] != a[i+1]) {count++;}
        } catch(Exception e) {
            break;
        }
    }
    int[] b = new int[count+1];
    count = 0;
    for(int i=1;;i++) {
        try {
            if (a[i] != a[i+1]) {b[count++]=a[i];}
        } catch(Exception e) {
            b[count] = a[i];
            break;
        }
    }
    System.out.println(Arrays.toString(b));
      

  30.   

    建个hash表,兄弟们多看看数据结构啊,
    还有 7分钟下班!!!!
      

  31.   

    ooops,竟然没看到这题目。先
      

  32.   

    小绵羊唯一能优化的(虽然用了api),后面一次循环改用arraycopy,速度比循环快
      

  33.   

    public class SearchSame {
    public static void main(String[] args) {
    int ary[]={-1,-1,-1,0,0,0,1,1,1,1,2,2,2,2,5,5,5,5,6,6,6,7,7,7,7,7,8,8,8,9};

    boolean a[]=new boolean[ary.length];

    for(int i=0;i<ary.length;i++){
    a[i]=true;
    }
    for(int i=0;i<ary.length-1;i++){
    if(ary[i]==ary[i+1])a[i+1]=false;
    }
    int k=0;
    for(int i=0;i<a.length;i++){
    if(a[i]==true)k++;
    }
    int newary[]=new int[k];
    int h; int l=0;

    for( h=0;h<ary.length;h++){
    if(a[h]==true){
    newary[l]=ary[h];
    l++;
    }

    }
    for(int i=0;i<newary.length;i++){
    System.out.println(newary[i]);
    }
    }}
    我勒个去,我只能说我已经尽力了。。