最近在找工作,接触到一些笔试题目,虽然网上也有会各种实现,但还是觉得自己写出来能理解的更深刻一点。
当时三道题只有一个小时的时间。平时这部分内容练的不多,只完成了一道,剩下两道题都是后面做的,一共花了2个半小时。。。。。
1.实现两个线程,使之交替打印1-100;
public class First {
static class NumThread implements Runnable {
private int i = 1;
@Override
public void run() {
while (i <= 100) {
synchronized (this) {
notify();
System.out.println(Thread.currentThread().getName() + ":\t" + i++);
try {
if (i <= 100) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("线程结束:" + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
NumThread numThread = new NumThread();
Thread thread1 = new Thread(numThread);
Thread thread2 = new Thread(numThread);
thread1.start();
thread2.start();
}
}
2.实现函数,给定一个字符串数组,求该数组的连续非空子集
思想是按游标分隔成左右两段,左段整体和右段每个数据进行连接
public class ArrayCollected {
public static void main(String[] args) {
List list = Arrays.asList("a", "b", "c", "d", "e");
collected(0, 1, list);