Skip to content

Commit e18f35b

Browse files
committed
Some additions
1 parent 043eff2 commit e18f35b

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed

src/com/leetcode/Combinations.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.leetcode;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/*
9+
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
10+
Example:
11+
Input: n = 4, k = 2
12+
Output:
13+
[
14+
[2,4],
15+
[3,4],
16+
[2,3],
17+
[1,2],
18+
[1,3],
19+
[1,4],
20+
]
21+
*/
22+
public class Combinations {
23+
public static List<List<Integer>> combine(int n, int k) {
24+
List<List<Integer>> output = new ArrayList<>();
25+
List<Integer> current = new ArrayList<>();
26+
addNumber(output, current, k, n, 1);
27+
return output;
28+
}
29+
30+
private static void addNumber(List<List<Integer>> output, List<Integer> current, int k, int n, int ni) {
31+
if (k == 0) {
32+
output.add(new ArrayList<>(current));
33+
return;
34+
}
35+
for (int skip = 0; skip <= n - ni; ++skip) {
36+
current.add(ni + skip);
37+
addNumber(output, current, k - 1, n, ni + skip + 1);
38+
current.remove(current.size() - 1);
39+
}
40+
}
41+
42+
@Test
43+
public void testCase1() {
44+
System.out.println(combine(4, 2));
45+
}
46+
}

src/com/leetcode/SetMatrixZeros.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public static void setZeroes(int[][] matrix) {
4646
for (int i = 0; i < n; ++i) {
4747
for (int j = 0; j < m; ++j) {
4848
if (matrix[i][j] == 0) {
49-
if (i == 0) firstCol = true;
50-
if (j == 0) firstRow = true;
49+
if (i == 0) firstRow = true;
50+
if (j == 0) firstCol = true;
5151
matrix[0][j] = 0;
5252
matrix[i][0] = 0;
5353
}
@@ -86,6 +86,12 @@ public void testCase2() {
8686
setZeroes(m);
8787
}
8888

89+
@Test
90+
public void testCase3() {
91+
int[][] m = {{1, 0, 3}};
92+
setZeroes(m);
93+
}
94+
8995
private static String printMat(int[][] mat) {
9096
int n = mat.length;
9197
int m = mat[0].length;

src/com/leetcode/SortColors.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.leetcode;
2+
3+
import org.junit.Test;
4+
5+
/*
6+
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are
7+
adjacent, with the colors in the order red, white and blue.
8+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
9+
Note: You are not suppose to use the library's sort function for this problem.
10+
11+
Example:
12+
13+
Input: [2,0,2,1,1,0]
14+
Output: [0,0,1,1,2,2]
15+
Follow up:
16+
17+
A rather straight forward solution is a two-pass algorithm using counting sort.
18+
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's,
19+
then 1's and followed by 2's.
20+
Could you come up with a one-pass algorithm using only constant space?
21+
*/
22+
public class SortColors {
23+
public static void sortColors(int[] nums) {
24+
int n = nums.length;
25+
int left = 0, right = n - 1;
26+
int i = 0;
27+
while (i <= right) {
28+
if (nums[i] < 1) {
29+
// Exchange with current left pointer
30+
nums[i] = nums[left];
31+
nums[left] = 0;
32+
++left;
33+
}
34+
if (nums[i] > 1) {
35+
// Exchange with right
36+
nums[i] = nums[right];
37+
nums[right] = 2;
38+
--right;
39+
--i;
40+
}
41+
++i;
42+
}
43+
System.out.println(matToString(nums));
44+
}
45+
46+
@Test
47+
public void testCase1() {
48+
int[] m = {2, 0, 2, 1, 1, 0};
49+
sortColors(m);
50+
}
51+
52+
private static String matToString(int[] mat) {
53+
int n = mat.length;
54+
StringBuilder sb = new StringBuilder();
55+
sb.append("[");
56+
for (int j = 0; j < n; ++j) {
57+
sb.append(mat[j]);
58+
if (j < n - 1) sb.append(",");
59+
}
60+
sb.append("]");
61+
return sb.toString();
62+
}
63+
64+
}

0 commit comments

Comments
 (0)