diff --git a/src/.idea/.gitignore b/src/.idea/.gitignore
new file mode 100644
index 0000000..ab1f416
--- /dev/null
+++ b/src/.idea/.gitignore
@@ -0,0 +1,10 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Ignored default folder with query files
+/queries/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/
diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml
new file mode 100644
index 0000000..a20905f
--- /dev/null
+++ b/src/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/.idea/modules.xml b/src/.idea/modules.xml
new file mode 100644
index 0000000..31eaf94
--- /dev/null
+++ b/src/.idea/modules.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/.idea/vcs.xml b/src/.idea/vcs.xml
new file mode 100644
index 0000000..6c0b863
--- /dev/null
+++ b/src/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java
index 21581ea..422ae33 100644
--- a/src/main/java/MainExercises.java
+++ b/src/main/java/MainExercises.java
@@ -1,3 +1,6 @@
+import java.util.ArrayList;
+import java.util.List;
+
public class MainExercises
{
/*
@@ -18,11 +21,21 @@ public class MainExercises
the output has to be a two-dimensional array of characters, so don't just print the triangle!
*/
- public char[][] generateTriangle(int n) {
-
- // todo
- return null;
-
+ public char[][] generateTriangle(int n)
+ {
+ char[][] triangle = new char[n][];
+ for(int i = 0; i < n; i++)
+ {
+ triangle[i] = new char[i+1];
+ for(int j = 0; j <= i; j++)
+ {
+ if(i == 0 || j == 0 || i == j || i == n-1)
+ triangle[i][j] = '*';
+ else
+ triangle[i][j] = ' ';
+ }
+ }
+ return triangle;
}
@@ -57,9 +70,38 @@ public class MainExercises
- Number of rows: matrix.length
- Number of columns: matrix[0].length (if rectangular)
*/
- public int[] spiralTraversal(int[][] matrix) {
- // todo
- return null;
+ public int[] spiralTraversal(int[][] matrix)
+ {
+
+ int m = matrix.length;
+ int n = matrix[0].length;
+ int x = 0;
+ int y = 0;
+ int idx = 0;
+
+ int[] numbers = new int[m * n];
+ boolean[][] visitedCell = new boolean[m][n];
+ int[][] dir = {{0,1},{1,0},{0,-1},{-1,0}};
+
+ for(int i = 0; i < (m * n); i++)
+ {
+ numbers[i] = matrix[x][y];
+ visitedCell[x][y] = true;
+
+ int nextX = x + dir[idx][0];
+ int nextY = y + dir[idx][1];
+
+ if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || visitedCell[nextX][nextY] == true)
+ {
+ idx = (idx + 1) % 4;
+
+ nextX = x + dir[idx][0];
+ nextY = y + dir[idx][1];
+ }
+ x = nextX;
+ y = nextY;
+ }
+ return numbers;
}
/*
@@ -90,11 +132,38 @@ public class MainExercises
body to use them instead of arrays.
*/
- public int[][] intPartitions(int n) {
- // todo
- return null;
+ public int[][] intPartitions(int n)
+ {
+ List result = new ArrayList<>();
+ findPartitions(n, n, new ArrayList<>(), result);
+ return result.toArray(new int[result.size()][]);
}
+ private void findPartitions(int r, int max, List list, List finalList)
+ {
+ if (r == 0)
+ {
+ int[] partition = new int[list.size()];
+ for (int i = 0; i < list.size(); i++)
+ {
+ partition[i] = list.get(i);
+ }
+ finalList.add(partition);
+ return;
+ }
+
+ if (r >= max)
+ {
+ list.add(max);
+ findPartitions(r - max, max, list, finalList);
+ list.remove(list.size() - 1);
+ }
+
+ if (max > 1)
+ {
+ findPartitions(r, max - 1, list, finalList);
+ }
+ }
public static void main()
{