leetcode1108-1111、1114-1117

1108.IP地址无效化

public String defangIPaddr(String address) {
    StringBuffer res = new StringBuffer();
    for(int i = 0;i < address.length();i++){
        char c = address.charAt(i);
        if(c != '.'){
            res.append(c);
        }else {
            res.append('[');
            res.append(c);
            res.append(']');
        }
    }
    return res.toString();
}
  1. 航班预订统计
public int[] corpFlightBookings(int[][] bookings, int n) {
    int[] res = new int[n];
    for(int i = 0; i < bookings.length;i++){
        int start = bookings[i][0];
        int end = bookings[i][1];
        res[start - 1] += bookings[i][2];
        if(end < n)
            res[end] -= bookings[i][2];
    }
    for(int i = 1;i < res.length;i++){
        res[i] += res[i-1];
    }
    return res;
}
  1. 删点成林
public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
    List<TreeNode> res = new LinkedList<>();
    Set<Integer> delete = new HashSet<>();
    for(int node:to_delete){
        delete.add(node);
    }
    postdelete(root, res, delete);
    if(!delete.contains(root.val)){
        res.add(root);
    }
    return res;
}

public void postdelete(TreeNode root, List<TreeNode> res, Set<Integer> delete) {
    if(root == null)
        return;
    
    postdelete(root.left, res, delete);
    postdelete(root.right, res, delete);
    
    if(delete.contains(root.val)){
        if(root.left != null){
            if(!delete.contains(root.left.val)){
                res.add(root.left);
                root.left = null;
            }
        }
        if(root.right != null){
            if(!delete.contains(root.right.val)){
                res.add(root.right);
                root.right = null;
            }
        }
    } else {
        if(root.left != null){
            if(delete.contains(root.left.val)){
                root.left = null;
            }
        }
        if(root.right != null){
            if(delete.contains(root.right.val)){
                root.right = null;
            }
        }
    }
}
  1. 有效括号的嵌套深度
public int[] maxDepthAfterSplit(String seq) {
    int[] res = new int[seq.length()];
    int deep = 0;
    for(int i = 0;i < seq.length();i++){
        if(seq.charAt(i) == '('){
            deep++;
            if(deep % 2 == 0){
                res[i] = 1;
            }
        } else {
            deep--;
            if(deep % 2 == 1){
                res[i] = 1;
            }
        }
    }
    return res;
}
  1. 按序打印
class Foo {
private CountDownLatch countDownLatchA;
private CountDownLatch countDownLatchB;

public Foo() {
    countDownLatchA = new CountDownLatch(1);
    countDownLatchB = new CountDownLatch(1);
}

public void first(Runnable printFirst) throws InterruptedException {
    
    // printFirst.run() outputs "first". Do not change or remove this line.
    printFirst.run();
    countDownLatchA.countDown();
}

public void second(Runnable printSecond) throws InterruptedException {
    countDownLatchA.await();
    // printSecond.run() outputs "second". Do not change or remove this line.
    printSecond.run();
    countDownLatchB.countDown();
}

public void third(Runnable printThird) throws InterruptedException {
    countDownLatchB.await();
    // printThird.run() outputs "third". Do not change or remove this line.
    printThird.run();
}
}
  1. 交替打印FooBar
class FooBar {
private int n;
private final Semaphore semaphoreFoo;
private final Semaphore semaphoreBar;

public FooBar(int n) {
    this.n = n;
    semaphoreFoo = new Semaphore(0);
    semaphoreBar = new Semaphore(0);
}

public void foo(Runnable printFoo) throws InterruptedException {
    
    for (int i = 0; i < n; i++) {
        
    	// printFoo.run() outputs "foo". Do not change or remove this line.
    	printFoo.run();
        semaphoreBar.release();
        semaphoreFoo.acquire();
    }
}

public void bar(Runnable printBar) throws InterruptedException {
    
    for (int i = 0; i < n; i++) {

        semaphoreBar.acquire();
        // printBar.run() outputs "bar". Do not change or remove this line.
    	printBar.run();
        semaphoreFoo.release();
    }
}
}
  1. 打印零与奇偶数
class ZeroEvenOdd {
private int n;
private final Semaphore z;
private final Semaphore e;
private final Semaphore o;

public ZeroEvenOdd(int n) {
    this.n = n;
    z = new Semaphore(1);
    e = new Semaphore(0);
    o = new Semaphore(0);
}

// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
    for (int i = 0; i < n; i++) {
        z.acquire();
        printNumber.accept(0);
        if(i % 2 == 0){
            o.release();
        } else {
            e.release();
        }
    }
}

public void even(IntConsumer printNumber) throws InterruptedException {
    for (int i = 2; i <= n; i+=2) {
        e.acquire();
        printNumber.accept(i);
        z.release();
    }
}

public void odd(IntConsumer printNumber) throws InterruptedException {
    for (int i = 1; i <= n; i+=2) {
        o.acquire();
        printNumber.accept(i);
        z.release();
    }
}
}
  1. H2O 生成
class H2O {
private Semaphore h;
private Semaphore o;

public H2O() {
    h = new Semaphore(2);
    o = new Semaphore(0);
}

public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
    h.acquire(1);
    // releaseHydrogen.run() outputs "H". Do not change or remove this line.
    releaseHydrogen.run();
    o.release(1);
}

public void oxygen(Runnable releaseOxygen) throws InterruptedException {
    o.acquire(2);
    // releaseOxygen.run() outputs "H". Do not change or remove this line.
    releaseOxygen.run();
    h.release(2);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值