Implement intPartitions method

This commit is contained in:
Arefe Talebi
2026-04-21 01:03:04 +03:30
parent e73ac6115f
commit 1c41ea38a2
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
*/
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
}
+86 -13
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
the triangle's area is empty, which means some characters should be " "
@@ -18,13 +19,29 @@ 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;
}
/*
@@ -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) {
// 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;
}
public static void main()
{
for (int i = Math.min(remaining, maxValue); i >= 1; i--) {
current.add(i);
generate(remaining - i, i, current, result);
current.remove(current.size() - 1);
}
}
}