(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53588062 冷血之心的博客)
题目1:
找到第一次出差错的版本
每个项目开发时,我们用代码版本控制工具,每次提交都会更新代码版本。版本号从1到n递增。有人提交代码后,导致后续的代码在单元测试中,都是错的。
请找到第一个错误的版本号。
代码如下:
package com.ywq.test1;
import org.junit.Test;
public class Solution1 implements VersionControl {
@Test
public void test(){
int result = findFirstBadVersion(9);
System.out.println("第一个错误版本为:"+result);
}
public int findFirstBadVersion(int n) {
int left = 1;
int right = n;
//建立循环,目标是找出好、坏的分界点。
while (left + 1 < right) {
int mid=(left+right)/2;
boolean flag = isBad(mid);
if (flag) {
right = mid;
} else {
left = mid;
}
}
// 当退出循环时,left和right相差1
if(isBad(left)){
return left;
}else {
return right;
}
}
@Override
public boolean isBad(int k) {
if (k >= 3) {
return true;
} else {
return false;
}
}
}
interface VersionControl {
public boolean isBad(int k);
}
题目2:
合并两个有序的链表。其中链表的节点的数据结构如下:
public class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
this.next = null;
}
}
代码如下:
package com.ywq.test2;
import org.junit.Test;
public class Solution2 {
@Test
public void test() {
//创建有序链表list1 0-2-5-7-9
ListNode list1 = new ListNode(0);
list1.next = new ListNode(2);
list1.next.next = new ListNode(5);
list1.next.next.next = new ListNode(7);
list1.next.next.next.next = new ListNode(9);
//创建有序链表list2 1-3-6-8
ListNode list2 = new ListNode(1);
list2.next = new ListNode(3);
list2.next.next = new ListNode(6);
list2.next.next.next = new ListNode(8);
//调用合并方法
ListNode list3 = mergeKLists(list1, list2);
//将结果输出
while (list3 != null) {
System.out.println(list3.val);
list3 = list3.next;
}
}
public ListNode mergeKLists(ListNode list1, ListNode list2) {
ListNode result=null;
if (list1 == null && list2 == null) {
return null;
}
if (list1 == null) {
result = list2;
return result;
}
if (list2 == null) {
result = list1;
return result;
}
if (list1.val > list2.val) {
result = list2;
list2 = list2.next;
} else {
result = list1;
list1 = list1.next;
}
result.next = mergeKLists(list1, list2);
return result;
}
}
class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
this.next = null;
}
}
题目3:
两数之和
给一个整数数组,找到两个数使得他们的和等于一个给定的数target。
你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。
代码如下:
package com.ywq.test3;
import java.util.HashMap;
import org.junit.Test;
public class Solution3 {
@Test
public void test() {
int[] a = { 2, 4, 8, 12, 5 };
int target = 17;
int[] sum = twoSum(a, target);
for (int i : sum) {
System.out.println(i);
}
}
public int[] twoSum(int[] a, int target) {
//创建结果数组
int[] result = new int[2];
HashMap<Integer, Integer> map = new HashMap<>();
//建立循环,遍历target-a[i]是否在map中
for (int i = 0; i < a.length; i++) {
if (map.containsKey(target - a[i])) {
//确定输出角标的先后顺序
if (i > map.get(target - a[i])) {
result[0] = map.get(target - a[i]);
result[1] = i;
} else {
result[0] = i;
result[1] = map.get(target - a[i]);
}
} else {
map.put(a[i], i);
}
}
return result;
}
}
题目4:
生产者消费者
写代码实现:
3个生产者生产消息,放到一个队列里。5个消费者消费消息。
生产者生产的消息格式:
生产者 ID + 时间戳 + 随机文本
消费者消费消息后,打印出消费者 ID 和消费的内容。
举例:
producer message:
1002_${timestamp}_helloworld42129
consumer:
consumer ID = 2003, consume message:1002_${timestamp}_helloworld42129
代码如下:
package com.ywq.test5;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Solution4 {
public static void main(String[] args) {
BlockingQueue q = new LinkedBlockingQueue();
Producer p1 = new Producer(q);
Producer p2 = new Producer(q);
Producer p3 = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
Consumer c3 = new Consumer(q);
Consumer c4 = new Consumer(q);
Consumer c5 = new Consumer(q);
p1.setName("生产者1");
p2.setName("生产者2");
p3.setName("生产者3");
c1.setName("消费者1");
c2.setName("消费者2");
c3.setName("消费者3");
c4.setName("消费者4");
c5.setName("消费者5");
p1.start();
p2.start();
p3.start();
c1.start();
c2.start();
c3.start();
c4.start();
c5.start();
}
}
class Producer extends Thread {
private final BlockingQueue queue;
Producer(BlockingQueue q) {
queue = q;
}
// 获取当前时间
public String getTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
String time = df.format(new Date());// new Date()为获取当前系统时间
return time;
}
public void run() {
while (true) {
try {
System.out.println("生产者生产的消息为:"+Thread.currentThread().getName() +" "+ getTime()+" "+Math.random());
queue.put(Thread.currentThread().getName() +" "+ getTime()+" "+Math.random());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer extends Thread {
private final BlockingQueue queue;
Consumer(BlockingQueue q) {
queue = q;
}
public void run() {
while (true) {
try {
System.out.println("consumer ID="+Thread.currentThread().getName() +", "+"consumer message:"+ queue.take());
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
如果对你有帮助,记得点赞哦~欢迎大家关注我的博客,可以加群366533258交流讨论哈~