Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd399fd00a | ||
|
|
95028697fd |
@@ -8,21 +8,26 @@ public class MainExercises
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
result[i] = new char[i+1];
|
||||
if (i == n-1)
|
||||
char[] line = new char[i+1];
|
||||
|
||||
if (i == n-1 || i == 0)
|
||||
{
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
result[i][j] = '*';
|
||||
line[j] = '*';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result[i][0] = '*';
|
||||
result[i][i] = '*';
|
||||
line[0] = '*';
|
||||
line[i] = '*';
|
||||
|
||||
for (int j = 1; j < i; j++) result[i][j] = ' ';
|
||||
for (int j = i - 1; j > 0; j--) {
|
||||
line[j] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
result[i] = line;
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -64,59 +69,35 @@ public class MainExercises
|
||||
}
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
int[][] result;
|
||||
ArrayList<ArrayList<Integer>> mainArray = new ArrayList<>();
|
||||
ArrayList<int[]> result = new ArrayList<>();
|
||||
ArrayList<Integer> curPartition = new ArrayList<>();
|
||||
|
||||
ArrayList<Integer> start = new ArrayList<>();
|
||||
start.add(n);
|
||||
mainArray.add(start);
|
||||
|
||||
while (true) {
|
||||
ArrayList<Integer> latestArray = mainArray.get(mainArray.size() - 1);
|
||||
fillPartition(n, n, curPartition, result);
|
||||
|
||||
Integer numberOfInterest = 0;
|
||||
for (Integer number : latestArray)
|
||||
{
|
||||
if (number > 1) {
|
||||
numberOfInterest = number;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result.toArray(new int[result.size()][]);
|
||||
}
|
||||
|
||||
if (numberOfInterest == 0) break;
|
||||
private void fillPartition(int max, int remaining, ArrayList<Integer> curPartition, ArrayList<int[]> result) {
|
||||
if (remaining == 0) {
|
||||
int size = curPartition.size();
|
||||
int[] savingArr = new int[size];
|
||||
|
||||
numberOfInterest--;
|
||||
Integer remain = n - numberOfInterest;
|
||||
ArrayList<Integer> newArray = new ArrayList<>();
|
||||
newArray.add(numberOfInterest);
|
||||
|
||||
for (Integer number = numberOfInterest;;) {
|
||||
if (number >= remain) {
|
||||
newArray.add(remain);
|
||||
break;
|
||||
}
|
||||
else {
|
||||
newArray.add(number);
|
||||
remain -= number;
|
||||
}
|
||||
for (int i = 0; i < size; i++) {
|
||||
savingArr[i] = curPartition.get(i);
|
||||
}
|
||||
|
||||
mainArray.add(newArray);
|
||||
result.add(savingArr);
|
||||
return;
|
||||
}
|
||||
|
||||
result = new int[mainArray.size()][];
|
||||
for (int i = Math.min(max, remaining); i >= 1; i--) {
|
||||
curPartition.add(i);
|
||||
int newRemaining = remaining - i;
|
||||
|
||||
for (int i = 0; i < mainArray.size(); i++) {
|
||||
ArrayList<Integer> array = mainArray.get(i);
|
||||
fillPartition(i, newRemaining, curPartition, result);
|
||||
|
||||
result[i] = new int[array.size()];
|
||||
|
||||
for (int j = 0; j < array.size(); j++) {
|
||||
result[i][j] = array.get(j);
|
||||
}
|
||||
curPartition.removeLast();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user