2 Commits
Author SHA1 Message Date
MehrdadShirvani e73ac6115f Revert "fix: remove redundant comment"
This reverts commit 959c579550.
2026-04-18 19:38:33 +03:30
MehrdadShirvani 959c579550 fix: remove redundant comment 2026-04-18 19:30:23 +03:30
+8 -86
View File
@@ -1,6 +1,3 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises public class MainExercises
{ {
/* /*
@@ -22,18 +19,9 @@ 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][];
for (int i = 0; i < n; i++) { // todo
triangle[i] = new char[i + 1]; return null;
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;
} }
@@ -70,47 +58,8 @@ 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) {
int[] result = new int[matrix.length * (matrix[0].length)]; // todo
int topI = 0; return null;
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;
} }
/* /*
@@ -135,42 +84,15 @@ 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) {
List<List<Integer>> partitions = new ArrayList<>(); // todo
List<Integer> current = new ArrayList<>(); return null;
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);
}
} }