Skip to content

Commit

Permalink
java
Browse files Browse the repository at this point in the history
  • Loading branch information
czqmike committed Mar 16, 2020
1 parent 9725865 commit 456353f
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
74 changes: 74 additions & 0 deletions 1114. 按序打印.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
我们提供了一个类:
public class Foo {
  public void one() { print("one"); }
  public void two() { print("two"); }
  public void three() { print("three"); }
}
三个不同的线程将会共用一个 Foo 实例。
线程 A 将会调用 one() 方法
线程 B 将会调用 two() 方法
线程 C 将会调用 three() 方法
请设计修改程序,以确保 two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行。
 
示例 1:
输入: [1,2,3]
输出: "onetwothree"
解释:
有三个线程会被异步启动。
输入 [1,2,3] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 two() 方法,线程 C 将会调用 three() 方法。
正确的输出是 "onetwothree"。
示例 2:
输入: [1,3,2]
输出: "onetwothree"
解释:
输入 [1,3,2] 表示线程 A 将会调用 one() 方法,线程 B 将会调用 three() 方法,线程 C 将会调用 two() 方法。
正确的输出是 "onetwothree"。
 
注意:
尽管输入中的数字似乎暗示了顺序,但是我们并不保证线程在操作系统中的调度顺序。
你看到的输入格式主要是为了确保测试的全面性。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/print-in-order
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
import java.util.concurrent.atomic.AtomicInteger;

class Foo {
private AtomicInteger firstJobDone = new AtomicInteger(0);
private AtomicInteger secondJobDone = new AtomicInteger(0);

public Foo() {
}

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

firstJobDone.getAndIncrement();
}

public void second(Runnable printSecond) throws InterruptedException {
while (firstJobDone.get() < 1) {}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();

secondJobDone.getAndIncrement();
}

public void third(Runnable printThird) throws InterruptedException {
while (secondJobDone.get() < 1) {}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
47 changes: 47 additions & 0 deletions 面试题 01.06. 字符串压缩.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变为a2b1c5a3。若“压缩”后的字符串没有变短,则返回原先的字符串。你可以假设字符串中只包含大小写英文字母(a至z)。
示例1:
输入:"aabcccccaaa"
输出:"a2b1c5a3"
示例2:
输入:"abbccd"
输出:"abbccd"
解释:"abbccd"压缩后为"a1b2c2d1",比原字符串长度更长。
提示:
字符串长度在[0, 50000]范围内。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/compress-string-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/

class Solution {
public String compressString(String S) {
if (S.length() == 0) return S;
StringBuffer ret = new StringBuffer();

char bef = S.charAt(0);
int cnt = 1;
char ch = ' ';
for (int i = 1; i < S.length(); ++i) {
ch = S.charAt(i);
if (ch == bef) {
cnt ++;
} else {
ret.append(bef);
ret.append(cnt);
cnt = 1;
bef = ch;
}
}
ret.append(bef);
ret.append(cnt);

String retS = ret.toString();
return retS.length() < S.length() ? retS : S;
}
}

0 comments on commit 456353f

Please sign in to comment.