Posts
Chapter 4 Data Processing 4.2 Implicit Sequences Sequence 可以在使用时才分配内存, 比如下面的 range(), 只有在使用时才分配内存, 而不是在定义时分配内存. >>> r = range(10000, 1000000000) >>> r[45006230] 45016230 4.2.1 Iterators 迭代器. >>> primes = [2, 3, 5, 7] >>> type(primes) <class 'list'> >>> iterator = iter(primes) >>> type(iterator) <class 'list_iterator'> >>> next(iterator) 2 >>> next(iterator) 3 >>> next(iterator) 5 当 next()到序列的最后一个元素之后, 会抛出 StopIteration 异常. 可以通过 try 来 catch 这个异常. >>> next(iterator) 7 >>> next(iterator) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> try: next(iterator) except StopIteration: print('No more values') No more values 每次调用 next(), 迭代器都会维护一个内部状态, 这个状态会记录当前迭代器的位置....
disc3 Q3 hw03 count_dollars Q5.6 hw04 berry_finder 和 max_path_sum hw04 和 lab04 的解析
Inheritance II: Extends, Casting, Higher Order Functions 10.1 Implementation Inheritance: Extends extend 语法: public class RotatingSLList<Blorp> extends SLList<Blorp>{ public void rotateRight() { Blorp oldBack = removeLast(); addFirst(oldBack); } } Constructor Behavior extend 的子类需要在构造函数中调用父类的构造函数,通过super(). 如果省略了 java 会帮忙自动调用。 public VengefulSLList() { deletedItems = new SLList<Item>(); } public VengefulSLList() { super(); deletedItems = new SLList<Item>(); } 如果子类构造函数需要调用父类带参数的构造函数,那么这时候必须主动调用super(...), 否则 java 只会自动调用不带参数的super(). public VengefulSLList(Item x) { super(x); deletedItems = new SLList<Item>(); } public VengefulSLList(Item x) { deletedItems = new SLList<Item>(); } 10....
Inheritance III: Subtype Polymorphism, Comparators, Comparable 11.1 A Review of Dynamic Method Selection Static Type vs. Dynamic Type static type: 变量声明时指定的类型。 dynamic type: 变量运行时的类型,根据 new 关键字。 11.2 ComparablesSubtype Polymorphism vs Explicit Higher Order Functions Explicit Higher Order Functions: def print_larger(x, y, compare, stringify): if compare(x, y): return stringify(x) return stringify(y) Subtype Polymorphism: def print_larger(x, y): if x.largerThan(y): return x.str() return y.str() 11.3 Comparables java 有一个 comparable interface: public interface Comparable<T> { /* Return negative number if o1 < o2....
12. Inheritance IV: Iterators, Object Methods 12.1 Lists and Sets in Java list java 提供了 built-in 的 List interface 和一些实现,比如 ArrayList. import java.util.List; import java.util.ArrayList; public class Example { public static void main(String[] args) { List<Integer> L = new ArrayList<>(); L.add(5); L.add(10); System.out.println(L); } } set java 提供了 built-in 的 Set interface 和一些实现,比如 HashSet. import java.util.Set; import java.util.HashSet; Set<String> s = new HashSet<>(); s.add("Tokyo"); s.add("Lagos"); System.out.println(s.contains("Tokyo")); // true 12.2 Exceptions java 抛出异常语法:...
Asymptotics 15.1 For Loops int N = A.length; for (int i = 0; i < N; i += 1) for (int j = i + 1; j < N; j += 1) if (A[i] == A[j]) return true; return false; 有两种方法来渐进分析: Methood 1: 计算运算符的数量 比如看==的执行次数,第一次循环为$N-1$次,第二次为$N-2$次,…,最后一次为$1$次,所以总共为$1+2+3+…+N-1$,即$N(N-1)/2$, 运行时间为 $\theta(N^2)$。 Methood 2: 几何分析 可以看到==的执行次数和三角形的两边 $N-1$ 都有关系,所以面积 in $N^2$ family, 可以得到运行时间为 $\theta(N^2)$ 再看一个例子: public static void printParty(int N) { for (int i = 1; i <= N; i = i * 2) { for (int j = 0; j < i; j += 1) { System....
ADTs and BSTs 16.1 Abstract Data Types 常见的 ADTs 有: Stacks push(int x) pop() Lists add(int i) get(int i) Sets add(int i) contains(int i) Maps put(K key, V value) V get(K key) 16.2 Binary Search Trees 二叉搜索树 node 的左边子节点都比 node 小,右边子节点都比 node 大。 private class BST<Key> { private Key key; private BST left; private BST right; public BST(Key key, BST left, BST Right) { this.key = key; this.left = left; this....
B-Tree BST Performance Binary Search Tree 根据插入的数据不同,最好的情况树的高度为 $O(logN)$, 最坏的情况为 $O(N)$。 17.3 B-Tree Operations Avoiding Imbalance 采用的一种方法是“过度填充”叶节点。我们不需要在插入时添加新节点,而是简单地将新值堆叠到适当位置的现有叶节点中。 Moving Items Up 为了改善过度填充叶节点的问题,我们可以考虑在叶节点达到一定数量的值时“上移”一个值。 然而,这遇到了一个问题,我们的二分搜索属性不再被保留——16 在 17 的右边。这时候我们需要将过度填充节点的子节点划分: 在这个结构上的搜索操作将与 BST 完全相同,除了值在 15 到 17 之间,我们去中间的子节点,而不是左边或右边。 如果我们设置一个常数𝐿作为节点大小的限制,那么我们的搜索时间只会增加一个常数因子。 添加到节点可能会导致级联链式反应,如下图所示,我们添加了 25 和 26。 当我们的根超过限制,我们需要增加树的高度: 这种数据结构的名称为 B-Tree, 如果每个节点最多有 3 个 items, 那么我们称之为 2-3-4 trees 或者 2-4 trees(一个 node 可以有 2/3/4 个子节点)。如果每个节点最多有 2 个 items,那么称之为 2-3 trees。 17.4 B-Tree Invariants 所有叶子节点到根节点的距离都是一样的。 具有 k 项的非叶节点必须恰好有 k + 1 个子节点。 17.5 B-Tree Performance 根据每个节点最多包含 $L$ 个 items, B-Tree 的最大高度在 $log_{L+1}N$ 到 $log_2N$ 之间。...
Inheritance I: Interface and Implementation Inheritance 9.2 Hypernyms, Hyponyms, and the Implements Keyword interface 类: public interface List61B<Item> { public void addFirst(Item x); public void add Last(Item y); public Item getFirst(); public Item getLast(); public Item removeLast(); public Item get(int i); public void insert(Item x, int position); public int size(); } 实现类: public class AList<Item> implements List61B<Item>{...} 9.3 Overriding, Interface Inheritance overriding 实现类实现 interface 类的时候要加上@Override关键字。 @Override public void addFirst(Item x) { insert(x, 0); } 9....