starting my prject and do 3 main exercise questions
This commit is contained in:
Generated
+1
-1
@@ -8,7 +8,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23_PREVIEW" project-jdk-name="23 (3)" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -9,8 +9,8 @@
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>25</maven.compiler.source>
|
||||
<maven.compiler.target>25</maven.compiler.target>
|
||||
<maven.compiler.source>23</maven.compiler.source>
|
||||
<maven.compiler.target>23</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -20,8 +20,26 @@ public class MainExercises
|
||||
*/
|
||||
public char[][] generateTriangle(int n) {
|
||||
|
||||
// todo
|
||||
return null;
|
||||
char[][] triangle = new char[n][n];
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
triangle[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
triangle[i][0] = '*';
|
||||
triangle[i][i] = '*';
|
||||
}
|
||||
|
||||
for (int j = 0; j < n; j++) {
|
||||
triangle[n-1][j] = '*';
|
||||
}
|
||||
|
||||
return triangle;
|
||||
|
||||
// return null;
|
||||
|
||||
}
|
||||
|
||||
@@ -58,8 +76,48 @@ public class MainExercises
|
||||
- Number of columns: matrix[0].length (if rectangular)
|
||||
*/
|
||||
public int[] spiralTraversal(int[][] matrix) {
|
||||
// todo
|
||||
return null;
|
||||
int rows = matrix.length;
|
||||
int cols = matrix[0].length;
|
||||
|
||||
int[] result = new int[rows * cols];
|
||||
|
||||
int top = 0;
|
||||
int bottom = rows - 1;
|
||||
int left = 0;
|
||||
int right = cols - 1;
|
||||
|
||||
int index = 0;
|
||||
|
||||
while (top <= bottom && left <= right) {
|
||||
|
||||
for (int i = left; i <= right; i++) {
|
||||
result[index++] = matrix[top][i];
|
||||
}
|
||||
top++;
|
||||
|
||||
for (int i = top; i <= bottom; i++) {
|
||||
result[index++] = matrix[i][right];
|
||||
}
|
||||
right--;
|
||||
|
||||
if (top <= bottom) {
|
||||
for (int i = right; i >= left; i--) {
|
||||
result[index++] = matrix[bottom][i];
|
||||
}
|
||||
bottom--;
|
||||
}
|
||||
|
||||
if (left <= right) {
|
||||
for (int i = bottom; i >= top; i--) {
|
||||
result[index++] = matrix[i][left];
|
||||
}
|
||||
left++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
//return null;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -91,11 +149,45 @@ public class MainExercises
|
||||
*/
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
java.util.List<int[]> result = new java.util.ArrayList<>();
|
||||
java.util.List<Integer> current = new java.util.ArrayList<>();
|
||||
|
||||
build(n, n, current, result);
|
||||
|
||||
int[][] out = new int[result.size()][];
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
out[i] = result.get(i);
|
||||
}
|
||||
|
||||
return out;
|
||||
|
||||
// return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void build(int remaining, int max,
|
||||
java.util.List<Integer> current,
|
||||
java.util.List<int[]> result) {
|
||||
|
||||
if (remaining == 0) {
|
||||
int[] arr = new int[current.size()];
|
||||
for (int i = 0; i < current.size(); i++) {
|
||||
arr[i] = current.get(i);
|
||||
}
|
||||
result.add(arr);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = Math.min(max, remaining); i >= 1; i--) {
|
||||
current.add(i);
|
||||
build(remaining - i, i, current, result);
|
||||
current.remove(current.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main()
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user