Compare commits
6
Commits
e73ac6115f
...
11c748526e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11c748526e | ||
|
|
aa6cdc766d | ||
|
|
2f72c0c8b6 | ||
|
|
3e85586939 | ||
|
|
c59b80f87c | ||
|
|
0a60c2e5e9 |
@@ -20,7 +20,7 @@ public class BonusExercises {
|
|||||||
- Each segment (between dots) must follow same hyphen rules
|
- Each segment (between dots) must follow same hyphen rules
|
||||||
*/
|
*/
|
||||||
public boolean validateEmail(String email) {
|
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);
|
Pattern pattern = Pattern.compile(regex);
|
||||||
Matcher matcher = pattern.matcher(email);
|
Matcher matcher = pattern.matcher(email);
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class MainExercises
|
public class MainExercises
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@@ -20,8 +22,22 @@ public class MainExercises
|
|||||||
*/
|
*/
|
||||||
public char[][] generateTriangle(int n) {
|
public char[][] generateTriangle(int n) {
|
||||||
|
|
||||||
// todo
|
char [][] triangle = new char[n][];
|
||||||
return null;
|
|
||||||
|
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)
|
- Number of columns: matrix[0].length (if rectangular)
|
||||||
*/
|
*/
|
||||||
public int[] spiralTraversal(int[][] matrix) {
|
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) {
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user