# 扬帆起航，再踏征程（三）

2017-01-13 15:03:13来源:csdn作者:qq_25827845人点击

（尊重劳动成果，转载请注明出处：http://blog.csdn.net/qq_25827845/article/details/53588062冷血之心的博客）

package com.ywq.test1;import org.junit.Test;
public class Solution1 implements VersionControl {@Test
public void test(){
System.out.println("第一个错误版本为："+result);
}
publicint 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
return left;
}else {
return right;
}}@Override
if (k >= 3) {
return true;
} else {
return false;
}}}interface VersionControl {
}题目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() {
//创建有序链表list10-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);//创建有序链表list21-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;}
}

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) {
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();
}
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) {
} catch (InterruptedException e) {
e.printStackTrace();
}
private final BlockingQueue queue;Consumer(BlockingQueue q) {
queue = q;
}public void run() {
while (true) {
try {
} catch (InterruptedException e) {
e.printStackTrace();
}
}}}