# Conflicts:
#	src/main/java/cn/whaifree/leetCode/LinkedList/LCR155.java
This commit is contained in:
whaifree 2024-09-16 00:00:32 +08:00
commit 1e4aa82952
4 changed files with 506 additions and 0 deletions

View File

@ -0,0 +1,197 @@
package cn.whaifree.interview;
import java.util.*;
public class TC1 {
}
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext hasNextLine 的区别
int i = in.nextInt();
int size = in.nextInt();
String next = in.next();
char[] charArray = next.toCharArray();
int[] nums = new int[i];
for (int i1 = 0; i1 < charArray.length; i1++) {
nums[i1] = charArray[i1] - '0';
}
method(nums, size);
}
public static void method(int[] nums, int k) {
int c = Integer.MAX_VALUE;
for (int i = 0; i <= nums.length - k; i++) {
int left = i;
int right = i + k - 1;
c = Math.min(c(nums, left, right, c), c);
}
System.out.println(c);
}
public static int c(int[] nums, int left, int right,int minNow) {
int[] newNums = Arrays.copyOfRange(nums, left, right + 1);
Arrays.sort(newNums);
left = 0;
right = newNums.length - 1;
int ans = 0 ;
while (left < right) {
ans += newNums[right] - newNums[left];
if (ans >= minNow) {
return minNow;
}
left++;
right--;
}
return ans;
}
}
class p2{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext hasNextLine 的区别
int points = in.nextInt();
int line = in.nextInt();
int s = in.nextInt();
int[][] map = new int[points][points];
for (int i = 0; i < map.length; i++) {
Arrays.fill(map[i], Integer.MIN_VALUE);
}
for (int i = 0; i < line; i++) {
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
map[a-1][b-1] = c;
map[b-1][a-1] = c;
}
for (int k = 0; k < points; k++) {
for (int i = 0; i < points; i++) {
for (int j = 0; j < points; j++) {
if (map[i][k] != Integer.MIN_VALUE && map[k][j] != Integer.MIN_VALUE) {
map[i][j] = Math.max(map[i][j], map[i][k] + map[k][j]);
}
}
}
}
System.out.println(map);
}
}
class zgyd1{
/**
* 代码中的类名方法名参数名已经指定请勿修改直接返回方法规定的值即可
*
*
* @param nums1 int整型ArrayList
* @param nums2 int整型ArrayList
* @return int整型ArrayList
*/
public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
HashSet<Integer> set1 = new HashSet<>(nums1);
HashSet<Integer> set2 = new HashSet<>(nums2);
set1.retainAll(set2);
return new ArrayList<>(set1);
}
}
class zgdx1{
public static void main(String[] args) {
int[][] ints = {
{1,0},
{2,1}
};
zgdx1 zgdx1 = new zgdx1();
System.out.println(Arrays.toString(zgdx1.findOrder(ints, 3)));
}
/**
* 代码中的类名方法名参数名已经指定请勿修改直接返回方法规定的值即可
* leetcode210
*
* @param prerequisites int整型二维数组
* @param n int整型
* @return int整型一维数组
*/
public int[] findOrder (int[][] prerequisites, int n) {
if (n==0) return new int[0];
int[] inDegress = new int[n];
for (int[] p : prerequisites) {
inDegress[p[0]]++;
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < inDegress.length; i++) {
if (inDegress[i] == 0) {
queue.offer(i);
}
}
int count = 0;
int[] res = new int[n];
while (!queue.isEmpty()) {
int cur = queue.poll();
res[count++] = cur;
for (int[] prerequisite : prerequisites) {
if (prerequisite[1] == cur) {
inDegress[prerequisite[0]]--;
if (inDegress[prerequisite[0]] == 0) {
queue.offer(prerequisite[0]);
}
}
}
}
if (count == n) {
return res;
}
return new int[0];
}
}
class t210{
// 方法 1 最简单的 BFS
public int[] findOrder(int numCourses, int[][] prerequisites) {
if (numCourses == 0) return new int[0];
int[] inDegrees = new int[numCourses];
// 建立入度表
for (int[] p : prerequisites) {
inDegrees[p[0]]++; // 记录每个节点的入度
}
// 入度为0的节点队列
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < inDegrees.length; i++) {
if (inDegrees[i] == 0) queue.offer(i); // 入度为 0 的节点可以进行执行
}
int count = 0; // 记录可以执行的任务数
int[] res = new int[numCourses]; // 完整拓扑排序的执行过程
// 根据提供的可以执行的任务入度为 0删除入度为 0 的节点
while (!queue.isEmpty()){
int curr = queue.poll(); // 拿到一个可以执行的任务
res[count++] = curr; // 这个任务可以执行作为下一次执行的节点
for (int[] p : prerequisites) {
if (p[1] == curr){ // {a,b} 表示 a 依赖 b b-->a
inDegrees[p[0]]--;
if (inDegrees[p[0]] == 0) queue.offer(p[0]);
}
}
}
if (count == numCourses) return res;
return new int[0];
}
}

View File

