2 Commits
+87 -9
View File
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -19,9 +22,18 @@ public class MainExercises
the output has to be a two-dimensional array of characters, so don't just print the triangle! the output has to be a two-dimensional array of characters, so don't just print the triangle!
*/ */
public char[][] generateTriangle(int n) { public char[][] generateTriangle(int n) {
char[][] triangle = new char[n][];
// todo for (int i = 0; i < n; i++) {
return null; triangle[i] = new char[i + 1];
for(int j = 0; j <= i; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == i) {
triangle[i][j] = '*';
} else {
triangle[i][j] = ' ';
}
}
}
return triangle;
} }
@@ -58,8 +70,47 @@ public class MainExercises
- Number of columns: matrix[0].length (if rectangular) - Number of columns: matrix[0].length (if rectangular)
*/ */
public int[] spiralTraversal(int[][] matrix) { public int[] spiralTraversal(int[][] matrix) {
// todo int[] result = new int[matrix.length * (matrix[0].length)];
return null; int topI = 0;
int leftJ = 0;
int botI = matrix.length - 1;
int rightJ = matrix[0].length - 1;
int count = 0;
int turn = 1; // 1 for topI, 2 for rightJ, 3 for botI, 4 for leftJ
while (count < matrix.length * matrix[0].length) {
if (turn == 1) {
for(int j = leftJ; j <= rightJ; j++) {
result[count] = matrix[topI][j];
count++;
}
topI++;
turn = 2;
} else if (turn == 2) {
for (int i = topI; i <= botI; i++) {
result[count] = matrix[i][rightJ];
count++;
}
rightJ--;
turn = 3;
} else if (turn == 3) {
for (int j = rightJ; j >= leftJ; j--) {
result[count] = matrix[botI][j];
count++;
}
botI--;
turn = 4;
} else if (turn == 4) {
for (int i = botI; i >= topI; i--) {
result[count] = matrix[i][leftJ];
count++;
}
leftJ++;
turn = 1;
}
}
return result;
} }
/* /*
@@ -84,16 +135,43 @@ public class MainExercises
You should generate all partitions of the input number. You should generate all partitions of the input number.
Hint: You can determine the size and order of the arrays by finding the pattern Hint: You can determine the size and order of the arrays by finding the pattern
of partitions and their count. Trust me, this one's fun and easy :) of partitions and their count. Trust me, this one's !fun and !easy :|
If you're familiar with Lists and ArrayLists, you can also edit the method's If you're familiar with Lists and ArrayLists, you can also edit the method's
body to use them instead of arrays. body to use them instead of arrays.
*/ */
public int[][] intPartitions(int n) { public int[][] intPartitions(int n) {
// todo List<List<Integer>> partitions = new ArrayList<>();
return null; List<Integer> current = new ArrayList<>();
addPartition(n, n,current, partitions);
int[][] result = new int[partitions.size()][];
for (int i = 0; i < partitions.size(); i++) {
List<Integer> p = partitions.get(i);
int size = p.size();
result[i] = new int[size];
for (int j = 0; j < size;j++) {
result[i][j] = p.get(j);
} }
}
return result;
}
private void addPartition(int remaining, int maxAllowed,
List<Integer> current, List<List<Integer>> partitions){
if (remaining == 0) {
partitions.add(new ArrayList<>(current));
return;
}
for (int j = Math.min(remaining, maxAllowed); j >=1; j--) {
current.add(j);
addPartition(remaining - j, j, current, partitions);
current.remove(current.size() - 1);
}
}
public static void main() public static void main()