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
118 changes: 59 additions & 59 deletions README.md

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/main/java/g0101_0200/s0126_word_ladder_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ public class Solution {
List<List<String>> ans,
Set<String> path) {
Set<String> next = graph.get(endWord);
if (next == null) return;
if (next == null) {
return;
}
for (String word : next) {
path.add(word);
if (beginWord.equals(word)) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/g0301_0400/s0313_super_ugly_number/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The <code>n<sup>th</sup></code> **super ugly number** is **guaranteed** to fit i
## Solution

```java
@SuppressWarnings("java:S3012")
public class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
long[] primes1 = new long[primes.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ public class Solution {
public int splitArray(int[] nums, int m) {
int maxVal = 0;
int minVal = nums[0];

for (int num : nums) {
maxVal += num;
minVal = Math.max(minVal, num);
}

while (minVal < maxVal) {
int midVal = minVal + (maxVal - minVal) / 2;
// if we can split, try to reduce the midVal so decrease maxVal
Expand All @@ -62,7 +60,6 @@ public class Solution {
minVal = midVal + 1;
}
}

return minVal;
}

Expand Down
34 changes: 18 additions & 16 deletions src/main/java/g0401_0500/s0437_path_sum_iii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ The path does not need to start or end at the root or a leaf, but it must go dow

```java
import com_github_leetcode.TreeNode;
import java.util.HashMap;
import java.util.Map;

/*
* Definition for a binary tree node.
Expand All @@ -54,25 +52,29 @@ import java.util.Map;
* }
*/
public class Solution {
private int count(TreeNode root, int targetSum, Map<Integer, Integer> hashMap, int currentSum) {
private int count = 0;

public int pathSum(TreeNode root, int targetSum) {
if (root == null) {
return 0;
}
int count = 0;
if (hashMap.containsKey(currentSum + root.val - targetSum)) {
count = count + hashMap.get(currentSum + root.val - targetSum);
}
hashMap.put(currentSum + root.val, hashMap.getOrDefault(currentSum + root.val, 0) + 1);
int l1 = count(root.left, targetSum, hashMap, currentSum + root.val);
int l2 = count(root.right, targetSum, hashMap, currentSum + root.val);
hashMap.put(currentSum + root.val, hashMap.getOrDefault(currentSum + root.val, 0) - 1);
return l1 + l2 + count;
helper(root, targetSum, 0);
pathSum(root.left, targetSum);
pathSum(root.right, targetSum);
return count;
}

public int pathSum(TreeNode root, int targetSum) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
return count(root, targetSum, map, 0);
public void helper(TreeNode node, int targetSum, long currSum) {
currSum += node.val;
if (targetSum == currSum) {
count++;
}
if (node.left != null) {
helper(node.left, targetSum, currSum);
}
if (node.right != null) {
helper(node.right, targetSum, currSum);
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class Solution {
public int minMoves(int[] nums) {
int min = nums[0];
int sum = nums[0];

// determining the total sum and smallest element of the input array
for (int i = 1; i <= nums.length - 1; i++) {
sum += nums[i];
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/g0401_0500/s0488_zuma_game/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ There are still balls remaining on the board, and you are out of balls to insert
## Solution

```java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Stack;

public class Solution {
private HashMap<String, Integer> map = new HashMap<>();

private String removeConsecutiveThreeOrMoreBalls(String board) {
Stack<Character> st = new Stack<>();
Deque<Character> st = new ArrayDeque<>();
int n = board.length();
for (int i = 0; i < n; i++) {
char ch = board.charAt(i);
Expand Down Expand Up @@ -115,7 +116,9 @@ public class Solution {
return map.get(key);
}
board = removeConsecutiveThreeOrMoreBalls(board);
int ans = 100, n = board.length(), m = hand.length();
int ans = 100;
int n = board.length();
int m = hand.length();
if (n == 0 || m == 0) {
map.put(key, n == 0 ? 0 : 100);
return n == 0 ? 0 : 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Solution {
prevLastCharIdx[s.charAt(0) - 'A'] = 0;
for (int i = 1; i < len; i++) {
char ch = s.charAt(i);
int chIdx = (int) (ch - 'A');
int chIdx = ch - 'A';
int lastSeenIdx = lastCharIdx[chIdx];
int prevLastIdx = prevLastCharIdx[chIdx];
dp[i] = dp[i - 1] + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ Return `true` if and only if you can choose `X >= 2` such that it is possible to

```java
import java.util.HashMap;
import java.util.Map;

public class Solution {
public boolean hasGroupsSizeX(int[] deck) {
HashMap<Integer, Integer> hmap = new HashMap<>();
for (int i = 0; i < deck.length; i++) {
if (hmap.containsKey(deck[i])) {
hmap.put(deck[i], hmap.get(deck[i]) + 1);
for (int j : deck) {
if (hmap.containsKey(j)) {
hmap.put(j, hmap.get(j) + 1);
} else {
hmap.put(deck[i], 1);
hmap.put(j, 1);
}
}
int x = hmap.get(deck[0]);
for (Integer i : hmap.keySet()) {
x = gcd(x, hmap.get(i));
for (Map.Entry<Integer, Integer> entry : hmap.entrySet()) {
x = gcd(x, entry.getValue());
}
return x >= 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class Solution {
}
nodes.add(root);
List<TreeNode> forests = new ArrayList<>();
while (nodes.size() > 0) {
while (!nodes.isEmpty()) {
TreeNode node = nodes.poll();
node = deleteAndSplit(node);
if (node != null) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/g1201_1300/s1260_shift_2d_grid/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,29 @@ Return the _2D grid_ after applying shift operation `k` times.

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

public class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
if (grid == null) {
return null;
return Collections.emptyList();
}
int[] flat = new int[grid.length * grid[0].length];
int index = 0;
for (int i = 0; i < grid.length; ++i) {
for (int[] ints : grid) {
for (int j = 0; j < grid[0].length; ++j) {
flat[index++] = grid[i][j];
flat[index++] = ints[j];
}
}
int mode = k % flat.length;
int readingIndex = flat.length - mode;
if (readingIndex == flat.length) {
readingIndex = 0;
}
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < grid.length; ++i) {
List<Integer> eachRow = new ArrayList<Integer>();
List<Integer> eachRow = new ArrayList<>();
for (int j = 0; j < grid[0].length; ++j) {
eachRow.add(flat[readingIndex++]);
if (readingIndex == flat.length) {
Expand Down
41 changes: 16 additions & 25 deletions src/main/java/g1401_1500/s1417_reformat_the_string/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,32 @@ Return _the reformatted string_ or return **an empty string** if it is impossibl
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("java:S5413")
public class Solution {
public String reformat(String s) {
List<Character> characterList = new ArrayList<>();
List<Character> numberList = new ArrayList<>();
List<Character> chars = new ArrayList<>();
List<Character> digits = new ArrayList<>();
for (char c : s.toCharArray()) {
if (Character.isAlphabetic(c)) {
characterList.add(c);
if (c >= '0' && c <= '9') {
digits.add(c);
} else {
numberList.add(c);
chars.add(c);
}
}
if (Math.abs(characterList.size() - numberList.size()) > 1) {
if (Math.abs(digits.size() - chars.size()) > 1) {
return "";
} else {
StringBuilder sb = new StringBuilder();
if (characterList.size() > numberList.size()) {
for (int i = 0; i < characterList.size() - 1; i++) {
sb.append(characterList.get(i));
sb.append(numberList.get(i));
}
sb.append(characterList.get(characterList.size() - 1));
} else if (characterList.size() == numberList.size()) {
for (int i = 0; i < numberList.size(); i++) {
sb.append(numberList.get(i));
sb.append(characterList.get(i));
}
}
boolean isDigit = digits.size() > chars.size();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (isDigit) {
sb.append(digits.remove(0));
} else {
for (int i = 0; i < numberList.size() - 1; i++) {
sb.append(numberList.get(i));
sb.append(characterList.get(i));
}
sb.append(numberList.get(numberList.size() - 1));
sb.append(chars.remove(0));
}
return sb.toString();
isDigit = !isDigit;
}
return sb.toString();
}
}
```