@ -0,0 +1,233 @@
package cn.whaifree.interview.qihu;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
public class p1 {
}
class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int num = cin.nextInt();
int can = cin.nextInt();
int[][] custom = new int[num][2];
for (int i = 0; i < num; i++) {
custom[i][0] = cin.nextInt();
custom[i][1] = cin.nextInt();
}
used = new boolean[can + 1];
getMaxSatisfied(custom, 0);
System.out.println(max);
}
static int now = 0;
static int max = Integer.MIN_VALUE;
static boolean[] used = null;
public static void getMaxSatisfied(int[][] custom, int start) {
if (start >= custom.length) {
max = Math.max(max, now);
}
for (int i = start; i < custom.length; i++) {
int[] ints = custom[i];
int a = ints[0];
int b = ints[1];
if (!used[a] && !used[b]) {
now++;
used[a] = true;
used[b] = true;
getMaxSatisfied(custom, i + 1);
used[a] = false;
used[b] = false;
now--;
}
}
}
}
class p323 {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for (int i = 0; i < n; i++) {
String next = cin.next();
System.out.println(cal(next) ? "Yes" : "No");
}
}
public static boolean cal(String s) {
for (int i = 0; i <= s.length(); i++) {
for (int j = 0; j <= 9; j++) {
StringBuilder stringBuilder = new StringBuilder(s);
stringBuilder.insert(i, j);
String[] split = stringBuilder.toString().split("=");
int z1 = calVal(split[0]);
int z2 = calVal(split[1]);
if (z1 == z2) {
return true;
}
}
}
return false;
}
/**
* 6
* 16=1+2*3
* 7*8*9=54
* 1+1=1+22
* 4*6=22+2
* 15+7=1+2
* 11+1=1+5
*/
public static int calVal(String s) {
Deque<Integer> deque = new LinkedList<>();
int res = 0;
char[] charArray = s.toCharArray();
char pre = ' ';
for (int i = 0; i < charArray.length; i++) {
StringBuilder str = new StringBuilder();
while (i < charArray.length && charArray[i] >= '0' && charArray[i] <= '9') {
str.append(charArray[i]);
i++;
}
int num = Integer.parseInt(str.toString());
if (pre == '+') {
deque.push(num);
} else if (pre == '-') {
deque.push(-num);
} else if (pre == '*') {
deque.push(deque.pop() * num);
} else {
deque.push(num);
}
if (i < charArray.length) {
pre = charArray[i];
}
}
while (!deque.isEmpty()) {
res += deque.pop();
}
return res;
}
}
class MeidiT1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext hasNextLine 的区别
int i = in.nextInt();
System.out.println(isAbsSS(i));
}
public static boolean isAbsSS(Integer integer) {
if (isSS(integer)) {
String s = String.valueOf(integer);
String string = new StringBuilder(s).reverse().toString();
return isSS(Integer.parseInt(string));
} else {
return false;
}
}
public static boolean isSS(Integer integer) {
if (integer < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(integer); i++) {
if (integer % i == 0) {
return false;
}
}
return true;
}
}
class MeidiT2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for (int i = 0; i < in.nextInt(); i++) {
int treeCount = in.nextInt();
int[] height = new int[treeCount];
int[] l = new int[treeCount];
int[] r = new int[treeCount];
for (int j = 0; j < treeCount; j++) {
l[i] = in.nextInt();
r[i] = in.nextInt();
height[i] = in.nextInt();
}
}
}
public static void method(int[] l, int[] r, int[] height) {
int opr = 0;
int n = height.length;
for (int i = 0; i < n; i++) {
if (height[i] > r[i]) {
int excess = height[i] - r[i];
opr++;
while (i < n && height[i] > r[i]) {
height[i] -= excess;
i++;
}
i--; // for
}
}
}
}
class MeidiT3 {
static HashMap<Long, Long> map = new HashMap<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext hasNextLine 的区别
int i = in.nextInt();
for (int i1 = 0; i1 < i; i1++) {
method(in.nextLong());
}
}
public static void method(long num) {
long cnt = countOne(num);
long target = 2 * cnt;
long x = 0;
while (countOne(x | num) != target) {
x++;
}
System.out.println(x | num);
}
public static long countOne(long num) {
long count = 0;
while (num != 0) {
num &= (num - 1);
count++;
}
return count;
}
}

View File

@ -2,6 +2,7 @@ package cn.whaifree.interview.random;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.*;
public class StackForQueue {
@ -64,6 +65,39 @@ public class StackForQueue {
class MyStack {
public static void main(String[] args) {
// 创建一个固定大小的线程池使用 SynchronousQueue 作为任务队列
ExecutorService executor = new ThreadPoolExecutor(
2, // 核心线程数
2, // 最大线程数
0L, TimeUnit.MILLISECONDS, // 线程空闲时间
new SynchronousQueue<Runnable>() // 使用 SynchronousQueue
);
ExecutorService executorService = Executors.newCachedThreadPool();
ExecutorService executorService1 = Executors.newFixedThreadPool(1);
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
ExecutorService executorService2 = Executors.newSingleThreadExecutor();
// 提交10个任务到线程池
for (int i = 0; i < 10; i++) {
final int taskNumber = i;
executor.submit(() -> {
System.out.println("Task " + taskNumber + " is running on thread " + Thread.currentThread().getName());
try {
// 模拟任务执行时间
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Task " + taskNumber + " is completed");
});
}
// 关闭线程池
executor.shutdown();
}
Queue<Integer> queue1 = null;
Queue<Integer> queue2 = null;
public MyStack() {

View File

@ -0,0 +1,42 @@
package cn.whaifree.test;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class FanxinTest {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
1,
2,
3
,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>()
);
}
static class A{
}
interface B{
}
interface C{
}
/**
* T 继承自 A实现接口 C B
* @param item
* @param <T>
*/
static <T extends A & C & B> void process(List<? extends Integer> item) {
}
}