From f52a3d29d92477650be6a78c0d3ff90e984e6da6 Mon Sep 17 00:00:00 2001 From: Fateme Azizi Date: Tue, 21 Apr 2026 20:19:48 +0330 Subject: [PATCH 1/3] implement generateTriangle method --- src/main/java/MainExercises.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..598838a 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -20,9 +20,20 @@ public class MainExercises */ public char[][] generateTriangle(int n) { - // todo - return null; + char[][] triangle = new char[n][]; + for (int i=0 ; i Date: Tue, 21 Apr 2026 22:37:05 +0330 Subject: [PATCH 2/3] implement spiralTraversal method --- src/main/java/MainExercises.java | 48 ++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 598838a..e13066e 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; + public class MainExercises { /* @@ -69,8 +71,50 @@ public class MainExercises - Number of columns: matrix[0].length (if rectangular) */ public int[] spiralTraversal(int[][] matrix) { - // todo - return null; + + int rows = matrix.length; + int columns = matrix[0].length; + + int[] result = new int[rows*columns]; + int count = 0; + + int top = 0; + int bottom = rows - 1; + int right = columns - 1; + int left = 0; + + while (top <= bottom && left <= right) { + + for(int j = left ; j<=right ; j++) { + result[count] = matrix[top][j]; + count++; + } + top++; + + for(int i = top ; i<=bottom ; i++) { + result[count] = matrix[i][right]; + count++; + } + right--; + + if(top <= bottom) { + for (int j = right; j >= left; j--) { + result[count] = matrix[bottom][j]; + count++; + } + } + bottom--; + + if(left <= right){ + for (int i = bottom; i >= top; i--) { + result[count] = matrix[i][left]; + count++; + } + } + left++; + + } + return result; } /* -- 2.54.0 From c287af8aaee6aa89d42ff07cd4980d296fe120bd Mon Sep 17 00:00:00 2001 From: Fateme Azizi Date: Thu, 23 Apr 2026 04:13:00 +0330 Subject: [PATCH 3/3] implement intPartitions method --- src/main/java/MainExercises.java | 52 ++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index e13066e..64b2f2d 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -146,11 +146,57 @@ public class MainExercises */ public int[][] intPartitions(int n) { - // todo - return null; - } + ArrayList result = new ArrayList<>(); + + int[] currentPartition = new int[n]; + + int k=0; //اخرین ایندکس currentPartition + + currentPartition[k] = n; + + while (true) { + + int[] temp = new int[k+1]; + for(int i=0 ; i<=k ; i++) { + temp[i] = currentPartition[i]; + } + result.add(temp); + + int remained = 0; + while(k>=0 && currentPartition[k]==1) { + remained += currentPartition[k]; + k--; + } + + if(k<0) + break; + + currentPartition[k]--; + remained++; + + + while (remained > currentPartition[k]) { + currentPartition[k+1] = currentPartition[k]; + remained -= currentPartition[k]; + k++; + } + + currentPartition[k+1] = remained; + k++; + + } + + int[][] output = new int[result.size()][]; + for(int i=0 ; i