Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c41ea38a2 |
@@ -65,11 +65,41 @@ public class BonusExercises {
|
||||
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
return list;
|
||||
List<String> result = new ArrayList<>();
|
||||
String[] words = string.split("\\s+");
|
||||
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) {
|
||||
// you can test your code here
|
||||
}
|
||||
|
||||
@@ -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
|
||||
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!
|
||||
*/
|
||||
public char[][] generateTriangle(int n) {
|
||||
public static char[][] generateTriangle(int n) {
|
||||
|
||||
// todo
|
||||
return null;
|
||||
char[][] triangle = new char[n][n];
|
||||
|
||||
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
|
||||
|
||||
@@ -58,8 +75,49 @@ 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;
|
||||
|
||||
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
|
||||
body to use them instead of arrays.
|
||||
*/
|
||||
public int[][] intPartitions(int n) {
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
java.util.ArrayList<int[]> result = new java.util.ArrayList<>();
|
||||
generate(n, n, new java.util.ArrayList<Integer>(), result);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user