6 Commits
2 changed files with 123 additions and 7 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ public class BonusExercises {
- Each segment (between dots) must follow same hyphen rules
*/
public boolean validateEmail(String email) {
String regex = ""; // todo
String regex = "^[a-zA-z0-9_+-]+(?:\\.[a-zA-Z0-9_+-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z0-9-]+$"; // todo
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
+122 -6
View File
@@ -1,3 +1,5 @@
import java.util.ArrayList;
public class MainExercises
{
/*
@@ -20,8 +22,22 @@ 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 == n - 1 || j == i){
triangle[i][j] = '*';
}
else{
triangle[i][j] = ' ';
}
}
}
return triangle;
}
@@ -58,8 +74,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 cols = matrix[0].length;
if(rows == 1) return matrix[0];
int[] result = new int[rows * cols];
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
int id = 0;
while(top <= bottom && left <= right){ // reaching the center
for(int col = left; col <= right; col++){
result[id] = matrix[top][col];
id++;
}
top++;
for(int row = top; row <= bottom; row++){
result[id] = matrix[row][right];
id++;
}
right--;
if(top <= right){
for(int col = right; col >= left; col--){
result[id] = matrix[bottom][col];
id++;
}
bottom--;
}
if(left <= right){
for(int row = bottom; row >= top; row--){
result[id] = matrix[row][left];
id++;
}
left++;
}
}
return result;
}
/*
@@ -91,8 +149,66 @@ public class MainExercises
*/
public int[][] intPartitions(int n) {
// todo
return null;
if(n == 1){
return new int[][]{{1}};
}
ArrayList<int[]> result = new ArrayList<>();
int[] current = new int[n]; // current partition, to be built
// we use stack arrays to have the situation saved.
int[] remainingStack = new int[n*n]; // remaining value, to be added
// int[] maxValueStack = new int[n*n]; // maximum number we can use, to prevent repeating partitions
int[] nextTryStack = new int[n*n]; // the next number we try
int[] lenStack = new int[n*n]; // length of current partition, gives us the next index in current
int head = 0; // works like a pointer, it makes stack arrays make sense.
remainingStack[0] = n;
// maxValueStack[0] = n;
nextTryStack[0] = n;
lenStack[0] = 0;
while(head >= 0){
int remaining = remainingStack[head];
// int maxValue = maxValueStack[head];
int nextTry = nextTryStack[head];
int partitionLen = lenStack[head];
if(remaining == 0){ // partition has been completed
//
int[] partition = new int[partitionLen];
System.arraycopy(current, 0, partition, 0, partitionLen);
result.add(partition);
head--;
continue;
}
if(nextTry <= 0){ // no choices left
head--;
continue;
}
int chosen = nextTry;
nextTryStack[head] = chosen - 1; // next value to be tried
current[partitionLen] = chosen; // adding selected number
// generating next number for the partition
head++;
remainingStack[head] = remaining - chosen;
// maxValueStack[head] = chosen;
nextTryStack[head] = Math.min(chosen, remaining - chosen);
lenStack[head] = partitionLen + 1;
}
int[][] output = new int[result.size()][];
for(int i = 0; i < result.size(); i++){
output[i] = result.get(i);
}
return output;
}