From e0d8ed2fba8bc3f8ac65b6c040a589c1744476f7 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Sun, 1 Aug 2021 17:02:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=AD=E7=BB=83=E8=90=A5=E5=91=A8=E8=B5=9B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../code/leet/contest/contest4/Solution1.java | 50 +++++++++++ .../code/leet/contest/contest4/Solution2.java | 83 +++++++++++++++++++ .../code/leet/contest/contest4/Solution3.java | 44 ++++++++++ .../code/leet/contest/contest4/Solution4.java | 58 +++++++++++++ .../code/leet/contest/contest4/Solution5.java | 40 +++++++++ .../code/leet/contest/contest4/Solution6.java | 68 +++++++++++++++ 6 files changed, 343 insertions(+) create mode 100644 src/main/java/com/code/leet/contest/contest4/Solution1.java create mode 100644 src/main/java/com/code/leet/contest/contest4/Solution2.java create mode 100644 src/main/java/com/code/leet/contest/contest4/Solution3.java create mode 100644 src/main/java/com/code/leet/contest/contest4/Solution4.java create mode 100644 src/main/java/com/code/leet/contest/contest4/Solution5.java create mode 100644 src/main/java/com/code/leet/contest/contest4/Solution6.java diff --git a/src/main/java/com/code/leet/contest/contest4/Solution1.java b/src/main/java/com/code/leet/contest/contest4/Solution1.java new file mode 100644 index 0000000..2574935 --- /dev/null +++ b/src/main/java/com/code/leet/contest/contest4/Solution1.java @@ -0,0 +1,50 @@ +package com.code.leet.contest.contest4; + +import javafx.util.Pair; + +import java.util.*; + +public class Solution1 { + public static void main(String[] args) { + Solution1 solution = new Solution1(); + } + + public int countIslands(int[][] grid) { + boolean[][] use = new boolean[grid.length][grid[0].length]; + int count = 0; + Queue> queue = new LinkedList<>(); + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if(!use[i][j]){ + continue; + } + use[i][j] = true; + if (grid[i][j] == 1) { + queue.add(new Pair<>(i, j)); + use[i][j] = true; + count++; + while (!queue.isEmpty()) { + Pair pair = queue.poll(); + for (int k = -1; k < 2; k++) { + for (int l = -1; l < 2; l++) { + if (k == 0 && l == 0) { + continue; + } + int x = k + pair.getKey(); + int y = l + pair.getValue(); + if (x < 0 || x >= grid.length || y < 0 || y > grid[0].length || use[x][y]) { + continue; + } + use[x][y] = true; + if (grid[x][y] == 1) { + queue.add(new Pair<>(x,y)); + } + } + } + } + } + } + } + return count; + } +} diff --git a/src/main/java/com/code/leet/contest/contest4/Solution2.java b/src/main/java/com/code/leet/contest/contest4/Solution2.java new file mode 100644 index 0000000..b6e03b3 --- /dev/null +++ b/src/main/java/com/code/leet/contest/contest4/Solution2.java @@ -0,0 +1,83 @@ +package com.code.leet.contest.contest4; + +import java.util.Arrays; + +public class Solution2 { + public static void main(String[] args) { + Solution2 solution = new Solution2(); + System.out.println(solution.countPlans(new int[]{31,18,-11,13},20)); + } + +// public int countPlans(int[] nums, int target) { +// Arrays.sort(nums); +// if (nums[0] > target) { +// return 0; +// } +// if (nums[0] >= 0 || nums[nums.length - 1] <= 0) { +// return count(0, nums.length, target, nums); +// } +// int c0 = 0; +// int index = 0; +// for (int i = 0; i < nums.length; i++) { +// if (nums[i] == 0) { +// c0++; +// } else if (nums[i] > 0) { +// index = i; +// break; +// } +// } +// int count = 0; +// for (int i = 0; i < index - c0; i++) { +// count += countPlans(Arrays.copyOfRange(nums, i + 1, nums.length), target - nums[i]); +// } +// +// count += count(index, nums.length, target, nums); +// return count; +// } +// +// private int count(int start, int end, int target, int[] nums) { +// Queue queue = new LinkedList<>(); +// queue.add(nums[start]); +// int sum = nums[start]; +// int count = 0; +// for (int i = start + 1; i < end; i++) { +// sum += nums[i]; +// queue.add(nums[i]); +// while (!queue.isEmpty() && sum > target) { +// sum -= queue.poll(); +// } +// if (sum == target) { +// count++; +// } +// if (queue.isEmpty()) { +// break; +// } +// } +// return count; +// } + + int count = 0; + public int countPlans(int[] nums, int target) { + Arrays.sort(nums); + count(nums, target, 0, 0); + return count; + } + + private void count(int[] nums, int target, int sum, int index) { + if (sum == target) { + count++; + return; + } + if (sum > target) { + return; + } + if (index == nums.length) { + return; + } + int count = 0; + for (int i = index; i < nums.length; i++) { + count(nums, target, sum + nums[i], index + 1); + count(nums, target, sum, index + 1); + } + } +} diff --git a/src/main/java/com/code/leet/contest/contest4/Solution3.java b/src/main/java/com/code/leet/contest/contest4/Solution3.java new file mode 100644 index 0000000..bd5bee7 --- /dev/null +++ b/src/main/java/com/code/leet/contest/contest4/Solution3.java @@ -0,0 +1,44 @@ +package com.code.leet.contest.contest4; + +import com.code.leet.entiy.TreeNode; + +import java.util.*; + +public class Solution3 { + public static void main(String[] args) { + Solution3 solution = new Solution3(); + } + + public int[][] solve(TreeNode root) { + if (root == null) { + return null; + } + Queue queue = new LinkedList<>(); + queue.add(root); + List> list = new ArrayList<>(); + while (!queue.isEmpty()) { + int size = queue.size(); + List temp = new ArrayList<>(); + for (int i = 0; i < size; i++) { + TreeNode node = queue.poll(); + temp.add(node.val); + if (node.right != null) { + queue.add(node.right); + } + if (node.left != null) { + queue.add(node.left); + } + } + list.add(temp); + } + int[][] arr = new int[list.size()][]; + for (int i = 0; i < list.size(); i++) { + List temp = list.get(i); + arr[i] = new int[temp.size()]; + for (int j = 0; j < temp.size(); j++) { + arr[i][j] = temp.get(j); + } + } + return arr; + } +} diff --git a/src/main/java/com/code/leet/contest/contest4/Solution4.java b/src/main/java/com/code/leet/contest/contest4/Solution4.java new file mode 100644 index 0000000..ecfd401 --- /dev/null +++ b/src/main/java/com/code/leet/contest/contest4/Solution4.java @@ -0,0 +1,58 @@ +package com.code.leet.contest.contest4; + +import javafx.util.Pair; + +import java.util.*; + +public class Solution4 { + public static void main(String[] args) { + Solution4 solution = new Solution4(); + } + + public int getDistance(String[] maze) { + int[] xIndex = new int[]{-1, 1, 0, 0}; + int[] yIndex = new int[]{0, 0, -1, 1}; + char[][] chs = new char[maze.length][maze[0].length()]; + for (int i = 0; i < maze.length; i++) { + chs[i] = maze[i].toCharArray(); + } + boolean[][] use = new boolean[maze.length][maze[0].length()]; + Pair start = null; + for (int i = 0; i < chs.length; i++) { + for (int j = 0; j < chs[0].length; j++) { + if (chs[i][j] == 'X') { + use[i][j] = true; + } + if (chs[i][j] == 'S') { + use[i][j] = true; + start = new Pair<>(i, j); + } + } + } + Queue> queue = new LinkedList<>(); + queue.add(start); + int count = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + count++; + for (int i = 0; i < size; i++) { + Pair 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 >= chs.length || y < 0 || y >= chs[0].length || use[x][y]) { + continue; + } + if (chs[x][y] == '.') { + queue.add(new Pair<>(x, y)); + use[x][y] = true; + } + if(chs[x][y]=='E'){ + return count; + } + } + } + } + return -1; + } +} diff --git a/src/main/java/com/code/leet/contest/contest4/Solution5.java b/src/main/java/com/code/leet/contest/contest4/Solution5.java new file mode 100644 index 0000000..f7a9289 --- /dev/null +++ b/src/main/java/com/code/leet/contest/contest4/Solution5.java @@ -0,0 +1,40 @@ +package com.code.leet.contest.contest4; + +import javafx.util.Pair; + +import java.util.HashMap; +import java.util.Map; + +public class Solution5 { + public static void main(String[] args) { + Solution5 solution = new Solution5(); + solution.shiftMatrix(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{3,1,9},{4,5,2},{7,8,6}}); + } + + public int shiftMatrix(int[][] source, int[][] target) { + Map[]> map = new HashMap<>(); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (source[i][j] != target[i][j]) { + Pair pair = new Pair<>(i,j); + Pair[] temp = map.getOrDefault(source[i][j], new Pair[2]); + temp[0] = pair; + map.put(source[i][j],temp); + temp = map.getOrDefault(target[i][j], new Pair[2]); + temp[1] = pair; + map.put(target[i][j],temp); + } + } + } + int max = 0; + for (int key : map.keySet()) { + Pair[] pairs = map.get(key); + int x = pairs[1].getKey() - pairs[0].getKey(); + int y = pairs[1].getValue() - pairs[0].getValue(); + x = x >= 0 ? x : x + 3; + y = y >= 0 ? y : y + 3; + max = Math.max(x + y, max); + } + return max; + } +} diff --git a/src/main/java/com/code/leet/contest/contest4/Solution6.java b/src/main/java/com/code/leet/contest/contest4/Solution6.java new file mode 100644 index 0000000..dbd29db --- /dev/null +++ b/src/main/java/com/code/leet/contest/contest4/Solution6.java @@ -0,0 +1,68 @@ +package com.code.leet.contest.contest4; + +import javafx.util.Pair; + +import java.util.*; + +public class Solution6 { + public static void main(String[] args) { + Solution6 solution = new Solution6(); + } + + public int getDistance(int[][] grid) { + int[] xI = new int[]{-1, 1, 0, 0}; + int[] yIndex = new int[]{0, 0, -1, 1}; + Queue> load = new LinkedList<>(); + boolean[][] use = new boolean[grid.length][grid[0].length]; + boolean bl = false; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == 1 && !use[i][j]) { + bl = true; + Queue> queue = new LinkedList<>(); + queue.add(new Pair<>(i, j)); + while (!queue.isEmpty()) { + Pair pair = queue.poll(); + load.add(new Pair<>(pair.getKey(), pair.getValue())); + for (int k = 0; k < 4; k++) { + int x = pair.getKey() + xI[k]; + int y = pair.getValue() + yIndex[k]; + if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || use[x][y]) { + continue; + } + if (grid[x][y] == 1) { + queue.add(new Pair<>(x, y)); + use[x][y] = true; + } + } + } + break; + } + } + if (bl) { + break; + } + } + int count = 0; + while (!load.isEmpty()) { + int size = load.size(); + count++; + for (int i = 0; i < size; i++) { + Pair pair = load.poll(); + for (int k = 0; k < 4; k++) { + int x = pair.getKey() + xI[k]; + int y = pair.getValue() + yIndex[k]; + if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || use[x][y]) { + continue; + } + if (grid[x][y] == 1) { + return count; + } + load.add(new Pair<>(x, y)); + use[x][y] = true; + } + } + } + return 0; + } +}