Skip to content

Commit 451a615

Browse files
authored
Added solutions and tests for problems 41 - 51.
1 parent b904be2 commit 451a615

File tree

22 files changed

+579
-0
lines changed

22 files changed

+579
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package s0041_first_missing_positive;
2+
3+
public class Solution {
4+
public int firstMissingPositive(int[] nums) {
5+
for (int i = 0; i < nums.length; i++) {
6+
if (nums[i] <= 0 || nums[i] > nums.length || nums[i] == i + 1) continue;
7+
8+
dfs(nums, nums[i]);
9+
}
10+
11+
for (int i = 0; i < nums.length; i++) {
12+
if (nums[i] != i + 1) return i + 1;
13+
}
14+
15+
return nums.length + 1;
16+
}
17+
18+
private void dfs(int[] nums, int val) {
19+
if (val <= 0 || val > nums.length || val == nums[val - 1]) return;
20+
21+
int temp = nums[val - 1];
22+
nums[val - 1] = val;
23+
dfs(nums, temp);
24+
}
25+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package s0042_trapping_rain_water;
2+
3+
public class Solution {
4+
public int trap(int[] height) {
5+
if (height == null || height.length <= 2) return 0;
6+
int l = 0;
7+
int r = height.length - 1;
8+
int maxL = height[l];
9+
int maxR = height[r];
10+
int res = 0;
11+
while (l < r) {
12+
if (maxL < maxR) {
13+
maxL = Math.max(height[++l], maxL);
14+
res += maxL - height[l];
15+
} else {
16+
maxR = Math.max(height[--r], maxR);
17+
res += maxR - height[r];
18+
}
19+
}
20+
return res;
21+
}
22+
}

Diff for: src/main/java/s0043_multiple_strings/Solution.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package s0043_multiple_strings;
2+
3+
public class Solution {
4+
private int[] getIntArray(String s) {
5+
char[] chars = s.toCharArray();
6+
int[] arr = new int[chars.length];
7+
for (int i = 0; i < chars.length; i++) {
8+
arr[i] = chars[i] - '0';
9+
}
10+
11+
return arr;
12+
}
13+
14+
private String convertToStr(int[] res, int i) {
15+
char[] chars = new char[res.length - i];
16+
int k = 0;
17+
18+
for (; i < res.length; i++) {
19+
chars[k] = (char) (res[i] + '0');
20+
k++;
21+
}
22+
23+
return new String(chars);
24+
}
25+
26+
public String multiply(String num1, String num2) {
27+
int[] arr1 = getIntArray(num1);
28+
int[] arr2 = getIntArray(num2);
29+
int[] res = new int[arr1.length + arr2.length];
30+
int index = arr1.length + arr2.length - 1;
31+
32+
for (int i = arr2.length - 1; i >= 0; i--) {
33+
int k = index--;
34+
for (int j = arr1.length - 1; j >= 0; j--) {
35+
res[k] += arr2[i] * arr1[j];
36+
k--;
37+
}
38+
}
39+
index = arr1.length + arr2.length - 1;
40+
int carry = 0;
41+
42+
for (int i = index; i >= 0; i--) {
43+
int temp = res[i] + carry;
44+
res[i] = temp % 10;
45+
carry = temp / 10;
46+
}
47+
48+
int i = 0;
49+
while (i < res.length && res[i] == 0) {
50+
i++;
51+
}
52+
53+
if (i > index) {
54+
return "0";
55+
} else {
56+
return convertToStr(res, i);
57+
}
58+
}
59+
}

Diff for: src/main/java/s0044_wildcard_matching/Solution.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package s0044_wildcard_matching;
2+
3+
public class Solution {
4+
public boolean isMatch(String inputString, String pattern) {
5+
int i = 0;
6+
int j = 0;
7+
int starIdx = -1;
8+
int lastMatch = -1;
9+
10+
while (i < inputString.length()) {
11+
if (j < pattern.length()
12+
&& (inputString.charAt(i) == pattern.charAt(j) || pattern.charAt(j) == '?')) {
13+
i++;
14+
j++;
15+
} else if (j < pattern.length() && pattern.charAt(j) == '*') {
16+
starIdx = j;
17+
lastMatch = i;
18+
j++;
19+
} else if (starIdx != -1) {
20+
// there is a no match and there was a previous star, we will reset the j to indx
21+
// after star_index
22+
// lastMatch will tell from which index we start comparing the string if we
23+
// encounter * in pattern
24+
j = starIdx + 1;
25+
lastMatch++; // we are saying we included more characters in * so we incremented the
26+
// index
27+
i = lastMatch;
28+
29+
} else {
30+
return false;
31+
}
32+
}
33+
boolean isMatch = true;
34+
while (j < pattern.length() && pattern.charAt(j) == '*') j++;
35+
36+
if (i != inputString.length() || j != pattern.length()) isMatch = false;
37+
38+
return isMatch;
39+
}
40+
}

Diff for: src/main/java/s0045_jump_game_ii/Solution.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package s0045_jump_game_ii;
2+
3+
public class Solution {
4+
public int jump(int[] nums) {
5+
int length = 0;
6+
int maxLength = 0;
7+
int minJump = 0;
8+
9+
for (int i = 0; i < nums.length - 1; ++i) {
10+
length--;
11+
maxLength--;
12+
13+
maxLength = Math.max(maxLength, nums[i]);
14+
15+
if (length <= 0) {
16+
length = maxLength;
17+
minJump++;
18+
}
19+
20+
if (length >= nums.length - i - 1) return minJump;
21+
}
22+
23+
return minJump;
24+
}
25+
}

Diff for: src/main/java/s0046_permutations/Solution.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package s0046_permutations;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Solution {
7+
public List<List<Integer>> permute(int[] nums) {
8+
9+
if (nums == null || nums.length == 0) {
10+
return new ArrayList<>();
11+
}
12+
13+
List<List<Integer>> finalResult = new ArrayList<>();
14+
permuteRecur(nums, finalResult, new ArrayList<>(), new boolean[nums.length]);
15+
return finalResult;
16+
}
17+
18+
private void permuteRecur(
19+
int[] nums, List<List<Integer>> finalResult, List<Integer> currResult, boolean[] used) {
20+
21+
if (currResult.size() == nums.length) {
22+
finalResult.add(new ArrayList<>(currResult));
23+
return;
24+
}
25+
26+
for (int i = 0; i < nums.length; i++) {
27+
if (used[i]) {
28+
continue;
29+
}
30+
currResult.add(nums[i]);
31+
used[i] = true;
32+
permuteRecur(nums, finalResult, currResult, used);
33+
used[i] = false;
34+
currResult.remove(currResult.size() - 1);
35+
}
36+
}
37+
}

Diff for: src/main/java/s0047_permutations_ii/Solution.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package s0047_permutations_ii;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Solution {
7+
private List<List<Integer>> ans;
8+
9+
public List<List<Integer>> permuteUnique(int[] nums) {
10+
ans = new ArrayList<>();
11+
permute(nums, 0);
12+
return ans;
13+
}
14+
15+
private void permute(int[] nums, int p) {
16+
if (p >= nums.length - 1) {
17+
List<Integer> t = new ArrayList<>(nums.length);
18+
for (int n : nums) {
19+
t.add(n);
20+
}
21+
ans.add(t);
22+
return;
23+
}
24+
permute(nums, p + 1);
25+
boolean[] used = new boolean[30];
26+
for (int i = p + 1; i < nums.length; i++) {
27+
if (nums[i] != nums[p] && !used[10 + nums[i]]) {
28+
used[10 + nums[i]] = true;
29+
swap(nums, p, i);
30+
permute(nums, p + 1);
31+
swap(nums, p, i);
32+
}
33+
}
34+
}
35+
36+
private void swap(int[] nums, int i, int j) {
37+
int t = nums[i];
38+
nums[i] = nums[j];
39+
nums[j] = t;
40+
}
41+
}

Diff for: src/main/java/s0048_rotate_image/Solution.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package s0048_rotate_image;
2+
3+
public class Solution {
4+
public void rotate(int[][] matrix) {
5+
int n = matrix.length;
6+
for (int i = 0; i < n / 2; i++) {
7+
for (int j = i; j < n - i - 1; j++) {
8+
int[][] pos =
9+
new int[][] {
10+
{i, j}, {j, n - 1 - i}, {n - 1 - i, n - 1 - j}, {n - 1 - j, i}
11+
};
12+
int t = matrix[pos[0][0]][pos[0][1]];
13+
for (int k = 1; k < pos.length; k++) {
14+
int temp = matrix[pos[k][0]][pos[k][1]];
15+
matrix[pos[k][0]][pos[k][1]] = t;
16+
t = temp;
17+
}
18+
matrix[pos[0][0]][pos[0][1]] = t;
19+
}
20+
}
21+
}
22+
}

Diff for: src/main/java/s0049_group_anagrams/Solution.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package s0049_group_anagrams;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class Solution {
10+
public List<List<String>> groupAnagrams(String[] strs) {
11+
Map<String, List<String>> hm = new HashMap<>();
12+
13+
for (String s : strs) {
14+
char[] ch = s.toCharArray();
15+
16+
Arrays.sort(ch);
17+
String temp = new String(ch);
18+
19+
hm.computeIfAbsent(temp, k -> new ArrayList<>());
20+
hm.get(temp).add(s);
21+
}
22+
23+
return (new ArrayList<>(hm.values()));
24+
}
25+
}

Diff for: src/main/java/s0050_powx_n/Solution.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package s0050_powx_n;
2+
3+
public class Solution {
4+
public double myPow(double x, int n) {
5+
long nn = n;
6+
double res = 1;
7+
8+
if (n < 0) {
9+
nn *= -1;
10+
}
11+
12+
while (nn > 0) {
13+
if (nn % 2 == 1) {
14+
nn--;
15+
res *= x;
16+
} else {
17+
x *= x;
18+
nn /= 2;
19+
}
20+
}
21+
22+
if (n < 0) {
23+
return (1.0 / res);
24+
}
25+
26+
return res;
27+
}
28+
}

Diff for: src/main/java/s0051_n_queens/Solution.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package s0051_n_queens;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Solution {
8+
public List<List<String>> solveNQueens(int n) {
9+
boolean[] pos = new boolean[n + 2 * n - 1 + 2 * n - 1];
10+
int[] pos2 = new int[n];
11+
List<List<String>> ans = new ArrayList<>();
12+
helper(n, 0, pos, pos2, ans);
13+
return ans;
14+
}
15+
16+
private void helper(int n, int row, boolean[] pos, int[] pos2, List<List<String>> ans) {
17+
if (row == n) {
18+
construct(n, pos2, ans);
19+
return;
20+
}
21+
for (int i = 0; i < n; i++) {
22+
if (pos[i] || pos[n + i + row] || pos[n + 2 * n - 1 + n - 1 + i - row]) continue;
23+
pos[i] = true;
24+
pos[n + i + row] = true;
25+
pos[n + 2 * n - 1 + n - 1 + i - row] = true;
26+
pos2[row] = i;
27+
helper(n, row + 1, pos, pos2, ans);
28+
pos[i] = false;
29+
pos[n + i + row] = false;
30+
pos[n + 2 * n - 1 + n - 1 + i - row] = false;
31+
}
32+
}
33+
34+
private void construct(int n, int[] pos, List<List<String>> ans) {
35+
List<String> sol = new ArrayList<>();
36+
for (int r = 0; r < n; r++) {
37+
char[] queenRow = new char[n];
38+
Arrays.fill(queenRow, '.');
39+
queenRow[pos[r]] = 'Q';
40+
sol.add(new String(queenRow));
41+
}
42+
ans.add(sol);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package s0041_first_missing_positive;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void firstMissingPositive() {
11+
assertThat(new Solution().firstMissingPositive(new int[] {1, 2, 0}), equalTo(3));
12+
}
13+
}

0 commit comments

Comments
 (0)