概述
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();
}
- 航班预订统计
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;
}
- 删点成林
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;
}
}
}
}
- 有效括号的嵌套深度
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;
}
- 按序打印
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();
}
}
- 交替打印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();
}
}
}
- 打印零与奇偶数
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();
}
}
}
- 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);
}
}
最后
以上就是发嗲星月为你收集整理的leetcode1108-1111、1114-1117的全部内容,希望文章能够帮你解决leetcode1108-1111、1114-1117所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复