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

为下面方法添加 SLList.insert 方法,要求签名如public void insert(int item, int position) {} ,如果position大于最后一个元素,那就是添加到最后。

(注意这个作业里的SLList和课程中介绍的SLList相比少点东西,故意的,可能是为了让学生开拓思路?)

public class SLList {
private class IntNode {
public int item;
public IntNode next;
public IntNode(int item, IntNode next) {
this.item = item;
this.next = next;
}
}

private IntNode first;

public void addFirst(int x) {
first = new IntNode(x, first);
}
}

如下

    public void insert(int item, int position) {
        IntNode ptr = this.first;
        int index = 1;
        if(ptr==null||position==0){
            this.addFirst(item);
            return;
        }

        while(index<position&&ptr.next!=null){
            index++;
            ptr=ptr.next;
        }
        IntNode newNode = new IntNode(item,ptr.next);
        ptr.next=newNode;

    }

添加reverse方法

public void reverse() {} 把列表内元素颠倒过来,这里不可以用new 新建对象。 也不让用数组什么其他东西。



public class SLList {
    private class IntNode {
        public int item;
        public IntNode next;
        public IntNode(int item, IntNode next) {
            this.item = item;
            this.next = next;
        }
    }

    private IntNode first;

    public void addFirst(int x) {
        first = new IntNode(x, first);
    }
    public void insert(int item, int position) {
        IntNode ptr = this.first;
        int index = 1;
        if(ptr==null||position==0){
            this.addFirst(item);
            return;
        }

        while(index<position&&ptr.next!=null){
            index++;
            ptr=ptr.next;
        }
        IntNode newNode = new IntNode(item,ptr.next);
        ptr.next=newNode;

    }
//    算法4 第104页 。。。
    public void reverse() {
        if(first==null||first.next==null){
            return;
        }
                IntNode replacement =null;
       while(first!=null){
            IntNode  second=first.next;
            first.next=replacement;
            replacement=first;
            first = second;
        }
      first= replacement;
    }

    public static void main(String[] args) {
    SLList list = new SLList();
//     0 ,1 ,2 ,3 ,4 ,999  ;这样的list
    list.addFirst(4);
    list.addFirst(3);
    list.addFirst(2);
    list.addFirst(0);
    list.insert(999,999);
    list.insert(1,1);

    list.reverse();
//     999 ,4 ,3 ,2 ,1,0;这样的list

    }
}

另外,因为上面用的是迭代版本的,请用递归重新编写一个同样效果的 reverse方法,网站上允许先使用一个 helper 函数。(我猜原因是 reverse 方法签名 返回void ,书上给的方法没用helper也挺好)
(额,都荒废6天了吗,好废。。。。
被生活好顿击倒。好歹今天缓过来了哈。)

```plaintext
 public void reRevers() {
        first = reReverseHelper(first);
    }

    private IntNode reReverseHelper(IntNode first) {
        if (first == null || first.next == null) {
            return first;
        }
        IntNode second = first.next;
        IntNode rest = reReverseHelper(second);
        second.next = first;
        first.next = null;
        return rest;

    }

数组Array 写一个 public static void insert(item,postion){} ,在position处,向数组插入一个item,如果position 大于数组长度,则插入到最后。

要求 destructively。

答案: 在看过一眼答案之后,答案说,不可能,因为数组长度固定,想要插入新元素,就必须新建一个数组。 (呃呃呃啊啊啊)

编写一个函数public static int[] insert(int[] arr, int item, int position) {}

  public static int[] insert(int[] arr, int item, int position) {
        int[] newArr=  new int[arr.length+1];
        int index =  Math.min(position,arr.length);
        for(int i =0;i<position;i++){
            newArr[i]= arr[i];
        }
        newArr[index] = item;

        for(int i=index;i<arr.length;i++){
            newArr[index+1]= arr[i];
        }
        return newArr;
    }

用尽可能少的步骤、变量,编写一个函数public static void reverse(int[] arr) {}

把数组分成2组,前后2组互换即可。 需要一个临时变量方便替换。

(是那天凑巧在算法4上看到的,不是我自发想到的。。。呃呃呃啊)

public static void reverse(int[] arr){
        int n = arr.length;
        for(int i =0;i<n/2;i++){
            int temp = arr[i];
            arr[i] = arr[n-1-i];
            arr[n-1-i]=temp;
        }

}

编写public static int[] replicate(int[] arr) {}

将arr替换成一个新数组,新数组的元素是原数组第i个元素重复arr[i]次后的结果。
比如[3, 2,1] 会变成 [3, 3, 3, 2, 2, 1]

(下面是我蒙的版本vsSol)


public static int[] replicate(int[] arr ){
        int sum = 0;
        for(int i =0;i<arr.length;i++){
            sum+=arr[i];
        }
        int[] newArr = new int[sum];

        int idx = 0;
        for(int i =0;i<arr.length;i++){
            int count=0;
            while(count<arr[i]){
                newArr[idx]=arr[i];
                count++;
                idx++;
            }
        }
        return newArr;
}

posted @ 2024-12-11 22:46  刘老六  阅读(13)  评论(0)    收藏  举报