From 815ccfc5a5ab52d25a87ab80a3978cfa48f4fbae Mon Sep 17 00:00:00 2001 From: ava Date: Mon, 20 Apr 2026 21:09:00 +0330 Subject: [PATCH] starting my prject and do 3 main exercise questions --- .idea/misc.xml | 2 +- pom.xml | 4 +- src/main/java/MainExercises.java | 104 +++++++++++++++++++++++++++++-- 3 files changed, 101 insertions(+), 9 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index d2b5d0f..02a273f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index d469778..11e205f 100644 --- a/pom.xml +++ b/pom.xml @@ -9,8 +9,8 @@ 1.0-SNAPSHOT - 25 - 25 + 23 + 23 UTF-8 diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..085fa3d 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -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 result = new java.util.ArrayList<>(); + java.util.List 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 current, + java.util.List 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() {