CS61B srping 2018 examprep03 https://sp18.datastructur.es/

flatten 方法接受二维数组,返回一个一维数组

比如,flatten({{1, 2, 3}, {}, {7, 8}}) should return {1, 2, 3, 7, 8}

补全下面的程序片段

public static int[] flatten(int[][] x) {
int totalLength = 0;

for (____________________________________) {

_______________________________________________
}

int[] a = new int[totalLength];
int aIndex = 0;

for (____________________________________) {

_______________________________________________

_______________________________________________

_______________________________________________

_______________________________________________
}

return a;
}

sol:

public class Arr {
    public static int[] flatten(int[][] x) {
        int totalLength = 0;

        for (int i = 0; i < x.length; i++) {
            totalLength += x[i].length;

        }

        int[] a = new int[totalLength];
        int aIndex = 0;

        for (int i = 0; i < x.length; i++) {
            int count = 0;
            while (count < x[i].length) {
                a[aIndex] = x[i][count];
                aIndex++;
                count++;
            }
        }

        return a;
    }

    public static void main(String[] args) {
        int[][] a ={{1, 2, 3}, {}, {7, 8}};
        int [] b =  flatten(a);
    }
}

skippify方法 会把List中的一些元素“跳过”

比如
IntList A = IntList.list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
IntList B = IntList.list(9, 8, 7, 6, 5, 4, 3, 2, 1);

  • After calling A.skippify(), A: (1, 3, 6, 10)
  • After calling B.skippify(), B: (9, 7, 4)
public class IntList {
public int first;
public IntList rest;

@Override
public boolean equals(Object o) { ... }
public static IntList list(int... args) { ... }

public void skippify() {
IntList p = this;
int n = 1;
while (p != null) {

IntList next = ___________________________;

for (_______________________________________) {

if (________________________________________) {

___________________________________________
}

___________________________________________
}

___________________________________________

___________________________________________

___________________________________________
}
}
}

sol:

import java.util.Formatter;

/**
 * A naked recursive list of integers, similar to what we saw in lecture 3, but
 * with a large number of additional methods.
 *
 * @author P. N. Hilfinger, with some modifications by Josh Hug and melaniecebula
 * [Do not modify this file.]
 */
public class IntList {
    /**
     * First element of list.
     */
    public int first;
    /**
     * Remaining elements of list.
     */
    public IntList rest;

    /**
     * A List with first FIRST0 and rest REST0.
     */
    public IntList(int first0, IntList rest0) {
        first = first0;
        rest = rest0;
    }

    public static IntList of(Integer... args) {
        IntList result, p;

        if (args.length > 0) {
            result = new IntList(args[0], null);
        } else {
            return null;
        }

        int k;
        for (k = 1, p = result; k < args.length; k += 1, p = p.rest) {
            p.rest = new IntList(args[k], null);
        }
        return result;
    }

    /**
     * A List with null rest, and first = 0.
     */
    public IntList() {
        /* NOTE: public IntList () { }  would also work. */
        this(0, null);
    }

    public void skippify() {
        IntList p = this;
        int n = 1;
        while (p != null) {

            IntList next = p.rest;

            for (int i = 0; i < n; i++) {

                if (next == null) {
                    break;

                }
                next=next.rest;


            }
            p.rest=next;
            p=next;
            n++;
        }
    }

    public static void main(String[] args) {
        IntList A = IntList.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        IntList B = IntList.of(9, 8, 7, 6, 5, 4, 3, 2, 1);

        A.skippify();
        B.skippify();
//        - After calling A.skippify(), A: (1, 3, 6, 10)
//        - After calling B.skippify(), B: (9, 7, 4)
    }
}

removeDuplicates 方法 会把List中重复的元素去掉

  • Given a sorted linked list of items - remove duplicates.
  • For example given 1 -> 2 -> 2 -> 2 -> 3,
  • Mutate it to become 1 -> 2 -> 3 (destructively)
public static void removeDuplicates(IntList p) {
if (p == null) {
return;
}

IntList current = _________________________;

IntList previous = ________________________;

while (__________________________________) {

if (_____________________________________) {

________________________________________
} else {

________________________________________
}

____________________________________________
}
}
}

sol:

   public static void removeDuplicates(IntList p) {
        if (p == null) {
            return;
        }

        IntList current =p.rest ;

        IntList previous = p;
        while (current!=null) {

            if (previous.first==current.first) {
                previous.rest=current.rest;
                current=current.rest;

            } else {
            previous=previous.rest;
            current=current.rest;
            }



    }

}

    public static void main(String[] args) {
        IntList A = IntList.of(1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5, 6, 7, 7, 7, 8, 9, 10, 10, 10, 10);
        removeDuplicates(A);
//        A 应该是{1,2,3,4,5,6,7,8,9,10}

    }
posted @ 2024-12-13 16:29  刘老六  阅读(42)  评论(0)    收藏  举报