import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author 1.實(shí)現(xiàn)兩個(gè)線程,使之交替打印1-100,
* 如:兩個(gè)線程分別為:Printer1和Printer2,
* 最后輸出結(jié)果為: Printer1 — 1 Printer2 一 2 Printer1 一 3 Printer2 一 4
* @date 2019-03-07
* @mondify
* @copyright
*/
public class Test {
private int number = 1;
private Lock lock = new ReentrantLock();
private Condition condition1 = lock.newCondition();
private Condition condition2 = lock.newCondition();
public void loopA() {
lock.lock();
try {
if (number % 2 == 0) {
condition1.await();
}
System.out.println(Thread.currentThread().getName() + "-" + number + " ");
number++;
condition2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void loopB() {
lock.lock();
try {
if (number % 2 != 0) {
condition2.await();
}
System.out.println(Thread.currentThread().getName() + "-" + number + " ");
number++;
condition1.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
Test ad = new Test();
new Thread(() -> {
for (int i = 1; i <= 50; i++) {
ad.loopA();
}
}, "Printer1").start();
new Thread(() -> {
for (int i = 1; i <= 50; i++) {
ad.loopB();
}
}, "Printer2").start();
}
}
package com.ksyun;
/**
* 2.實(shí)現(xiàn)函數(shù),給定一個(gè)字符串?dāng)?shù)組,求該數(shù)組的連續(xù)非空子集赎懦,分別打印出來各子集
* 舉例數(shù)組為[abc]澳泵,輸出[a],[b],[c],[ab],[bc],[abc]
*
* @author xubh
* @date 2019-03-07
* @mondify
* @copyright
*/
public class Test2 {
public static void main(String[] args) {
String s = "abc";
char[] in = s.toCharArray();
print(in, new StringBuilder(), 0);
System.out.println();
print2(s, new StringBuilder());
}
public static void print(char[] in, StringBuilder sb, int start) {
int len = in.length;
for (int i = start; i < len; i++) {
sb.append(in[i]);
System.out.print("[" + sb + "],");
if (i < len - 1) {
print(in, sb, i + 1);
}
sb.setLength(sb.length() - 1);
}
}
public static void print2(String str, StringBuilder sb) {
for (int i = 0; i < str.length(); i++) {
for (int j = i; j < str.length(); j++) {
sb.append("[").append(str, i, j + 1).append("]").append(",");
}
}
sb.setLength(sb.length() - 1);
System.out.println(sb);
}
}
package com.ksyun;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* 3.文件系統(tǒng)中按逗號(hào)分割保存了1億個(gè)正整數(shù)(一行10個(gè)數(shù)鸭巴,1000萬(wàn)行)终娃,找出其中最大的100個(gè)數(shù)
*
* @author xubh
* @date 2019-03-08
* @mondify
* @copyright
*/
public class Test3 {
/**
* 用PriorityQueue默認(rèn)是自然順序排序内边,要選擇最大的k個(gè)數(shù)挠他,構(gòu)造小頂堆蔬墩,
* 每次取數(shù)組中剩余數(shù)與堆頂?shù)脑剡M(jìn)行比較,如果新數(shù)比堆頂元素大佑淀,則刪除堆頂元素留美,并添加這個(gè)新數(shù)到堆中。
*/
public static void main(String[] args) {
System.out.println(getTopK("C:\\Users\\Administrator\\Desktop\\note\\TopKTest.txt", 6));
}
public static List<Integer> getTopK(String filePath, int k) {
List<Integer> list = new ArrayList<>();
Queue<Integer> queue = new PriorityQueue<>(k);
File file = new File(filePath);
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
String tmp;
while ((tmp = reader.readLine()) != null) {
String[] strings = tmp.split(",");
for (String str : strings) {
int num = Integer.parseInt(str);
if (queue.size() < k) {
queue.offer(num);
} else if (queue.peek() < num) {
queue.poll();
queue.offer(num);
}
}
}
while (k-- > 0) {
list.add(queue.poll());
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 啟動(dòng)3個(gè)線程打印遞增的數(shù)字, 線程1先打印1,2,3,4,5,
* 然后是線程2打印6,7,8,9,10, 然后是線程3打印11,12,13,14,15.
* 接著再由線程1打印16,17,18,19,20….以此類推, 直到打印到75
*
* @author xubh
* @date 2019-03-19
* @mondify
* @copyright
*/
public class Test4 {
private int no = 1;
private final Lock lock = new ReentrantLock();
private final Condition con1 = lock.newCondition();
private final Condition con2 = lock.newCondition();
private final Condition con3 = lock.newCondition();
private int curNum = 1;
private void printNum() {
if (curNum > 75) {
Thread.currentThread().interrupt();
return;
}
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " " + (curNum++));
}
}
public void process1() {
lock.lock();
try {
while (no != 1) {
con1.await();
}
printNum();
no = 2;
con2.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void process2() {
lock.lock();
try {
while (no != 2) {
con2.await();
}
printNum();
no = 3;
con3.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void process3() {
lock.lock();
try {
while (no != 3) {
con3.await();
}
printNum();
no = 1;
con1.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
Test4 p = new Test4();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
p.process1();
}
}, "A").start();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
p.process2();
}
}, "B").start();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
p.process3();
}
}, "C").start();
}
}
import java.util.concurrent.Semaphore;
/**
* 啟動(dòng)3個(gè)線程打印遞增的數(shù)字, 線程1先打印1,2,3,4,5,
* 然后是線程2打印6,7,8,9,10, 然后是線程3打印11,12,13,14,15.
* 接著再由線程1打印16,17,18,19,20….以此類推, 直到打印到75
*
* @author xubh
* @date 2019-03-19
* @mondify
* @copyright
*/
public class Test4 {
/**
* 以A開始的信號(hào)量,初始信號(hào)量數(shù)量為1
*/
private static Semaphore A = new Semaphore(1);
/**
* B伸刃、C信號(hào)量,A完成后開始,初始信號(hào)數(shù)量為0
*/
private static Semaphore B = new Semaphore(0);
private static Semaphore C = new Semaphore(0);
private int curNum = 1;
private void printNum() {
if (curNum > 75) {
Thread.currentThread().interrupt();
return;
}
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " " + (curNum++));
}
}
public void process1() {
try {
// A獲取信號(hào)執(zhí)行,A信號(hào)量減1,當(dāng)A為0時(shí)將無(wú)法繼續(xù)獲得該信號(hào)量
A.acquire();
printNum();
// B釋放信號(hào)谎砾,B信號(hào)量加1(初始為0),此時(shí)可以獲取B信號(hào)量
B.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void process2() {
try {
B.acquire();
printNum();
C.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void process3() {
try {
C.acquire();
printNum();
A.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test4 p = new Test4();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
p.process1();
}
}, "A").start();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
p.process2();
}
}, "B").start();
new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
p.process3();
}
}, "C").start();
}
}
import java.util.HashMap;
import java.util.Map;
/**
* 打印一個(gè)數(shù)組中和為K的連續(xù)子數(shù)組
*
* @author xubh
* @date 2019-03-19
* @mondify
* @copyright
*/
public class Test4 {
/**
* 用一個(gè)哈希表來建立連續(xù)子數(shù)組之和跟其出現(xiàn)次數(shù)之間的映射奕枝,初始化要加入{0,1}這對(duì)映射棺榔,
* 我們建立哈希表的目的是為了讓我們可以快速的查找sum-k是否存在,即是否有連續(xù)子數(shù)組的和為sum-k隘道,
* 如果存在的話症歇,那么和為k的子數(shù)組一定也存在,這樣當(dāng)sum剛好為k的時(shí)候谭梗,那么數(shù)組從起始到當(dāng)前位置的這段子數(shù)組的和就是k忘晤,
* 滿足題意,如果哈希表中事先沒有m[0]項(xiàng)的話激捏,這個(gè)符合題意的結(jié)果就無(wú)法累加到結(jié)果res中设塔,這就是初始化的用途。
*/
public static int subarraySum(int[] nums, int k) {
int sum = 0;
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (map.containsKey(sum - k)) {
res += map.get(sum - k);
}
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return res;
}
public static int subarraySum2(int[] nums, int k) {
int n = nums.length;
int res = 0;
int[] sum = new int[n + 1];
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + nums[i - 1];
}
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (sum[j + 1] - sum[i] == k) {
res++;
}
}
}
return res;
}
public static void main(String[] args) {
int[] nums = {1, 1, 3, 4, 1};
int k = 5;
int res = subarraySum2(nums, k);
System.out.println(res);
}
}