matin mortazavi / exercises + bonus
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
public class MainExercises
|
||||
{
|
||||
import java.util.ArrayList ;
|
||||
import java.util.List;
|
||||
|
||||
public class MainExercises {
|
||||
/*
|
||||
you should create a triangle with "*" and return a two-dimensional array of characters based on that
|
||||
the triangle's area is empty, which means some characters should be " "
|
||||
@@ -20,13 +22,23 @@ public class MainExercises
|
||||
*/
|
||||
public char[][] generateTriangle(int n) {
|
||||
|
||||
// todo
|
||||
return null;
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
|
||||
|
||||
@@ -57,11 +69,52 @@ 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 rows = matrix.length;
|
||||
int col = matrix[0].length;
|
||||
|
||||
int size = 0 ;
|
||||
boolean increase = true;
|
||||
int range = rows * col ;
|
||||
int check = 0 ;
|
||||
int UP = 0 ;
|
||||
int DOWN = rows-1 ;
|
||||
int LEFT = 0 ;
|
||||
int RIGHT = col -1;
|
||||
|
||||
int[] newmat = new int[rows*col] ;
|
||||
while (UP <= DOWN && LEFT<= RIGHT) {
|
||||
for (int j = LEFT; j <= RIGHT; j++) {
|
||||
newmat[size] = matrix[UP][j];
|
||||
size++;
|
||||
}
|
||||
|
||||
UP++;
|
||||
for (int i = UP; i <= DOWN; i++) {
|
||||
newmat[size] = matrix[i][RIGHT ];
|
||||
size++;
|
||||
}
|
||||
RIGHT--;
|
||||
if(UP <= DOWN) {
|
||||
for (int j = RIGHT ; j >= LEFT; j--) {
|
||||
newmat[size] = matrix[DOWN ][j];
|
||||
size++;
|
||||
}
|
||||
DOWN--;
|
||||
}
|
||||
if(LEFT <= RIGHT) {
|
||||
for (int i = DOWN ; i >= UP; i--) {
|
||||
newmat[size] = matrix[i][LEFT];
|
||||
size++;
|
||||
}
|
||||
LEFT++;
|
||||
}
|
||||
|
||||
}
|
||||
return newmat ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
integer partitioning is a combinatorics problem in discreet maths
|
||||
the problem is to generate sum numbers which their summation is the input number
|
||||
@@ -90,9 +143,41 @@ public class MainExercises
|
||||
body to use them instead of arrays.
|
||||
*/
|
||||
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
ArrayList <int[]> all = new ArrayList<>() ;
|
||||
ArrayList<Integer>list = new ArrayList<>() ;
|
||||
part(n , n ,list , all);
|
||||
return convertTo2DArray(all) ;
|
||||
|
||||
|
||||
}
|
||||
public static int[][] convertTo2DArray(ArrayList<int[]> all) {
|
||||
int [][] mat = new int [all.size()][] ;
|
||||
for (int i = 0 ; i < all.size() ; i ++ )
|
||||
{
|
||||
mat[i] = all.get(i) ;
|
||||
}
|
||||
return mat;
|
||||
}
|
||||
public static void part(int rem , int max , ArrayList<Integer> list , ArrayList<int []> all )
|
||||
{
|
||||
if (rem == 0)
|
||||
{
|
||||
int[] final1 = new int [list.size()] ;
|
||||
for (int i = 0 ; i < list.size(); i ++ )
|
||||
{
|
||||
final1[i] = list.get(i) ;
|
||||
}
|
||||
all.add(final1) ;
|
||||
return;
|
||||
}
|
||||
for (int i = Math.min(rem , max) ; i >=1 ; i -- )
|
||||
{
|
||||
list.add(i) ;
|
||||
part(rem-i , i , list, all );
|
||||
list.remove(list.size()-1) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user