1.最长连续子数组
/**
* 最长连续子数组的长度
* 定义一个计数器和最大长度
* 遍历的时候,当当前元素比下一个元素小,则count++,不然count重置为1,同时更新最大长度
*
* @param nums arr
* @return int
* @author huikf
* @since 2022/4/8 9:53
*/
private static int longestArr(int[] nums) {
if (nums.length == 1) {
return 1;
}
int count = 1;
int max = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] <= nums[i + 1]) {
count++;
} else {
count = 1;
}
max = Math.max(max, count);
}
return max;
}
2.数组中只出现一次的数字
/**
* 数组中只出现一次的数字
* 该数组中只有一个数字出现一次,其他均是出现2次
* 因此可以使用异或,相同为0,不同为1
* 将整个数组元素异或起来,相同的抵消掉。
*
* @param arr arr
* @return int
* @author huikf
* @since 2022/4/8 10:44
*/
private static int singleNum(int[] arr) {
int result = 0;
for (int i : arr) {
result = result ^ i;
}
return result;
}
3.交替打印
//双线程交替打印AB
public static void main(String[] args) {
printAB b=new printAB();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
b.printA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
b.printB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
class printAB {
static boolean flag = true;
synchronized void printA() throws InterruptedException {
while (!flag) {
this.wait();
}
System.out.print("A--");
flag = false;
this.notify();
}
synchronized void printB() throws InterruptedException {
while (flag) {
this.wait();
}
System.out.print("B--");
flag = true;
this.notify();
}
//lock 控制
static int position = 1;
public static void main(String[] args) {
for(int i=1;i<=3;i++){
int finalI = i;
new Thread(()->{
while (true){
print(finalI);
}
}).start();
}
}
private static void print(int i) {
Lock lock = new ReentrantLock();
lock.lock();
if (position == i) {
System.out.println(i + "--");
position = i % 3 + 1;
}
lock.unlock();
}
//多condition交替打印
class Print {
Lock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Condition condition3 = lock.newCondition();
int num = 1;
public void print1() {
try {
lock.lock();
if (num != 1) {
condition1.await();
}
System.out.println(Thread.currentThread().getName()+num);
num = 2;
condition2.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void print2() {
try {
lock.lock();
if (num != 2) {
condition2.await();
}
System.out.println(Thread.currentThread().getName()+num);
num = 3;
condition3.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void print3() {
try {
lock.lock();
if (num != 3) {
condition3.await();
}
System.out.println(Thread.currentThread().getName()+num);
num = 1;
condition1.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
4.数天上同时最多的飞机
/**
* Definition of Interval:
* public class Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
public class Solution {
static class Node{
int start;
int val;
public Node(int start,int val){
this.start=start;
this.val=val;
}
}
/**
* @param airplanes: An interval array
* @return: Count of airplanes are in the sky.
*/
public int countOfAirplanes(List<Interval> airplanes) {
List<Node> list=new ArrayList<>();
// write your code here
for(Interval i: airplanes){
list.add(new Node(i.start,1));
list.add(new Node(i.end,-1));
}
Comparator<Node> c= (Node i1,Node i2)->{
if(i1.start!=i2.start){
return i1.start-i2.start;
}else{
//降落优先
return i1.val-i2.val;
}
};
//排序后,尽可能多的飞机在天上
Collections.sort(list, c);
int ans=0;
int max=0;
for(Node node:list){
ans+=node.val;
max=Math.max(ans,max);
}
return max;
}
}
约瑟夫
约瑟夫问题是个著名的问题:N个人围成一圈,第一个人从1开始报数,报M的将被杀掉,下一个人接着从1开始报。如此反复,最后剩下一个,求最后的胜利者。
//n:N个人
//m:数到第M个人做掉
//下一次的坐标为所在坐标位置-3,
//所以前一次的坐标为下次坐标位置+3.
//反推,最后剩下的那个人数组坐标为0,即变量p
//那么他的上一次的坐标为(p+m)% i, i为上一次的人数,取模防止越界,越界后应到数组的位置。
//最后返回下标+1。
//f(1,3):只有1个人了,那个人就是获胜者,他的下标位置是0
int cir(int n,int m){
int p=0;
for(int i=2;i<=n;i++)
{
p=(p+m)%i;
}
return p+1;
}