训练营6

This commit is contained in:
huangge1199@hotmail.com 2021-08-15 19:54:30 +08:00
parent 034f14fd41
commit cdc3f43599
5 changed files with 219 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package leet.book.contest.contest6;
public class Solution1 {
public static void main(String[] args) {
Solution1 solution = new Solution1();
}
public int solve(int num) {
int count = 0;
while (num >= 5) {
count += num / 5 * 2;
num = num % 5 + num / 5 * 2;
}
while (num >= 3) {
count += num / 3;
num = num % 3 + num / 3;
}
return count;
}
}

View File

@ -0,0 +1,23 @@
package leet.book.contest.contest6;
import com.code.leet.entiy.TreeNode;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Solution2 {
public static void main(String[] args) {
Solution2 solution = new Solution2();
}
public TreeNode swapTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode treeNode = root.left;
root.left = swapTree(root.right);
root.right = swapTree(treeNode);
return root;
}
}

View File

@ -0,0 +1,58 @@
package leet.book.contest.contest6;
import com.code.leet.entiy.TwoArray;
import javafx.util.Pair;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
public class Solution4 {
public static void main(String[] args) {
Solution4 solution = new Solution4();
TwoArray twoArray = new TwoArray("[[0,2,0,0,0],[2,2,2,0,0],[3,2,0,1,1],[3,3,3,1,0],[3,3,3,3,0]]", true);
System.out.println(solution.miniumDistance(twoArray.getArr()));
}
public int miniumDistance(int[][] grid) {
Map<Pair<Integer, Integer>, boolean[]> map = new HashMap<>();
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
int[] xindex = new int[]{-1, 1, 0, 0};
int[] yindex = new int[]{0, 0, -1, 1};
boolean[][] use = new boolean[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 0) {
queue.add(new Pair<>(i, j));
use[i][j] = true;
}
}
}
int count = 0;
while (!queue.isEmpty()) {
int size = queue.size();
count++;
for (int i = 0; i < size; i++) {
Pair<Integer, Integer> pair = queue.poll();
for (int j = 0; j < 4; j++) {
int x = pair.getKey() + xindex[j];
int y = pair.getValue() + yindex[j];
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || use[x][y]) {
continue;
}
Pair<Integer, Integer> add = new Pair<>(x, y);
boolean[] arr = map.getOrDefault(add, new boolean[3]);
arr[grid[x][y] - 1] = true;
if (arr[0] && arr[1] && arr[2]) {
return count;
}
map.put(add,arr);
use[x][y] = true;
queue.add(add);
}
}
}
return 0;
}
}

View File

@ -0,0 +1,55 @@
package leet.book.contest.contest6;
import com.code.leet.entiy.ListNode;
import com.code.leet.entiy.TreeNode;
import javafx.util.Pair;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution5 {
public static void main(String[] args) {
Solution5 solution = new Solution5();
ListNode head = new ListNode(Arrays.asList(14, 7, 13, 15, 7, 1, 5, 9, 8, 7));
solution.linkedListGame(head, 4);
}
public ListNode linkedListGame(ListNode head, int num) {
ListNode temp = head;
for (int i = 2; i <= num; i++) {
if (i % 2 == 1) {
temp.next = rev(temp.next, i);
for (int j = 0; j < i; j++) {
temp = temp.next;
}
} else {
temp.next = del(temp.next, i);
}
}
return head;
}
private ListNode del(ListNode head, int num) {
if (num == 0) {
return head;
}
return del(head.next, num - 1);
}
private ListNode rev(ListNode head, int num) {
ListNode tHead = null;
ListNode next = null;
ListNode temp = head;
for (int i = 0; i < num; i++) {
next = temp.next;
temp.next = tHead;
tHead = temp;
temp = next;
}
head.next = next;
return tHead;
}
}

View File

@ -0,0 +1,62 @@
package leet.book.contest.contest6;
import com.code.leet.entiy.TwoArray;
import java.util.*;
public class Solution6 {
public static void main(String[] args) {
Solution6 solution = new Solution6();
}
public boolean checkBracketSequence(String origin) {
int count = 0;
for (int i = 1; i < origin.length(); i++) {
char fch = origin.charAt(i - 1);
char sch = origin.charAt(i);
if (fch == '(') {
if (Character.isDigit(sch)) {
String str = "";
while (Character.isDigit(sch)){
str+=sch;
i++;
if(i==origin.length()){
break;
}
sch = origin.charAt(i);
}
count += Integer.parseInt(str);
i--;
} else {
count++;
}
} else if (fch == ')') {
if (Character.isDigit(sch)) {
String str = "";
while (Character.isDigit(sch)){
str+=sch;
i++;
if(i==origin.length()){
break;
}
sch = origin.charAt(i);
}
count -= Integer.parseInt(str);
i--;
} else {
count--;
}
if (count < 0) {
return false;
}
}
}
char ch = origin.charAt(origin.length() - 1);
if (ch == '(') {
count++;
} else if (ch == ')') {
count--;
}
return count == 0;
}
}