祭奠迅雷JAVA笔试和UC笔试

=三个线程顺序执行


package lee.hao.review;


import java.io.IOException;


public class Test3 {


	public static  void main(String arg[]) throws IOException {
		final Test obj = new Test();


		new Thread() {
			public void run() {
				obj.m1();
			}
		}.start();
		new Thread() {
			public void run() {
				obj.m2();
			}
		}.start();
		new Thread() {
			public void run() {
				obj.m3();
			}
		}.start();


	}


}


class Test {
	static int count;
	volatile int target = 1;


	synchronized void m1() {
		for (int i = 0; i < 10; i++) {
			while (target == 2 || target == 3) {
				try {
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("m1() =" + i);
			target = 2;
			notifyAll();
		}
	}


	synchronized void m2() {
		for (int i = 0; i < 10; i++) {
			while (target == 1 || target == 3) {
				try {
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("m2() =" + i);
			target = 3;
			notifyAll();
		}
	}


	synchronized void m3() {
		for (int i = 0; i < 10; i++) {
			while (target == 1 || target == 2) {
				try {
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("m3() =" + i);
			target = 1;
			notifyAll();
		}
	}
}

迅雷笔试最后一道题,三个线程顺序执行。。。哎



数组交叉移动问题


题目:输入数组:{a1,a2,…,an,b1,b2,…,bn}, 在O(n)的时间,O(1)的空间将这个数组的顺序变为{a1,b1,a2,b2,a3,b3,…,an,bn}, 且不需要移动,通过交换完成,只需一个交换空间。

解答:从结果入手,结果数组的中垂线两边分别a数组的一半和b数组的一半的混合,继续将子数组以中垂线划分下去,可以看到类似的规律,因此,可以使用类似的分治算法实现。

http://s.sousb.com/2011/10/07/%E8%B0%B7%E6%AD%8C%E9%9D%A2%E8%AF%95%E9%A2%98%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E7%A7%BB%E5%8A%A8%E4%BA%A4%E5%8F%89/



posted @ 2012-10-16 15:53  Leeforall  阅读(301)  评论(0编辑  收藏  举报