训练营周赛1

This commit is contained in:
huangge1199@hotmail.com 2021-07-11 22:23:40 +08:00
parent ae2f1c7055
commit f9d7b13972
6 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.code.leet.contest1;
import java.util.*;
public class Solution1 {
public static void main(String[] args) {
Solution1 solution = new Solution1();
System.out.println(solution.solve(new int[][]{{0, 2}, {0, 2}}, new int[]{1, 4, 5, 8, 2}, 3));
}
public int solve(int[][] types, int[] nums, int kth) {
if (types.length == 1) {
if (types[0][0] == 1) {
return nums[kth];
} else {
return nums[types[0][1] - 1 + kth - 1];
}
} else {
if (types[0][0] == 1) {
if (types[1][0] == 1) {
return nums[kth - 1];
} else {
return nums[types[1][1] - 1 + kth - 1];
}
} else {
if (types[1][0] == 1) {
return nums[types[0][1] - 1 + kth - 1];
} else {
return nums[types[0][1] - 1 + types[1][1] - 1 + kth - 1];
}
}
}
}
}

View File

@ -0,0 +1,33 @@
package com.code.leet.contest1;
import java.util.*;
public class Solution2 {
public static void main(String[] args) {
Solution2 solution = new Solution2();
System.out.println(solution.solve(new int[]{1,1,1,1,1,1}));
}
public int[] solve(int[] nums) {
int size = nums.length;
int[] arr = new int[size];
Arrays.fill(arr, -1);
Map<Integer, Queue<Integer>> map = new HashMap<>();
for (int i = 0; i < size; i++) {
Queue<Integer> queue = map.getOrDefault(nums[i],new LinkedList<>());
if(queue.size()==5){
Queue<Integer> temp = new LinkedList<>();
arr[i]=0;
while (!queue.isEmpty()){
arr[i]+= queue.peek();
temp.add(queue.poll());
}
queue = temp;
queue.poll();
}
queue.add(i);
map.put(nums[i],queue);
}
return arr;
}
}

View File

@ -0,0 +1,36 @@
package com.code.leet.contest1;
import com.code.leet.entiy.ListNode;
public class Solution3 {
public static void main(String[] args) {
Solution3 solution = new Solution3();
System.out.println();
}
public ListNode solve(ListNode head) {
if (head == null) {
return null;
}
int num1 = 1;
int num2 = 2;
ListNode result = new ListNode(head.val);
int index = 3;
ListNode temp = result;
ListNode temp1 = head.next;
temp.next = new ListNode(temp1.val);
temp1 = temp1.next;
temp = temp.next;
while (temp1 != null) {
if (index == num1 + num2) {
temp.next = new ListNode(temp1.val);
temp = temp.next;
num1 = num2;
num2 = index;
}
index++;
temp1 = temp1.next;
}
return result;
}
}

View File

@ -0,0 +1,32 @@
package com.code.leet.contest1;
import java.util.Arrays;
public class Solution4 {
public static void main(String[] args) {
Solution4 solution = new Solution4();
System.out.println(solution.solve("kyClMl"));
}
public String solve(String s) {
char[] chs = s.toCharArray();
int start = -1;
for (int i = 0; i < chs.length; i++) {
if (chs[i] > 'Z' && start != -1) {
char ch = chs[i];
if (i - start >= 0) System.arraycopy(chs, start, chs, start + 1, i - start);
chs[start] = ch;
start++;
} else if (chs[i] <= 'Z') {
if (start == -1) {
start = i;
}
}
}
StringBuilder sBuilder = new StringBuilder();
for (char ch : chs) {
sBuilder.append(ch);
}
return sBuilder.toString();
}
}

View File

@ -0,0 +1,31 @@
package com.code.leet.contest1;
public class Solution5 {
public static void main(String[] args) {
Solution5 solution = new Solution5();
System.out.println(solution.solve("abccaba"));
}
public int solve(String tmp) {
// return dfs(tmp, "abcba");
return 0;
}
private int dfs(String str, String target) {
if (str.equals(target)) {
return 1;
}
if (target.equals("")) {
return 0;
}
int index = str.indexOf(target.charAt(0));
int count = 0;
while (index >= 0) {
str = str.substring(index + 1);
count += dfs(str, target.substring(1));
count %= 1000000007;
index = str.indexOf(target.charAt(0));
}
return count;
}
}

View File

@ -0,0 +1,84 @@
package com.code.leet.contest1;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Solution6 {
public static void main(String[] args) {
Solution6 solution = new Solution6();
int[] nums;
int[] nums1;
for (int i = 2; i < 100; i++) {
nums = new int[i];
for (int j = 0; j < nums.length; j++) {
nums[j] = j;
}
nums1 = Arrays.copyOf(nums, nums.length);
int index = 1;
while (true) {
nums1 = solution.change(nums1);
if (solution.bl(nums, nums1)) {
System.out.println(i + ":" + index);
break;
}
index++;
}
i++;
}
}
private boolean bl(int[] nums1, int[] nums2) {
for (int i = 0; i < nums1.length; i++) {
if (nums1[i] != nums2[i]) {
return false;
}
}
return true;
}
public int solve(int[] nums, int m, int kth) {
int[] nums1 = Arrays.copyOf(nums, nums.length);
int index = -1;
for (int i = 0; i < m; i++) {
nums = change(nums);
if (bl(nums1, nums)) {
index = i + 1;
break;
}
}
if (index > 0) {
for (int i = 0; i < m % index; i++) {
nums = change(nums);
if (bl(nums1, nums)) {
index = i + 1;
break;
}
}
}
return nums[kth - 1];
}
private int[] change(int[] nums) {
Stack<Integer> stack = new Stack<>();
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < nums.length; i++) {
if (i % 2 == 0) {
stack.push(nums[i]);
} else {
queue.add(nums[i]);
}
}
int index = 0;
while (!queue.isEmpty()) {
nums[index] = queue.poll();
index++;
}
while (!stack.isEmpty()) {
nums[index] = stack.pop();
index++;
}
return nums;
}
}