diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..64b2f2d 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; + public class MainExercises { /* @@ -20,9 +22,20 @@ public class MainExercises */ public char[][] generateTriangle(int n) { - // todo - return null; + char[][] triangle = new char[n][]; + for (int i=0 ; i= 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; } /* @@ -91,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