2 Commits
2 changed files with 124 additions and 21 deletions
+33 -3
View File
@@ -65,11 +65,41 @@ public class BonusExercises {
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
*/ */
public List<String> findPalindromes(String string) { public List<String> findPalindromes(String string) {
List<String> list = new ArrayList<>(); List<String> result = new ArrayList<>();
// todo String[] words = string.split("\\s+");
return list; for (int i = 0; i < words.length; i++) {
String word = words[i];
word = word.replaceAll("[^a-zA-Z]", "");
if (word.length() < 3) {
continue;
}
String lower = word.toLowerCase();
int left = 0;
int right = lower.length() - 1;
boolean isPalindrome = true;
while (left < right) {
if (lower.charAt(left) != lower.charAt(right)) {
isPalindrome = false;
break;
}
left++;
right--;
}
if (isPalindrome) {
result.add(word);
}
}
return result;
} }
public static void main(String[] args) { public static void main(String[] args) {
// you can test your code here // you can test your code here
} }
+87 -14
View File
@@ -1,5 +1,6 @@
public class MainExercises import javax.xml.transform.Result;
{
public class MainExercises {
/* /*
you should create a triangle with "*" and return a two-dimensional array of characters based on that 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 " " the triangle's area is empty, which means some characters should be " "
@@ -18,15 +19,31 @@ public class MainExercises
the output has to be a two-dimensional array of characters, so don't just print the triangle! the output has to be a two-dimensional array of characters, so don't just print the triangle!
*/ */
public char[][] generateTriangle(int n) { public static char[][] generateTriangle(int n) {
// todo char[][] triangle = new char[n][n];
return null;
int i = 0;
while (i < n) {
int j = 0;
while (j < n) {
if (j == 0 || j == i || i == n - 1) {
triangle[i][j] = '*';
} else {
triangle[i][j] = ' ';
}
j = j + 1;
}
i = i + 1;
}
return triangle;
} }
/* /*
SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX
@@ -58,8 +75,49 @@ 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 int rows = matrix.length;
return null; int cols = matrix[0].length;
int[] result = new int[rows * cols];
int index = 0;
int top = 0;
int bottom = rows - 1;
int left = 0;
int right = cols - 1;
while (top <= bottom && left <= right) {
// left → right
for (int j = left; j <= right; j++) {
result[index++] = matrix[top][j];
}
top++;
// top → bottom
for (int i = top; i <= bottom; i++) {
result[index++] = matrix[i][right];
}
right--;
// right → left
if (top <= bottom) {
for (int j = right; j >= left; j--) {
result[index++] = matrix[bottom][j];
}
bottom--;
}
// bottom → top
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result[index++] = matrix[i][left];
}
left++;
}
}
return result;
} }
/* /*
@@ -89,15 +147,30 @@ public class MainExercises
If you're familiar with Lists and ArrayLists, you can also edit the method's If you're familiar with Lists and ArrayLists, you can also edit the method's
body to use them instead of arrays. body to use them instead of arrays.
*/ */
public int[][] intPartitions(int n) {
public int[][] intPartitions(int n) { java.util.ArrayList<int[]> result = new java.util.ArrayList<>();
// todo generate(n, n, new java.util.ArrayList<Integer>(), result);
return null; return result.toArray(new int[result.size()][]);
} }
private void generate(int remaining, int maxValue,
java.util.ArrayList<Integer> current,
java.util.ArrayList<int[]> result) {
if (remaining == 0) {
int[] arr = new int[current.size()];
for (int i = 0; i < current.size(); i++) {
arr[i] = current.get(i);
}
public static void main() result.add(arr);
{ return;
}
for (int i = Math.min(remaining, maxValue); i >= 1; i--) {
current.add(i);
generate(remaining - i, i, current, result);
current.remove(current.size() - 1);
}
} }
} }