Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<int[]>[] stop, int[] stopIndex, int cur) {
if (cur == stopIndex.length) {
int[][] p = new int[positions.length][2];
for (int i = 0; i < p.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
94 changes: 0 additions & 94 deletions src/test/java/com_github_leetcode/CommonUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com_github_leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CommonUtils {
Expand Down Expand Up @@ -54,40 +52,6 @@ public static boolean compareMatrix(List<List<Integer>> mt1, List<List<Integer>>
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
Expand Down Expand Up @@ -173,62 +137,4 @@ public static int[][] convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(S
}
return output;
}

public static List<List<String>> 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<List<String>> result = new ArrayList<>();
for (int i = 0; i < arrays.length; i++) {
List<String> 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<String> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void numTrees() {
assertThat(new Solution().numTrees(3), equalTo(5));
}

@Test
void numTrees2() {
assertThat(new Solution().numTrees(1), equalTo(1));
}
Expand Down