hw1 clab6

Java 中不允许直接创建泛型数组。可以使用 Class 对象作为类型标识

    public ArrayRingBuffer(Class<T> type, int capacity) {
        // TODO: Create new array with capacity elements.
        //       first, last, and fillCount should all be set to 0.
        first = 0;
        last = 0;
        fillCount = 0;
        rb = (T[]) Array.newInstance(type, capacity);
    }
这种方法返回arry时会出错: rb = (T[]) new Object[capacity];

throw 是语句抛出一个异常;throws 是方法抛出一个异常;
throw要么和try-catch-finally语句配套使用,要么与throws配套使用。但throws可以单独使用,然后再由处理异常的方法捕获。


环形数组 

    private int plusOne(int index) {
        if (index + 1 == capacity) {
            return 0;
        }
        return index + 1;
    }

HashSet中不能存放重复元素,可以用来做去重操作


Iterator是迭代器类,而Iterable是接口。  只能单向移动
实现Iterable接口后,对象就可以调用iterator()方法。 

遍历自定义的类用自定义迭代器
 

public class KeyIterator implements Iterator<K> {
private int ptr;

    public KeyIterator() {
        ptr = 0;
    }

    @Override
    public boolean hasNext() {
        return (ptr != size);
    }

    @Override
    public K next() {
        K returnItem = keys[ptr];
        ptr += 1;
        return returnItem;
    }
}

How to check an Object' s class:

if (obj instanceof C) {
//your code
}

对象怎样算相等
1.引用相等性  用==
2.对象相等性(两个对象)如果想把两个不同的对象视为相等,需要覆盖过从Object上继承下来的equal()和hashCode()方法

    @Override
    public boolean equals(Object o) {
       if (o instanceof ArrayRingBuffer )
       {
           ArrayRingBuffer<T> ring = (ArrayRingBuffer<T>) o;
           if (capacity == ring.capacity()) {
               while (capacity != 0) {
                   if(dequeue() != ring.dequeue()) {
                       return false;
                   }
               }
               return true;
           }
       }
       return false;
    }

感觉我写的太复杂,应该有简单的写法。


challenge lab 6

看懂题目就花了好久 Orthogonal means right next to in either direction, but not diagonally. 

逆向思路,将darts中的bubbles全部打掉后检查与天花板形成连通分量的bubbles,这些是不会掉落的,然后darts逆序恢复被掉落的bubbles,对darts[i]将它周围的所有bubbles,且这些bubbles并没有被之前恢复过的bubble恢复,此时bubbles是扔泥巴之前的泡泡数目。逆序 每次扔泥后掉落的数目=扔之前的bubbles数目-扔之后的bubbles数目;
利用的是uf.sizeOf(ceiling)的变化

二维并查集要降维,降维之后,因为还是四个方向的,所以要有方向数组

private int[][] dirs = new int[][]{{-1, 0}, {1,0}, {0, -1}, {0, 1}};

union的时候降维

private int ufIndex(int row, int col) {
    return row * colNum + col + 1;  // begin from 1
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值