提交 6f8f145f 编写于 作者: C chenguangjian.jk

test pass

上级 b76757e1
class Solution {
public boolean robot(String command, int[][] barriers, int x, int y) {
System.out.println();
System.out.println(command);
System.out.println(Arrays.deepToString(barriers));
System.out.println(x);
System.out.println(y);
import java.util.Scanner;
import java.util.*;
class Main {
public static void main(String[] args) {
// 控制台输入
Scanner scanner = new Scanner(System.in);
String command = scanner.nextLine();
String barriersStr = scanner.nextLine();
int[][] barriers = new int[][]{};
// 解析字符串,构造二维数组
barriersStr = barriersStr.replaceAll(" ","");
if(!"[]".equals(barriersStr)){
String[] strArr = barriersStr.replaceAll("\\[\\[", "").replaceAll("\\]\\]", "").split("\\],\\[");
barriers = new int[strArr.length][];
for (int i = 0; i < strArr.length; i++) {
String[] innerArr = strArr[i].split(",");
barriers[i] = new int[innerArr.length];
for (int j = 0; j < innerArr.length; j++) {
barriers[i][j] = Integer.parseInt(innerArr[j].trim());
}
}
}
int x = scanner.nextInt();
int y = scanner.nextInt();
scanner.close();
// 调用方法
boolean result = robot(command, barriers, x, y);
// 输出结果
System.out.println(result);
}
public static boolean robot(String command, int[][] barriers, int x, int y) {
int dx = 0, dy = 0;
char[] cmd = command.toCharArray(); // 把String转为数组,方便遍历。
......@@ -36,7 +76,7 @@ class Solution {
}
// 判断是否经过该点,经过返回走的次数,没经过返回-1。
public int isPassed(char[] cmd, int x, int y, int dx, int dy) {
public static int isPassed(char[] cmd, int x, int y, int dx, int dy) {
int round = Math.min(x / dx, y / dy); // 计算走到第x-1或y-1层需要多少轮
int cnt = cmd.length * round; // 前几轮的总次数
dx *= round;
......@@ -59,4 +99,6 @@ class Solution {
return -1;
}
}
}
\ No newline at end of file
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int ax1 = Integer.parseInt(scan.nextLine().trim());
int ay1 = Integer.parseInt(scan.nextLine().trim());
int ax2 = Integer.parseInt(scan.nextLine().trim());
int ay2 = Integer.parseInt(scan.nextLine().trim());
int bx1 = Integer.parseInt(scan.nextLine().trim());
int by1 = Integer.parseInt(scan.nextLine().trim());
int bx2 = Integer.parseInt(scan.nextLine().trim());
int by2 = Integer.parseInt(scan.nextLine().trim());
scan.close();
int result = solution(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2);
System.out.println(result);
}
public static int solution(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1), area2 = (bx2 - bx1) * (by2 - by1);
int overlapWidth = Math.min(ax2, bx2) - Math.max(ax1, bx1), overlapHeight = Math.min(ay2, by2) - Math.max(ay1, by1);
int overlapArea = Math.max(overlapWidth, 0) * Math.max(overlapHeight, 0);
return area1 + area2 - overlapArea;
}
}
\ No newline at end of file
}
class Solution {
public int[][] matrixRankTransform(int[][] matrix) {
import java.util.Scanner;
import java.util.*;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
// 控制台输入
Scanner scanner = new Scanner(System.in);
String matrixStr = scanner.nextLine();
int[][] matrix = new int[][]{};
// 解析字符串,构造二维数组
matrixStr = matrixStr.replaceAll(" ","");
if(!"[]".equals(matrixStr)){
String[] strArr = matrixStr.replaceAll("\\[\\[", "").replaceAll("\\]\\]", "").split("\\],\\[");
matrix = new int[strArr.length][];
for (int i = 0; i < strArr.length; i++) {
String[] innerArr = strArr[i].split(",");
matrix[i] = new int[innerArr.length];
for (int j = 0; j < innerArr.length; j++) {
matrix[i][j] = Integer.parseInt(innerArr[j].trim());
}
}
}
scanner.close();
// 调用方法
int[][] res = matrixRankTransform(matrix);
// 输出结果
System.out.println(Arrays.deepToString(res));
}
public static int[][] matrixRankTransform(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
UnionFind uf = new UnionFind(m, n);
for (int i = 0; i < m; i++) {
......@@ -119,8 +158,10 @@ class Solution {
}
return res;
}
}
}
class UnionFind {
int m, n;
int[][][] root;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册