starting my prject and do 3 main exercise questions #1
Generated
+1
-1
@@ -8,7 +8,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -9,8 +9,8 @@
|
|||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>25</maven.compiler.source>
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
<maven.compiler.target>25</maven.compiler.target>
|
<maven.compiler.target>23</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,26 @@ public class MainExercises
|
|||||||
*/
|
*/
|
||||||
public char[][] generateTriangle(int n) {
|
public char[][] generateTriangle(int n) {
|
||||||
|
|
||||||
// todo
|
char[][] triangle = new char[n][n];
|
||||||
return null;
|
|
||||||
|
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)
|
- Number of columns: matrix[0].length (if rectangular)
|
||||||
*/
|
*/
|
||||||
public int[] spiralTraversal(int[][] matrix) {
|
public int[] spiralTraversal(int[][] matrix) {
|
||||||
// todo
|
int rows = matrix.length;
|
||||||
return null;
|
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,10 +149,44 @@ public class MainExercises
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public int[][] intPartitions(int n) {
|
public int[][] intPartitions(int n) {
|
||||||
// todo
|
java.util.List<int[]> result = new java.util.ArrayList<>();
|
||||||
return null;
|
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()
|
public static void main()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user