diff --git a/src/main/java/g2001_2100/s2056_number_of_valid_move_combinations_on_chessboard/Solution.java b/src/main/java/g2001_2100/s2056_number_of_valid_move_combinations_on_chessboard/Solution.java index 3adbfc6b0..1686f95a2 100644 --- a/src/main/java/g2001_2100/s2056_number_of_valid_move_combinations_on_chessboard/Solution.java +++ b/src/main/java/g2001_2100/s2056_number_of_valid_move_combinations_on_chessboard/Solution.java @@ -55,7 +55,7 @@ public int countCombinations(String[] pieces, int[][] positions) { return dfs(positions, endPosition, new int[pieces.length], 0); } - private int dfs(int[][] positions, ArrayList[] stop, int[] stopIndex, int cur) { + private int dfs(int[][] positions, ArrayList[] stop, int[] stopIndex, int cur) { if (cur == stopIndex.length) { int[][] p = new int[positions.length][2]; for (int i = 0; i < p.length; i++) { diff --git a/src/main/java/g2301_2400/s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/Solution.java b/src/main/java/g2301_2400/s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/Solution.java index 647d6f493..cffc70d89 100644 --- a/src/main/java/g2301_2400/s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/Solution.java +++ b/src/main/java/g2301_2400/s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/Solution.java @@ -15,7 +15,7 @@ public long countPairs(int n, int[][] edges) { long ans = 0; for (int i = 0; i < n; i++) { int p = d.findParent(i); - int cnt = map.containsKey(p) ? map.get(p) : 0; + int cnt = map.getOrDefault(p, 0); ans += i - cnt; map.put(p, map.getOrDefault(p, 0) + 1); } diff --git a/src/test/java/com_github_leetcode/CommonUtils.java b/src/test/java/com_github_leetcode/CommonUtils.java index 551c5f036..c5d595c37 100644 --- a/src/test/java/com_github_leetcode/CommonUtils.java +++ b/src/test/java/com_github_leetcode/CommonUtils.java @@ -1,7 +1,5 @@ package com_github_leetcode; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; public class CommonUtils { @@ -54,40 +52,6 @@ public static boolean compareMatrix(List> mt1, List> return true; } - public static char[][] convertLeetCodeRegular2DCharArrayInputIntoJavaArray(String input) { - /* - * LeetCode 2-d char array usually comes in like this: - * ["#"," ","#"],[" "," ","#"],["#","c"," "] which is wrapped in double quotes instead - * of single quotes which makes it not usable in Java code. - * This method helps with the conversion. - */ - String[] arrays = input.split("],\\["); - int m = arrays.length; - int n = arrays[1].split(",").length; - char[][] ans = new char[m][n]; - for (int i = 0; i < m; i++) { - if (i == 0) { - String str = arrays[i].substring(1); - String[] strs = str.split(","); - for (int j = 0; j < strs.length; j++) { - ans[i][j] = strs[j].charAt(1); - } - } else if (i == m - 1) { - String str = arrays[i].substring(0, arrays[i].length() - 1); - String[] strs = str.split(","); - for (int j = 0; j < strs.length; j++) { - ans[i][j] = strs[j].charAt(1); - } - } else { - String[] strs = arrays[i].split(","); - for (int j = 0; j < strs.length; j++) { - ans[i][j] = strs[j].charAt(1); - } - } - } - return ans; - } - public static int[][] convertLeetCodeRegularRectangleArrayInputIntoJavaArray(String input) { /* * LeetCode 2-d array input usually comes like this: it's a REGULAR rectangle @@ -173,62 +137,4 @@ public static int[][] convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(S } return output; } - - public static List> convertLeetCode2DStringArrayInputIntoJavaArray(String input) { - /* - * How to copy LeetCode 2-d String array into this method: - * 1. remove the beginning and ending quotes; - * 2. put double quotes into this method parameter; - * 3. copy the input into the double quotes. - * - * LeetCode 2-d array input usually comes like this: each row could have different length - * [["A","B"],["C"],["B","C"],["D"]] - * The expected input for this method is: "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]" - * just copy the LeetCode input: ["A","B"],["C"],["B","C"],["D"] into double quotes in Java, - * it'll auto escape the double quotes. - * i.e. strip off the beginning and ending square brackets, that's it. - * The output of this method will be a standard Java 2-d array. - * */ - String[] arrays = input.split("],\\["); - List> result = new ArrayList<>(); - for (int i = 0; i < arrays.length; i++) { - List level = new ArrayList<>(); - String[] strings; - if (i == 0) { - strings = arrays[i].substring(1).split(","); - } else if (i == arrays.length - 1) { - strings = arrays[i].substring(0, arrays[i].length() - 1).split(","); - } else { - strings = arrays[i].split(","); - } - Collections.addAll(level, strings); - result.add(level); - } - return result; - } - - public static List convertLeetCode1DStringArrayInputIntoJavaArray(String input) { - /* - * LeetCode 2-d array input usually comes like this: each row could have different length - * ["A","B","C"] - * The expected input for this method is: "[\"A\",\"B\",\"C\"]" - * just copy the LeetCode input: ["A","B","C"] into double quotes in Java, - * it'll auto escape the double quotes. - * The output of this method will be a standard Java 1-d array. - * */ - String[] arrays = input.split(","); - List result = new ArrayList<>(); - for (int i = 0; i < arrays.length; i++) { - String word; - if (i == 0) { - word = arrays[i].substring(1); - } else if (i == arrays.length - 1) { - word = arrays[i].substring(0, arrays[i].length() - 1); - } else { - word = arrays[i]; - } - result.add(word); - } - return result; - } } diff --git a/src/test/java/g0001_0100/s0096_unique_binary_search_trees/SolutionTest.java b/src/test/java/g0001_0100/s0096_unique_binary_search_trees/SolutionTest.java index cab57f587..69eeb3597 100644 --- a/src/test/java/g0001_0100/s0096_unique_binary_search_trees/SolutionTest.java +++ b/src/test/java/g0001_0100/s0096_unique_binary_search_trees/SolutionTest.java @@ -11,6 +11,7 @@ void numTrees() { assertThat(new Solution().numTrees(3), equalTo(5)); } + @Test void numTrees2() { assertThat(new Solution().numTrees(1), equalTo(1)); }