首页 > 其他分享 >CS61B srping 2018 examprep03 https://sp18.datastructur.es/

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

时间:2024-12-13 16:45:53浏览次数:4  
标签:srping sp18 int CS61B rest static IntList null public

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}

    }

标签:srping,sp18,int,CS61B,rest,static,IntList,null,public
From: https://www.cnblogs.com/nulixuexipython/p/18603541

相关文章

  • CS61B srping 2018 disc03 https://sp18.datastructur.es/
    为下面方法添加SLList.insert方法,要求签名如publicvoidinsert(intitem,intposition){},如果position大于最后一个元素,那就是添加到最后。(注意这个作业里的SLList和课程中介绍的SLList相比少点东西,故意的,可能是为了让学生开拓思路?)publicclassSLList{privateclassIn......
  • CS61B srping 2018 project00 https://sp18.datastructur.es/
    GettingtheSkeletonFiles,网站上应该有仓库地址,这个也行,https://gitee.com/heqilaoge/skeleton-sp18。拉下来找到proj0,就能开始作业。可以不使用IDE。2.ThePlanetClassandItsConstructor创建Planet类publicclassPlanet{publicdoublexxPos;publicdo......
  • CS61B srping 2018 examprep01(?02) https://sp18.datastructur.es/
    1.写出第21、24行的运行结果。(画出box-pointer指示图会对答题很有帮助)1publicclassShock{2publicstaticintbang;3publicstaticShockbaby;4publicShock(){5this.bang=100;6}7publicShock(intnum){8this.bang=num;9baby=starter();10this......
  • CS61B srping 2018 disc02 sol https://sp18.datastructur.es/
    第19行的变量level是静态方法change方法内部的本地变量,他对main方法里的level或者是实例变量level没什么影响。publicclassPokemon{//一个名为Pokemon的类publicStringname;//实例变量namepublicintlevel;//实例变量levelpublicPokemon(String......
  • CS61B srping 2018 disc02 https://sp18.datastructur.es/
    passbywhat?a.下列程序每一行怎么运行?b.画出box-and-pointer指示图c.在第19行,level被赋值为50,level是什么?是Pokemon的实例变量?(instancevariable)还是change方法的本地变量?(localvariable?)还是main方法里的变量?还是其他什么东西?1publicclassPokemon{2public......
  • CS61B srping 2018 disc01 answer
    1intsize=27;//声明一个int类型的变量size,并把27赋值给他2Stringname="Fido";//声明一个String类型的变量,并把“Fido”赋值给他3DogmyDog=newDog(name,size);//声明一个Dog类型的变量myDog,并调用Dog构造函数,分别把name和size传入其中,生成一个Dog类型的对......
  • CS61B srping 2018 disc01
    //1.写出每一行(你认为)的运行结果intsize=27;Stringname="Fido";DogmyDog=newDog(name,size);intx=size-5;if(x<15){myDog.bark(8);}while(x>3){x-=1;myDog.play();}int[]numList={2,4,6,8};System.out.print("Hell......
  • CS61B 二叉搜索树的改进:B 树 笔记
    CS61B二叉搜索树的改进:B树笔记笔记的来源:CS61B-2024春季的课程课程主要内容:数据结构与算法分析课程运用语言:Java你可以在我的笔记网站里获得更多有用的资源。这个课有6个Homework,10个Lab,9个Project。其中第一个project是一个完整的2024游戏的实现,很......
  • CS61B 渐进分析笔记 2
    CS61B渐进分析笔记2笔记的来源:CS61B-2024春季的课程课程主要内容:数据结构与算法分析课程运用语言:Java你可以在我的笔记网站里获得更多有用的资源。这个课有6个Homework,10个Lab,9个Project。其中第一个project是一个完整的2024游戏的实现,很有意思。此文......
  • CS61B不相交集笔记
    CS61B不相交集笔记笔记的来源:CS61B-2024春季的课程课程主要内容:数据结构与算法分析课程运用语言:Java你可以在我的笔记网站里获得更多有用的资源。这个课有6个Homework,10个Lab,9个Project。其中第一个project是一个完整的2024游戏的实现,很有意思。此文章......